wizard-links.js.es6 2.2 KB

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