11<template >
22 <div >
33 <v-data-table
4+ id =" script-list"
45 :headers =" headers"
56 :items =" scripts"
67 sort-by =" sort"
78 :items-per-page =" 1000"
8- :sort-desc =" true"
9- class =" elevation-1"
109 v-model =" selected"
1110 :single-select =" false"
1211 show-select
158157 </template >
159158
160159 <template v-slot :[` item.sort ` ]=" " >
161- <v-icon small style =" cursor : pointer " > mdi-menu </v-icon >
160+ <v-icon small class = " handle " style =" cursor : move " > mdi-menu </v-icon >
162161 </template >
163162
164163 <template v-slot :[` item.origin ` ]=" { item } " >
@@ -616,6 +615,8 @@ import BgCloud from "@components/BgCloud.vue";
616615import { BackgroundGrant } from " @App/apps/grant/background" ;
617616import { scriptModule } from " ../store/script" ;
618617
618+ import Sortable from " sortablejs" ;
619+
619620dayjs .locale (" zh-cn" );
620621dayjs .extend (relativeTime );
621622
@@ -688,9 +689,9 @@ export default class ScriptList extends Vue {
688689 { text: " 开启" , value: " status" },
689690 { text: " 名称" , value: " name" },
690691 { text: " 版本" , value: " version" },
691- { text: " 应用至/运行状态" , value: " site" },
692- { text: " 来源" , value: " origin" },
693- { text: " 主页" , value: " home" },
692+ { text: " 应用至/运行状态" , value: " site" , sortable: false },
693+ { text: " 来源" , value: " origin" , sortable: false },
694+ { text: " 主页" , value: " home" , sortable: false },
694695 { text: " 排序" , value: " sort" , align: " center" },
695696 { text: " 最后更新" , value: " updatetime" , width: 100 , align: " center" },
696697 { text: " 操作" , value: " actions" , sortable: false },
@@ -711,13 +712,72 @@ export default class ScriptList extends Vue {
711712 // fab开启与关闭时,显示不同的图标
712713 fab = false ;
713714
715+ scriptlist(result : Script []) {
716+ // 校对排序位置
717+ for (let i = 0 ; i < result .length ; i ++ ) {
718+ if (result [i ].sort !== i ) {
719+ this .scriptController .scriptModel .update (result [i ].id , {
720+ sort: i ,
721+ });
722+ result [i ].sort = i ;
723+ }
724+ }
725+ this .scripts = result ;
726+ this .handleScriptConfig (this .scripts );
727+ }
728+
714729 created() {
715730 // todo 监听脚本列表更新,自动同步最新(比如新建)
716731 // todo 目前的排序,是当前页的排序,而不是所有脚本的排序,实现为所有脚本
717- this .scriptController .scriptList (undefined ).then (async (result ) => {
718- this .scripts = result ;
719- this .handleScriptConfig (this .scripts );
720- });
732+ this .scriptController
733+ .scriptList ((table ) => {
734+ return table .orderBy (" sort" );
735+ })
736+ .then (async (result ) => {
737+ this .scriptlist (result );
738+ this .$nextTick (() => {
739+ Sortable .create (
740+ <HTMLElement >document .querySelector (" #script-list table tbody" )! ,
741+ {
742+ handle: " .handle" ,
743+ animation: 150 ,
744+ onEnd : (ev ) => {
745+ console .log (ev );
746+ // 修改中间部分索引
747+ let start = 0 ,
748+ end = 0 ,
749+ add = 0 ,
750+ scripts = this .scripts ,
751+ tmp: Script = scripts [ev .oldIndex ! ],
752+ index = ev .newIndex ! ;
753+ start = ev .oldIndex ! ;
754+ end = ev .newIndex ! ;
755+ if (ev .oldIndex ! > ev .newIndex ! ) {
756+ // 选中前移,范围后移
757+ add = - 1 ;
758+ } else {
759+ // 选中后移,范围前移
760+ add = 1 ;
761+ }
762+ for (let i = start ; i - end !== 0 ; i += add ) {
763+ scripts [i ] = scripts [i + add ];
764+ scripts [i ].sort = i ;
765+ // 修改排序
766+ this .scriptController .scriptModel .update (scripts [i ].id , {
767+ sort: i ,
768+ });
769+ }
770+ tmp .sort = index ;
771+ scripts [index ] = tmp ;
772+ this .scriptController .scriptModel .update (tmp .id , {
773+ sort: index ,
774+ });
775+ this .scripts = scripts ;
776+ },
777+ }
778+ );
779+ });
780+ });
721781 // 监听script状态变更
722782 MsgCenter .listener (ScriptRunStatusChange , (param ) => {
723783 for (let i = 0 ; i < this .scripts .length ; i ++ ) {
@@ -730,19 +790,25 @@ export default class ScriptList extends Vue {
730790
731791 MsgCenter .listener (SyncTaskEvent , () => {
732792 // 同步完成,刷新页面
733- this .scriptController .scriptList (undefined ).then (async (result ) => {
734- this .scripts = result ;
735- this .handleScriptConfig (this .scripts );
736- });
793+ this .scriptController
794+ .scriptList ((table ) => {
795+ return table .orderBy (" sort" );
796+ })
797+ .then (async (result ) => {
798+ this .scriptlist (result );
799+ });
737800 });
738801
739802 // todo 监听脚本列表更新,自动同步最新(比如新建)
740803 // MsgCenter.listener(ScriptUpdate, () => {
741804 eventBus .$on (EventType .UpdateScriptList , () => {
742- this .scriptController .scriptList (undefined ).then ((result ) => {
743- this .scripts = result ;
744- this .handleScriptConfig (this .scripts );
745- });
805+ this .scriptController
806+ .scriptList ((table ) => {
807+ return table .orderBy (" sort" );
808+ })
809+ .then ((result ) => {
810+ this .scriptlist (result );
811+ });
746812 });
747813
748814 MsgCenter .connect (ListenGmLog , " init" ).addListener ((msg ) => {
0 commit comments