custom.js.es6 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 CustomWizard = requirejs('discourse/plugins/discourse-custom-wizard/wizard/models/custom').default;
  10. const WizardStep = requirejs('wizard/components/wizard-step').default;
  11. const WizardField = requirejs('wizard/components/wizard-field').default;
  12. const getUrl = requirejs('discourse-common/lib/get-url').default;
  13. const FieldModel = requirejs('wizard/models/wizard-field').default;
  14. const autocomplete = requirejs('discourse/lib/autocomplete').default;
  15. const cook = requirejs('discourse/plugins/discourse-custom-wizard/wizard/lib/text-lite').cook;
  16. $.fn.autocomplete = autocomplete;
  17. // this is for discourse/lib/utilities.avatarImg;
  18. Discourse.getURLWithCDN = getUrl;
  19. Router.reopen({
  20. rootURL: getUrl('/w/')
  21. });
  22. Router.map(function() {
  23. this.route('custom', { path: '/:wizard_id' }, function() {
  24. this.route('steps');
  25. this.route('step', { path: '/steps/:step_id' });
  26. });
  27. });
  28. ApplicationRoute.reopen({
  29. redirect() {
  30. this.transitionTo('custom');
  31. },
  32. model() {}
  33. });
  34. WizardStep.reopen({
  35. classNameBindings: ['step.id'],
  36. animateInvalidFields() {
  37. Ember.run.scheduleOnce('afterRender', () => {
  38. $('.invalid input[type=text], .invalid textarea, .invalid input[type=checkbox]').wiggle(2, 100);
  39. });
  40. },
  41. ensureStartsAtTop: function() {
  42. window.scrollTo(0,0);
  43. }.observes('step.id'),
  44. showQuitButton: function() {
  45. const index = this.get('step.index');
  46. const required = this.get('wizard.required');
  47. return index === 0 && !required;
  48. }.property('step.index', 'wizard.required'),
  49. cookedDescription: function() {
  50. return cook(this.get('step.description'));
  51. }.property('step.description'),
  52. bannerImage: function() {
  53. const src = this.get('step.banner');
  54. if (!src) return;
  55. return getUrl(src);
  56. }.property('step.banner'),
  57. handleMessage: function() {
  58. const message = this.get('step.message');
  59. this.sendAction('showMessage', message);
  60. }.observes('step.message'),
  61. advance() {
  62. this.set('saving', true);
  63. this.get('step').save()
  64. .then(response => {
  65. if (this.get('finalStep')) {
  66. CustomWizard.finished(response);
  67. } else {
  68. this.sendAction('goNext', response);
  69. }
  70. })
  71. .catch(() => this.animateInvalidFields())
  72. .finally(() => this.set('saving', false));
  73. },
  74. actions: {
  75. quit() {
  76. this.get('wizard').skip();
  77. },
  78. done() {
  79. this.set('finalStep', true);
  80. this.send('nextStep');
  81. },
  82. showMessage(message) {
  83. this.sendAction('showMessage', message);
  84. }
  85. }
  86. });
  87. StepModel.reopen({
  88. save() {
  89. const wizardId = this.get('wizardId');
  90. const fields = {};
  91. this.get('fields').forEach(f => {
  92. if (f.type !== 'text-only') {
  93. fields[f.id] = f.value;
  94. }
  95. });
  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 StandardFieldValidation = ['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 (StandardFieldValidation.indexOf(type) > -1) {
  168. valid = val && val.length > 0;
  169. }
  170. }
  171. this.setValid(valid);
  172. return valid;
  173. }
  174. });
  175. }
  176. };