custom.js.es6 4.5 KB

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