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";
|
2021-07-20 09:32:18 +02:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2021-03-28 11:06:49 +02:00
|
|
|
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,
|
2021-03-28 11:06:49 +02:00
|
|
|
} from "../lib/wizard-schema";
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2020-04-05 03:37:09 +02:00
|
|
|
export default Component.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
classNameBindings: [":wizard-links", "itemType"],
|
2020-04-05 03:37:09 +02:00
|
|
|
items: A(),
|
2021-03-28 11:06:49 +02:00
|
|
|
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;
|
2021-03-28 11:06:49 +02:00
|
|
|
const item = items.findBy("id", itemId);
|
2017-10-17 09:18:53 +02:00
|
|
|
items.removeObject(item);
|
2021-04-20 19:58:19 +02:00
|
|
|
item.set("index", newIndex);
|
2017-10-17 09:18:53 +02:00
|
|
|
items.insertAt(newIndex, item);
|
|
|
|
},
|
|
|
|
|
2021-03-28 11:06:49 +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
|
|
|
|
2021-03-28 11:06:49 +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
|
|
|
|
2021-07-20 09:32:18 +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 = {
|
2021-03-28 11:06:49 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
link.label = `${label} (${item.id})`;
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
let classes = "btn";
|
2020-04-01 07:03:26 +02:00
|
|
|
if (current && item.id === current.id) {
|
2021-03-28 11:06:49 +02:00
|
|
|
classes += " btn-primary";
|
|
|
|
}
|
2017-10-17 09:18:53 +02:00
|
|
|
|
2020-04-08 09:59:54 +02:00
|
|
|
link.classes = classes;
|
2021-07-20 09:32:18 +02:00
|
|
|
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() {
|
2021-03-28 11:06:49 +02:00
|
|
|
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);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
params.isNew = true;
|
2022-10-12 10:57:40 +02:00
|
|
|
params.index = this.getNextIndex();
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2022-04-04 12:15:13 +02:00
|
|
|
let id = `${itemType}_${params.index + 1}`;
|
2021-03-28 11:06:49 +02:00
|
|
|
if (itemType === "field") {
|
2020-04-16 05:33:07 +02:00
|
|
|
id = `${this.parentId}_${id}`;
|
2020-04-11 08:22:12 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
params.id = id;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-16 04:04:27 +02:00
|
|
|
let objectArrays = wizardSchema[itemType].objectArrays;
|
2020-04-11 08:22:12 +02:00
|
|
|
if (objectArrays) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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
|
|
|
});
|
2021-03-28 11:06:49 +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);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
this.set("current", newItem);
|
2017-10-17 09:18:53 +02:00
|
|
|
},
|
|
|
|
|
2021-07-20 09:32:18 +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) {
|
2021-03-28 11:06:49 +02:00
|
|
|
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;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
items.forEach((it, ind) => {
|
|
|
|
if (it.id === itemId) {
|
|
|
|
item = it;
|
|
|
|
index = ind;
|
|
|
|
}
|
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
let nextIndex;
|
|
|
|
if (this.current.id === itemId) {
|
2021-03-28 11:06:49 +02:00
|
|
|
nextIndex = index < items.length - 2 ? index + 1 : index - 1;
|
2020-04-11 08:22:12 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
items.removeObject(item);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-04-11 08:22:12 +02:00
|
|
|
if (nextIndex) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("current", items[nextIndex]);
|
2020-04-11 08:22:12 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
|
|
|
},
|
2017-10-17 09:18:53 +02:00
|
|
|
});
|