custom.js.es6 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. export default {
  2. name: 'custom-routes',
  3. initialize(app) {
  4. console.log('running initializer', app.constructor.name, app.get('rootElement'))
  5. if (app.constructor.name !== 'Class' || app.get('rootElement') !== '#custom-wizard-main') return;
  6. const Router = requirejs('wizard/router').default;
  7. const ApplicationRoute = requirejs('wizard/routes/application').default;
  8. const ajax = requirejs('wizard/lib/ajax').ajax;
  9. const StepModel = requirejs('wizard/models/step').default;
  10. const WizardStep = requirejs('wizard/components/wizard-step').default;
  11. const getUrl = requirejs('discourse-common/lib/get-url').default;
  12. const FieldModel = requirejs('wizard/models/wizard-field').default;
  13. Router.reopen({
  14. rootURL: getUrl('/w/')
  15. });
  16. Router.map(function() {
  17. this.route('custom', { path: '/:wizard_id' }, function() {
  18. this.route('steps');
  19. this.route('step', { path: '/steps/:step_id' });
  20. });
  21. });
  22. ApplicationRoute.reopen({
  23. redirect() {
  24. this.transitionTo('custom');
  25. },
  26. model() {}
  27. });
  28. WizardStep.reopen({
  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('/images/') > -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. if ($(event.target).hasClass('quit')) {
  63. this.get('wizard').skip();
  64. } else {
  65. this.set('finalStep', true);
  66. this.send('nextStep');
  67. };
  68. },
  69. showMessage(message) {
  70. this.sendAction('showMessage', message);
  71. }
  72. }
  73. });
  74. StepModel.reopen({
  75. save() {
  76. const wizardId = this.get('wizardId');
  77. const fields = {};
  78. this.get('fields').forEach(f => fields[f.id] = f.value);
  79. return ajax({
  80. url: `/w/${wizardId}/steps/${this.get('id')}`,
  81. type: 'PUT',
  82. data: { fields }
  83. }).catch(response => {
  84. if (response && response.responseJSON && response.responseJSON.errors) {
  85. let wizardErrors = [];
  86. response.responseJSON.errors.forEach(err => {
  87. if (err.field === wizardId) {
  88. wizardErrors.push(err.description);
  89. } else if (err.field) {
  90. this.fieldError(err.field, err.description);
  91. } else if (err) {
  92. wizardErrors.push(err);
  93. }
  94. });
  95. if (wizardErrors.length) {
  96. this.handleWizardError(wizardErrors.join('\n'));
  97. }
  98. throw response;
  99. }
  100. if (response && response.responseText) {
  101. const responseText = response.responseText;
  102. const start = responseText.indexOf('>') + 1;
  103. const end = responseText.indexOf('plugins');
  104. const message = responseText.substring(start, end);
  105. this.handleWizardError(message);
  106. throw message;
  107. }
  108. });
  109. },
  110. handleWizardError(message) {
  111. this.set('message', {
  112. state: 'error',
  113. text: message
  114. });
  115. Ember.run.later(() => this.set('message', null), 6000);
  116. }
  117. });
  118. FieldModel.reopen({
  119. check() {
  120. let valid = this.get('valid');
  121. if (!this.get('required')) {
  122. this.setValid(true);
  123. return true;
  124. }
  125. if (!this.get('customValidation')) {
  126. const val = this.get('value');
  127. valid = val && val.length > 0;
  128. this.setValid(valid);
  129. }
  130. return valid;
  131. }
  132. });
  133. }
  134. };