custom.js.es6 4.5 KB

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