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=========================================================
23 angular.module('abs').service('fileNavigator', [
24 '$http', '$q', 'fileManagerConfig', 'item', function ($http, $q, fileManagerConfig, Item) {
26 $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
28 var FileNavigator = function() {
29 this.requesting = false;
31 this.currentPath = [];
36 FileNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
37 if (!data || typeof data !== 'object') {
38 this.error = 'Bridge response error, please check the docs';
40 if (!this.error && data.result && data.result.error) {
41 this.error = data.result.error;
43 if (!this.error && data.error) {
44 this.error = data.error.message;
46 if (!this.error && defaultMsg) {
47 this.error = defaultMsg;
50 return deferred.reject(data);
52 return deferred.resolve(data);
55 FileNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
58 if (!data || typeof data !== 'object') {
59 this.error = 'Bridge response error, please check the docs';
61 if (!this.error && data.result && data.result.error) {
62 this.error = data.result.error;
64 if (!this.error && data.error) {
65 this.error = data.error.message;
67 if (!this.error && defaultMsg) {
68 this.error = defaultMsg;
71 return deferred.reject(data);
73 return deferred.resolve(data);
76 FileNavigator.prototype.list = function() {
78 var deferred = $q.defer();
79 var path = self.currentPath.join('/');
86 self.requesting = true;
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;
97 return deferred.promise;
100 FileNavigator.prototype.refresh = function() {
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);
107 self.buildTree(path);
111 FileNavigator.prototype.buildTree = function(path) {
112 var flatNodes = [], selectedNode = {};
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) {
119 if (parent.name !== path) {
120 for (var i in parent.nodes) {
121 recursive(parent.nodes[i], item, path);
124 for (var e in parent.nodes) {
125 if (parent.nodes[e].name === absName) {
129 parent.nodes.push({item: item, name: absName, nodes: []});
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;
136 function flatten(node, array) {
138 for (var n in node.nodes) {
139 flatten(node.nodes[n], array);
143 function findNode(data, path) {
144 return data.filter(function (n) {
145 return n.name === path;
149 !this.history.length && this.history.push({name: '', nodes: []});
150 flatten(this.history[0], flatNodes);
151 selectedNode = findNode(flatNodes, path);
152 selectedNode.nodes = [];
154 for (var o in this.fileList) {
155 var item = this.fileList[o];
156 item.isFolder() && recursive(this.history[0], item, path);
160 FileNavigator.prototype.folderClick = function(item) {
161 this.currentPath = [];
162 if (item && item.isFolder()) {
163 this.currentPath = item.model.fullPath().split('/').splice(1);
168 FileNavigator.prototype.upDir = function() {
169 if (this.currentPath[0]) {
170 this.currentPath = this.currentPath.slice(0, -1);
175 FileNavigator.prototype.goTo = function(index) {
176 this.currentPath = this.currentPath.slice(0, index + 1);
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()) {
189 FileNavigator.prototype.listHasFolders = function() {
190 for (var item in this.fileList) {
191 if (this.fileList[item].model.type === 'dir') {
197 return FileNavigator;