custom-wizard.js.es6 4.1 KB

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