From d9ea487c1a2bdbec19470c56ef688aaa9fcc2b53 Mon Sep 17 00:00:00 2001 From: Robin Scholtes Date: Mon, 24 Jul 2023 14:54:34 +1200 Subject: [PATCH] add option to c-op-list to create a sortable list --- src/web/components/c-operation-list.mjs | 31 ++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/web/components/c-operation-list.mjs b/src/web/components/c-operation-list.mjs index 0f39defc..b6c1f7ca 100644 --- a/src/web/components/c-operation-list.mjs +++ b/src/web/components/c-operation-list.mjs @@ -1,4 +1,5 @@ import {COperationLi} from "./c-operation-li.mjs"; +import Sortable from "sortablejs"; /** * c(ustom element)-operation-list @@ -9,13 +10,20 @@ import {COperationLi} from "./c-operation-li.mjs"; * @param {Object} icon ( { class: string, innerText: string } ). check-icon by default */ export class COperationList extends HTMLElement { - constructor(app, opNames, includeStarIcon, icon) { + constructor( + app, + opNames, + includeStarIcon, + icon, + isSortable = false + ) { super(); this.app = app; this.opNames = opNames; this.includeStarIcon = includeStarIcon; this.icon = icon; + this.isSortable = isSortable; } /** @@ -39,6 +47,27 @@ export class COperationList extends HTMLElement { ul.appendChild(li); })) + if (this.isSortable) { + const sortableList = Sortable.create(ul, { + group: "sorting", + sort: true, + draggable: "c-operation-li", + onFilter: function (e) { + const el = sortableList.closest(e.item); + if (el && el.parentNode) { + $(el).popover("dispose"); + el.parentNode.removeChild(el); + } + }, + onEnd: function(e) { + if (this.removeIntent) { + $(e.item).popover("dispose"); + e.item.remove(); + } + }.bind(this), + }); + } + ul.dispatchEvent(this.app.manager.oplistcreate); this.append(ul);