Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / src / js / services / filenavigator.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
21 (function(angular) {
22     'use strict';
23     angular.module('abs').service('fileNavigator', [
24         '$http', '$q', 'fileManagerConfig', 'item', function ($http, $q, fileManagerConfig, Item) {
25
26         $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
27
28         var FileNavigator = function() {
29             this.requesting = false;
30             this.fileList = [];
31             this.currentPath = [];
32             this.history = [];
33             this.error = '';
34         };
35
36         FileNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
37             if (!data || typeof data !== 'object') {
38                 this.error = 'Bridge response error, please check the docs';
39             }
40             if (!this.error && data.result && data.result.error) {
41                 this.error = data.result.error;
42             }
43             if (!this.error && data.error) {
44                 this.error = data.error.message;
45             }
46             if (!this.error && defaultMsg) {
47                 this.error = defaultMsg;
48             }
49             if (this.error) {
50                 return deferred.reject(data);
51             }
52             return deferred.resolve(data);
53         };
54         
55         FileNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
56                 self.fileList = [];
57                  self.error = '';
58             if (!data || typeof data !== 'object') {
59                 this.error = 'Bridge response error, please check the docs';
60             }
61             if (!this.error && data.result && data.result.error) {
62                 this.error = data.result.error;
63             }
64             if (!this.error && data.error) {
65                 this.error = data.error.message;
66             }
67             if (!this.error && defaultMsg) {
68                 this.error = defaultMsg;
69             }
70             if (this.error) {
71                 return deferred.reject(data);
72             }
73             return deferred.resolve(data);
74         };
75
76         FileNavigator.prototype.list = function() {
77             var self = this;
78             var deferred = $q.defer();
79             var path = self.currentPath.join('/');
80             var data = {params: {
81                 mode: 'LIST',
82                 onlyFolders: false,
83                 path: '/' + path
84             }};
85
86             self.requesting = true;
87             self.fileList = [];
88             self.error = '';
89
90             $http.post(fileManagerConfig.listUrl, data).success(function(data) {
91                 self.deferredHandler(data, deferred);
92             }).error(function(data) {
93                 self.deferredHandler(data, deferred, 'Unknown error listing, check the response');
94             })['finally'](function() {
95                 self.requesting = false;
96             });
97             return deferred.promise;
98         };
99
100         FileNavigator.prototype.refresh = function() {
101             var self = this;
102             var path = self.currentPath.join('/');
103             return self.list().then(function(data) {
104                 self.fileList = (data.result || []).map(function(file) {
105                     return new Item(file, self.currentPath);
106                 });
107                 self.buildTree(path);
108             });
109         };
110         
111         FileNavigator.prototype.buildTree = function(path) {
112             var flatNodes = [], selectedNode = {};
113
114             function recursive(parent, item, path) {
115                 var absName = path ? (path + '/' + item.model.name) : item.model.name;
116                 if (parent.name.trim() && path.trim().indexOf(parent.name) !== 0) {
117                     parent.nodes = [];
118                 }
119                 if (parent.name !== path) {
120                     for (var i in parent.nodes) {
121                         recursive(parent.nodes[i], item, path);
122                     }
123                 } else {
124                     for (var e in parent.nodes) {
125                         if (parent.nodes[e].name === absName) {
126                             return;
127                         }
128                     }
129                     parent.nodes.push({item: item, name: absName, nodes: []});
130                 }
131                 parent.nodes = parent.nodes.sort(function(a, b) {
132                     return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : a.name.toLowerCase() === b.name.toLowerCase() ? 0 : 1;
133                 });
134             }
135
136             function flatten(node, array) {
137                 array.push(node);
138                 for (var n in node.nodes) {
139                     flatten(node.nodes[n], array);
140                 }
141             }
142
143             function findNode(data, path) {
144                 return data.filter(function (n) {
145                     return n.name === path;
146                 })[0];
147             }
148
149             !this.history.length && this.history.push({name: '', nodes: []});
150             flatten(this.history[0], flatNodes);
151             selectedNode = findNode(flatNodes, path);
152             selectedNode.nodes = [];
153
154             for (var o in this.fileList) {
155                 var item = this.fileList[o];
156                 item.isFolder() && recursive(this.history[0], item, path);
157             }
158         };
159
160         FileNavigator.prototype.folderClick = function(item) {
161             this.currentPath = [];
162             if (item && item.isFolder()) {
163                 this.currentPath = item.model.fullPath().split('/').splice(1);
164             }
165             this.refresh();
166         };
167
168         FileNavigator.prototype.upDir = function() {
169             if (this.currentPath[0]) {
170                 this.currentPath = this.currentPath.slice(0, -1);
171                 this.refresh();
172             }
173         };
174
175         FileNavigator.prototype.goTo = function(index) {
176             this.currentPath = this.currentPath.slice(0, index + 1);
177             this.refresh();
178         };
179
180         FileNavigator.prototype.fileNameExists = function(fileName) {
181             for (var item in this.fileList) {
182                 item = this.fileList[item];
183                 if (fileName.trim && item.model.name.trim() === fileName.trim()) {
184                     return true;
185                 }
186             }
187         };
188
189         FileNavigator.prototype.listHasFolders = function() {
190             for (var item in this.fileList) {
191                 if (this.fileList[item].model.type === 'dir') {
192                     return true;
193                 }
194             }
195         };
196
197         return FileNavigator;
198     }]);
199 })(angular);