custom.js.es6 4.0 KB

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