custom.js.es6 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. export default {
  2. name: 'custom-routes',
  3. initialize(app) {
  4. if (app.constructor.name !== 'Class' || app.get('rootElement') !== '#custom-wizard-main') return;
  5. const WizardApplicationRoute = requirejs('wizard/routes/application').default;
  6. const findCustomWizard = requirejs('discourse/plugins/discourse-custom-wizard/wizard/models/custom').findCustomWizard;
  7. const Router = requirejs('wizard/router').default;
  8. const ajax = requirejs('wizard/lib/ajax').ajax;
  9. const StepRoute = requirejs('wizard/routes/step').default;
  10. const StepModel = requirejs('wizard/models/step').default;
  11. const WizardStep = requirejs('wizard/components/wizard-step').default;
  12. const getUrl = requirejs('discourse-common/lib/get-url').default;
  13. Router.map(function() {
  14. this.route('custom', { path: '/custom/:id' }, function() {
  15. this.route('step', { path: '/steps/:step_id' });
  16. });
  17. });
  18. WizardApplicationRoute.reopen({
  19. model() {
  20. const customParams = this.paramsFor('custom');
  21. return findCustomWizard(customParams.id);
  22. },
  23. afterModel(model) {
  24. return ajax({
  25. url: `/site/basic-info`,
  26. type: 'GET',
  27. }).then((result) => {
  28. return model.set('siteInfo', result);
  29. });
  30. },
  31. setupController(controller, model) {
  32. controller.setProperties({
  33. customWizard: true,
  34. siteInfo: model.get('siteInfo')
  35. });
  36. }
  37. });
  38. StepModel.reopen({
  39. save() {
  40. const fields = {};
  41. this.get('fields').forEach(f => fields[f.id] = f.value);
  42. return ajax({
  43. url: `/wizard/custom/${this.get('wizardId')}/steps/${this.get('id')}`,
  44. type: 'PUT',
  45. data: { fields }
  46. }).catch(response => {
  47. response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
  48. throw response;
  49. });
  50. }
  51. });
  52. StepRoute.reopen({
  53. afterModel(model) {
  54. const wizard = this.modelFor('application');
  55. return model.set("wizardId", wizard.id);
  56. }
  57. });
  58. WizardStep.reopen({
  59. bannerImage: function() {
  60. const src = this.get('step.banner');
  61. if (!src) return;
  62. if (src.indexOf('/uploads/') > -1) {
  63. return getUrl(src);
  64. } else {
  65. return getUrl(`/images/wizard/${src}`);
  66. };
  67. }.property('step.banner'),
  68. advance() {
  69. this.set('saving', true);
  70. this.get('step').save()
  71. .then(response => {
  72. if (this.get('finalStep')) {
  73. document.location = getUrl("/");
  74. } else {
  75. this.sendAction('goNext', response);
  76. }
  77. })
  78. .catch(() => this.animateInvalidFields())
  79. .finally(() => this.set('saving', false));
  80. },
  81. actions: {
  82. quit() {
  83. this.set('finalStep', true);
  84. this.send('nextStep');
  85. }
  86. }
  87. });
  88. }
  89. };