PageRenderTime 3204ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/bridge/plugins/js_composer/js_composer/assets/js/backend/composer-models.js

https://gitlab.com/juanito.abelo/nlmobile
JavaScript | 197 lines | 145 code | 4 blank | 48 comment | 28 complexity | cc97ef1c535264a63d41ec92509f5313 MD5 | raw file
  1. /* =========================================================
  2. * composer-models.js v0.2
  3. * =========================================================
  4. * Copyright 2013 Wpbakery
  5. *
  6. * Visual composer backbone/underscore models for shortcodes.
  7. * ========================================================= */
  8. (function ($) {
  9. var store = vc.storage;
  10. /**
  11. * Shortcode model.
  12. * Represents shortcode as an object.
  13. * @type {*}
  14. */
  15. vc.shortcode = Backbone.Model.extend({
  16. settings: false,
  17. defaults:function () {
  18. var id = window.vc_guid();
  19. return {
  20. id:id,
  21. shortcode:'vc_text_block',
  22. order:vc.shortcodes.getNextOrder(),
  23. params:{
  24. },
  25. parent_id:false,
  26. root_id:id,
  27. cloned:false,
  28. html:false,
  29. view: false
  30. };
  31. },
  32. initialize:function () {
  33. this.bind('remove', this.removeChildren, this);
  34. },
  35. /**
  36. * Synchronize data with our storage.
  37. * @param method
  38. * @param model
  39. * @param options
  40. */
  41. sync:function (method, model, options) {
  42. var resp;
  43. // Select action to do with data in you storage
  44. switch (method) {
  45. case "read":
  46. resp = model.id ? store.find(model) : store.findAll();
  47. break;
  48. case "create":
  49. resp = store.create(model);
  50. break;
  51. case "update":
  52. resp = store.update(model);
  53. break;
  54. case "delete":
  55. resp = store.destroy(model);
  56. break;
  57. }
  58. // Response
  59. if (resp) {
  60. options.success(resp);
  61. } else {
  62. options.error("Record not found");
  63. }
  64. },
  65. getParam: function(key) {
  66. return _.isObject(this.get('params')) && !_.isUndefined(this.get('params')[key]) ? this.get('params')[key] : '';
  67. },
  68. /**
  69. * Remove all children of model from storage.
  70. * Will remove children of children models too.
  71. * @param parent - model which is parent
  72. */
  73. removeChildren:function (parent) {
  74. var models = vc.shortcodes.where({parent_id:parent.id});
  75. _.each(models, function (model) {
  76. vc.storage.lock();
  77. model.destroy();
  78. this.removeChildren(model);
  79. }, this);
  80. if (models.length) vc.storage.save();
  81. },
  82. setting: function(name) {
  83. if(this.settings === false) this.settings = vc.getMapped(this.get('shortcode')) || {};
  84. return this.settings[name];
  85. }
  86. });
  87. /**
  88. * Collection of shortcodes.
  89. * Extended Backbone.Collection object.
  90. * This collection can be used for root(raw) shortcodes list and inside another shortcodes list as inner shortcodes.
  91. * @type {*}
  92. */
  93. var Shortcodes = vc.shortcodes_collection = Backbone.Collection.extend({
  94. model:vc.shortcode,
  95. last_index: 0,
  96. getNextOrder:function () {
  97. return this.last_index++;
  98. },
  99. comparator:function (model) {
  100. return model.get('order');
  101. },
  102. initialize:function () {
  103. // this.on('add', this.checkUpdateOrder, this);
  104. },
  105. /**
  106. * Updates order of other models if new one has not last order value.
  107. */
  108. checkUpdateOrder:function (model) {
  109. var model_order = model.get('order');
  110. if (model_order < this.length) {
  111. _.each(this.filter(function (shortcode) {
  112. return model.id != shortcode.id && model.get('parent_id') === shortcode.get('parent_id') && shortcode.get('order') >= model_order;
  113. }), function (shortcode) {
  114. shortcode.save({order:shortcode.get('order') + 1});
  115. });
  116. }
  117. },
  118. /**
  119. * Create new models from shortcode string.
  120. * @param shortcodes_string - string of shortcodes.
  121. * @param parent_model - parent shortcode model for parsed objects from string.
  122. */
  123. createFromString:function (shortcodes_string, parent_model) {
  124. var data = vc.storage.parseContent({}, shortcodes_string, _.isObject(parent_model) ? parent_model.toJSON() : false);
  125. _.each(_.values(data), function (model) {
  126. vc.shortcodes.create(model);
  127. }, this);
  128. },
  129. /**
  130. * Synchronize data with our storage.
  131. * @param method
  132. * @param model
  133. * @param options
  134. */
  135. sync:function (method, model, options) {
  136. var resp;
  137. // Select action to do with data in you storage
  138. switch (method) {
  139. case "read":
  140. resp = model.id ? store.find(model) : store.findAll();
  141. break;
  142. case "create":
  143. resp = store.create(model);
  144. break;
  145. case "update":
  146. resp = store.update(model);
  147. break;
  148. case "delete":
  149. resp = store.destroy(model);
  150. break;
  151. }
  152. // Response
  153. if (resp) {
  154. options.success(resp);
  155. } else {
  156. options.error("Record not found");
  157. }
  158. }
  159. });
  160. vc.shortcodes = new vc.shortcodes_collection();
  161. vc.getDefaults = function (tag) {
  162. var defaults = {},
  163. params = _.isObject(vc.map[tag]) && _.isArray(vc.map[tag].params) ? vc.map[tag].params : [];
  164. _.each(params, function (param) {
  165. if(_.isObject(param)) {
  166. if(!_.isUndefined(param.std)) {
  167. defaults[param.param_name] = param.std;
  168. } else if (!_.isUndefined(param.value)) {
  169. if( vc.atts[param.type] && vc.atts[param.type].defaults ) {
  170. defaults[param.param_name] = vc.atts[param.type].defaults(param);
  171. } else if (_.isObject(param.value)) {
  172. defaults[param.param_name] = _.values(param.value)[0];
  173. } else if (_.isArray(param.value)) {
  174. defaults[param.param_name] = param.value[0];
  175. } else {
  176. defaults[param.param_name] = param.value;
  177. }
  178. }
  179. }
  180. });
  181. return defaults;
  182. };
  183. vc.getParamSettings = _.memoize(function(tag, paramName) {
  184. var params = _.isObject(vc.map[tag]) && _.isArray(vc.map[tag].params) ? vc.map[tag].params : [],
  185. paramSettings;
  186. paramSettings = _.find(params, function(settings){
  187. return _.isObject(settings) && settings.param_name === paramName;
  188. }, this);
  189. return paramSettings;
  190. }, function(){
  191. return arguments[0]+','+arguments[1];
  192. });
  193. })(window.jQuery);