custom.js.es6 4.3 KB

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