1 define([], function () {
4 function PathUtilsService(ArrayUtilsService){
8 var Idenfitier = function(label, value) {
10 this.value = value || '';
13 var PathElem = function (name, module, identifierNames, moduleChanged, revision) {
16 this.identifiers = identifierNames ? identifierNames.map(function(name) {
17 return new Idenfitier(name);
19 this.moduleChanged = moduleChanged || false;
20 this.revision = revision;
22 this.equals = function(comparedElem, compareIdentifierValues) {
23 var result = this.name === comparedElem.name && this.module === comparedElem.module && this.identifiers.length === comparedElem.identifiers.length;
26 var identifiersCnt = this.identifiers.length,
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;
40 this.hasIdentifier = function () {
41 return this.identifiers.length > 0;
44 this.addIdentifier = function(name) {
45 this.identifiers.push(new Idenfitier(name));
48 this.getIdentifierValues = function() {
49 return this.identifiers.map(function(i) {
54 this.toString = function () {
55 return (this.module ? this.module + ':' : '') + this.name + '/' + (this.hasIdentifier() ? this.getIdentifierValues().join('/') + '/' : '');
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);
62 this.clone = function() {
63 var copy = new PathElem(this.name, this.module, null, this.moduleChanged, this.revision);
65 copy.identifiers = this.identifiers.map(function(i) {
66 return new Idenfitier(i.label, i.value);
73 var getModuleNodePair = function (pathString, defaultModule) {
74 return pathString.indexOf(':') > -1 ? pathString.split(':') : [defaultModule, pathString];
77 var isIdentifier = function (item) {
78 return (item.indexOf('{') === item.indexOf('}')) === false;
81 var searchForRevisionInImportNodes = function(module, importNodes) {
83 node = importNodes.filter(function(i) {
84 return i.label === module;
88 revision = node._revisionDate;
94 pathUtils.createPathElement = function (name, module, identifierStrings, moduleChanged, revision) {
95 return new PathElem(name, module, identifierStrings, moduleChanged, revision);
98 pathUtils.search = function (node, path) {
99 var pathElem = path.shift(),
100 selNode = pathElem.name === parentPath ?
102 ArrayUtilsService.getFirstElementByCondition(node.children, function (child) {
103 return pathElem.checkNode(child);
106 if (selNode !== null) {
108 return pathUtils.search(selNode, path);
113 console.warn('pathUtils.search: cannot find element ',pathElem.name);
118 pathUtils.translate = function(path, prefixConverter, importNodes, getDefaultModuleCallback) {
119 var pathStrElements = path.split('/').filter(function(e) {
122 pathArrayElements = [],
124 maxIndex = pathStrElements.length,
125 getLastElement = function(a) {
126 return pathArrayElements.length > 0 ? pathArrayElements[pathArrayElements.length - 1] : null;
128 getElementModule = function(e) {
129 return e ? e.module : '';
131 getModuleChange = function(actModule, lastElemModule) {
132 return (lastElemModule !== null) ? actModule !== lastElemModule : false;
135 for(index = 0; index < maxIndex; index += 1) {
136 var actElem = pathStrElements[index],
137 lastElem = getLastElement(pathArrayElements);
139 if(isIdentifier(actElem) && lastElem) {
140 lastElem.addIdentifier(actElem.slice(1, -1));
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);
150 pathArrayElements.push(pathElem);
154 return pathArrayElements;
157 pathUtils.translatePathArray = function(pathArray) {
158 var getIdentifiersValues = function(identifiers) {
159 return identifiers.map(function(i) {
160 return i.value.replace(/\//g, '%2F');
163 getLastElem = function(i) {
166 result = pathArray[i - 1];
170 getModuleStr = function(actElem, lastElem) {
171 return ((lastElem && actElem.module && lastElem.module !== actElem.module) ? (actElem.module + ':') : '');
173 getIdentifiersStr = function(actElem) {
174 return (actElem.hasIdentifier() ? '/' + getIdentifiersValues(actElem.identifiers) : '');
176 getElemStr = function(actElem, lastElem) {
177 return getModuleStr(actElem, lastElem) + actElem.name + getIdentifiersStr(actElem);
180 return pathArray.map(function(pe, i) {
181 return getElemStr(pe, getLastElem(i));
185 var trimPath = function(pathString) {
186 var searchStr = 'restconf',
189 if(pathString.indexOf(searchStr) > -1) {
190 output = pathString.slice(pathString.indexOf(searchStr)+searchStr.length+1);
196 var changeTreeDataNode = function(treeApiNode, treeData, prop, val) {
197 var sel = treeApiNode ? treeData.filter(function(d) {
198 return d.branch.uid === treeApiNode.uid;
201 if(sel.length === 1) {
202 sel[0].branch[prop] = val;
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];
214 pathUtils.fillPath = function(pathArrayIn, pathString) {
215 var pathArray = trimPath(pathString).split('/'),
218 pathArrayIn.forEach(function(pathItem, index){
219 if ( pathItem.hasIdentifier() ){
220 pathItem.identifiers.forEach(function(identifier){
222 identifier.value = pathArray[pathPosition];
230 var getActElementChild = function(actElem, childLabel) {
231 var sel = actElem.children.filter(function(child) {
232 return child.label === childLabel;
234 ret = sel.length === 1 ? sel[0] : null;
239 pathUtils.getModuleNameFromPath = function(path){
240 var pathArray = pathUtils.translate(trimPath(path));
242 return pathArray.length > 1 ? pathArray[1].module : null;
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;
253 if(selectedTreeApi && pathArray.length) {
254 var actElem = selectedTreeApi,
255 continueCondition = true;
257 if ( !disabledExpand ) {
258 changeTreeDataByProp(treeData, ['expanded','selected'], [false, false]);
261 for(var i = 0; i < pathArray.length && continueCondition; ) {
262 if ( !disabledExpand ) {
263 changeTreeDataNode(actElem, treeData, 'expanded', true);
266 var nextElem = getActElementChild(actElem, pathArray[i].name);
267 if(nextElem !== null) {
269 i = i + ( actElem && actElem.identifiersLength > 0 ? actElem.identifiersLength + 1 : 1);
271 continueCondition = false;
275 if ( !disabledExpand ) {
276 changeTreeDataNode(actElem, treeData, 'selected', true);
280 retObj = { indexApi: actElem.indexApi, indexSubApi: actElem.indexSubApi };
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 || '';
293 return identifierMatch;
297 pathUtils.fillListNode = function(node, label, value) {
298 if(node.type === 'list' && node.actElemStructure !== null) {
299 var nodeToFill = node.actElemStructure.getChildren('leaf', label)[0];
302 nodeToFill.fill(nodeToFill.label, value);
307 pathUtils.fillListRequestData = function(data, listLabel, label, value){
308 if ( data.hasOwnProperty(listLabel) && data[listLabel].length ) {
309 data[listLabel][0][label] = value;
313 pathUtils.findIndexOfStrInPathStr = function(pathParts, targetStr) { //pathParts is path string split by '/'
314 var targetIndex = -1;
316 pathParts.some(function(p, i) {
317 var condition = p === targetStr;
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('/');
333 return { storage: storage, normalizedPath: normalizedPath };
338 getModuleNodePair: getModuleNodePair,
339 isIdentifier: isIdentifier
345 PathUtilsService.$inject=['ArrayUtilsService'];
347 return PathUtilsService;