Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / js / services / policynavigator.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 (function(angular) {
21     'use strict';
22     angular.module('abs').service('policyNavigator', [
23         '$http', '$q', 'policyManagerConfig', 'item', function ($http, $q, policyManagerConfig, Item) {
24
25         $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
26
27         var PolicyNavigator = function() {
28             this.requesting = false;
29             this.fileList = [];
30             this.currentPath = [];
31             this.history = [];
32             this.error = '';
33         };
34
35         PolicyNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
36             if (!data || typeof data !== 'object') {
37                 this.error = 'Bridge response error, please check the docs';
38             }
39             if (!this.error && data.result && data.result.error) {
40                 this.error = data.result.error;
41             }
42             if (!this.error && data.error) {
43                 this.error = data.error.message;
44             }
45             if (!this.error && defaultMsg) {
46                 this.error = defaultMsg;
47             }
48             if (this.error) {
49                 return deferred.reject(data);
50             }
51             return deferred.resolve(data);
52         };
53         
54         PolicyNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
55                 self.fileList = [];
56                  self.error = '';
57             if (!data || typeof data !== 'object') {
58                 this.error = 'Bridge response error, please check the docs';
59             }
60             if (!this.error && data.result && data.result.error) {
61                 this.error = data.result.error;
62             }
63             if (!this.error && data.error) {
64                 this.error = data.error.message;
65             }
66             if (!this.error && defaultMsg) {
67                 this.error = defaultMsg;
68             }
69             if (this.error) {
70                 return deferred.reject(data);
71             }
72             return deferred.resolve(data);
73         };
74
75         PolicyNavigator.prototype.list = function() {
76             var self = this;
77             var deferred = $q.defer();
78             var path = self.currentPath.join('/');
79             var data = {params: {
80                 mode: 'LIST',
81                 onlyFolders: false,
82                 path: '/' + path
83             }};
84
85             self.requesting = true;
86             self.fileList = [];
87             self.error = '';
88
89             $http.post(policyManagerConfig.listUrl, data).success(function(data) {
90                 self.deferredHandler(data, deferred);
91             }).error(function(data) {
92                 self.deferredHandler(data, deferred, 'Unknown error listing, check the response');
93             })['finally'](function() {
94                 self.requesting = false;
95             });
96             return deferred.promise;
97         };
98
99         PolicyNavigator.prototype.refresh = function() {
100             var self = this;
101             var path = self.currentPath.join('/');
102             return self.list().then(function(data) {
103                 self.fileList = (data.result || []).map(function(file) {
104                     return new Item(file, self.currentPath);
105                 });
106                 self.buildTree(path);
107             });
108         };
109         
110         PolicyNavigator.prototype.policylist = function(finalpath) {
111             var self = this;
112             var deferred = $q.defer();
113             var path = finalpath;
114             var data = {params: {
115                 mode: 'LIST',
116                 onlyFolders: false,
117                 path: '/' + path
118             }};
119
120             self.requesting = true;
121             self.fileList = [];
122             self.error = '';
123
124             $http.post(policyManagerConfig.listUrl, data).success(function(data) {
125                 self.deferredHandler(data, deferred);
126             }).error(function(data) {
127                 self.deferredHandler(data, deferred, 'Unknown error listing, check the response');
128             })['finally'](function() {
129                 self.requesting = false;
130             });
131             return deferred.promise;
132         };
133
134         PolicyNavigator.prototype.policyrefresh = function(finalpath) {
135             var self = this;
136             var path = finalpath;
137             self.currentPath = path;
138             return self.policylist(finalpath).then(function(data) {
139                 self.fileList = (data.result || []).map(function(file) {
140                     return new Item(file, self.currentPath);
141                 });
142                 self.buildTree(path);
143             });
144         };
145         
146         
147         PolicyNavigator.prototype.buildTree = function(path) {
148             var flatNodes = [], selectedNode = {};
149
150             function recursive(parent, item, path) {
151                 var absName = path ? (path + '/' + item.model.name) : item.model.name;
152                 if (parent.name.trim() && path.trim().indexOf(parent.name) !== 0) {
153                     parent.nodes = [];
154                 }
155                 if (parent.name !== path) {
156                     for (var i in parent.nodes) {
157                         recursive(parent.nodes[i], item, path);
158                     }
159                 } else {
160                     for (var e in parent.nodes) {
161                         if (parent.nodes[e].name === absName) {
162                             return;
163                         }
164                     }
165                     parent.nodes.push({item: item, name: absName, nodes: []});
166                 }
167                 parent.nodes = parent.nodes.sort(function(a, b) {
168                     return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : a.name.toLowerCase() === b.name.toLowerCase() ? 0 : 1;
169                 });
170             }
171
172             function flatten(node, array) {
173                 array.push(node);
174                 for (var n in node.nodes) {
175                     flatten(node.nodes[n], array);
176                 }
177             }
178
179             function findNode(data, path) {
180                 return data.filter(function (n) {
181                     return n.name === path;
182                 })[0];
183             }
184
185             !this.history.length && this.history.push({name: '', nodes: []});
186             flatten(this.history[0], flatNodes);
187             selectedNode = findNode(flatNodes, path);
188             selectedNode.nodes = [];
189
190             for (var o in this.fileList) {
191                 var item = this.fileList[o];
192                 item.isFolder() && recursive(this.history[0], item, path);
193             }
194         };
195
196         PolicyNavigator.prototype.folderClick = function(item) {
197             this.currentPath = [];
198             if (item && item.isFolder()) {
199                 this.currentPath = item.model.fullPath().split('/').splice(1);
200             }
201             this.refresh();
202         };
203
204         PolicyNavigator.prototype.upDir = function() {
205             if (this.currentPath[0]) {
206                 this.currentPath = this.currentPath.slice(0, -1);
207                 this.refresh();
208             }
209         };
210
211         PolicyNavigator.prototype.goTo = function(index) {
212             this.currentPath = this.currentPath.slice(0, index + 1);
213             this.refresh();
214         };
215
216         PolicyNavigator.prototype.fileNameExists = function(fileName) {
217             for (var item in this.fileList) {
218                 item = this.fileList[item];
219                 if (fileName.trim && item.model.name.trim() === fileName.trim()) {
220                     return true;
221                 }
222             }
223         };
224
225         PolicyNavigator.prototype.listHasFolders = function() {
226             for (var item in this.fileList) {
227                 if (this.fileList[item].model.type === 'dir') {
228                     return true;
229                 }
230             }
231         };
232
233         return PolicyNavigator;
234     }]);
235 })(angular);