Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / src / js / services / filenavigator.js
diff --git a/ecomp-sdk-app/src/main/webapp/app/policyApp/policy-models/Editor/src/js/services/filenavigator.js b/ecomp-sdk-app/src/main/webapp/app/policyApp/policy-models/Editor/src/js/services/filenavigator.js
new file mode 100644 (file)
index 0000000..95d22bd
--- /dev/null
@@ -0,0 +1,199 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ECOMP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+(function(angular) {
+    'use strict';
+    angular.module('abs').service('fileNavigator', [
+        '$http', '$q', 'fileManagerConfig', 'item', function ($http, $q, fileManagerConfig, Item) {
+
+        $http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
+
+        var FileNavigator = function() {
+            this.requesting = false;
+            this.fileList = [];
+            this.currentPath = [];
+            this.history = [];
+            this.error = '';
+        };
+
+        FileNavigator.prototype.deferredHandler = function(data, deferred, defaultMsg) {
+            if (!data || typeof data !== 'object') {
+                this.error = 'Bridge response error, please check the docs';
+            }
+            if (!this.error && data.result && data.result.error) {
+                this.error = data.result.error;
+            }
+            if (!this.error && data.error) {
+                this.error = data.error.message;
+            }
+            if (!this.error && defaultMsg) {
+                this.error = defaultMsg;
+            }
+            if (this.error) {
+                return deferred.reject(data);
+            }
+            return deferred.resolve(data);
+        };
+        
+        FileNavigator.prototype.deferredSearchHandler = function(data, deferred, defaultMsg) {
+               self.fileList = [];
+                self.error = '';
+            if (!data || typeof data !== 'object') {
+                this.error = 'Bridge response error, please check the docs';
+            }
+            if (!this.error && data.result && data.result.error) {
+                this.error = data.result.error;
+            }
+            if (!this.error && data.error) {
+                this.error = data.error.message;
+            }
+            if (!this.error && defaultMsg) {
+                this.error = defaultMsg;
+            }
+            if (this.error) {
+                return deferred.reject(data);
+            }
+            return deferred.resolve(data);
+        };
+
+        FileNavigator.prototype.list = function() {
+            var self = this;
+            var deferred = $q.defer();
+            var path = self.currentPath.join('/');
+            var data = {params: {
+                mode: 'LIST',
+                onlyFolders: false,
+                path: '/' + path
+            }};
+
+            self.requesting = true;
+            self.fileList = [];
+            self.error = '';
+
+            $http.post(fileManagerConfig.listUrl, data).success(function(data) {
+                self.deferredHandler(data, deferred);
+            }).error(function(data) {
+                self.deferredHandler(data, deferred, 'Unknown error listing, check the response');
+            })['finally'](function() {
+                self.requesting = false;
+            });
+            return deferred.promise;
+        };
+
+        FileNavigator.prototype.refresh = function() {
+            var self = this;
+            var path = self.currentPath.join('/');
+            return self.list().then(function(data) {
+                self.fileList = (data.result || []).map(function(file) {
+                    return new Item(file, self.currentPath);
+                });
+                self.buildTree(path);
+            });
+        };
+        
+        FileNavigator.prototype.buildTree = function(path) {
+            var flatNodes = [], selectedNode = {};
+
+            function recursive(parent, item, path) {
+                var absName = path ? (path + '/' + item.model.name) : item.model.name;
+                if (parent.name.trim() && path.trim().indexOf(parent.name) !== 0) {
+                    parent.nodes = [];
+                }
+                if (parent.name !== path) {
+                    for (var i in parent.nodes) {
+                        recursive(parent.nodes[i], item, path);
+                    }
+                } else {
+                    for (var e in parent.nodes) {
+                        if (parent.nodes[e].name === absName) {
+                            return;
+                        }
+                    }
+                    parent.nodes.push({item: item, name: absName, nodes: []});
+                }
+                parent.nodes = parent.nodes.sort(function(a, b) {
+                    return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : a.name.toLowerCase() === b.name.toLowerCase() ? 0 : 1;
+                });
+            }
+
+            function flatten(node, array) {
+                array.push(node);
+                for (var n in node.nodes) {
+                    flatten(node.nodes[n], array);
+                }
+            }
+
+            function findNode(data, path) {
+                return data.filter(function (n) {
+                    return n.name === path;
+                })[0];
+            }
+
+            !this.history.length && this.history.push({name: '', nodes: []});
+            flatten(this.history[0], flatNodes);
+            selectedNode = findNode(flatNodes, path);
+            selectedNode.nodes = [];
+
+            for (var o in this.fileList) {
+                var item = this.fileList[o];
+                item.isFolder() && recursive(this.history[0], item, path);
+            }
+        };
+
+        FileNavigator.prototype.folderClick = function(item) {
+            this.currentPath = [];
+            if (item && item.isFolder()) {
+                this.currentPath = item.model.fullPath().split('/').splice(1);
+            }
+            this.refresh();
+        };
+
+        FileNavigator.prototype.upDir = function() {
+            if (this.currentPath[0]) {
+                this.currentPath = this.currentPath.slice(0, -1);
+                this.refresh();
+            }
+        };
+
+        FileNavigator.prototype.goTo = function(index) {
+            this.currentPath = this.currentPath.slice(0, index + 1);
+            this.refresh();
+        };
+
+        FileNavigator.prototype.fileNameExists = function(fileName) {
+            for (var item in this.fileList) {
+                item = this.fileList[item];
+                if (fileName.trim && item.model.name.trim() === fileName.trim()) {
+                    return true;
+                }
+            }
+        };
+
+        FileNavigator.prototype.listHasFolders = function() {
+            for (var item in this.fileList) {
+                if (this.fileList[item].model.type === 'dir') {
+                    return true;
+                }
+            }
+        };
+
+        return FileNavigator;
+    }]);
+})(angular);
\ No newline at end of file