Ver código fonte

资料归集,跨父项移动节点

MaiXinRong 2 anos atrás
pai
commit
e8d1b56d61
2 arquivos alterados com 103 adições e 29 exclusões
  1. 76 17
      app/public/js/file_detail.js
  2. 27 12
      app/service/filing.js

+ 76 - 17
app/public/js/file_detail.js

@@ -20,9 +20,7 @@ $(document).ready(function() {
             this.expandCache = cache ? _.uniq(cache.split(',')) : [];
             this.curFilingKey = 'cur-filing-' + window.location.pathname.split('/')[2];
         }
-        analysisFiling(data) {
-            const self = this;
-            this.dragTree.loadDatas(data);
+        calcTotalFileCount() {
             this.dragTree.recursiveFun(this.dragTree.children, x => {
                 if (x.children && x.children.length > 0) {
                     x.total_file_count = x.children.map(y => {
@@ -34,19 +32,34 @@ $(document).ready(function() {
                     x.total_file_count = x.file_count;
                 }
             });
+        }
+        _loadFilingSourceNode() {
+            const self = this;
+            const loadChildren = function(children) {
+                for (const child of children) {
+                    if (child.children && child.children.length > 0) loadChildren(child.children);
+                    child.source_node = self.dragTree.getItems(child.id);
+                }
+            };
+            const nodes = this.filingTree.getNodes();
+            loadChildren(nodes);
+        }
+        loadFiling() {
+            const self = this;
+            if (this.filingTree) $.fn.zTree.destroy(this.setting.treeId);
             const sortNodes = this.dragTree.nodes.map(x => {
                 const result = {
                     id: x.id,
                     tree_pid: x.tree_pid,
                     name: x.name + (x.total_file_count > 0 ? `(${x.total_file_count})` : ''),
                     spid: x.spid,
-                    source_node: x,
                 };
-                if (result.source_node.is_fixed) result.isParent = true;
-                if (result.source_node.is_folder || result.source_node.is_fixed) result.open = self.expandCache.indexOf(result.id) >= 0;
+                if (x.is_fixed) result.isParent = true;
+                if (x.is_folder || x.is_fixed) result.open = self.expandCache.indexOf(result.id) >= 0;
                 return result;
             });
             this.filingTree = $.fn.zTree.init($('#filing'), this.setting, sortNodes);
+            this._loadFilingSourceNode();
             const curCache = getLocalCache(this.curFilingKey);
             const curNode = curCache ? this.filingTree.getNodeByParam('id', curCache) : null;
             if (curNode){
@@ -54,6 +67,11 @@ $(document).ready(function() {
                 filingObj.setCurFiling(curNode);
             }
         }
+        analysisFiling(data) {
+            this.dragTree.loadDatas(data);
+            this.calcTotalFileCount();
+            this.loadFiling();
+        }
         _getFileNameHtml(file) {
             const editHtml = file.canEdit ? `<a href="javascript: void(0);" class="mr-1" name="edit-file" fid="${file.id}"><i class="fa fa-pencil fa-fw"></i></a>` : '';
             const viewHtml = file.viewpath ? `<a href="${file.viewpath}" class="mr-1" target="_blank"><i class="fa fa-eye fa-fw"></i></a>` : '';
@@ -133,7 +151,8 @@ $(document).ready(function() {
             postData('filing/add', { tree_pid: node.tree_pid, tree_pre_id: node.id }, function(result) {
                 const refreshData = self.dragTree.loadPostData(result);
                 const newNode = refreshData.create[0];
-                self.filingTree.addNodes(node.getParentNode(), node.getIndex() + 1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid, source_node: newNode}]);
+                const nodes = self.filingTree.addNodes(node.getParentNode(), node.getIndex() + 1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid }]);
+                nodes[0].source_node = newNode;
             });
         }
         addChildFiling(node) {
@@ -141,7 +160,8 @@ $(document).ready(function() {
             postData('filing/add', { tree_pid: node.id }, function(result) {
                 const refreshData = self.dragTree.loadPostData(result);
                 const newNode = refreshData.create[0];
-                self.filingTree.addNodes(node, -1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid, source_node: newNode}]);
+                const nodes = self.filingTree.addNodes(node, -1, [{ id: newNode.id, tree_pid: newNode.tree_pid, name: newNode.name, spid: newNode.spid}]);
+                nodes[0].source_node = newNode;
             });
         }
         delFiling(node, callback) {
@@ -305,16 +325,25 @@ $(document).ready(function() {
             }
             setLocalCache(this.expandKey, this.expandCache.join(','));
         }
-        moveFiling(node, tree_order) {
-            if (tree_order === node.source_node.tree_order) return;
+        moveFiling(node, tree_pid, tree_order) {
+            if (tree_pid === node.source_node.tree_pid && tree_order === node.source_node.tree_order) return;
 
             const self = this;
-            postData('filing/move', { id: node.id, tree_order }, function(result) {
-                self.dragTree.loadPostData(result);
+            postData('filing/move', { id: node.id, tree_pid, tree_order }, function(result) {
+                const refresh = self.dragTree.loadPostData(result);
+                self.calcTotalFileCount();
+                for (const u of refresh.update) {
+                    const node = self.filingTree.getNodeByParam('id', u.id);
+                    if (node) {
+                        node.name = node.source_node.name + (node.source_node.total_file_count > 0 ? `(${node.source_node.total_file_count})` : '');
+                        filingObj.filingTree.updateNode(node);
+                    }
+                }
             });
         }
     }
     const levelTreeSetting = {
+        treeId: 'filing',
         view: {
             selectedMulti: false
         },
@@ -375,14 +404,44 @@ $(document).ready(function() {
             },
             beforeDrop: function(key, nodes, target, moveType, isCopy) {
                 if (!target) return false;
-                if (nodes[0].tree_pid !== target.tree_pid) return false;
+                if (nodes[0].level < 1) {
+                    toastr.error('顶层节点请勿移动');
+                    return false;
+                }
+                if (nodes[0].source_node.filing_type !== target.source_node.filing_type) {
+                    toastr.error('请勿跨越最顶层节点移动');
+                    return false;
+                }
+                if (target.source_node.file_count > 0 && moveType === 'inner') {
+                    toastr.error(`节点[${target.source_node.name}]下存在文件,不可添加子级`);
+                    return false;
+                }
 
-                const order = target.getIndex() + 1;
-                const max = target.getParentNode().children.length;
+                const order = nodes[0].getIndex() + 1;
+                const targetOrder = target.getIndex() + 1;
+                const targetMax = target.getParentNode().children.length;
                 if (moveType === 'prev') {
-                    filingObj.moveFiling(nodes[0], order === 1 ? 1 : order - 1)
+                    if (target.tree_pid === nodes[0].tree_pid) {
+                        if (targetOrder > order) {
+                            filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder - 1);
+                        } else {
+                            filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
+                        }
+                    } else {
+                        filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === 1 ? 1 : targetOrder);
+                    }
                 } else if (moveType === 'next') {
-                    filingObj.moveFiling(nodes[0], order === max ? max : order + 1);
+                    if (target.tree_pid === nodes[0].tree_pid) {
+                        if (targetOrder < order) {
+                            filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder + 1);
+                        } else {
+                            filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder === targetMax ? targetMax : targetOrder);
+                        }
+                    } else {
+                        filingObj.moveFiling(nodes[0], target.tree_pid, targetOrder + 1);
+                    }
+                } else if (moveType === 'inner') {
+                    filingObj.moveFiling(nodes[0], target.tree_id, targetMax + 1);
                 }
             }
         }

+ 27 - 12
app/service/filing.js

@@ -177,22 +177,37 @@ module.exports = app => {
 
         async move(data) {
             const filing = await this.getDataById(data.id);
-            const silbing = await this.getAllDataByCondition({ where: { tree_pid: filing.tree_pid, is_deleted: 0 } });
-            const updateData = [{ id: filing.id, tree_order: data.tree_order }];
-            if (data.tree_order < filing.tree_order) {
-                silbing.forEach(x => {
-                    if (x.id === filing.id) return;
-                    if (x.tree_order < data.tree_order) return;
-                    if (x.tree_order > filing.tree_order) return;
-                    updateData.push({id: x.id, tree_order: x.tree_order + 1});
-                });
+            if (!filing) throw '移动的分类不存在,请刷新页面后重试';
+            const parent = await this.getDataById(data.tree_pid);
+            if (!parent) throw '移动后的分类不存在,请刷新页面后重试';
+            const sibling = await this.getAllDataByCondition({ where: { tree_pid: data.tree_pid, is_deleted: 0 } });
+            const updateData = [{ id: filing.id, tree_order: data.tree_order, tree_pid: data.tree_pid, tree_level: parent.tree_level + 1 }];
+            if (data.tree_pid === filing.tree_pid) {
+                if (data.tree_order < filing.tree_order) {
+                    sibling.forEach(x => {
+                        if (x.id === filing.id) return;
+                        if (x.tree_order < data.tree_order) return;
+                        if (x.tree_order > filing.tree_order) return;
+                        updateData.push({id: x.id, tree_order: x.tree_order + 1});
+                    });
+                } else {
+                    sibling.forEach(x => {
+                        if (x.id === filing.id) return;
+                        if (x.tree_order < filing.tree_order) return;
+                        if (x.tree_order > data.tree_order) return;
+                        updateData.push({id: x.id, tree_order: x.tree_order - 1});
+                    });
+                }
             } else {
-                silbing.forEach(x => {
-                    if (x.id === filing.id) return;
+                const orgSibling = await this.getAllDataByCondition({ where: { tree_pid: filing.tree_pid, is_deleted: 0 } });
+                orgSibling.forEach(x => {
                     if (x.tree_order < filing.tree_order) return;
-                    if (x.tree_order > data.tree_order) return;
                     updateData.push({id: x.id, tree_order: x.tree_order - 1});
                 });
+                sibling.forEach(x => {
+                    if (x.tree_order < data.tree_order) return;
+                    updateData.push({id: x.id, tree_order: x.tree_order + 1});
+                })
             }
             await this.db.updateRows(this.tableName, updateData);
             return { update: updateData };