custom.js.es6 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. ensureStartsAtTop: function() {
  35. window.scrollTo(0,0);
  36. }.observes('step.id'),
  37. showQuitButton: function() {
  38. const index = this.get('step.index');
  39. const required = this.get('wizard.required');
  40. return index === 0 && !required;
  41. }.property('step.index', 'wizard.required'),
  42. bannerImage: function() {
  43. const src = this.get('step.banner');
  44. if (!src) return;
  45. if (src.indexOf('/uploads/') > -1 || src.indexOf('/plugins/') > -1) {
  46. return getUrl(src);
  47. } else {
  48. return getUrl(`/images/wizard/${src}`);
  49. };
  50. }.property('step.banner'),
  51. handleMessage: function() {
  52. const message = this.get('step.message');
  53. this.sendAction('showMessage', message);
  54. }.observes('step.message'),
  55. advance() {
  56. this.set('saving', true);
  57. this.get('step').save()
  58. .then(response => {
  59. if (this.get('finalStep')) {
  60. this.get('wizard').finished(response);
  61. } else {
  62. this.sendAction('goNext', response);
  63. }
  64. })
  65. .catch(() => this.animateInvalidFields())
  66. .finally(() => this.set('saving', false));
  67. },
  68. actions: {
  69. quit() {
  70. this.get('wizard').skip();
  71. },
  72. done() {
  73. this.set('finalStep', true);
  74. this.send('nextStep');
  75. },
  76. showMessage(message) {
  77. this.sendAction('showMessage', message);
  78. }
  79. }
  80. });
  81. StepModel.reopen({
  82. save() {
  83. const wizardId = this.get('wizardId');
  84. const fields = {};
  85. this.get('fields').forEach(f => fields[f.id] = f.value);
  86. return ajax({
  87. url: `/w/${wizardId}/steps/${this.get('id')}`,
  88. type: 'PUT',
  89. data: { fields }
  90. }).catch(response => {
  91. if (response && response.responseJSON && response.responseJSON.errors) {
  92. let wizardErrors = [];
  93. response.responseJSON.errors.forEach(err => {
  94. if (err.field === wizardId) {
  95. wizardErrors.push(err.description);
  96. } else if (err.field) {
  97. this.fieldError(err.field, err.description);
  98. } else if (err) {
  99. wizardErrors.push(err);
  100. }
  101. });
  102. if (wizardErrors.length) {
  103. this.handleWizardError(wizardErrors.join('\n'));
  104. }
  105. throw response;
  106. }
  107. if (response && response.responseText) {
  108. const responseText = response.responseText;
  109. const start = responseText.indexOf('>') + 1;
  110. const end = responseText.indexOf('plugins');
  111. const message = responseText.substring(start, end);
  112. this.handleWizardError(message);
  113. throw message;
  114. }
  115. });
  116. },
  117. handleWizardError(message) {
  118. this.set('message', {
  119. state: 'error',
  120. text: message
  121. });
  122. Ember.run.later(() => this.set('message', null), 6000);
  123. }
  124. });
  125. WizardField.reopen({
  126. inputComponentName: function() {
  127. const type = this.get('field.type');
  128. const id = this.get('field.id');
  129. if (type === 'text-only') return false;
  130. return (type === 'component') ? Ember.String.dasherize(id) : `wizard-field-${type}`;
  131. }.property('field.type', 'field.id')
  132. });
  133. FieldModel.reopen({
  134. check() {
  135. let valid = this.get('valid');
  136. if (!this.get('required')) {
  137. this.setValid(true);
  138. return true;
  139. }
  140. if (!this.get('customValidation')) {
  141. const val = this.get('value');
  142. valid = val && val.length > 0;
  143. this.setValid(valid);
  144. }
  145. return valid;
  146. }
  147. });
  148. }
  149. };