custom.js.es6 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. console.log(model)
  33. Ember.run.scheduleOnce('afterRender', this, function(){
  34. $('body.custom-wizard').css('background', model.get('background'));
  35. });
  36. controller.setProperties({
  37. customWizard: true,
  38. siteInfo: model.get('siteInfo')
  39. });
  40. }
  41. });
  42. StepModel.reopen({
  43. save() {
  44. const fields = {};
  45. this.get('fields').forEach(f => fields[f.id] = f.value);
  46. return ajax({
  47. url: `/wizard/custom/${this.get('wizardId')}/steps/${this.get('id')}`,
  48. type: 'PUT',
  49. data: { fields }
  50. }).catch(response => {
  51. response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
  52. throw response;
  53. });
  54. }
  55. });
  56. StepRoute.reopen({
  57. afterModel(model) {
  58. if (!model) {
  59. return document.location = getUrl("/");
  60. }
  61. const wizard = this.modelFor('application');
  62. return model.set("wizardId", wizard.id);
  63. }
  64. });
  65. WizardStep.reopen({
  66. bannerImage: function() {
  67. const src = this.get('step.banner');
  68. if (!src) return;
  69. if (src.indexOf('/uploads/') > -1 || src.indexOf('/images/') > -1) {
  70. return getUrl(src);
  71. } else {
  72. return getUrl(`/images/wizard/${src}`);
  73. };
  74. }.property('step.banner'),
  75. advance() {
  76. this.set('saving', true);
  77. this.get('step').save()
  78. .then(response => {
  79. if (this.get('finalStep')) {
  80. document.location = getUrl("/");
  81. } else {
  82. this.sendAction('goNext', response);
  83. }
  84. })
  85. .catch(() => this.animateInvalidFields())
  86. .finally(() => this.set('saving', false));
  87. },
  88. actions: {
  89. quit() {
  90. this.set('finalStep', true);
  91. this.send('nextStep');
  92. }
  93. }
  94. });
  95. }
  96. };