wizard-custom-step.js.es6 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { default as computed, on, observes } from 'ember-addons/ember-computed-decorators';
  2. export default Ember.Component.extend({
  3. classNames: 'wizard-custom-step',
  4. currentField: null,
  5. currentAction: null,
  6. @on('init')
  7. @observes('step')
  8. setCurrent() {
  9. this.set('existingId', this.get('step.id'));
  10. const fields = this.get('step.fields') || [];
  11. const actions = this.get('step.actions') || [];
  12. this.set('currentField', fields[0]);
  13. this.set('currentAction', actions[0]);
  14. },
  15. @computed('step.fields.@each.id', 'currentField')
  16. fieldLinks(fields, current) {
  17. if (!fields) return;
  18. return fields.map((f) => {
  19. if (f) {
  20. const id = f.get('id');
  21. const label = f.get('label');
  22. let link = { id, label: label || id || 'new' };
  23. let classes = 'btn';
  24. if (current && f.get('id') === current.get('id')) {
  25. classes += ' btn-primary';
  26. };
  27. link['classes'] = classes;
  28. return link;
  29. }
  30. });
  31. },
  32. @computed('step.actions.@each.id', 'currentAction')
  33. actionLinks(actions, current) {
  34. if (!actions) return;
  35. return actions.map((a) => {
  36. if (a) {
  37. const id = a.get('id');
  38. const label = a.get('label');
  39. let link = { id, label: label || id || 'new' };
  40. let classes = 'btn';
  41. if (current && a.get('id') === current.get('id')) {
  42. classes += ' btn-primary';
  43. };
  44. link['classes'] = classes;
  45. return link;
  46. }
  47. });
  48. },
  49. actions: {
  50. addField() {
  51. const fields = this.get('step.fields');
  52. const field = Ember.Object.create();
  53. fields.pushObject(field);
  54. this.set('currentField', field);
  55. },
  56. addAction() {
  57. const actions = this.get('step.actions');
  58. const action = Ember.Object.create();
  59. actions.pushObject(action);
  60. this.set('currentAction', action);
  61. },
  62. removeField(fieldId) {
  63. const fields = this.get('step.fields');
  64. fields.removeObject(fields.findBy('id', fieldId));
  65. this.set('currentField', fields[fields.length - 1]);
  66. },
  67. removeAction(actionId) {
  68. const actions = this.get('step.actions');
  69. actions.removeObject(actions.findBy('id', actionId));
  70. this.set('currentAction', actions[actions.length - 1]);
  71. },
  72. changeField(fieldId) {
  73. const fields = this.get('step.fields');
  74. this.set('currentField', fields.findBy('id', fieldId));
  75. },
  76. changeAction(actionId) {
  77. const actions = this.get('step.actions');
  78. this.set('currentAction', actions.findBy('id', actionId));
  79. }
  80. }
  81. });