walnut_launchpad.html/js/lib/ez-file-tree/ez-file-tree.min.js

2 lines
6.7 KiB
JavaScript
Raw Normal View History

2017-09-22 22:17:42 +00:00
!function(angular){"use strict";angular.module("ez.fileTree",["ez.object2array"]).constant("EzFileTreeConfig",{enableChecking:!1,enableFolderSelection:!0,enableFileSelection:!0,multiSelect:!1,recursiveSelect:!1,recursiveUnselect:!0,icons:{chevronRight:"fa fa-chevron-right",chevronDown:"fa fa-chevron-down",folder:"fa fa-folder",openFolder:"fa fa-folder-open",file:"fa fa-file"},childrenField:"children",idField:"id",isFolder:function(file){return"folder"===file.type},preSelect:!1,ancestorField:"ancestors",showRoot:!1,movingFolder:null}).directive("ezFileTree",["$compile","$timeout","$parse","EzFileTreeConfig",function($compile,$timeout,$parse,EzFileTreeConfig){return{restrict:"EA",replace:!0,scope:{tree:"=ezFileTree",getChildren:"=?",config:"=?config"},templateUrl:"ez-file-tree-container.html",compile:function(element,attrs){var config=angular.extend({},EzFileTreeConfig);for(var key in EzFileTreeConfig)"undefined"!=typeof attrs[key]&&(config[key]=$parse(attrs[key])());return function(scope){scope.data={showTree:!1};var init=function(){scope.config=angular.extend(config,scope.config),scope.disableSelect=!1;var cachedSelectedFile=angular.extend({},scope.tree._selectedFile);if(delete scope.tree._selectedFile,scope.config.showRoot){var wrappedTree={};wrappedTree[scope.config.idField]="root",wrappedTree.name="root",wrappedTree[scope.config.childrenField]={},wrappedTree[scope.config.childrenField][scope.tree[scope.config.idField]]=$.extend(!0,{},scope.tree),wrappedTree[scope.config.childrenField][scope.tree[scope.config.idField]]._open=!0,wrappedTree[scope.config.childrenField][scope.tree[scope.config.idField]].type="folder",scope.tree=wrappedTree}scope.config.multiSelect?scope.tree._selectedFiles={}:scope.tree._selectedFile=null,scope.tree._activeFiles={},scope.tree[scope.config.childrenField]&&(setParentOnChildren(scope.tree,!0),scope.config.preSelect?scope.findAndSelectFile(cachedSelectedFile):scope.data.showTree=!0)};scope.folderFindAndSelect=function(folder,file,ancestors){if(ancestors.length>1)scope.recursiveFindAndSelect(folder,file,ancestors.slice(1));else for(var k in folder[scope.config.childrenField]){var innerFolder=folder[scope.config.childrenField][k];if(k===file.id)return select(innerFolder),void(scope.data.showTree=!0)}},scope.recursiveFindAndSelect=function(parentFolder,file,ancestors){var folder=parentFolder[scope.config.childrenField][ancestors[0][scope.config.idField]];folder._open?scope.folderFindAndSelect(folder,file,ancestors):scope.toggle(null,folder,function(updatedFolder){scope.folderFindAndSelect(updatedFolder,file,ancestors)})},scope.findAndSelectFile=function(file){for(var k in scope.tree[scope.config.childrenField]){var rootFolder=scope.tree[scope.config.childrenField][k];if(scope.config.ancestorField in file){var ancestors=file[scope.config.ancestorField];if(0===ancestors.length){if(k===file.id)return select(rootFolder),void(scope.data.showTree=!0)}else scope.recursiveFindAndSelect(scope.tree,file,ancestors)}}};var setParentOnChildren=function(file,recursive){for(var k in file[scope.config.childrenField])file[scope.config.childrenField][k]._parent=file,recursive&&file[scope.config.childrenField][k][scope.config.childrenField]&&setParentOnChildren(file[scope.config.childrenField][k],!0)},hasChildrenSelected=function(file){var selected=!1;for(var key in file[scope.config.childrenField]){if(file[scope.config.childrenField][key]._selected){selected=!0;break}if(file[scope.config.childrenField].files&&hasChildrenSelected(file[scope.config.childrenField])){selected=!0;break}}return selected},activate=function(file){file._active=!0,scope.tree._activeFiles[file[scope.config.idField]]=file,file._parent&&activate(file._parent)},deactivate=function(file){var active=!1;file[scope.config.childrenField]&&hasChildrenSelected(file)&&(active=!0),active||delete scope.tree._activeFiles[file[scope.config.idField]],file._active=active,active===!1&&void 0!==file._parent&&deactivate(file._parent)},selectChildren=function(folder){for(var key in folder[scope.config.childrenField])folder[scope.config.childrenField][key]._sel