0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-16 06:40:29 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/wizard-links.js.es6

150 Zeilen
3,3 KiB
Text

import {
default as discourseComputed,
observes,
2021-04-12 07:10:02 +02:00
on,
} from "discourse-common/utils/decorators";
import { generateName } from "../lib/wizard";
import {
setWizardDefaults,
2021-04-12 07:10:02 +02:00
default as wizardSchema,
} from "../lib/wizard-schema";
2020-03-29 09:49:33 +02:00
import { notEmpty } from "@ember/object/computed";
2021-04-12 07:44:47 +02:00
import { scheduleOnce } from "@ember/runloop";
2020-04-02 07:21:57 +02:00
import EmberObject from "@ember/object";
2020-04-05 03:37:09 +02:00
import Component from "@ember/component";
import { A } from "@ember/array";
2017-10-17 09:18:53 +02:00
2020-04-05 03:37:09 +02:00
export default Component.extend({
classNameBindings: [":wizard-links", "itemType"],
2020-04-05 03:37:09 +02:00
items: A(),
anyLinks: notEmpty("links"),
2017-10-17 09:18:53 +02:00
@on("didInsertElement")
@observes("links.[]")
2020-04-16 04:13:26 +02:00
setupSortable() {
scheduleOnce("afterRender", () => this.applySortable());
2017-10-17 09:18:53 +02:00
},
applySortable() {
$(this.element)
.find(".link-list")
.sortable({ tolerance: "pointer" })
.on("sortupdate", (e, ui) => {
this.updateItemOrder(ui.item.data("id"), ui.item.index());
2020-04-11 08:22:12 +02:00
});
2017-10-17 09:18:53 +02:00
},
updateItemOrder(itemId, newIndex) {
2020-04-01 07:03:26 +02:00
const items = this.items;
const item = items.findBy("id", itemId);
2017-10-17 09:18:53 +02:00
items.removeObject(item);
item.set("index", newIndex);
2017-10-17 09:18:53 +02:00
items.insertAt(newIndex, item);
scheduleOnce("afterRender", this, () => this.applySortable());
2017-10-17 09:18:53 +02:00
},
@discourseComputed("itemType")
2020-04-10 09:57:49 +02:00
header: (itemType) => `admin.wizard.${itemType}.header`,
2017-10-17 09:18:53 +02:00
@discourseComputed(
"current",
"items.@each.id",
"items.@each.type",
"items.@each.label",
"items.@each.title"
)
2020-04-06 03:54:16 +02:00
links(current, items) {
2021-04-12 08:12:20 +02:00
if (!items) {
return;
}
2017-10-17 09:18:53 +02:00
return items.map((item) => {
if (item) {
2020-04-08 09:59:54 +02:00
let link = {
id: item.id,
};
2020-04-08 09:59:54 +02:00
let label = item.label || item.title || item.id;
if (this.generateLabels && item.type) {
label = generateName(item.type);
}
2020-04-11 08:22:12 +02:00
link.label = `${label} (${item.id})`;
2017-10-17 09:18:53 +02:00
let classes = "btn";
2020-04-01 07:03:26 +02:00
if (current && item.id === current.id) {
classes += " btn-primary";
}
2017-10-17 09:18:53 +02:00
2020-04-08 09:59:54 +02:00
link.classes = classes;
2017-10-17 09:18:53 +02:00
return link;
}
});
},
actions: {
add() {
const items = this.get("items");
2020-04-11 08:22:12 +02:00
const itemType = this.itemType;
2020-04-20 11:41:13 +02:00
let params = setWizardDefaults({}, itemType);
2020-04-20 11:41:13 +02:00
params.isNew = true;
let index = 0;
2020-04-11 08:22:12 +02:00
if (items.length) {
index = items.length;
2020-04-16 05:33:07 +02:00
}
params.index = index;
let id = `${itemType}_${index + 1}`;
if (itemType === "field") {
2020-04-16 05:33:07 +02:00
id = `${this.parentId}_${id}`;
2020-04-11 08:22:12 +02:00
}
2020-04-20 11:41:13 +02:00
params.id = id;
let objectArrays = wizardSchema[itemType].objectArrays;
2020-04-11 08:22:12 +02:00
if (objectArrays) {
Object.keys(objectArrays).forEach((objectType) => {
2020-04-10 09:57:49 +02:00
params[objectArrays[objectType].property] = A();
2020-04-08 09:59:54 +02:00
});
}
2020-04-02 07:21:57 +02:00
const newItem = EmberObject.create(params);
2017-10-17 09:18:53 +02:00
items.pushObject(newItem);
this.set("current", newItem);
2017-10-17 09:18:53 +02:00
},
change(itemId) {
this.set("current", this.items.findBy("id", itemId));
2017-10-17 09:18:53 +02:00
},
remove(itemId) {
2020-04-01 07:03:26 +02:00
const items = this.items;
2020-04-11 08:22:12 +02:00
let item;
let index;
2020-04-11 08:22:12 +02:00
items.forEach((it, ind) => {
if (it.id === itemId) {
item = it;
index = ind;
}
});
2020-04-11 08:22:12 +02:00
let nextIndex;
if (this.current.id === itemId) {
nextIndex = index < items.length - 2 ? index + 1 : index - 1;
2020-04-11 08:22:12 +02:00
}
2020-04-11 08:22:12 +02:00
items.removeObject(item);
2020-04-11 08:22:12 +02:00
if (nextIndex) {
this.set("current", items[nextIndex]);
2020-04-11 08:22:12 +02:00
}
},
},
2017-10-17 09:18:53 +02:00
});