custom-wizard.js.es6 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { default as computed } from 'ember-addons/ember-computed-decorators';
  2. import { ajax } from 'discourse/lib/ajax';
  3. const CustomWizard = Discourse.Model.extend({
  4. init() {
  5. const id = this.get('id');
  6. if (id) this.set('existingId', id);
  7. },
  8. @computed('name')
  9. id(name) {
  10. return name ? Ember.String.dasherize(name) : null;
  11. },
  12. save() {
  13. const stepsObj = this.get('steps');
  14. let steps = [];
  15. stepsObj.forEach((s) => {
  16. let step = {
  17. id: Ember.String.dasherize(s.title),
  18. title: s.title,
  19. banner: s.banner,
  20. description: s.description,
  21. fields: [],
  22. actions: []
  23. };
  24. const fields = s.get('fields');
  25. fields.forEach((f) => {
  26. f.set('id', Ember.String.dasherize(f.get('label')));
  27. if (f.get('type') === 'dropdown') {
  28. const choices = f.get('choices');
  29. choices.forEach((c) => {
  30. c.set('id', c.get('label'));
  31. });
  32. }
  33. step['fields'].push(f);
  34. });
  35. s.actions.forEach((a) => {
  36. a['id'] = Ember.String.dasherize(a.label);
  37. step['actions'].push(a);
  38. });
  39. steps.push(step);
  40. });
  41. const id = this.get('id');
  42. const name = this.get('name');
  43. const save_submissions = this.get('save_submissions');
  44. let wizard = { id, name, save_submissions, steps };
  45. const existingId = this.get('existingId');
  46. if (existingId && existingId !== id) {
  47. wizard['existing_id'] = existingId;
  48. };
  49. return ajax("/admin/wizards/custom/save", {
  50. type: 'PUT',
  51. data: {
  52. wizard: JSON.stringify(wizard)
  53. }
  54. });
  55. },
  56. remove() {
  57. return ajax("/admin/wizards/custom/remove", {
  58. type: 'DELETE',
  59. data: {
  60. id: this.get('id')
  61. }
  62. }).then(() => this.destroy());
  63. }
  64. });
  65. CustomWizard.reopenClass({
  66. findAll() {
  67. return ajax("/admin/wizards/custom/all", {
  68. type: 'GET'
  69. }).then(result => {
  70. return result.wizards.map(w => CustomWizard.create(w));
  71. });
  72. },
  73. findAllSubmissions() {
  74. return ajax("/admin/wizards/submissions/all", {
  75. type: "GET"
  76. }).then(result => {
  77. return result.submissions;
  78. });
  79. },
  80. create(w) {
  81. const wizard = this._super.apply(this);
  82. let steps = Ember.A();
  83. let props = { steps };
  84. if (w) {
  85. props['id'] = w.id;
  86. props['name'] = w.name;
  87. if (w.steps) {
  88. w.steps.forEach((s) => {
  89. let fields = Ember.A();
  90. s.fields.forEach((f) => {
  91. let field = Ember.Object.create(f);
  92. let choices = Ember.A();
  93. f.choices.forEach((c) => {
  94. choices.pushObject(Ember.Object.create(c));
  95. });
  96. field.set('choices', choices);
  97. fields.pushObject(field);
  98. });
  99. let actions = Ember.A();
  100. s.actions.forEach((a) => {
  101. actions.pushObject(Ember.Object.create(a));
  102. });
  103. steps.pushObject(Ember.Object.create({
  104. id: s.id,
  105. title: s.title,
  106. description: s.description,
  107. fields,
  108. actions
  109. }));
  110. });
  111. };
  112. } else {
  113. props['save_submissions'] = true;
  114. };
  115. wizard.setProperties(props);
  116. return wizard;
  117. }
  118. });
  119. export default CustomWizard;