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