This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionalModule.js
73 lines (57 loc) · 2.22 KB
/
FunctionalModule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var xmldom = require('xmldom');
var DOMParser = xmldom.DOMParser;
function FunctionalModule(name, loadOnStartup) {
this.name = name;
this.loadOnStartup = loadOnStartup || false;
this.jsModules = []; // String[]
this.dependentModules = []; // String[]
this.views = []; // String[]
}
/* STATIC METHODS */
FunctionalModule.loadModules = function loadModulesFromXmlDocument(documentElement) {
var modules = [];
var xmlElements = documentElement.getElementsByTagName('functionalModule');
for (var i = 0; i < xmlElements.length; i++)
modules.push(FunctionalModule.loadModule(xmlElements[i]));
return modules;
}
FunctionalModule.loadModule = function loadModuleFromXmlElement(xmlElement) {
if (xmlElement.nodeName != 'functionalModule')
throw "XML node is not from a functional module";
var getIdentifierList = (tagName) => {
var elements = xmlElement.getElementsByTagName(tagName)[0];
if (!!elements)
return elements.firstChild.nodeValue.split(',').map(text => text.trim());
return [];
};
var module = new FunctionalModule(xmlElement.getAttribute('name'), false);
if (xmlElement.hasAttribute('loadOnStartUp'))
module.loadOnStartup = xmlElement.getAttribute('loadOnStartUp').toLowerCase() === 'true';
module.jsModules = getIdentifierList('jsModules');
module.views = getIdentifierList('views');
module.dependentModules = getIdentifierList('dependentModules');
return module;
}
/* METHODS */
FunctionalModule.prototype.toXMLNode = function() {
var dom = new DOMParser().parseFromString('<functionalModule/>');
var doc = dom.documentElement;
doc.setAttribute('name', this.name);
doc.setAttribute('loadOnStartUp', this.loadOnStartup);
var appendIdentifierListNode = (ids, nodeName) => {
if (ids && ids.length) {
var node = dom.createElement(nodeName);
var text = dom.createTextNode(ids.join(','));
node.appendChild(text);
doc.appendChild(node);
};
}
appendIdentifierListNode(this.jsModules, 'jsModules');
appendIdentifierListNode(this.views, 'views');
appendIdentifierListNode(this.dependentModules, 'dependentModules');
return doc;
};
FunctionalModule.prototype.toString = function() {
return this.name;
};
module.exports = FunctionalModule;