[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / js / services / policynavigator.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP 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             this.searchModalActive = false;
34         };
35
36         PolicyNavigator.prototype.setSearchModalActiveStatus = function(){
37                 this.searchModalActive = true;
38         };
39         
40         PolicyNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
41             if (!data || typeof data !== 'object') {
42                 this.error = 'Bridge response error, please check the docs';
43             }
44             if (!this.error && data.result && data.result.error) {
45                 this.error = data.result.error;
46             }
47             if (!this.error && data.error) {
48                 this.error = data.error.message;
49             }
50             if (!this.error && defaultMsg) {
51                 this.error = defaultMsg;
52             }
53             if (this.error) {
54                 return deferred.reject(data);
55             }
56             return deferred.resolve(data);
57         };
58         
59         PolicyNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
60                 self.fileList = [];
61                  self.error = '';
62             if (!data || typeof data !== 'object') {
63                 this.error = 'Bridge response error, please check the docs';
64             }
65             if (!this.error && data.result && data.result.error) {
66                 this.error = data.result.error;
67             }
68             if (!this.error && data.error) {
69                 this.error = data.error.message;
70             }
71             if (!this.error && defaultMsg) {
72                 this.error = defaultMsg;
73             }
74             if (this.error) {
75                 return deferred.reject(data);
76             }
77             return deferred.resolve(data);
78         };
79
80         PolicyNavigator.prototype.list = function() {
81             var self = this;
82             var deferred = $q.defer();
83             var path = self.currentPath.join('/');
84             var data = {params: {
85                 mode: 'LIST',
86                 onlyFolders: false,
87                 path: '/' + path
88             }};
89
90             self.requesting = true;
91             self.fileList = [];
92             self.error = '';
93
94             $http.post(policyManagerConfig.listUrl, data).success(function(data) {
95                 self.deferredHandler(data, deferred);
96             }).error(function(data) {
97                 self.deferredHandler(data, deferred, 'Unknown error listing, check the response');
98             })['finally'](function() {
99                 self.requesting = false;
100             });
101             return deferred.promise;
102         };
103
104         PolicyNavigator.prototype.refresh = function() {
105             var self = this;
106             var path = self.currentPath.join('/');
107             if(self.searchModalActive){
108                 return self.searchlist(null).then(function(data) {
109                         self.fileList = (data.result || []).map(function(file) {
110                                 return new Item(file, self.currentPath);
111                         });
112                         self.buildTree(path);
113                 });     
114             }else{
115                 return self.list().then(function(data) {
116                         self.fileList = (data.result || []).map(function(file) {
117                                 return new Item(file, self.currentPath);
118                         });
119                         self.buildTree(path);
120                 });
121             }
122         };
123         
124         PolicyNavigator.prototype.searchlist = function(policyList) {
125             var self = this;
126             var deferred = $q.defer();
127             var path = self.currentPath.join('/');
128             var data;
129             if(policyList == null){
130                  data = {params: {
131                      mode: 'SEARCHLIST',
132                      onlyFolders: false,
133                      path: '/' + path
134                  }};
135             }else{
136                  data = {params: {
137                      mode: 'SEARCHLIST',
138                      onlyFolders: false,
139                      path: '/' + path,
140                      policyList : policyList
141                  }};
142             }
143            
144
145             self.requesting = true;
146             self.fileList = [];
147             self.error = '';
148
149             $http.post(policyManagerConfig.searchListUrl, data).success(function(data) {
150                 self.deferredHandler(data, deferred);
151             }).error(function(data) {
152                 self.deferredHandler(data, deferred, 'Unknown error listing, check the response');
153             })['finally'](function() {
154                 self.requesting = false;
155             });
156             return deferred.promise;
157         };
158
159         PolicyNavigator.prototype.searchrefresh = function(policyList) {
160                 var self = this;
161                 var path = self.currentPath.join('/');
162                 return self.searchlist(policyList).then(function(data) {
163                         self.fileList = (data.result || []).map(function(file) {
164                                 return new Item(file, self.currentPath);
165                         });
166                         self.buildTree(path);
167                 });     
168         };
169         
170         
171         PolicyNavigator.prototype.buildTree = function(path) {
172             var flatNodes = [], selectedNode = {};
173
174             function recursive(parent, item, path) {
175                 var absName = path ? (path + '/' + item.model.name) : item.model.name;
176                 if (parent.name.trim() && path.trim().indexOf(parent.name) !== 0) {
177                     parent.nodes = [];
178                 }
179                 if (parent.name !== path) {
180                     for (var i in parent.nodes) {
181                         recursive(parent.nodes[i], item, path);
182                     }
183                 } else {
184                     for (var e in parent.nodes) {
185                         if (parent.nodes[e].name === absName) {
186                             return;
187                         }
188                     }
189                     parent.nodes.push({item: item, name: absName, nodes: []});
190                 }
191                 parent.nodes = parent.nodes.sort(function(a, b) {
192                     return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : a.name.toLowerCase() === b.name.toLowerCase() ? 0 : 1;
193                 });
194             }
195
196             function flatten(node, array) {
197                 array.push(node);
198                 for (var n in node.nodes) {
199                     flatten(node.nodes[n], array);
200                 }
201             }
202
203             function findNode(data, path) {
204                 return data.filter(function (n) {
205                     return n.name === path;
206                 })[0];
207             }
208
209             !this.history.length && this.history.push({name: '', nodes: []});
210             flatten(this.history[0], flatNodes);
211             selectedNode = findNode(flatNodes, path);
212             selectedNode.nodes = [];
213
214             for (var o in this.fileList) {
215                 var item = this.fileList[o];
216                 item.isFolder() && recursive(this.history[0], item, path);
217             }
218         };
219
220         PolicyNavigator.prototype.folderClick = function(item) {
221             this.currentPath = [];
222             if (item && item.isFolder()) {
223                 this.currentPath = item.model.fullPath().split('/').splice(1);
224             }
225             this.refresh();
226         };
227
228         PolicyNavigator.prototype.upDir = function() {
229             if (this.currentPath[0]) {
230                 this.currentPath = this.currentPath.slice(0, -1);
231                 this.refresh();
232             }
233         };
234
235         PolicyNavigator.prototype.goTo = function(index) {
236             this.currentPath = this.currentPath.slice(0, index + 1);
237             this.refresh();
238         };
239
240         PolicyNavigator.prototype.fileNameExists = function(fileName) {
241             for (var item in this.fileList) {
242                 item = this.fileList[item];
243                 if (fileName.trim && item.model.name.trim() === fileName.trim()) {
244                     return true;
245                 }
246             }
247         };
248
249         PolicyNavigator.prototype.listHasFolders = function() {
250             for (var item in this.fileList) {
251                 if (this.fileList[item].model.type === 'dir') {
252                     return true;
253                 }
254             }
255         };
256
257         return PolicyNavigator;
258     }]);
259 })(angular);