1 define([], function () {
4 function PathUtilsService(ArrayUtilsService){
8 checkEmptyIdentifiers: checkEmptyIdentifiers,
9 createPathElement: createPathElement,
10 getModuleNameFromPath: getModuleNameFromPath,
11 getStorageAndNormalizedPath: getStorageAndNormalizedPath,
12 fillIdentifiers: fillIdentifiers,
13 findIndexOfStrInPathStr: findIndexOfStrInPathStr,
14 fillListNode: fillListNode,
15 fillListRequestData: fillListRequestData,
18 searchNodeByPath: searchNodeByPath,
20 translatePathArray: translatePathArray,
23 getModuleNodePair: getModuleNodePair,
24 isIdentifier: isIdentifier,
31 // TODO: add service's description
32 function createPathElement(name, module, identifierStrings, moduleChanged, revision) {
33 return new PathElem(name, module, identifierStrings, moduleChanged, revision);
36 // TODO: add service's description
37 function search(node, path) {
38 var pathElem = path.shift(),
39 selNode = pathElem.name === parentPath ?
41 ArrayUtilsService.getFirstElementByCondition(node.children, function (child) {
42 return pathElem.checkNode(child);
45 if (selNode !== null) {
47 return search(selNode, path);
52 console.warn('search: cannot find element ', pathElem.name);
58 * Translate path url into path elements array
60 * @param prefixConverter
62 * @param getDefaultModuleCallback
63 * @param notIdentifiers
66 function translate(path, prefixConverter, importNodes, getDefaultModuleCallback, notIdentifiers) {
67 var pathStrElements = path.split('/').filter(function (e) {
70 pathArrayElements = [],
72 maxIndex = pathStrElements.length,
73 getLastElement = function (a) {
74 return pathArrayElements.length > 0 ? pathArrayElements[pathArrayElements.length - 1] : null;
76 getElementModule = function (e) {
77 return e ? e.module : '';
79 getModuleChange = function (actModule, lastElemModule) {
80 return (lastElemModule !== null) ? actModule !== lastElemModule : false;
83 for (index = 0; index < maxIndex; index += 1) {
84 var actElem = pathStrElements[index],
85 lastElem = getLastElement(pathArrayElements),
86 checkIdentifier = notIdentifiers ? false : isIdentifier(actElem);
88 if (checkIdentifier && lastElem) {
89 lastElem.addIdentifier(actElem.slice(1, -1));
92 var lastElemModule = getElementModule(lastElem),
93 defaultModule = getDefaultModuleCallback ? getDefaultModuleCallback() : lastElemModule,
94 pair = getModuleNodePair(actElem, defaultModule),
95 processedModule = (prefixConverter && pair[0] !== lastElemModule) ?
96 prefixConverter(pair[0]) : pair[0],
97 revision = importNodes ? searchForRevisionInImportNodes(processedModule, importNodes) : null,
98 pathElem = createPathElement(pair[1], processedModule, null, getModuleChange(processedModule, lastElemModule), revision);
100 pathArrayElements.push(pathElem);
104 return pathArrayElements;
107 // TODO: add service's description
108 function translatePathArray(pathArray) {
109 var getIdentifiersValues = function (identifiers) {
110 return identifiers.map(function (i) {
111 return i.value.length ? i.value.replace(/\//g, '%2F') : '{' + i.label + '}';
114 getLastElem = function (i) {
117 result = pathArray[i - 1];
121 getModuleStr = function (actElem, lastElem) {
122 return ((lastElem && actElem.module && lastElem.module !== actElem.module) ?
123 (actElem.module + ':') : '');
125 getIdentifiersStr = function (actElem) {
126 return (actElem.hasIdentifier() ? '/' + getIdentifiersValues(actElem.identifiers) : '');
128 getElemStr = function (actElem, lastElem) {
129 return getModuleStr(actElem, lastElem) + actElem.name + getIdentifiersStr(actElem);
132 return pathArray.map(function (pe, i) {
133 return getElemStr(pe, getLastElem(i));
138 * Check if in path elements array is empty identifier
142 function checkEmptyIdentifiers(pathArray){
143 return pathArray.some(function (item) {
144 return item.hasEmptyIdentifier();
149 * Service for filling API url object from url string data
153 function fillPath(pathArrayIn, pathString) {
154 var pathArray = trimPath(pathString).split('/'),
157 pathArrayIn.forEach(function (pathItem){
158 if ( pathItem.hasIdentifier() ){
159 pathItem.identifiers.forEach(function (identifier){
161 identifier.value = isIdentifier(pathArray[pathPosition]) ? '' : pathArray[pathPosition];
170 * Service for clearing api path object
173 function clearPath(pathArrayIn){
174 pathArrayIn.forEach(function (pathItem){
175 if ( pathItem.hasIdentifier() ){
176 pathItem.identifiers.forEach(function (identifier){
177 identifier.value = '';
183 // TODO: add service's description
184 function getModuleNameFromPath(path){
185 var pathArray = translate(trimPath(path));
187 return pathArray.length > 1 ? pathArray[1].module : null;
190 // TODO: add service's description
191 function searchNodeByPath(pathString, treeApis, treeData, disabledExpand, notIdentifiers) {
192 var pathArray = translate(trimPath(pathString), null, null, null, notIdentifiers),
193 module = pathArray.length > 1 ? pathArray[1].module : null,
194 selectedTreeApi = module ? treeApis.filter(function (treeApi) {
195 return treeApi.module === module;
199 if (selectedTreeApi && pathArray.length) {
200 var actElem = selectedTreeApi,
201 continueCondition = true;
203 if ( !disabledExpand ) {
204 changeTreeDataByProp(treeData, ['expanded', 'selected'], [false, false]);
207 for (var i = 0; i < pathArray.length && continueCondition; ) {
208 if ( !disabledExpand ) {
209 changeTreeDataNode(actElem, treeData, 'expanded', true);
212 var nextElem = getActElementChild(actElem, pathArray[i].name);
213 if (nextElem !== null) {
215 i = i + ( actElem && actElem.identifiersLength > 0 ? actElem.identifiersLength + 1 : 1);
217 continueCondition = false;
221 if ( !disabledExpand ) {
222 changeTreeDataNode(actElem, treeData, 'selected', true);
226 retObj = { indexApi: actElem.indexApi, indexSubApi: actElem.indexSubApi };
233 * Fill path element's identifiers
238 function fillIdentifiers(identifiers, label, value) {
239 identifiers.some(function (i) {
240 var identifierMatch = i.label === label;
241 if (identifierMatch) {
242 i.value = value || '';
245 return identifierMatch;
249 // TODO: add service's description
250 function fillListNode(node, label, value) {
251 if (node.type === 'list' && node.actElemStructure !== null) {
252 var nodeToFill = node.actElemStructure.getChildren('leaf', label)[0];
255 nodeToFill.fill(nodeToFill.label, value);
260 // TODO: add service's description
261 function fillListRequestData(data, listLabel, label, value){
262 if ( data.hasOwnProperty(listLabel) && data[listLabel].length ) {
263 data[listLabel][0][label] = value;
267 // TODO: add service's description
268 function findIndexOfStrInPathStr(pathParts, targetStr) { // pathParts is path string split by '/'
269 var targetIndex = -1;
271 pathParts.some(function (p, i) {
272 var condition = p === targetStr;
282 // TODO: add service's description
283 function getStorageAndNormalizedPath(pathStr) {
284 var pathParts = pathStr.split('/'),
285 restconfIndex = findIndexOfStrInPathStr(pathParts, 'restconf'),
286 storage = pathParts[restconfIndex + 1],
287 normalizedPath = pathParts.slice(restconfIndex + 1).join('/');
289 return { storage: storage, normalizedPath: normalizedPath };
293 * Base identifier object
298 function Identifier(label, value) {
300 this.value = value || '';
304 * Base path element object
307 * @param identifierNames
308 * @param moduleChanged
312 function PathElem(name, module, identifierNames, moduleChanged, revision) {
314 this.module = module;
315 this.identifiers = identifierNames ? identifierNames.map(function (name) {
316 return new Identifier(name);
318 this.moduleChanged = moduleChanged || false;
319 this.revision = revision;
321 this.equals = function (comparedElem, compareIdentifierValues) {
322 var result = this.name === comparedElem.name &&
323 this.module === comparedElem.module &&
324 this.identifiers.length === comparedElem.identifiers.length;
327 var identifiersCnt = this.identifiers.length,
330 for (i = 0; i < identifiersCnt && result; i++) {
331 result = this.identifiers[i].label === comparedElem.identifiers[i].label;
332 if (compareIdentifierValues) {
333 result = this.identifiers[i].value === comparedElem.identifiers[i].value;
341 this.hasIdentifier = function () {
342 return this.identifiers.length > 0;
345 this.addIdentifier = function (name) {
346 this.identifiers.push(new Identifier(name));
349 this.getIdentifierValues = function () {
350 return this.identifiers.map(function (i) {
355 this.hasEmptyIdentifier = function () {
356 return this.identifiers.some(function (item) {
357 return item.value.length === 0;
361 this.toString = function () {
362 return (this.module ? this.module + ':' : '') + this.name + '/' + (this.hasIdentifier() ?
363 this.getIdentifierValues().join('/') + '/' : '');
366 this.checkNode = function (node) {
367 return (this.module ? this.module === node.module : true) &&
368 (this.name ? this.name === node.label : true) &&
369 (this.revision ? this.revision === node.moduleRevision : true);
372 this.clone = function () {
373 var copy = new PathElem(this.name, this.module, null, this.moduleChanged, this.revision);
375 copy.identifiers = this.identifiers.map(function (i) {
376 return new Identifier(i.label, i.value);
383 // TODO: add function's description
384 function trimPath(pathString) {
385 var searchStr = 'restconf',
388 if (pathString.indexOf(searchStr) > -1) {
389 output = pathString.slice(pathString.indexOf(searchStr) + searchStr.length + 1);
395 // TODO: add function's description
396 function changeTreeDataNode(treeApiNode, treeData, prop, val) {
397 var sel = treeApiNode ? treeData.filter(function (d) {
398 return d.branch.uid === treeApiNode.uid;
401 if (sel.length === 1) {
402 sel[0].branch[prop] = val;
406 // TODO: add function's description
407 function changeTreeDataByProp(treeData, props, vals) {
408 treeData.forEach(function (d, index) {
409 props.forEach(function (v, i){
410 d.branch[v] = vals[i];
415 // TODO: add function's description
416 function getActElementChild(actElem, childLabel) {
417 var sel = actElem.children.filter(function (child) {
418 return child.label === childLabel;
420 ret = sel.length === 1 ? sel[0] : null;
425 // TODO: add function's description
426 function getModuleNodePair(pathString, defaultModule) {
427 return pathString.indexOf(':') > -1 ? pathString.split(':') : [defaultModule, pathString];
431 * Tool for check if item is identifier
435 function isIdentifier(item) {
436 return (item.indexOf('{') === item.indexOf('}')) === false;
439 // TODO: add function's description
440 function searchForRevisionInImportNodes(module, importNodes) {
442 node = importNodes.filter(function (i) {
443 return i.label === module;
447 revision = node._revisionDate;
454 PathUtilsService.$inject = ['ArrayUtilsService'];
456 return PathUtilsService;