0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-14 13:52:54 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/wizard-links.js.es6

137 Zeilen
3,5 KiB
Text

2020-04-05 03:37:09 +02:00
import { default as discourseComputed, on, observes } from 'discourse-common/utils/decorators';
import { generateName } from '../lib/wizard';
2020-04-20 11:41:13 +02:00
import { default as wizardSchema, setWizardDefaults } from '../lib/wizard-schema';
2020-03-29 09:49:33 +02:00
import { notEmpty } from "@ember/object/computed";
2020-04-05 03:37:09 +02:00
import { scheduleOnce, bind } 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({
2020-04-10 09:57:49 +02:00
classNameBindings: [':wizard-links', 'itemType'],
2020-04-05 03:37:09 +02:00
items: A(),
2020-03-29 09:49:33 +02:00
anyLinks: notEmpty('links'),
2017-10-17 09:18:53 +02:00
2018-06-29 08:54:06 +02:00
@on('didInsertElement')
2020-04-16 04:13:26 +02:00
@observes('links.[]')
setupSortable() {
2020-03-30 01:53:28 +02:00
scheduleOnce('afterRender', () => (this.applySortable()));
2017-10-17 09:18:53 +02:00
},
applySortable() {
2020-04-16 04:13:26 +02:00
$(this.element).find(".link-list")
2020-04-11 08:22:12 +02:00
.sortable({ tolerance: 'pointer' })
.on('sortupdate', (e, ui) => {
this.updateItemOrder(ui.item.data('id'), ui.item.index());
});
2017-10-17 09:18:53 +02:00
},
updateItemOrder(itemId, newIndex) {
2020-04-01 07:03:26 +02:00
const items = this.items;
2017-10-17 09:18:53 +02:00
const item = items.findBy('id', itemId);
items.removeObject(item);
items.insertAt(newIndex, item);
2020-03-30 01:53:28 +02:00
scheduleOnce('afterRender', this, () => this.applySortable());
2017-10-17 09:18:53 +02:00
},
2020-04-10 09:57:49 +02:00
@discourseComputed('itemType')
header: (itemType) => `admin.wizard.${itemType}.header`,
2017-10-17 09:18:53 +02:00
2020-04-10 09:57: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) {
2017-10-17 09:18:53 +02:00
if (!items) return;
return items.map((item) => {
if (item) {
2020-04-08 09:59:54 +02:00
let link = {
id: item.id
}
let label = item.label || item.title || item.id;
if (this.generateLabels && item.type) {
label = generateName(item.type);
}
2020-04-10 09:57:49 +02:00
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) {
2017-10-17 09:18:53 +02:00
classes += ' btn-primary';
};
2020-04-08 09:59:54 +02:00
link.classes = classes;
2017-10-17 09:18:53 +02:00
return link;
}
});
},
actions: {
add() {
2020-04-16 05:33:07 +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);
params.isNew = true;
2020-04-11 08:22:12 +02:00
let next = 1;
2020-04-16 05:33:07 +02:00
2020-04-11 08:22:12 +02:00
if (items.length) {
next = Math.max.apply(Math, items.map((i) => {
2020-04-16 05:33:07 +02:00
let parts = i.id.split('_');
let lastPart = parts[parts.length - 1];
return isNaN(lastPart) ? 0 : lastPart;
2020-04-16 05:33:07 +02:00
})) + 1;
}
let id = `${itemType}_${next}`;
if (itemType === 'field') {
id = `${this.parentId}_${id}`;
2020-04-11 08:22:12 +02:00
}
2020-04-20 11:41:13 +02:00
params.id = id;
2020-04-08 09:59:54 +02:00
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-10 09:57:49 +02:00
};
2020-04-20 11:41:13 +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);
2020-04-08 09:59:54 +02:00
2017-10-17 09:18:53 +02:00
this.set('current', newItem);
},
change(itemId) {
2020-04-01 07:03:26 +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;
items.forEach((it, ind) => {
if (it.id === itemId) {
item = it;
index = ind;
}
});
let nextIndex;
if (this.current.id === itemId) {
nextIndex = index < (items.length-2) ? (index+1) : (index-1);
}
items.removeObject(item);
if (nextIndex) {
this.set('current', items[nextIndex]);
}
2017-10-17 09:18:53 +02:00
}
}
});