Yang UI, Yang Utils - split services into separate files
[dlux.git] / modules / common-yangutils-resources / src / main / resources / yangutils / services / path-utils.services.js
1 define([], function () {
2     'use strict';
3
4     function PathUtilsService(ArrayUtilsService){
5         var pathUtils = {},
6             parentPath = '..';
7
8         var Idenfitier = function(label, value) {
9             this.label = label;
10             this.value = value || '';
11         };
12
13         var PathElem = function (name, module, identifierNames, moduleChanged, revision) {
14             this.name = name;
15             this.module = module;
16             this.identifiers = identifierNames ? identifierNames.map(function(name) {
17                 return new Idenfitier(name);
18             }) : [];
19             this.moduleChanged = moduleChanged || false;
20             this.revision = revision;
21
22             this.equals = function(comparedElem, compareIdentifierValues) {
23                 var result = this.name === comparedElem.name && this.module === comparedElem.module && this.identifiers.length === comparedElem.identifiers.length;
24
25                 if(result) {
26                     var identifiersCnt = this.identifiers.length,
27                         i;
28
29                     for(i = 0; i < identifiersCnt && result; i++) {
30                         result = this.identifiers[i].label === comparedElem.identifiers[i].label;
31                         if(compareIdentifierValues) {
32                             result = this.identifiers[i].value === comparedElem.identifiers[i].value;
33                         }
34                     }
35                 }
36
37                 return result;
38             };
39
40             this.hasIdentifier = function () {
41                 return this.identifiers.length > 0;
42             };
43
44             this.addIdentifier = function(name) {
45                 this.identifiers.push(new Idenfitier(name));
46             };
47
48             this.getIdentifierValues = function() {
49                 return this.identifiers.map(function(i) {
50                     return i.value;
51                 });
52             };
53
54             this.toString = function () {
55                 return (this.module ? this.module + ':' : '') + this.name + '/' + (this.hasIdentifier() ? this.getIdentifierValues().join('/') + '/' : '');
56             };
57
58             this.checkNode = function (node) {
59                 return (this.module ? this.module === node.module : true) && (this.name ? this.name === node.label : true) && (this.revision ? this.revision === node.moduleRevision : true);
60             };
61
62             this.clone = function() {
63                 var copy = new PathElem(this.name, this.module, null, this.moduleChanged, this.revision);
64
65                 copy.identifiers = this.identifiers.map(function(i) {
66                     return new Idenfitier(i.label, i.value);
67                 });
68
69                 return copy;
70             };
71         };
72
73         var getModuleNodePair = function (pathString, defaultModule) {
74             return pathString.indexOf(':') > -1 ? pathString.split(':') : [defaultModule, pathString];
75         };
76
77         var isIdentifier = function (item) {
78             return (item.indexOf('{') === item.indexOf('}')) === false;
79         };
80
81         var searchForRevisionInImportNodes = function(module, importNodes) {
82             var revision = null,
83                 node = importNodes.filter(function(i) {
84                     return i.label === module;
85                 })[0];
86
87             if(node) {
88                 revision = node._revisionDate;
89             }
90
91             return revision;
92         };
93
94         pathUtils.createPathElement = function (name, module, identifierStrings, moduleChanged, revision) {
95             return new PathElem(name, module, identifierStrings, moduleChanged, revision);
96         };
97
98         pathUtils.search = function (node, path) {
99             var pathElem = path.shift(),
100                 selNode = pathElem.name === parentPath ?
101                     node.parent :
102                     ArrayUtilsService.getFirstElementByCondition(node.children, function (child) {
103                         return pathElem.checkNode(child);
104                     });
105
106             if (selNode !== null) {
107                 if (path.length) {
108                     return pathUtils.search(selNode, path);
109                 } else {
110                     return selNode;
111                 }
112             } else {
113                 console.warn('pathUtils.search: cannot find element ',pathElem.name);
114                 return null;
115             }
116         };
117
118         pathUtils.translate = function(path, prefixConverter, importNodes, getDefaultModuleCallback) {
119             var pathStrElements = path.split('/').filter(function(e) {
120                     return e !== '';
121                 }),
122                 pathArrayElements = [],
123                 index,
124                 maxIndex = pathStrElements.length,
125                 getLastElement = function(a) {
126                     return pathArrayElements.length > 0 ? pathArrayElements[pathArrayElements.length - 1] : null;
127                 },
128                 getElementModule = function(e) {
129                     return e ? e.module : '';
130                 },
131                 getModuleChange = function(actModule, lastElemModule) {
132                     return (lastElemModule !== null) ? actModule !== lastElemModule : false;
133                 };
134
135             for(index = 0; index < maxIndex; index += 1) {
136                 var actElem = pathStrElements[index],
137                     lastElem = getLastElement(pathArrayElements);
138
139                 if(isIdentifier(actElem) && lastElem) {
140                     lastElem.addIdentifier(actElem.slice(1, -1));
141                 } else {
142
143                     var lastElemModule = getElementModule(lastElem),
144                         defaultModule = getDefaultModuleCallback ? getDefaultModuleCallback() : lastElemModule,
145                         pair = getModuleNodePair(actElem, defaultModule),
146                         processedModule = (prefixConverter && pair[0] !== lastElemModule) ? prefixConverter(pair[0]) : pair[0],
147                         revision = importNodes ? searchForRevisionInImportNodes(processedModule, importNodes) : null,
148                         pathElem = pathUtils.createPathElement(pair[1], processedModule, null, getModuleChange(processedModule, lastElemModule), revision);
149
150                     pathArrayElements.push(pathElem);
151                 }
152             }
153
154             return pathArrayElements;
155         };
156
157         pathUtils.translatePathArray = function(pathArray) {
158             var getIdentifiersValues = function(identifiers) {
159                     return identifiers.map(function(i) {
160                         return i.value.replace(/\//g, '%2F');
161                     }).join('/');
162                 },
163                 getLastElem = function(i) {
164                     var result = null;
165                     if((i - 1) >= 0) {
166                         result = pathArray[i - 1];
167                     }
168                     return result;
169                 },
170                 getModuleStr = function(actElem, lastElem) {
171                     return ((lastElem && actElem.module && lastElem.module !== actElem.module) ? (actElem.module + ':') : '');
172                 },
173                 getIdentifiersStr = function(actElem) {
174                     return (actElem.hasIdentifier() ? '/' + getIdentifiersValues(actElem.identifiers) : '');
175                 },
176                 getElemStr = function(actElem, lastElem) {
177                     return getModuleStr(actElem, lastElem) + actElem.name + getIdentifiersStr(actElem);
178                 };
179
180             return pathArray.map(function(pe, i) {
181                 return getElemStr(pe, getLastElem(i));
182             });
183         };
184
185         var trimPath = function(pathString) {
186             var searchStr = 'restconf',
187                 output = pathString;
188
189             if(pathString.indexOf(searchStr) > -1) {
190                 output = pathString.slice(pathString.indexOf(searchStr)+searchStr.length+1);
191             }
192
193             return output;
194         };
195
196         var changeTreeDataNode = function(treeApiNode, treeData, prop, val) {
197             var sel = treeApiNode ? treeData.filter(function(d) {
198                 return d.branch.uid === treeApiNode.uid;
199             }) : [];
200
201             if(sel.length === 1) {
202                 sel[0].branch[prop] = val;
203             }
204         };
205
206         var changeTreeDataByProp = function(treeData, props, vals) {
207             treeData.forEach(function(d, index) {
208                 props.forEach(function(v, i){
209                     d.branch[v] = vals[i];
210                 });
211             });
212         };
213
214         pathUtils.fillPath = function(pathArrayIn, pathString) {
215             var pathArray = trimPath(pathString).split('/'),
216                 pathPosition = 0;
217
218             pathArrayIn.forEach(function(pathItem, index){
219                 if ( pathItem.hasIdentifier() ){
220                     pathItem.identifiers.forEach(function(identifier){
221                         pathPosition++;
222                         identifier.value = pathArray[pathPosition];
223                     });
224                 }
225                 pathPosition++;
226             });
227
228         };
229
230         var getActElementChild = function(actElem, childLabel) {
231             var sel = actElem.children.filter(function(child) {
232                     return child.label === childLabel;
233                 }),
234                 ret = sel.length === 1 ? sel[0] : null;
235
236             return ret;
237         };
238
239         pathUtils.getModuleNameFromPath = function(path){
240             var pathArray = pathUtils.translate(trimPath(path));
241
242             return pathArray.length > 1 ? pathArray[1].module : null;
243         };
244
245         pathUtils.searchNodeByPath = function(pathString, treeApis, treeData, disabledExpand) {
246             var pathArray = pathUtils.translate(trimPath(pathString)),
247                 module = pathArray.length > 1 ? pathArray[1].module : null,
248                 selectedTreeApi = module ? treeApis.filter(function(treeApi) {
249                     return treeApi.module === module;
250                 })[0] : null,
251                 retObj = null;
252
253             if(selectedTreeApi && pathArray.length) {
254                 var actElem = selectedTreeApi,
255                     continueCondition = true;
256
257                 if ( !disabledExpand ) {
258                     changeTreeDataByProp(treeData, ['expanded','selected'], [false, false]);
259                 }
260
261                 for(var i = 0; i < pathArray.length && continueCondition; ) {
262                     if ( !disabledExpand ) {
263                         changeTreeDataNode(actElem, treeData, 'expanded', true);
264                     }
265
266                     var nextElem = getActElementChild(actElem, pathArray[i].name);
267                     if(nextElem !== null) {
268                         actElem = nextElem;
269                         i = i + ( actElem && actElem.identifiersLength > 0 ? actElem.identifiersLength + 1 : 1);
270                     } else {
271                         continueCondition = false;
272                     }
273                 }
274
275                 if ( !disabledExpand ) {
276                     changeTreeDataNode(actElem, treeData, 'selected', true);
277                 }
278
279                 if(actElem) {
280                     retObj = { indexApi: actElem.indexApi, indexSubApi: actElem.indexSubApi };
281                 }
282             }
283             return retObj;
284         };
285
286         pathUtils.fillIdentifiers = function(identifiers, label, value) {
287             identifiers.some(function(i) {
288                 var identifierMatch = i.label === label;
289                 if(identifierMatch) {
290                     i.value = value || '';
291                 }
292
293                 return identifierMatch;
294             });
295         };
296
297         pathUtils.fillListNode = function(node, label, value) {
298             if(node.type === 'list' && node.actElemStructure !== null) {
299                 var nodeToFill = node.actElemStructure.getChildren('leaf', label)[0];
300
301                 if(nodeToFill) {
302                     nodeToFill.fill(nodeToFill.label, value);
303                 }
304             }
305         };
306
307         pathUtils.fillListRequestData = function(data, listLabel, label, value){
308             if ( data.hasOwnProperty(listLabel) && data[listLabel].length ) {
309                 data[listLabel][0][label] = value;
310             }
311         };
312
313         pathUtils.findIndexOfStrInPathStr = function(pathParts, targetStr) { //pathParts is path string split by '/'
314             var targetIndex = -1;
315
316             pathParts.some(function(p, i) {
317                 var condition = p === targetStr;
318                 if(condition) {
319                     targetIndex = i;
320                 }
321                 return condition;
322             });
323
324             return targetIndex;
325         };
326
327         pathUtils.getStorageAndNormalizedPath = function(pathStr) {
328             var pathParts = pathStr.split('/'),
329                 restconfIndex = pathUtils.findIndexOfStrInPathStr(pathParts, 'restconf'),
330                 storage = pathParts[restconfIndex + 1],
331                 normalizedPath = pathParts.slice(restconfIndex + 1).join('/');
332
333             return { storage: storage, normalizedPath: normalizedPath };
334         };
335
336         pathUtils.__test = {
337             PathElem: PathElem,
338             getModuleNodePair: getModuleNodePair,
339             isIdentifier: isIdentifier
340         };
341
342         return pathUtils;
343     }
344
345     PathUtilsService.$inject=['ArrayUtilsService'];
346
347     return PathUtilsService;
348
349 });