custom.js.es6 4.9 KB

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