custom.js.es6 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. export default {
  2. name: 'custom-routes',
  3. initialize(app) {
  4. if (app.constructor.name !== 'Class' || app.get('rootElement') !== '#custom-wizard-main') return;
  5. const Router = requirejs('wizard/router').default;
  6. const ApplicationRoute = requirejs('wizard/routes/application').default;
  7. const ajax = requirejs('wizard/lib/ajax').ajax;
  8. const StepModel = requirejs('wizard/models/step').default;
  9. const WizardStep = requirejs('wizard/components/wizard-step').default;
  10. const getUrl = requirejs('discourse-common/lib/get-url').default;
  11. const FieldModel = requirejs('wizard/models/wizard-field').default;
  12. Router.reopen({
  13. rootURL: getUrl('/w/')
  14. });
  15. Router.map(function() {
  16. this.route('custom', { path: '/:wizard_id' }, function() {
  17. this.route('steps');
  18. this.route('step', { path: '/steps/:step_id' });
  19. });
  20. });
  21. ApplicationRoute.reopen({
  22. redirect() {
  23. this.transitionTo('custom');
  24. },
  25. model() {}
  26. });
  27. WizardStep.reopen({
  28. bannerImage: function() {
  29. const src = this.get('step.banner');
  30. if (!src) return;
  31. if (src.indexOf('/uploads/') > -1 || src.indexOf('/images/') > -1) {
  32. return getUrl(src);
  33. } else {
  34. return getUrl(`/images/wizard/${src}`);
  35. };
  36. }.property('step.banner'),
  37. handleMessage: function() {
  38. const message = this.get('step.message');
  39. this.sendAction('showMessage', message);
  40. }.observes('step.message'),
  41. advance() {
  42. this.set('saving', true);
  43. this.get('step').save()
  44. .then(response => {
  45. if (this.get('finalStep')) {
  46. this.sendAction('finished', response);
  47. } else {
  48. this.sendAction('goNext', response);
  49. }
  50. })
  51. .catch(() => this.animateInvalidFields())
  52. .finally(() => this.set('saving', false));
  53. },
  54. actions: {
  55. quit() {
  56. this.set('finalStep', true);
  57. this.send('nextStep');
  58. },
  59. showMessage(message) {
  60. this.sendAction('showMessage', message);
  61. }
  62. }
  63. });
  64. StepModel.reopen({
  65. save() {
  66. const wizardId = this.get('wizardId');
  67. const fields = {};
  68. this.get('fields').forEach(f => fields[f.id] = f.value);
  69. return ajax({
  70. url: `/w/${wizardId}/steps/${this.get('id')}`,
  71. type: 'PUT',
  72. data: { fields }
  73. }).catch(response => {
  74. if (response && response.responseJSON && response.responseJSON.errors) {
  75. let wizardErrors = [];
  76. response.responseJSON.errors.forEach(err => {
  77. if (err.field === wizardId) {
  78. wizardErrors.push(err.description);
  79. } else if (err.field) {
  80. this.fieldError(err.field, err.description);
  81. } else if (err) {
  82. wizardErrors.push(err);
  83. }
  84. });
  85. if (wizardErrors.length) {
  86. this.handleWizardError(wizardErrors.join('\n'));
  87. }
  88. throw response;
  89. }
  90. if (response && response.responseText) {
  91. const responseText = response.responseText;
  92. const start = responseText.indexOf('>') + 1;
  93. const end = responseText.indexOf('plugins');
  94. const message = responseText.substring(start, end);
  95. this.handleWizardError(message);
  96. throw message;
  97. }
  98. });
  99. },
  100. handleWizardError(message) {
  101. this.set('message', {
  102. state: 'error',
  103. text: message
  104. });
  105. Ember.run.later(() => this.set('message', null), 6000);
  106. }
  107. });
  108. FieldModel.reopen({
  109. check() {
  110. let valid = this.get('valid');
  111. if (!this.get('required')) {
  112. this.setValid(true);
  113. return true;
  114. }
  115. if (!this.get('customValidation')) {
  116. const val = this.get('value');
  117. valid = val && val.length > 0;
  118. this.setValid(valid);
  119. }
  120. return valid;
  121. }
  122. });
  123. }
  124. };