PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/js_composer/assets/js/backend/composer-models.js

https://gitlab.com/mattswann/launch-housing
JavaScript | 252 lines | 193 code | 7 blank | 52 comment | 33 complexity | d3c97ecc11a7291f23292cdb9f832924 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. parent_id: false,
  25. root_id: id,
  26. cloned: false,
  27. html: false,
  28. view: false
  29. };
  30. },
  31. initialize: function () {
  32. this.bind( 'remove', this.removeChildren, this );
  33. this.bind( 'remove', this.removeEvents, this );
  34. },
  35. removeEvents: function ( model ) {
  36. //triggering shortcodes and shortcodes:destroy events
  37. vc.events.triggerShortcodeEvents( 'destroy', model );
  38. },
  39. /**
  40. * Synchronize data with our storage.
  41. * @param method
  42. * @param model
  43. * @param options
  44. */
  45. sync: function ( method, model, options ) {
  46. var resp;
  47. // Select action to do with data in you storage
  48. switch ( method ) {
  49. case "read":
  50. resp = model.id ? store.find( model ) : store.findAll();
  51. break;
  52. case "create":
  53. resp = store.create( model );
  54. break;
  55. case "update":
  56. resp = store.update( model );
  57. break;
  58. case "delete":
  59. resp = store.destroy( model );
  60. break;
  61. }
  62. // Response
  63. if ( resp ) {
  64. options.success( resp );
  65. } else {
  66. options.error( "Record not found" );
  67. }
  68. },
  69. getParam: function ( key ) {
  70. return _.isObject( this.get( 'params' ) ) && ! _.isUndefined( this.get( 'params' )[ key ] ) ? this.get( 'params' )[ key ] : '';
  71. },
  72. /**
  73. * Remove all children of model from storage.
  74. * Will remove children of children models too.
  75. * @param parent - model which is parent
  76. */
  77. removeChildren: function ( parent ) {
  78. var models = vc.shortcodes.where( { parent_id: parent.id } );
  79. _.each( models, function ( model ) {
  80. vc.storage.lock();
  81. model.destroy();
  82. this.removeChildren( model );
  83. }, this );
  84. if ( models.length ) {
  85. vc.storage.save();
  86. }
  87. },
  88. setting: function ( name ) {
  89. if ( this.settings === false ) {
  90. this.settings = vc.getMapped( this.get( 'shortcode' ) ) || {};
  91. }
  92. return this.settings[ name ];
  93. }
  94. } );
  95. /**
  96. * Collection of shortcodes.
  97. * Extended Backbone.Collection object.
  98. * This collection can be used for root(raw) shortcodes list and inside another shortcodes list as inner shortcodes.
  99. * @type {*}
  100. */
  101. var Shortcodes = vc.shortcodes_collection = Backbone.Collection.extend( {
  102. model: vc.shortcode,
  103. last_index: 0,
  104. getNextOrder: function () {
  105. return this.last_index ++;
  106. },
  107. comparator: function ( model ) {
  108. return model.get( 'order' );
  109. },
  110. initialize: function () {
  111. // this.on('add', this.checkUpdateOrder, this);
  112. },
  113. /**
  114. * Updates order of other models if new one has not last order value.
  115. */
  116. checkUpdateOrder: function ( model ) {
  117. var model_order = model.get( 'order' );
  118. if ( model_order < this.length ) {
  119. _.each( this.filter( function ( shortcode ) {
  120. return model.id != shortcode.id && model.get( 'parent_id' ) === shortcode.get( 'parent_id' ) && shortcode.get( 'order' ) >= model_order;
  121. } ), function ( shortcode ) {
  122. shortcode.save( { order: shortcode.get( 'order' ) + 1 } );
  123. } );
  124. }
  125. },
  126. /**
  127. * Create new models from shortcode string.
  128. * @param shortcodes_string - string of shortcodes.
  129. * @param parent_model - parent shortcode model for parsed objects from string.
  130. */
  131. createFromString: function ( shortcodes_string, parent_model ) {
  132. var data = vc.storage.parseContent( {},
  133. shortcodes_string,
  134. _.isObject( parent_model ) ? parent_model.toJSON() : false );
  135. _.each( _.values( data ), function ( model ) {
  136. vc.shortcodes.create( model );
  137. }, this );
  138. },
  139. /**
  140. * Synchronize data with our storage.
  141. * @param method
  142. * @param model
  143. * @param options
  144. */
  145. sync: function ( method, model, options ) {
  146. var resp;
  147. // Select action to do with data in you storage
  148. switch ( method ) {
  149. case "read":
  150. resp = model.id ? store.find( model ) : store.findAll();
  151. break;
  152. case "create":
  153. resp = store.create( model );
  154. break;
  155. case "update":
  156. resp = store.update( model );
  157. break;
  158. case "delete":
  159. resp = store.destroy( model );
  160. break;
  161. }
  162. // Response
  163. if ( resp ) {
  164. options.success( resp );
  165. } else {
  166. options.error( "Record not found" );
  167. }
  168. },
  169. stringify: function ( state ) {
  170. return this.modelsToString( vc.shortcodes.where( { parent_id: false } ), state );
  171. },
  172. modelsToString: function ( models, state ) {
  173. var string = '';
  174. _.each( models, function ( model ) {
  175. var tag = model.get( 'shortcode' ),
  176. params = model.get( 'params' ),
  177. content = _.isString( params.content ) ? params.content : '';
  178. content += this.modelsToString( vc.shortcodes.where( { parent_id: model.get( 'id' ) } ), state );
  179. var data = {
  180. tag: tag,
  181. attrs: _.omit( params, 'content' ),
  182. content: content,
  183. type: _.isUndefined( vc.getParamSettings( tag,
  184. 'content' ) ) && ! vc.getMapped( tag ).is_container ? 'single' : ''
  185. };
  186. if ( _.isUndefined( state ) ) {
  187. model.trigger( 'stringify', model, data );
  188. } else {
  189. model.trigger( 'stringify:' + state, model, data );
  190. }
  191. string += wp.shortcode.string( data );
  192. }, this );
  193. return string;
  194. }
  195. } );
  196. vc.shortcodes = new vc.shortcodes_collection();
  197. vc.getDefaults = function ( tag ) {
  198. var defaults = {},
  199. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  200. _.each( params, function ( param ) {
  201. if ( _.isObject( param ) ) {
  202. if ( ! _.isUndefined( param.std ) ) {
  203. defaults[ param.param_name ] = param.std;
  204. } else if ( ! _.isUndefined( param.value ) ) {
  205. if ( vc.atts[ param.type ] && vc.atts[ param.type ].defaults ) {
  206. defaults[ param.param_name ] = vc.atts[ param.type ].defaults( param );
  207. } else if ( _.isObject( param.value ) ) {
  208. defaults[ param.param_name ] = _.values( param.value )[ 0 ];
  209. } else if ( _.isArray( param.value ) ) {
  210. defaults[ param.param_name ] = param.value[ 0 ];
  211. } else {
  212. defaults[ param.param_name ] = param.value;
  213. }
  214. }
  215. }
  216. } );
  217. return defaults;
  218. };
  219. vc.getParamSettings = _.memoize( function ( tag, paramName ) {
  220. var params, paramSettings;
  221. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  222. paramSettings = _.find( params, function ( settings ) {
  223. return _.isObject( settings ) && settings.param_name === paramName;
  224. }, this );
  225. return paramSettings;
  226. }, function () {
  227. return arguments[ 0 ] + ',' + arguments[ 1 ];
  228. } );
  229. vc.getParamSettingsByType = _.memoize( function ( tag, paramType ) {
  230. var params, paramSettings;
  231. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  232. paramSettings = _.find( params, function ( settings ) {
  233. return _.isObject( settings ) && settings.type === paramType;
  234. }, this );
  235. return paramSettings;
  236. }, function () {
  237. return arguments[ 0 ] + ',' + arguments[ 1 ];
  238. } );
  239. /**
  240. * Checks if given shortcode has el_id param
  241. */
  242. vc.shortcodeHasIdParam = _.memoize( function ( tag ) {
  243. return vc.getParamSettingsByType( tag, 'el_id' );
  244. } );
  245. })( window.jQuery );