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 = [];
35 PolicyNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
36 if (!data || typeof data !== 'object') {
37 this.error = 'Bridge response error, please check the docs';
39 if (!this.error && data.result && data.result.error) {
40 this.error = data.result.error;
42 if (!this.error && data.error) {
43 this.error = data.error.message;
45 if (!this.error && defaultMsg) {
46 this.error = defaultMsg;
49 return deferred.reject(data);
51 return deferred.resolve(data);
54 PolicyNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
57 if (!data || typeof data !== 'object') {
58 this.error = 'Bridge response error, please check the docs';
60 if (!this.error && data.result && data.result.error) {
61 this.error = data.result.error;
63 if (!this.error && data.error) {
64 this.error = data.error.message;
66 if (!this.error && defaultMsg) {
67 this.error = defaultMsg;
70 return deferred.reject(data);
72 return deferred.resolve(data);
75 PolicyNavigator.prototype.list = function() {
77 var deferred = $q.defer();
78 var path = self.currentPath.join('/');
85 self.requesting = true;
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;
96 return deferred.promise;
99 PolicyNavigator.prototype.refresh = function() {
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);
106 self.buildTree(path);
110 PolicyNavigator.prototype.policylist = function(finalpath) {
112 var deferred = $q.defer();
113 var path = finalpath;
114 var data = {params: {
120 self.requesting = true;
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;
131 return deferred.promise;
134 PolicyNavigator.prototype.policyrefresh = function(finalpath) {
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);
142 self.buildTree(path);
147 PolicyNavigator.prototype.buildTree = function(path) {
148 var flatNodes = [], selectedNode = {};
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) {
155 if (parent.name !== path) {
156 for (var i in parent.nodes) {
157 recursive(parent.nodes[i], item, path);
160 for (var e in parent.nodes) {
161 if (parent.nodes[e].name === absName) {
165 parent.nodes.push({item: item, name: absName, nodes: []});
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;
172 function flatten(node, array) {
174 for (var n in node.nodes) {
175 flatten(node.nodes[n], array);
179 function findNode(data, path) {
180 return data.filter(function (n) {
181 return n.name === path;
185 !this.history.length && this.history.push({name: '', nodes: []});
186 flatten(this.history[0], flatNodes);
187 selectedNode = findNode(flatNodes, path);
188 selectedNode.nodes = [];
190 for (var o in this.fileList) {
191 var item = this.fileList[o];
192 item.isFolder() && recursive(this.history[0], item, path);
196 PolicyNavigator.prototype.folderClick = function(item) {
197 this.currentPath = [];
198 if (item && item.isFolder()) {
199 this.currentPath = item.model.fullPath().split('/').splice(1);
204 PolicyNavigator.prototype.upDir = function() {
205 if (this.currentPath[0]) {
206 this.currentPath = this.currentPath.slice(0, -1);
211 PolicyNavigator.prototype.goTo = function(index) {
212 this.currentPath = this.currentPath.slice(0, index + 1);
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()) {
225 PolicyNavigator.prototype.listHasFolders = function() {
226 for (var item in this.fileList) {
227 if (this.fileList[item].model.type === 'dir') {
233 return PolicyNavigator;