Yangman - strings to constants
[dlux.git] / modules / common-yangutils-resources / src / main / resources / yangutils / services / custom-funct.services.js
1 define([], function () {
2     'use strict';
3
4     function CustomFuncService(){
5
6         var service = {
7             createCustomFunctionalityApis: createCustomFunctionalityApis,
8             createNewFunctionality: createNewFunctionality,
9             getMPCustFunctionality: getMPCustFunctionality,
10         };
11
12         return service;
13
14         /**
15          * Base custom functionality object
16          * @param label
17          * @param node
18          * @param callback
19          * @param viewStr
20          * @param hideButtonOnSelect
21          * @constructor
22          */
23         function CustFunctionality(label, node, callback, viewStr, hideButtonOnSelect) {
24             this.label = label;
25             this.callback = callback;
26             this.viewStr = viewStr;
27             this.hideButtonOnSelect = hideButtonOnSelect;
28
29             this.setCallback = function (callback) {
30                 this.callback = callback;
31             };
32
33             this.runCallback = function (args){
34                 (this.callback || angular.noop)(args);
35             };
36         }
37
38         // TODO: add function's description
39         function cmpApiToTemplatePath(subApi, templateStr) {
40             var subApiStr = subApi.storage + '/' + subApi.pathTemplateString;
41             return subApiStr === templateStr;
42         }
43
44         // TODO: add service's description
45         function createNewFunctionality(label, node, callback, viewStr, hideButtonOnSelect) {
46             if (node && callback) {
47                 return new CustFunctionality(label, node, callback, viewStr, hideButtonOnSelect);
48             } else {
49                 console.error('no node or callback is set for custom functionality');
50             }
51         }
52
53         // TODO: add service's description
54         function getMPCustFunctionality(funcList) {
55             var mpCF = funcList.filter(function (cf) {
56                 return cf.label === 'YANGUI_CUST_MOUNT_POINTS';
57             });
58
59             return mpCF[0];
60         }
61
62         // TODO: add service's description
63         function createCustomFunctionalityApis(apis, module, revision, pathString, label,
64                                                callback, viewStr, hideButtonOnSelect) {
65             apis = apis.map(function (item) {
66                 if ((module ? item.module === module : true) && (revision ? item.revision === revision : true)) {
67
68                     item.subApis = item.subApis.map(function (subApi) {
69
70                         if (cmpApiToTemplatePath(subApi, pathString)) {
71                             subApi.addCustomFunctionality(label, callback, viewStr, hideButtonOnSelect);
72                         }
73
74                         return subApi;
75                     });
76                 }
77
78                 return item;
79             });
80         }
81     }
82
83     CustomFuncService.$inject = [];
84
85     return CustomFuncService;
86
87 });