wizard-links.js.es6 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { default as computed } from 'ember-addons/ember-computed-decorators';
  2. export default Ember.Component.extend({
  3. classNames: 'wizard-links',
  4. items: Ember.A(),
  5. didInsertElement() {
  6. this.applySortable();
  7. },
  8. applySortable() {
  9. this.$("ul").sortable({tolerance: 'pointer'}).on('sortupdate', (e, ui) => {
  10. const itemId = ui.item.data('id');
  11. const index = ui.item.index();
  12. Ember.run.bind(this, this.updateItemOrder(itemId, index));
  13. });
  14. },
  15. updateItemOrder(itemId, newIndex) {
  16. const items = this.get('items');
  17. const item = items.findBy('id', itemId);
  18. items.removeObject(item);
  19. items.insertAt(newIndex, item);
  20. Ember.run.scheduleOnce('afterRender', this, () => this.applySortable());
  21. },
  22. @computed('type')
  23. header: (type) => `admin.wizard.${type}.header`,
  24. @computed('items.@each.id', 'current')
  25. links(items, current) {
  26. if (!items) return;
  27. return items.map((item) => {
  28. if (item) {
  29. const id = item.get('id');
  30. const label = item.get('label') || item.get('title');
  31. let link = { id, label: label || id };
  32. let classes = 'btn';
  33. if (current && item.get('id') === current.get('id')) {
  34. classes += ' btn-primary';
  35. };
  36. link['classes'] = classes;
  37. return link;
  38. }
  39. });
  40. },
  41. actions: {
  42. add() {
  43. const items = this.get('items');
  44. const newId = `step_${items.length + 1}`;
  45. const type = this.get('type');
  46. let params = { id: newId, isNew: true };
  47. if (type === 'step') {
  48. params['fields'] = Ember.A();
  49. params['actions'] = Ember.A();
  50. };
  51. const newItem = Ember.Object.create(params);
  52. items.pushObject(newItem);
  53. this.set('current', newItem);
  54. },
  55. change(itemId) {
  56. const items = this.get('items');
  57. this.set('current', items.findBy('id', itemId));
  58. },
  59. remove(itemId) {
  60. const items = this.get('items');
  61. items.removeObject(items.findBy('id', itemId));
  62. this.set('current', items[items.length - 1]);
  63. }
  64. }
  65. });