UNPKG

acha-framework

Version:

is a modular framework on both client (angular.js) and server (node.js) side, it provides security, orm, ioc, obfuscation and ...

153 lines 5.16 kB
(function ($, angular, underscore, window, document, undefined) { 'use strict'; angular.module('frontend.directives').factory('filePickerServerService', [ 'apiService', function (apiService) { var getBackend = function (backend, action) { return '{0}/{1}'.format([ backend || 'file-explorer', action ]).replace(/\/\//g, '/'); }; return { listDirectory: function (backend, path) { var url = getBackend(backend, 'listDirectory'); return apiService.post(url, { path: path }); } }; } ]).directive('filePickerServer', [ '$document', 'filePickerServerService', function ($document, filePickerServerService) { return { restrict: 'E', replace: true, scope: { tag: '<?', disabled: '=?', visible: '=?', cssClass: '=?', placeHolderTranslate: '=?', placeHolder: '=?', backend: '=?', model: '=?' }, templateUrl: '/templates/framework/directives/file-picker-server/template.html', link: function (scope, element, attr) { scope.vm = { path: '/', plate: false }; scope.vm.init = function () { if (angular.isUndefined(scope.disabled)) { scope.disabled = false; } if (angular.isUndefined(scope.visible)) { scope.visible = true; } if (angular.isUndefined(scope.cssClass)) { scope.cssClass = ''; } if (angular.isUndefined(scope.model)) { scope.model = ''; } scope.vm.bind(); scope.vm.refresh(); }; scope.vm.closeElseWhere = function (e) { scope.$apply(function () { scope.vm.plate = false; }); }; scope.vm.prevent = function ($event) { $event.stopPropagation(); $event.preventDefault(); }; scope.vm.refresh = function () { scope.vm.waiting = true; filePickerServerService.listDirectory(scope.backend, scope.vm.path).then(function (res) { if (res.data.status) { scope.vm.path = res.data.data.path; scope.vm.fileExplorer = res.data.data.content; if (scope.vm.path === '/') { scope.vm.tree = res.data.data.content.filter(function (item) { return item.folder; }).map(function (item) { return { title: item.name, expandable: true, path: item.path, folder: true }; }); } } scope.vm.waiting = false; }); }; scope.vm.togglePlate = function () { if (scope.disabled) return; scope.vm.plate = !scope.vm.plate; }; scope.vm.bind = function () { scope.$on('$destroy', function () { $document.off('click', scope.vm.closeElseWhere); }); $document.on('click', scope.vm.closeElseWhere); }; scope.vm.onNodeExpand = function (node) { if (node.waiting || node.items && node.item.length) return; if (node.folder) { node.waiting = true; filePickerServerService.listDirectory(scope.backend, node.path).then(function (res) { if (res.data.status) { node.expanded = true; node.items = res.data.data.content.filter(function (item) { return item.folder; }).map(function (item) { return { title: item.name, expandable: true, path: item.path, folder: true }; }); } node.waiting = false; }); } }; scope.vm.onNodeSelect = function (node) { if (node.items && node.items.length) return; if (node.folder) { scope.vm.path = node.path; scope.vm.refresh(); } }; scope.vm.onExplorerParent = function () { var current = scope.vm.path.split('/'); current.pop(); scope.vm.path = current.join('/'); scope.vm.refresh(); }; scope.vm.onExplorerDoubleSelect = function (node) { if (node.folder) { scope.vm.path = node.path; scope.vm.refresh(); } }; scope.vm.onExplorerSelect = function (item) { if (item[0].folder) return; scope.model = item[0].path; }; scope.vm.init(); } }; } ]); }(jQuery, angular, _, window, document));