custom.js.es6 4.3 KB

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