admin-wizard.js.es6 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { default as computed } from 'ember-addons/ember-computed-decorators';
  2. export default Ember.Controller.extend({
  3. @computed('model.steps.@each.id', 'currentStep')
  4. stepLinks(steps, currentStep) {
  5. return steps.map((s) => {
  6. if (s) {
  7. const id = s.get('id');
  8. const title = s.get('title');
  9. let link = { id, title: title || id || 'new' };
  10. let classes = 'btn';
  11. if (currentStep && id === currentStep.get('id')) {
  12. classes += ' btn-primary';
  13. };
  14. link['classes'] = classes;
  15. return link;
  16. }
  17. });
  18. },
  19. @computed('model.id', 'model.name')
  20. wizardUrl(wizardId) {
  21. return window.location.origin + '/w/' + Ember.String.dasherize(wizardId);
  22. },
  23. actions: {
  24. save() {
  25. this.setProperties({
  26. saving: true,
  27. error: null
  28. });
  29. const wizard = this.get('model');
  30. wizard.save().then(() => {
  31. this.set('saving', false);
  32. if (this.get('newWizard')) {
  33. this.send("refreshAllWizards");
  34. } else {
  35. this.send("refreshWizard");
  36. }
  37. }).catch((error) => {
  38. this.set('saving', false);
  39. this.set('error', I18n.t(`admin.wizard.error.${error}`));
  40. Ember.run.later(() => this.set('error', null), 10000);
  41. });
  42. },
  43. remove() {
  44. this.get('model').remove().then(() => {
  45. this.send("refreshAllWizards");
  46. });
  47. },
  48. addStep() {
  49. const steps = this.get('model.steps');
  50. const step = Ember.Object.create({
  51. fields: Ember.A(),
  52. actions: Ember.A()
  53. });
  54. steps.pushObject(step);
  55. this.set('currentStep', step);
  56. },
  57. removeStep(stepId) {
  58. const steps = this.get('model.steps');
  59. steps.removeObject(steps.findBy('id', stepId));
  60. this.set('currentStep', steps[steps.length - 1]);
  61. },
  62. changeStep(stepId) {
  63. const steps = this.get('model.steps');
  64. this.set('currentStep', steps.findBy('id', stepId));
  65. }
  66. }
  67. });