custom-wizard.js.es6 2.7 KB

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