custom-wizard.js.es6 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. step['fields'].push(f);
  49. });
  50. }
  51. const actions = s.actions;
  52. if (actions.length) {
  53. step['actions'] = [];
  54. actions.some((a) => {
  55. let id = a.get('id');
  56. if (!id || !id.underscore()) reject('id_required');
  57. a.set('id', id.underscore());
  58. step['actions'].push(a);
  59. });
  60. }
  61. steps.push(step);
  62. });
  63. return steps;
  64. },
  65. remove() {
  66. return ajax("/admin/wizards/custom/remove", {
  67. type: 'DELETE',
  68. data: {
  69. id: this.get('id')
  70. }
  71. }).then(() => this.destroy());
  72. }
  73. });
  74. CustomWizard.reopenClass({
  75. findAll() {
  76. return ajax("/admin/wizards/custom/all", {
  77. type: 'GET'
  78. }).then(result => {
  79. return result.wizards.map(w => CustomWizard.create(w));
  80. });
  81. },
  82. findAllSubmissions() {
  83. return ajax("/admin/wizards/submissions/all", {
  84. type: "GET"
  85. }).then(result => {
  86. return result.submissions;
  87. });
  88. },
  89. create(w) {
  90. const wizard = this._super.apply(this);
  91. let steps = Ember.A();
  92. let props = { steps };
  93. if (w) {
  94. props['id'] = w.id;
  95. props['existingId'] = true;
  96. props['name'] = w.name;
  97. props['background'] = w.background;
  98. props['save_submissions'] = w.save_submissions;
  99. props['multiple_submissions'] = w.multiple_submissions;
  100. if (w.steps && w.steps.length) {
  101. w.steps.forEach((s) => {
  102. // clean empty strings
  103. Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);
  104. let fields = Ember.A();
  105. if (s.fields && s.fields.length) {
  106. s.fields.forEach((f) => {
  107. Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);
  108. let field = Ember.Object.create(f);
  109. if (f.choices) {
  110. let choices = Ember.A();
  111. f.choices.forEach((c) => {
  112. choices.pushObject(Ember.Object.create(c));
  113. });
  114. field.set('choices', choices);
  115. }
  116. fields.pushObject(field);
  117. });
  118. }
  119. let actions = Ember.A();
  120. if (s.actions && s.actions.length) {
  121. s.actions.forEach((a) => {
  122. actions.pushObject(Ember.Object.create(a));
  123. });
  124. }
  125. steps.pushObject(Ember.Object.create({
  126. id: s.id,
  127. key: s.key,
  128. title: s.title,
  129. description: s.description,
  130. banner: s.banner,
  131. fields,
  132. actions
  133. }));
  134. });
  135. };
  136. } else {
  137. props['id'] = '';
  138. props['name'] = '';
  139. props['background'] = '';
  140. props['save_submissions'] = true;
  141. props['multiple_submissions'] = false;
  142. props['steps'] = Ember.A();
  143. };
  144. wizard.setProperties(props);
  145. return wizard;
  146. }
  147. });
  148. export default CustomWizard;