custom.js.es6 6.1 KB

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