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

86 Zeilen
2,2 KiB
Text

2020-03-21 18:30:11 +01:00
import { default as computed, on, observes } from 'discourse-common/utils/decorators';
2020-03-29 09:49:33 +02:00
import { notEmpty } from "@ember/object/computed";
2020-03-30 01:53:28 +02:00
import { scheduleOnce } from "@ember/runloop";
2017-10-17 09:18:53 +02:00
export default Ember.Component.extend({
2020-03-30 01:53:28 +02:00
classNameBindings: [':wizard-links', 'type'],
2017-10-17 09:18:53 +02:00
items: Ember.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')
@observes('links.@each')
2017-10-17 09:18:53 +02:00
didInsertElement() {
2020-03-30 01:53:28 +02:00
scheduleOnce('afterRender', () => (this.applySortable()));
2017-10-17 09:18:53 +02:00
},
applySortable() {
2020-03-29 09:49:33 +02:00
$(this.element).find("ul").sortable({tolerance: 'pointer'}).on('sortupdate', (e, ui) => {
2017-10-17 09:18:53 +02:00
const itemId = ui.item.data('id');
const index = ui.item.index();
Ember.run.bind(this, this.updateItemOrder(itemId, index));
});
},
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
},
@computed('type')
header: (type) => `admin.wizard.${type}.header`,
@computed('items.@each.id', 'current')
links(items, current) {
if (!items) return;
return items.map((item) => {
if (item) {
2020-04-01 07:03:26 +02:00
const id = item.id;
const type = this.type;
const label = type === 'action' ? id : (item.label || item.title || id);
2017-10-17 15:17:53 +02:00
let link = { id, label };
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';
};
link['classes'] = classes;
return link;
}
});
},
actions: {
add() {
2020-04-01 07:03:26 +02:00
const items = this.items;
const type = this.type;
2017-10-17 15:17:53 +02:00
const newId = `${type}_${items.length + 1}`;
2017-10-17 09:18:53 +02:00
let params = { id: newId, isNew: true };
if (type === 'step') {
params['fields'] = Ember.A();
params['actions'] = Ember.A();
};
const newItem = Ember.Object.create(params);
items.pushObject(newItem);
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;
2017-10-17 09:18:53 +02:00
items.removeObject(items.findBy('id', itemId));
this.set('current', items[items.length - 1]);
}
}
});