0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-24 10:20:28 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/wizard-links.js

151 Zeilen
3,4 KiB
JavaScript

2024-11-22 17:11:51 +01:00
import { A } from "@ember/array";
import Component from "@ember/component";
import EmberObject from "@ember/object";
import { notEmpty } from "@ember/object/computed";
import discourseComputed from "discourse-common/utils/decorators";
import { generateName } from "../lib/wizard";
import {
2021-04-12 07:10:02 +02:00
default as wizardSchema,
2024-11-22 17:11:51 +01:00
setWizardDefaults,
} from "../lib/wizard-schema";
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
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);
},
@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, index) => {
2017-10-17 09:18:53 +02:00
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;
link.index = index;
if (index === 0) {
link.first = true;
}
if (index === items.length - 1) {
link.last = true;
}
2017-10-17 09:18:53 +02:00
return link;
}
});
},
2022-10-12 10:57:40 +02:00
getNextIndex() {
const items = this.items;
if (!items || items.length === 0) {
return 0;
}
2022-10-19 18:07:19 +02:00
const numbers = items
.map((i) => Number(i.id.split("_").pop()))
.sort((a, b) => a - b);
return numbers[numbers.length - 1];
2022-10-12 10:57:40 +02:00
},
2017-10-17 09:18:53 +02:00
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;
2022-10-12 10:57:40 +02:00
params.index = this.getNextIndex();
2022-04-04 12:15:13 +02:00
let id = `${itemType}_${params.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
},
back(item) {
this.updateItemOrder(item.id, item.index - 1);
},
forward(item) {
this.updateItemOrder(item.id, item.index + 1);
},
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
});