custom.js.es6 4.4 KB

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