wizard-custom-step.js.es6 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. setup() {
  9. this._super(...arguments);
  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.[]', '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 };
  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.[]', '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 };
  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 newNum = fields.length + 1;
  53. const field = Ember.Object.create({
  54. id: `field-${newNum}`
  55. });
  56. fields.pushObject(field);
  57. this.set('currentField', field);
  58. },
  59. addAction() {
  60. const actions = this.get('step.actions');
  61. const newNum = actions.length + 1;
  62. const action = Ember.Object.create({
  63. id: `action-${newNum}`
  64. });
  65. actions.pushObject(action);
  66. this.set('currentAction', action);
  67. },
  68. removeField(fieldId) {
  69. const fields = this.get('step.fields');
  70. fields.removeObject(fields.findBy('id', fieldId));
  71. this.set('currentField', fields[fields.length - 1]);
  72. },
  73. removeAction(actionId) {
  74. const actions = this.get('step.actions');
  75. actions.removeObject(actions.findBy('id', actionId));
  76. this.set('currentAction', actions[actions.length - 1]);
  77. },
  78. changeField(fieldId) {
  79. const fields = this.get('step.fields');
  80. this.set('currentField', fields.findBy('id', fieldId));
  81. },
  82. changeAction(actionId) {
  83. const actions = this.get('step.actions');
  84. this.set('currentAction', actions.findBy('id', actionId));
  85. }
  86. }
  87. });