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

2 行
6.7 KiB
JavaScript

!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]._selected=!0,folder[scope.config.childrenField][key]._active=!0,scope.config.isFolder(folder[scope.config.childrenField][key])&&selectChildren(folder[scope.config.childrenField][key])},select=function(file){scope.$emit("ez-file-tree.select",file),(scope.config.enableFolderSelection||!scope.config.isFolder(file))&&(scope.config.enableFileSelection||scope.config.isFolder(file))&&(scope.config.movingFolder&&scope.config.movingFolder.length&&file[scope.config.idField]===scope.config.movingFolder||(scope.config.multiSelect?scope.tree._selectedFiles[file[scope.config.idField]]=file:(scope.tree._selectedFile=file,unselectAll(scope.tree[scope.config.childrenField])),file._selected=!0,activate(file),scope.config.recursiveSelect&&scope.config.isFolder(file)&&selectChildren(file)))},unselect=function(file){if(file._selected=!1,scope.$emit("ez-file-tree.unselect",file),scope.config.multiSelect?delete scope.tree._selectedFiles[file[scope.config.idField]]:scope.tree._selectedFile=null,deactivate(file),scope.config.recursiveUnselect&&scope.config.isFolder(file))for(var key in file[scope.config.childrenField])unselect(file[scope.config.childrenField][key])},unselectAll=function(files){for(var key in files)"undefined"!=typeof files[key][scope.config.childrenField]&&unselectAll(files[key][scope.config.childrenField]),files[key]._selected=!1,scope.$emit("ez-file-tree.unselect",files[key])};scope.toggle=function(e,file,callback){if(scope.config.isFolder(file)&&(!scope.config.movingFolder||!scope.config.movingFolder.length||file[scope.config.idField]!==scope.config.movingFolder)&&(e&&(scope.disableSelect=!0,$timeout(function(){scope.disableSelect=!1},500)),file._open=!file._open,!file[scope.config.childrenField]||file[scope.config.childrenField]&&!file[scope.config.childrenField].length)){if("undefined"==typeof scope.getChildren)throw new Error("You must add a getChildren method to the directive scope or hard code a children field on your folder objects.");scope.getChildren(file).then(function(children){for(var key in children)children[key]._parent=file,scope.config.multiSelect?"undefined"!=typeof scope.tree._selectedFiles[children[key][scope.config.idField]]&&(children[key]._selected=!0):scope.tree._selectedFile&&scope.tree._selectedFile[scope.config.idField]===children[key][scope.config.idField]&&(children[key]._selected=!0);file[scope.config.childrenField]=children,callback&&callback(file)})}},scope.showCheckbox=function(file){return!scope.config.enableChecking||scope.config.isFolder(file)&&!config.enableFolderSelection?!1:!0},scope.select=function(e,file){e.preventDefault(),$timeout(function(){scope.disableSelect!==!0&&(scope.disableSelect=!1,file._selected?unselect(file):select(file))},200)},init()}}}}])}(angular);