Skip to content

Commit 0055191

Browse files
committed
Refactor components service and factory
1 parent f86adb4 commit 0055191

File tree

8 files changed

+526
-489
lines changed

8 files changed

+526
-489
lines changed

zeppelin-web/src/components/arrayOrderingSrv/arrayOrdering.service.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@
1212
* limitations under the License.
1313
*/
1414
'use strict';
15+
(function() {
1516

16-
angular.module('zeppelinWebApp').service('arrayOrderingSrv', function() {
17+
angular.module('zeppelinWebApp').service('arrayOrderingSrv', arrayOrderingSrv);
1718

18-
var arrayOrderingSrv = this;
19+
function arrayOrderingSrv() {
20+
var arrayOrderingSrv = this;
1921

20-
this.notebookListOrdering = function(note) {
21-
return arrayOrderingSrv.getNoteName(note);
22-
};
22+
this.notebookListOrdering = function(note) {
23+
return arrayOrderingSrv.getNoteName(note);
24+
};
2325

24-
this.getNoteName = function(note) {
25-
if (note.name === undefined || note.name.trim() === '') {
26-
return 'Note ' + note.id;
27-
} else {
28-
return note.name;
29-
}
30-
};
26+
this.getNoteName = function(note) {
27+
if (note.name === undefined || note.name.trim() === '') {
28+
return 'Note ' + note.id;
29+
} else {
30+
return note.name;
31+
}
32+
};
33+
}
3134

32-
});
35+
})();

zeppelin-web/src/components/baseUrl/baseUrl.service.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,41 @@
1212
* limitations under the License.
1313
*/
1414
'use strict';
15+
(function() {
1516

16-
angular.module('zeppelinWebApp').service('baseUrlSrv', function() {
17+
angular.module('zeppelinWebApp').service('baseUrlSrv', baseUrlSrv);
1718

18-
this.getPort = function() {
19-
var port = Number(location.port);
20-
if (!port) {
21-
port = 80;
22-
if (location.protocol === 'https:') {
23-
port = 443;
19+
function baseUrlSrv() {
20+
this.getPort = function() {
21+
var port = Number(location.port);
22+
if (!port) {
23+
port = 80;
24+
if (location.protocol === 'https:') {
25+
port = 443;
26+
}
2427
}
25-
}
26-
//Exception for when running locally via grunt
27-
if (port === 3333 || port === 9000) {
28-
port = 8080;
29-
}
30-
return port;
31-
};
28+
//Exception for when running locally via grunt
29+
if (port === 3333 || port === 9000) {
30+
port = 8080;
31+
}
32+
return port;
33+
};
3234

33-
this.getWebsocketUrl = function() {
34-
var wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
35-
return wsProtocol + '//' + location.hostname + ':' + this.getPort() + skipTrailingSlash(location.pathname) + '/ws';
36-
};
35+
this.getWebsocketUrl = function() {
36+
var wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
37+
return wsProtocol + '//' + location.hostname + ':' + this.getPort() +
38+
skipTrailingSlash(location.pathname) + '/ws';
39+
};
3740

38-
this.getRestApiBase = function() {
39-
return location.protocol + '//' + location.hostname + ':' + this.getPort() + skipTrailingSlash(location.pathname) +
40-
'/api';
41-
};
41+
this.getRestApiBase = function() {
42+
return location.protocol + '//' + location.hostname + ':' +
43+
this.getPort() + skipTrailingSlash(location.pathname) +
44+
'/api';
45+
};
4246

43-
var skipTrailingSlash = function(path) {
44-
return path.replace(/\/$/, '');
45-
};
47+
var skipTrailingSlash = function(path) {
48+
return path.replace(/\/$/, '');
49+
};
50+
}
4651

47-
});
52+
})();

zeppelin-web/src/components/browser-detect/browserDetect.service.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,32 @@
1212
* limitations under the License.
1313
*/
1414
'use strict';
15+
(function() {
1516

16-
angular.module('zeppelinWebApp').service('browserDetectService', function() {
17+
angular.module('zeppelinWebApp').service('browserDetectService', browserDetectService);
1718

18-
this.detectIE = function() {
19-
var ua = window.navigator.userAgent;
20-
var msie = ua.indexOf('MSIE ');
21-
if (msie > 0) {
22-
// IE 10 or older => return version number
23-
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
24-
}
25-
var trident = ua.indexOf('Trident/');
26-
if (trident > 0) {
27-
// IE 11 => return version number
28-
var rv = ua.indexOf('rv:');
29-
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
30-
}
31-
var edge = ua.indexOf('Edge/');
32-
if (edge > 0) {
33-
// IE 12 (aka Edge) => return version number
34-
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
35-
}
36-
// other browser
37-
return false;
38-
};
19+
function browserDetectService() {
20+
this.detectIE = function() {
21+
var ua = window.navigator.userAgent;
22+
var msie = ua.indexOf('MSIE ');
23+
if (msie > 0) {
24+
// IE 10 or older => return version number
25+
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
26+
}
27+
var trident = ua.indexOf('Trident/');
28+
if (trident > 0) {
29+
// IE 11 => return version number
30+
var rv = ua.indexOf('rv:');
31+
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
32+
}
33+
var edge = ua.indexOf('Edge/');
34+
if (edge > 0) {
35+
// IE 12 (aka Edge) => return version number
36+
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
37+
}
38+
// other browser
39+
return false;
40+
};
41+
}
3942

40-
});
43+
})();

zeppelin-web/src/components/notebookListDataFactory/notebookList.datafactory.js

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,58 @@
1212
* limitations under the License.
1313
*/
1414
'use strict';
15+
(function() {
1516

16-
angular.module('zeppelinWebApp').factory('notebookListDataFactory', function() {
17+
angular.module('zeppelinWebApp').factory('notebookListDataFactory', notebookListDataFactory);
1718

18-
var notes = {
19-
root: {children: []},
20-
flatList: [],
19+
function notebookListDataFactory() {
20+
var notes = {
21+
root: {children: []},
22+
flatList: [],
2123

22-
setNotes: function(notesList) {
23-
// a flat list to boost searching
24-
notes.flatList = angular.copy(notesList);
24+
setNotes: function(notesList) {
25+
// a flat list to boost searching
26+
notes.flatList = angular.copy(notesList);
2527

26-
// construct the folder-based tree
27-
notes.root = {children: []};
28-
_.reduce(notesList, function(root, note) {
29-
var noteName = note.name || note.id;
30-
var nodes = noteName.match(/([^\/][^\/]*)/g);
28+
// construct the folder-based tree
29+
notes.root = {children: []};
30+
_.reduce(notesList, function(root, note) {
31+
var noteName = note.name || note.id;
32+
var nodes = noteName.match(/([^\/][^\/]*)/g);
3133

32-
// recursively add nodes
33-
addNode(root, nodes, note.id);
34+
// recursively add nodes
35+
addNode(root, nodes, note.id);
3436

35-
return root;
36-
}, notes.root);
37-
}
38-
};
37+
return root;
38+
}, notes.root);
39+
}
40+
};
3941

40-
var addNode = function(curDir, nodes, noteId) {
41-
if (nodes.length === 1) { // the leaf
42-
curDir.children.push({
43-
name: nodes[0],
44-
id: noteId
45-
});
46-
} else { // a folder node
47-
var node = nodes.shift();
48-
var dir = _.find(curDir.children,
49-
function(c) {return c.name === node && c.children !== undefined;});
50-
if (dir !== undefined) { // found an existing dir
51-
addNode(dir, nodes, noteId);
52-
} else {
53-
var newDir = {
54-
name: node,
55-
hidden: true,
56-
children: []
57-
};
58-
curDir.children.push(newDir);
59-
addNode(newDir, nodes, noteId);
42+
var addNode = function(curDir, nodes, noteId) {
43+
if (nodes.length === 1) { // the leaf
44+
curDir.children.push({
45+
name: nodes[0],
46+
id: noteId
47+
});
48+
} else { // a folder node
49+
var node = nodes.shift();
50+
var dir = _.find(curDir.children,
51+
function(c) {return c.name === node && c.children !== undefined;});
52+
if (dir !== undefined) { // found an existing dir
53+
addNode(dir, nodes, noteId);
54+
} else {
55+
var newDir = {
56+
name: node,
57+
hidden: true,
58+
children: []
59+
};
60+
curDir.children.push(newDir);
61+
addNode(newDir, nodes, noteId);
62+
}
6063
}
61-
}
62-
};
64+
};
65+
66+
return notes;
67+
}
6368

64-
return notes;
65-
});
69+
})();

zeppelin-web/src/components/saveAs/saveAs.service.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,44 @@
1212
* limitations under the License.
1313
*/
1414
'use strict';
15+
(function() {
1516

16-
angular.module('zeppelinWebApp').service('saveAsService', function(browserDetectService) {
17+
angular.module('zeppelinWebApp').service('saveAsService', saveAsService);
1718

18-
this.saveAs = function(content, filename, extension) {
19-
var BOM = '\uFEFF';
20-
if (browserDetectService.detectIE()) {
21-
angular.element('body').append('<iframe id="SaveAsId" style="display: none"></iframe>');
22-
var frameSaveAs = angular.element('body > iframe#SaveAsId')[0].contentWindow;
23-
content = BOM + content;
24-
frameSaveAs.document.open('text/json', 'replace');
25-
frameSaveAs.document.write(content);
26-
frameSaveAs.document.close();
27-
frameSaveAs.focus();
28-
var t1 = Date.now();
29-
frameSaveAs.document.execCommand('SaveAs', false, filename + '.' + extension);
30-
var t2 = Date.now();
19+
saveAsService.$inject = ['browserDetectService'];
3120

32-
//This means, this version of IE dosen't support auto download of a file with extension provided in param
33-
//falling back to ".txt"
34-
if (t1 === t2) {
35-
frameSaveAs.document.execCommand('SaveAs', true, filename + '.txt');
21+
function saveAsService(browserDetectService) {
22+
this.saveAs = function(content, filename, extension) {
23+
var BOM = '\uFEFF';
24+
if (browserDetectService.detectIE()) {
25+
angular.element('body').append('<iframe id="SaveAsId" style="display: none"></iframe>');
26+
var frameSaveAs = angular.element('body > iframe#SaveAsId')[0].contentWindow;
27+
content = BOM + content;
28+
frameSaveAs.document.open('text/json', 'replace');
29+
frameSaveAs.document.write(content);
30+
frameSaveAs.document.close();
31+
frameSaveAs.focus();
32+
var t1 = Date.now();
33+
frameSaveAs.document.execCommand('SaveAs', false, filename + '.' + extension);
34+
var t2 = Date.now();
35+
36+
//This means, this version of IE dosen't support auto download of a file with extension provided in param
37+
//falling back to ".txt"
38+
if (t1 === t2) {
39+
frameSaveAs.document.execCommand('SaveAs', true, filename + '.txt');
40+
}
41+
angular.element('body > iframe#SaveAsId').remove();
42+
} else {
43+
content = 'data:image/svg;charset=utf-8,' + BOM + encodeURIComponent(content);
44+
angular.element('body').append('<a id="SaveAsId"></a>');
45+
var saveAsElement = angular.element('body > a#SaveAsId');
46+
saveAsElement.attr('href', content);
47+
saveAsElement.attr('download', filename + '.' + extension);
48+
saveAsElement.attr('target', '_blank');
49+
saveAsElement[0].click();
50+
saveAsElement.remove();
3651
}
37-
angular.element('body > iframe#SaveAsId').remove();
38-
} else {
39-
content = 'data:image/svg;charset=utf-8,' + BOM + encodeURIComponent(content);
40-
angular.element('body').append('<a id="SaveAsId"></a>');
41-
var saveAsElement = angular.element('body > a#SaveAsId');
42-
saveAsElement.attr('href', content);
43-
saveAsElement.attr('download', filename + '.' + extension);
44-
saveAsElement.attr('target', '_blank');
45-
saveAsElement[0].click();
46-
saveAsElement.remove();
47-
}
48-
};
49-
});
52+
};
53+
}
54+
55+
})();

zeppelin-web/src/components/searchService/search.service.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@
1212
* limitations under the License.
1313
*/
1414
'use strict';
15+
(function() {
1516

16-
angular.module('zeppelinWebApp').service('searchService', function($resource, baseUrlSrv) {
17+
angular.module('zeppelinWebApp').service('searchService', searchService);
1718

18-
this.search = function(term) {
19-
this.searchTerm = term.q;
20-
console.log('Searching for: %o', term.q);
21-
if (!term.q) { //TODO(bzz): empty string check
22-
return;
23-
}
24-
var encQuery = window.encodeURIComponent(term.q);
25-
return $resource(baseUrlSrv.getRestApiBase() + '/notebook/search?q=' + encQuery, {}, {
26-
query: {method: 'GET'}
27-
});
28-
};
19+
searchService.$inject = ['$resource', 'baseUrlSrv'];
2920

30-
this.searchTerm = '';
21+
function searchService($resource, baseUrlSrv) {
22+
this.search = function(term) {
23+
this.searchTerm = term.q;
24+
console.log('Searching for: %o', term.q);
25+
if (!term.q) { //TODO(bzz): empty string check
26+
return;
27+
}
28+
var encQuery = window.encodeURIComponent(term.q);
29+
return $resource(baseUrlSrv.getRestApiBase() + '/notebook/search?q=' + encQuery, {}, {
30+
query: {method: 'GET'}
31+
});
32+
};
3133

32-
});
34+
this.searchTerm = '';
35+
}
36+
37+
})();

0 commit comments

Comments
 (0)