custom.js.es6 4.0 KB

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