custom.js.es6 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. const FieldModel = requirejs('wizard/models/wizard-field').default;
  14. Router.map(function() {
  15. this.route('custom', { path: '/custom/:id' }, function() {
  16. this.route('step', { path: '/steps/:step_id' });
  17. });
  18. });
  19. WizardApplicationRoute.reopen({
  20. model() {
  21. const customParams = this.paramsFor('custom');
  22. return findCustomWizard(customParams.id);
  23. },
  24. afterModel(model) {
  25. return Ember.RSVP.hash({
  26. info: ajax({
  27. url: `/site/basic-info`,
  28. type: 'GET',
  29. }).then((result) => {
  30. return model.set('siteInfo', result);
  31. }),
  32. settings: ajax({
  33. url: `/site/settings`,
  34. type: 'GET',
  35. }).then((result) => {
  36. Object.assign(Wizard.SiteSettings, result);
  37. })
  38. });
  39. },
  40. setupController(controller, model) {
  41. Ember.run.scheduleOnce('afterRender', this, function(){
  42. $('body.custom-wizard').css('background', model.get('background'));
  43. });
  44. controller.setProperties({
  45. customWizard: true,
  46. siteInfo: model.get('siteInfo')
  47. });
  48. }
  49. });
  50. StepModel.reopen({
  51. save() {
  52. const fields = {};
  53. this.get('fields').forEach(f => fields[f.id] = f.value);
  54. return ajax({
  55. url: `/wizard/custom/${this.get('wizardId')}/steps/${this.get('id')}`,
  56. type: 'PUT',
  57. data: { fields }
  58. }).catch(response => {
  59. response.responseJSON.errors.forEach(err => this.fieldError(err.field, err.description));
  60. throw response;
  61. });
  62. }
  63. });
  64. StepRoute.reopen({
  65. afterModel(model) {
  66. if (!model) {
  67. return document.location = getUrl("/");
  68. }
  69. const wizard = this.modelFor('application');
  70. return model.set("wizardId", wizard.id);
  71. }
  72. });
  73. WizardStep.reopen({
  74. bannerImage: function() {
  75. const src = this.get('step.banner');
  76. if (!src) return;
  77. if (src.indexOf('/uploads/') > -1 || src.indexOf('/images/') > -1) {
  78. return getUrl(src);
  79. } else {
  80. return getUrl(`/images/wizard/${src}`);
  81. };
  82. }.property('step.banner'),
  83. advance() {
  84. this.set('saving', true);
  85. this.get('step').save()
  86. .then(response => {
  87. if (this.get('finalStep')) {
  88. document.location = getUrl("/");
  89. } else {
  90. this.sendAction('goNext', response);
  91. }
  92. })
  93. .catch(() => this.animateInvalidFields())
  94. .finally(() => this.set('saving', false));
  95. },
  96. actions: {
  97. quit() {
  98. this.set('finalStep', true);
  99. this.send('nextStep');
  100. }
  101. }
  102. });
  103. FieldModel.reopen({
  104. check() {
  105. let valid = this.get('valid');
  106. if (!this.get('required')) {
  107. this.setValid(true);
  108. return true;
  109. }
  110. if (!this.get('customValidation')) {
  111. const val = this.get('value');
  112. valid = val && val.length > 0;
  113. this.setValid(valid);
  114. }
  115. return valid;
  116. }
  117. });
  118. }
  119. };