From 911a47db2846fc6f85f3a6f1ca78dde98150c870 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 20 Jul 2021 15:32:18 +0800 Subject: [PATCH 1/5] IMPROVE: Remove sortable and add manual step and field sorting (#135) * IMPROVE: Remove sortable and add manual step and field sorting * Apply eslint * Apply prettier --- .../discourse/components/wizard-links.js.es6 | 42 +++++++++---------- .../templates/components/wizard-links.hbs | 14 +++++-- plugin.rb | 3 +- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-links.js.es6 b/assets/javascripts/discourse/components/wizard-links.js.es6 index c32809aa..50dadfe6 100644 --- a/assets/javascripts/discourse/components/wizard-links.js.es6 +++ b/assets/javascripts/discourse/components/wizard-links.js.es6 @@ -1,15 +1,10 @@ -import { - default as discourseComputed, - observes, - on, -} from "discourse-common/utils/decorators"; +import discourseComputed from "discourse-common/utils/decorators"; import { generateName } from "../lib/wizard"; import { setWizardDefaults, default as wizardSchema, } from "../lib/wizard-schema"; import { notEmpty } from "@ember/object/computed"; -import { scheduleOnce } from "@ember/runloop"; import EmberObject from "@ember/object"; import Component from "@ember/component"; import { A } from "@ember/array"; @@ -19,28 +14,12 @@ export default Component.extend({ items: A(), anyLinks: notEmpty("links"), - @on("didInsertElement") - @observes("links.[]") - setupSortable() { - scheduleOnce("afterRender", () => this.applySortable()); - }, - - applySortable() { - $(this.element) - .find(".link-list") - .sortable({ tolerance: "pointer" }) - .on("sortupdate", (e, ui) => { - this.updateItemOrder(ui.item.data("id"), ui.item.index()); - }); - }, - updateItemOrder(itemId, newIndex) { const items = this.items; const item = items.findBy("id", itemId); items.removeObject(item); item.set("index", newIndex); items.insertAt(newIndex, item); - scheduleOnce("afterRender", this, () => this.applySortable()); }, @discourseComputed("itemType") @@ -58,7 +37,7 @@ export default Component.extend({ return; } - return items.map((item) => { + return items.map((item, index) => { if (item) { let link = { id: item.id, @@ -77,6 +56,15 @@ export default Component.extend({ } link.classes = classes; + link.index = index; + + if (index === 0) { + link.first = true; + } + + if (index === items.length - 1) { + link.last = true; + } return link; } @@ -118,6 +106,14 @@ export default Component.extend({ this.set("current", newItem); }, + back(item) { + this.updateItemOrder(item.id, item.index - 1); + }, + + forward(item) { + this.updateItemOrder(item.id, item.index + 1); + }, + change(itemId) { this.set("current", this.items.findBy("id", itemId)); }, diff --git a/assets/javascripts/discourse/templates/components/wizard-links.hbs b/assets/javascripts/discourse/templates/components/wizard-links.hbs index 5271b043..a7a7662e 100644 --- a/assets/javascripts/discourse/templates/components/wizard-links.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-links.hbs @@ -2,10 +2,16 @@