2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 angular.module('abs').service('policyNavigator', [
23 '$http', '$q', 'policyManagerConfig', 'item', function ($http, $q, policyManagerConfig, Item) {
25 $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
27 var PolicyNavigator = function() {
28 this.requesting = false;
30 this.currentPath = [];
33 this.searchModalActive = false;
36 PolicyNavigator.prototype.setSearchModalActiveStatus = function(){
37 this.searchModalActive = true;
40 PolicyNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
41 if (!data || typeof data !== 'object') {
42 this.error = 'Bridge response error, please check the docs';
44 if (!this.error && data.result && data.result.error) {
45 this.error = data.result.error;
47 if (!this.error && data.error) {
48 this.error = data.error.message;
50 if (!this.error && defaultMsg) {
51 this.error = defaultMsg;
54 return deferred.reject(data);
56 return deferred.resolve(data);
59 PolicyNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
62 if (!data || typeof data !== 'object') {
63 this.error = 'Bridge response error, please check the docs';
65 if (!this.error && data.result && data.result.error) {
66 this.error = data.result.error;
68 if (!this.error && data.error) {
69 this.error = data.error.message;
71 if (!this.error && defaultMsg) {
72 this.error = defaultMsg;
75 return deferred.reject(data);
77 return deferred.resolve(data);
80 PolicyNavigator.prototype.list = function() {
82 var deferred = $q.defer();
83 var path = self.currentPath.join('/');
90 self.requesting = true;
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;
101 return deferred.promise;
104 PolicyNavigator.prototype.refresh = function() {
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);
112 self.buildTree(path);
115 return self.list().then(function(data) {
116 self.fileList = (data.result || []).map(function(file) {
117 return new Item(file, self.currentPath);
119 self.buildTree(path);
124 PolicyNavigator.prototype.searchlist = function(policyList) {
126 var deferred = $q.defer();
127 var path = self.currentPath.join('/');
129 if(policyList == null){
140 policyList : policyList
145 self.requesting = true;
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;
156 return deferred.promise;
159 PolicyNavigator.prototype.searchrefresh = function(policyList) {
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);
166 self.buildTree(path);
171 PolicyNavigator.prototype.buildTree = function(path) {
172 var flatNodes = [], selectedNode = {};
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) {
179 if (parent.name !== path) {
180 for (var i in parent.nodes) {
181 recursive(parent.nodes[i], item, path);
184 for (var e in parent.nodes) {
185 if (parent.nodes[e].name === absName) {
189 parent.nodes.push({item: item, name: absName, nodes: []});
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;
196 function flatten(node, array) {
198 for (var n in node.nodes) {
199 flatten(node.nodes[n], array);
203 function findNode(data, path) {
204 return data.filter(function (n) {
205 return n.name === path;
209 !this.history.length && this.history.push({name: '', nodes: []});
210 flatten(this.history[0], flatNodes);
211 selectedNode = findNode(flatNodes, path);
212 selectedNode.nodes = [];
214 for (var o in this.fileList) {
215 var item = this.fileList[o];
216 item.isFolder() && recursive(this.history[0], item, path);
220 PolicyNavigator.prototype.folderClick = function(item) {
221 this.currentPath = [];
222 if (item && item.isFolder()) {
223 this.currentPath = item.model.fullPath().split('/').splice(1);
228 PolicyNavigator.prototype.upDir = function() {
229 if (this.currentPath[0]) {
230 this.currentPath = this.currentPath.slice(0, -1);
235 PolicyNavigator.prototype.goTo = function(index) {
236 this.currentPath = this.currentPath.slice(0, index + 1);
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()) {
249 PolicyNavigator.prototype.listHasFolders = function() {
250 for (var item in this.fileList) {
251 if (this.fileList[item].model.type === 'dir') {
257 return PolicyNavigator;