custom.js.es6 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { default as computed } from 'ember-addons/ember-computed-decorators';
  2. import getUrl from 'discourse-common/lib/get-url';
  3. import WizardField from 'wizard/models/wizard-field';
  4. import { ajax } from 'wizard/lib/ajax';
  5. import Step from 'wizard/models/step';
  6. const CustomWizard = Ember.Object.extend({
  7. @computed('steps.length')
  8. totalSteps: length => length,
  9. skip() {
  10. if (this.get('required') && (!this.get('completed') && this.get('permitted'))) return;
  11. const id = this.get('id');
  12. CustomWizard.skip(id);
  13. }
  14. });
  15. CustomWizard.reopenClass({
  16. skip(wizardId) {
  17. ajax({ url: `/w/${wizardId}/skip`, type: 'PUT' }).then((result) => {
  18. CustomWizard.finished(result);
  19. });
  20. },
  21. finished(result) {
  22. let url = "/";
  23. if (result.redirect_to) {
  24. url = result.redirect_to;
  25. }
  26. window.location.href = getUrl(url);
  27. }
  28. });
  29. export function findCustomWizard(wizardId) {
  30. return ajax({ url: `/w/${wizardId}` }).then(result => {
  31. const wizard = result.wizard;
  32. if (!wizard) return null;
  33. if (!wizard.completed) {
  34. wizard.steps = wizard.steps.map(step => {
  35. const stepObj = Step.create(step);
  36. stepObj.fields = stepObj.fields.map(f => WizardField.create(f));
  37. return stepObj;
  38. });
  39. }
  40. return CustomWizard.create(wizard);
  41. });
  42. };
  43. export default CustomWizard;