custom-wizard.js.es6 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { ajax } from 'discourse/lib/ajax';
  2. const wizardProperties = [
  3. 'name',
  4. 'background',
  5. 'save_submissions',
  6. 'multiple_submissions',
  7. 'after_signup',
  8. 'after_time',
  9. 'after_time_scheduled',
  10. 'required',
  11. 'prompt_completion',
  12. 'min_trust',
  13. 'theme_id'
  14. ];
  15. const CustomWizard = Discourse.Model.extend({
  16. save() {
  17. return new Ember.RSVP.Promise((resolve, reject) => {
  18. const id = this.get('id');
  19. if (!id || !id.underscore()) return reject({ error: 'id_required' });
  20. let wizard = { id: id.underscore() };
  21. wizardProperties.forEach((p) => {
  22. const value = this.get(p);
  23. if (value) wizard[p] = value;
  24. });
  25. if (wizard['after_time'] && !wizard['after_time_scheduled']) {
  26. return reject({ error: 'after_time_need_time' });
  27. };
  28. const steps = this.get('steps');
  29. if (steps.length > 0) {
  30. const stepsResult = this.buildSteps(steps);
  31. if (stepsResult.error) {
  32. reject({ error: stepsResult.error });
  33. } else {
  34. wizard['steps'] = stepsResult.steps;
  35. }
  36. }
  37. if (steps.length < 1 || !wizard['steps'] || wizard['steps'].length < 1) {
  38. return reject({ error: 'steps_required' });
  39. }
  40. ajax("/admin/wizards/custom/save", {
  41. type: 'PUT',
  42. data: {
  43. wizard: JSON.stringify(wizard)
  44. }
  45. }).then((result) => {
  46. if (result.error) {
  47. reject(result);
  48. } else {
  49. resolve(result);
  50. }
  51. });
  52. });
  53. },
  54. buildSteps(stepsObj) {
  55. let steps = [];
  56. let error = null;
  57. stepsObj.some((s) => {
  58. if (!s.id || !s.id.underscore()) {
  59. error = 'id_required';
  60. return;
  61. };
  62. let step = { id: s.id.underscore() };
  63. if (s.title) step['title'] = s.title;
  64. if (s.key) step['key'] = s.key;
  65. if (s.banner) step['banner'] = s.banner;
  66. if (s.raw_description) step['raw_description'] = s.raw_description;
  67. const fields = s.get('fields');
  68. if (fields.length) {
  69. step['fields'] = [];
  70. fields.some((f) => {
  71. let id = f.id;
  72. if (!id || !id.underscore()) {
  73. error = 'id_required';
  74. return;
  75. }
  76. if (!f.type) {
  77. error = 'type_required';
  78. return;
  79. }
  80. f.set('id', id.underscore());
  81. if (f.label === '') delete f.label;
  82. if (f.description === '') delete f.description;
  83. if (f.type === 'dropdown') {
  84. const choices = f.choices;
  85. if ((!choices || choices.length < 1) && !f.choices_key && !f.choices_preset) {
  86. error = 'field.need_choices';
  87. return;
  88. }
  89. if (f.dropdown_none === '') delete f.dropdown_none;
  90. }
  91. delete f.isNew;
  92. step['fields'].push(f);
  93. });
  94. if (error) return;
  95. }
  96. const actions = s.actions;
  97. if (actions.length) {
  98. step['actions'] = [];
  99. actions.some((a) => {
  100. let id = a.get('id');
  101. if (!id || !id.underscore()) {
  102. error = 'id_required';
  103. return;
  104. }
  105. a.set('id', id.underscore());
  106. delete a.isNew;
  107. step['actions'].push(a);
  108. });
  109. if (error) return;
  110. }
  111. steps.push(step);
  112. });
  113. if (error) {
  114. return { error };
  115. } else {
  116. return { steps };
  117. };
  118. },
  119. remove() {
  120. return ajax("/admin/wizards/custom/remove", {
  121. type: 'DELETE',
  122. data: {
  123. id: this.get('id')
  124. }
  125. }).then(() => this.destroy());
  126. }
  127. });
  128. CustomWizard.reopenClass({
  129. all() {
  130. return ajax("/admin/wizards/custom/all", {
  131. type: 'GET'
  132. }).then(result => {
  133. return result.wizards.map(w => CustomWizard.create(w));
  134. });
  135. },
  136. submissions(wizardId) {
  137. return ajax(`/admin/wizards/submissions/${wizardId}`, {
  138. type: "GET"
  139. }).then(result => {
  140. return result.submissions;
  141. });
  142. },
  143. create(w) {
  144. const wizard = this._super.apply(this);
  145. let steps = Ember.A();
  146. let props = { steps };
  147. if (w) {
  148. props['id'] = w.id;
  149. props['existingId'] = true;
  150. wizardProperties.forEach((p) => {
  151. props[p] = w[p];
  152. });
  153. if (w.steps && w.steps.length) {
  154. w.steps.forEach((s) => {
  155. // clean empty strings
  156. Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);
  157. let fields = Ember.A();
  158. if (s.fields && s.fields.length) {
  159. s.fields.forEach((f) => {
  160. Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);
  161. const fieldParams = { isNew: false };
  162. let field = Ember.Object.create($.extend(f, fieldParams));
  163. if (f.choices) {
  164. let choices = Ember.A();
  165. f.choices.forEach((c) => {
  166. choices.pushObject(Ember.Object.create(c));
  167. });
  168. field.set('choices', choices);
  169. }
  170. fields.pushObject(field);
  171. });
  172. }
  173. let actions = Ember.A();
  174. if (s.actions && s.actions.length) {
  175. s.actions.forEach((a) => {
  176. const actionParams = { isNew: false };
  177. const action = Ember.Object.create($.extend(a, actionParams));
  178. actions.pushObject(action);
  179. });
  180. }
  181. steps.pushObject(Ember.Object.create({
  182. id: s.id,
  183. key: s.key,
  184. title: s.title,
  185. raw_description: s.raw_description,
  186. banner: s.banner,
  187. fields,
  188. actions,
  189. isNew: false
  190. }));
  191. });
  192. };
  193. } else {
  194. props['id'] = '';
  195. props['name'] = '';
  196. props['background'] = '';
  197. props['save_submissions'] = true;
  198. props['multiple_submissions'] = false;
  199. props['after_signup'] = false;
  200. props['after_time'] = false;
  201. props['required'] = false;
  202. props['prompt_completion'] = false;
  203. props['min_trust'] = 0;
  204. props['steps'] = Ember.A();
  205. };
  206. wizard.setProperties(props);
  207. return wizard;
  208. }
  209. });
  210. export default CustomWizard;