custom.js.es6 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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')) return;
  11. const id = this.get('id');
  12. ajax({ url: `/w/${id}/skip`, type: 'PUT' }).then((result) => {
  13. this.finished(result);
  14. });
  15. },
  16. finished(result) {
  17. let url = "/";
  18. if (result.redirect_to) {
  19. url = result.redirect_to;
  20. }
  21. window.location.href = getUrl(url);
  22. }
  23. });
  24. export function findCustomWizard(wizardId) {
  25. return ajax({ url: `/w/${wizardId}` }).then(result => {
  26. const wizard = result.wizard;
  27. if (!wizard) return null;
  28. if (!wizard.completed) {
  29. wizard.steps = wizard.steps.map(step => {
  30. const stepObj = Step.create(step);
  31. stepObj.fields = stepObj.fields.map(f => WizardField.create(f));
  32. return stepObj;
  33. });
  34. }
  35. return CustomWizard.create(wizard);
  36. });
  37. };
  38. export default CustomWizard;