custom.js.es6 5.7 KB

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