custom-wizard.js.es6 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { ajax } from 'discourse/lib/ajax';
  2. const CustomWizard = Discourse.Model.extend({
  3. save() {
  4. return new Ember.RSVP.Promise((resolve, reject) => {
  5. const id = this.get('id');
  6. if (!id || !id.underscore()) reject('id_required');
  7. let wizard = { id: id.underscore() };
  8. const steps = this.get('steps');
  9. if (steps.length) wizard['steps'] = this.buildSteps(steps, reject);
  10. const name = this.get('name');
  11. if (name) wizard['name'] = name;
  12. const background = this.get('background');
  13. if (background) wizard['background'] = background;
  14. const save_submissions = this.get('save_submissions');
  15. if (save_submissions) wizard['save_submissions'] = save_submissions;
  16. const multiple_submissions = this.get('multiple_submissions');
  17. if (multiple_submissions) wizard['multiple_submissions'] = multiple_submissions;
  18. ajax("/admin/wizards/custom/save", {
  19. type: 'PUT',
  20. data: {
  21. wizard: JSON.stringify(wizard)
  22. }
  23. }).then((result) => resolve(result));
  24. });
  25. },
  26. buildSteps(stepsObj, reject) {
  27. let steps = [];
  28. stepsObj.some((s) => {
  29. if (!s.id || !s.id.underscore()) reject('id_required');
  30. let step = { id: s.id.underscore() };
  31. if (s.title) step['title'] = s.title;
  32. if (s.key) step['key'] = s.key;
  33. if (s.banner) step['banner'] = s.banner;
  34. if (s.description) step['description'] = s.description;
  35. const fields = s.get('fields');
  36. if (fields.length) {
  37. step['fields'] = [];
  38. fields.some((f) => {
  39. let id = f.get('id');
  40. if (!id || !id.underscore()) reject('id_required');
  41. f.set('id', id.underscore());
  42. if (f.get('type') === 'dropdown') {
  43. const choices = f.get('choices');
  44. if (choices && choices.length < 1 && !f.get('choices_key') && !f.get('choices_categories')) {
  45. reject('field.need_choices');
  46. }
  47. }
  48. delete f.isNew;
  49. step['fields'].push(f);
  50. });
  51. }
  52. const actions = s.actions;
  53. if (actions.length) {
  54. step['actions'] = [];
  55. actions.some((a) => {
  56. let id = a.get('id');
  57. if (!id || !id.underscore()) reject('id_required');
  58. a.set('id', id.underscore());
  59. delete a.isNew;
  60. step['actions'].push(a);
  61. });
  62. }
  63. steps.push(step);
  64. });
  65. return steps;
  66. },
  67. remove() {
  68. return ajax("/admin/wizards/custom/remove", {
  69. type: 'DELETE',
  70. data: {
  71. id: this.get('id')
  72. }
  73. }).then(() => this.destroy());
  74. }
  75. });
  76. CustomWizard.reopenClass({
  77. findAll() {
  78. return ajax("/admin/wizards/custom/all", {
  79. type: 'GET'
  80. }).then(result => {
  81. return result.wizards.map(w => CustomWizard.create(w));
  82. });
  83. },
  84. findAllSubmissions() {
  85. return ajax("/admin/wizards/submissions/all", {
  86. type: "GET"
  87. }).then(result => {
  88. return result.submissions;
  89. });
  90. },
  91. create(w) {
  92. const wizard = this._super.apply(this);
  93. let steps = Ember.A();
  94. let props = { steps };
  95. if (w) {
  96. props['id'] = w.id;
  97. props['existingId'] = true;
  98. props['name'] = w.name;
  99. props['background'] = w.background;
  100. props['save_submissions'] = w.save_submissions;
  101. props['multiple_submissions'] = w.multiple_submissions;
  102. if (w.steps && w.steps.length) {
  103. w.steps.forEach((s) => {
  104. // clean empty strings
  105. Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);
  106. let fields = Ember.A();
  107. if (s.fields && s.fields.length) {
  108. s.fields.forEach((f) => {
  109. Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);
  110. const fieldParams = { isNew: false };
  111. let field = Ember.Object.create(Object.assign(f, fieldParams));
  112. if (f.choices) {
  113. let choices = Ember.A();
  114. f.choices.forEach((c) => {
  115. choices.pushObject(Ember.Object.create(c));
  116. });
  117. field.set('choices', choices);
  118. }
  119. fields.pushObject(field);
  120. });
  121. }
  122. let actions = Ember.A();
  123. if (s.actions && s.actions.length) {
  124. s.actions.forEach((a) => {
  125. const actionParams = { isNew: false };
  126. const action = Ember.Object.create(Object.assign(a, actionParams));
  127. actions.pushObject(action);
  128. });
  129. }
  130. steps.pushObject(Ember.Object.create({
  131. id: s.id,
  132. key: s.key,
  133. title: s.title,
  134. description: s.description,
  135. banner: s.banner,
  136. fields,
  137. actions,
  138. isNew: false
  139. }));
  140. });
  141. };
  142. } else {
  143. props['id'] = '';
  144. props['name'] = '';
  145. props['background'] = '';
  146. props['save_submissions'] = true;
  147. props['multiple_submissions'] = false;
  148. props['steps'] = Ember.A();
  149. };
  150. wizard.setProperties(props);
  151. return wizard;
  152. }
  153. });
  154. export default CustomWizard;