/src/jquery.Midgard.midgardWorkflows.js

https://github.com/szabyg/create · JavaScript · 242 lines · 209 code · 27 blank · 6 comment · 15 complexity · 72f57d82cb4238560c75ca335300b5b4 MD5 · raw file

  1. // Create - On-site web editing interface
  2. // (c) 2012 Jerry Jalava, IKS Consortium
  3. // Create may be freely distributed under the MIT license.
  4. // For all details and documentation:
  5. // http://createjs.org/
  6. (function (jQuery, undefined) {
  7. jQuery.widget('Midgard.midgardWorkflows', {
  8. options: {
  9. url: function (model) {},
  10. renderers: {
  11. button: function (model, workflow, action_cb, final_cb) {
  12. button_id = 'midgardcreate-workflow_' + workflow.get('name');
  13. html = jQuery('<button class="create-ui-btn" id="' + button_id + '">' + workflow.get('label') + '</button>').button();
  14. html.bind('click', function (evt) {
  15. action_cb(model, workflow, final_cb);
  16. });
  17. return html;
  18. }
  19. },
  20. action_types: {
  21. backbone_save: function (model, workflow, callback) {
  22. copy_of_url = model.url;
  23. original_model = model.clone();
  24. original_model.url = copy_of_url;
  25. action = workflow.get('action');
  26. if (action.url) {
  27. model.url = action.url;
  28. }
  29. original_model.save(null, {
  30. success: function (m) {
  31. model.url = copy_of_url;
  32. model.change();
  33. callback(null, model);
  34. },
  35. error: function (m, err) {
  36. model.url = copy_of_url;
  37. model.change();
  38. callback(err, model);
  39. }
  40. });
  41. },
  42. backbone_destroy: function (model, workflow, callback) {
  43. copy_of_url = model.url;
  44. original_model = model.clone();
  45. original_model.url = copy_of_url;
  46. action = workflow.get('action');
  47. if (action.url) {
  48. model.url = action.url;
  49. }
  50. model.destroy({
  51. success: function (m) {
  52. model.url = copy_of_url;
  53. model.change();
  54. callback(null, m);
  55. },
  56. error: function (m, err) {
  57. model.url = copy_of_url;
  58. model.change();
  59. callback(err, model);
  60. }
  61. });
  62. },
  63. http: function (model, workflow, callback) {
  64. action = workflow.get('action');
  65. if (!action.url) {
  66. return callback('No action url defined!');
  67. }
  68. wf_opts = {};
  69. if (action.http) {
  70. wf_opts = action.http;
  71. }
  72. ajax_options = jQuery.extend({
  73. url: action.url,
  74. type: 'POST',
  75. data: model.toJSON(),
  76. success: function () {
  77. model.fetch({
  78. success: function (model) {
  79. callback(null, model);
  80. },
  81. error: function (model, err) {
  82. callback(err, model);
  83. }
  84. });
  85. }
  86. }, wf_opts);
  87. jQuery.ajax(ajax_options);
  88. }
  89. }
  90. },
  91. _init: function () {
  92. this._renderers = {};
  93. this._action_types = {};
  94. this._parseRenderersAndTypes();
  95. this._last_instance = null;
  96. this.ModelWorkflowModel = Backbone.Model.extend({
  97. defaults: {
  98. name: '',
  99. label: '',
  100. type: 'button',
  101. action: {
  102. type: 'backbone_save'
  103. }
  104. }
  105. });
  106. this.workflows = {};
  107. var widget = this;
  108. jQuery(this.element).bind('midgardeditableactivated', function (event, options) {
  109. widget._fetchWorkflows(options.instance);
  110. });
  111. },
  112. _fetchWorkflows: function (model) {
  113. var widget = this;
  114. if (model.isNew()) {
  115. widget._trigger('changed', null, {
  116. instance: model,
  117. workflows: []
  118. });
  119. return;
  120. }
  121. if (widget._last_instance == model) {
  122. if (widget.workflows[model.cid]) {
  123. widget._trigger('changed', null, {
  124. instance: model,
  125. workflows: widget.workflows[model.cid]
  126. });
  127. }
  128. return;
  129. }
  130. widget._last_instance = model;
  131. if (widget.workflows[model.cid]) {
  132. widget._trigger('changed', null, {
  133. instance: model,
  134. workflows: widget.workflows[model.cid]
  135. });
  136. return;
  137. }
  138. if (widget.options.url) {
  139. widget._fetchModelWorkflows(model);
  140. } else {
  141. flows = new(widget._generateCollectionFor(model))([], {});
  142. widget._trigger('changed', null, {
  143. instance: model,
  144. workflows: flows
  145. });
  146. }
  147. },
  148. _parseRenderersAndTypes: function () {
  149. var widget = this;
  150. jQuery.each(this.options.renderers, function (k, v) {
  151. widget.setRenderer(k, v);
  152. });
  153. jQuery.each(this.options.action_types, function (k, v) {
  154. widget.setActionType(k, v);
  155. });
  156. },
  157. setRenderer: function (name, callbacks) {
  158. this._renderers[name] = callbacks;
  159. },
  160. getRenderer: function (name) {
  161. if (!this._renderers[name]) {
  162. return false;
  163. }
  164. return this._renderers[name];
  165. },
  166. setActionType: function (name, callback) {
  167. this._action_types[name] = callback;
  168. },
  169. getActionType: function (name) {
  170. return this._action_types[name];
  171. },
  172. prepareItem: function (model, workflow, final_cb) {
  173. var widget = this;
  174. renderer = this.getRenderer(workflow.get("type"));
  175. action_type_cb = this.getActionType(workflow.get("action").type);
  176. return renderer(model, workflow, action_type_cb, function (err, m) {
  177. delete widget.workflows[model.cid];
  178. widget._last_instance = null;
  179. if (workflow.get('action').type !== 'backbone_destroy') {
  180. // Get an updated list of workflows
  181. widget._fetchModelWorkflows(model);
  182. }
  183. final_cb(err, m);
  184. });
  185. },
  186. _generateCollectionFor: function (model) {
  187. var collectionSettings = {
  188. model: this.ModelWorkflowModel
  189. };
  190. if (this.options.url) {
  191. collectionSettings.url = this.options.url(model);
  192. }
  193. return Backbone.Collection.extend(collectionSettings);
  194. },
  195. _fetchModelWorkflows: function (model) {
  196. if (model.isNew()) {
  197. return;
  198. }
  199. var widget = this;
  200. widget.workflows[model.cid] = new(this._generateCollectionFor(model))([], {});
  201. widget.workflows[model.cid].fetch({
  202. success: function (collection) {
  203. widget.workflows[model.cid].reset(collection.models);
  204. widget._trigger('changed', null, {
  205. instance: model,
  206. workflows: widget.workflows[model.cid]
  207. });
  208. },
  209. error: function (model, err) {
  210. console.log('error fetching flows', err);
  211. }
  212. });
  213. }
  214. });
  215. })(jQuery);