wizard-links.js.es6 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 type = this.get('type');
  31. const label = type === 'action' ? id : (item.get('label') || item.get('title') || id);
  32. let link = { id, label };
  33. let classes = 'btn';
  34. if (current && item.get('id') === current.get('id')) {
  35. classes += ' btn-primary';
  36. };
  37. link['classes'] = classes;
  38. return link;
  39. }
  40. });
  41. },
  42. actions: {
  43. add() {
  44. const items = this.get('items');
  45. const type = this.get('type');
  46. const newId = `${type}_${items.length + 1}`;
  47. let params = { id: newId, isNew: true };
  48. if (type === 'step') {
  49. params['fields'] = Ember.A();
  50. params['actions'] = Ember.A();
  51. };
  52. const newItem = Ember.Object.create(params);
  53. items.pushObject(newItem);
  54. this.set('current', newItem);
  55. this.sendAction('isNew');
  56. },
  57. change(itemId) {
  58. const items = this.get('items');
  59. this.set('current', items.findBy('id', itemId));
  60. },
  61. remove(itemId) {
  62. const items = this.get('items');
  63. items.removeObject(items.findBy('id', itemId));
  64. this.set('current', items[items.length - 1]);
  65. }
  66. }
  67. });