PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/furrutia1991/imosa_web
JavaScript | 375 lines | 289 code | 22 blank | 64 comment | 66 complexity | 64afe6af9797d0af376b5dbbd8ee62c3 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. /**
  10. * Collection of shortcodes.
  11. * Extended Backbone.Collection object.
  12. * This collection can be used for root(raw) shortcodes list and inside another shortcodes list as inner shortcodes.
  13. * @type {*}
  14. */
  15. var Shortcodes;
  16. var store = vc.storage;
  17. /**
  18. * Shortcode model.
  19. * Represents shortcode as an object.
  20. * @type {*}
  21. */
  22. vc.shortcode = Backbone.Model.extend( {
  23. settings: false,
  24. defaults: function () {
  25. var id = window.vc_guid();
  26. return {
  27. id: id,
  28. shortcode: 'vc_text_block',
  29. order: vc.shortcodes.getNextOrder(),
  30. params: {},
  31. parent_id: false,
  32. root_id: id,
  33. cloned: false,
  34. html: false,
  35. view: false
  36. };
  37. },
  38. initialize: function () {
  39. this.bind( 'remove', this.removeChildren, this );
  40. this.bind( 'remove', this.removeEvents, this );
  41. },
  42. removeEvents: function ( model ) {
  43. //triggering shortcodes and shortcodes:destroy events
  44. vc.events.triggerShortcodeEvents( 'destroy', model );
  45. },
  46. /**
  47. * Synchronize data with our storage.
  48. * @param method
  49. * @param model
  50. * @param options
  51. */
  52. sync: function ( method, model, options ) {
  53. var response;
  54. // Select action to do with data in you storage
  55. switch ( method ) {
  56. case "read":
  57. response = model.id ? store.find( model ) : store.findAll();
  58. break;
  59. case "create":
  60. response = store.create( model );
  61. break;
  62. case "update":
  63. response = store.update( model );
  64. break;
  65. case "delete":
  66. response = store.destroy( model );
  67. break;
  68. }
  69. // Response
  70. if ( response ) {
  71. options.success( response );
  72. } else {
  73. options.error( "Record not found" );
  74. }
  75. },
  76. getParam: function ( key ) {
  77. return _.isObject( this.get( 'params' ) ) && ! _.isUndefined( this.get( 'params' )[ key ] ) ? this.get( 'params' )[ key ] : '';
  78. },
  79. /**
  80. * Remove all children of model from storage.
  81. * Will remove children of children models too.
  82. * @param parent - model which is parent
  83. */
  84. removeChildren: function ( parent ) {
  85. var models = vc.shortcodes.where( { parent_id: parent.id } );
  86. _.each( models, function ( model ) {
  87. vc.storage.lock();
  88. model.destroy();
  89. this.removeChildren( model );
  90. }, this );
  91. if ( models.length ) {
  92. vc.storage.save();
  93. }
  94. },
  95. setting: function ( name ) {
  96. if ( false === this.settings ) {
  97. this.settings = vc.getMapped( this.get( 'shortcode' ) ) || {};
  98. }
  99. return this.settings[ name ];
  100. }
  101. } );
  102. Shortcodes = vc.shortcodes_collection = Backbone.Collection.extend( {
  103. model: vc.shortcode,
  104. last_index: 0,
  105. getNextOrder: function () {
  106. return this.last_index ++;
  107. },
  108. comparator: function ( model ) {
  109. return model.get( 'order' );
  110. },
  111. initialize: function () {
  112. },
  113. /**
  114. * Create new models from shortcode string.
  115. * @param shortcodes_string - string of shortcodes.
  116. * @param parent_model - parent shortcode model for parsed objects from string.
  117. */
  118. createFromString: function ( shortcodes_string, parent_model ) {
  119. var data;
  120. data = vc.storage.parseContent(
  121. {},
  122. shortcodes_string,
  123. _.isObject( parent_model ) ? parent_model.toJSON() : false
  124. );
  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 response;
  137. // Select action to do with data in you storage
  138. switch ( method ) {
  139. case "read":
  140. response = model.id ? store.find( model ) : store.findAll();
  141. break;
  142. case "create":
  143. response = store.create( model );
  144. break;
  145. case "update":
  146. response = store.update( model );
  147. break;
  148. case "delete":
  149. response = store.destroy( model );
  150. break;
  151. }
  152. // Response
  153. if ( response ) {
  154. options.success( response );
  155. } else {
  156. options.error( "Record not found" );
  157. }
  158. },
  159. stringify: function ( state ) {
  160. return this.modelsToString( _.sortBy( vc.shortcodes.where( { parent_id: false } ), function ( model ) {
  161. return model.get( 'order' );
  162. } ), state );
  163. },
  164. modelsToString: function ( models, state ) {
  165. var string = '';
  166. _.each( models, function ( model ) {
  167. var data, tag, params, content, mergedParams, paramsForString;
  168. tag = model.get( 'shortcode' );
  169. params = _.extend( {}, model.get( 'params' ) );
  170. paramsForString = {};
  171. mergedParams = vc.getMergedParams( tag, params );
  172. _.each( mergedParams, function ( value, key ) {
  173. if ( 'content' !== key ) {
  174. paramsForString[ key ] = vc.storage.escapeParam( value );
  175. }
  176. }, this );
  177. content = _.isString( params.content ) ? params.content : '';
  178. content += this.modelsToString( _.sortBy( vc.shortcodes.where( { parent_id: model.get( 'id' ) } ),
  179. function ( model ) {
  180. return model.get( 'order' );
  181. } ), state );
  182. var mapped = vc.getMapped( tag );
  183. data = {
  184. tag: tag,
  185. attrs: paramsForString,
  186. content: content,
  187. type: _.isUndefined( vc.getParamSettings( tag,
  188. 'content' ) ) && ! mapped.is_container && _.isEmpty( mapped.as_parent ) ? 'single' : ''
  189. };
  190. if ( _.isUndefined( state ) ) {
  191. model.trigger( 'stringify', model, data );
  192. } else {
  193. model.trigger( 'stringify:' + state, model, data );
  194. }
  195. string += wp.shortcode.string( data );
  196. }, this );
  197. return string;
  198. }
  199. } );
  200. vc.shortcodes = new vc.shortcodes_collection();
  201. vc.getDefaults = vc.memoizeWrapper( function ( tag ) {
  202. var defaults, params;
  203. defaults = {};
  204. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  205. _.each( params, function ( param ) {
  206. if ( _.isObject( param ) ) {
  207. if ( ! _.isUndefined( param.std ) ) {
  208. defaults[ param.param_name ] = param.std;
  209. } else if ( ! _.isUndefined( param.value ) ) {
  210. if ( vc.atts[ param.type ] && vc.atts[ param.type ].defaults ) {
  211. defaults[ param.param_name ] = vc.atts[ param.type ].defaults( param );
  212. } else if ( _.isObject( param.value ) ) {
  213. defaults[ param.param_name ] = _.values( param.value )[ 0 ];
  214. } else if ( _.isArray( param.value ) ) {
  215. defaults[ param.param_name ] = param.value[ 0 ];
  216. } else {
  217. defaults[ param.param_name ] = param.value;
  218. }
  219. }
  220. }
  221. } );
  222. return defaults;
  223. } );
  224. vc.getDefaultsAndDependencyMap = vc.memoizeWrapper( function ( tag ) {
  225. var defaults, dependencyMap, params;
  226. dependencyMap = {};
  227. defaults = {};
  228. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  229. _.each( params, function ( param ) {
  230. if ( _.isObject( param ) && 'content' !== param.param_name ) {
  231. // Building defaults
  232. if ( ! _.isUndefined( param.std ) ) {
  233. defaults[ param.param_name ] = param.std;
  234. } else if ( ! _.isUndefined( param.value ) ) {
  235. if ( vc.atts[ param.type ] && vc.atts[ param.type ].defaults ) {
  236. defaults[ param.param_name ] = vc.atts[ param.type ].defaults( param );
  237. } else if ( _.isObject( param.value ) ) {
  238. defaults[ param.param_name ] = _.values( param.value )[ 0 ];
  239. } else if ( _.isArray( param.value ) ) {
  240. defaults[ param.param_name ] = param.value[ 0 ];
  241. } else {
  242. defaults[ param.param_name ] = param.value;
  243. }
  244. }
  245. // Building dependency map
  246. if ( ! _.isUndefined( param.dependency ) && ! _.isUndefined( param.dependency.element ) ) {
  247. // We can only hook dependency to exact element value
  248. dependencyMap[ param.param_name ] = param.dependency;
  249. }
  250. }
  251. } );
  252. return { defaults: defaults, dependencyMap: dependencyMap };
  253. } );
  254. vc.getMergedParams = function ( tag, values ) {
  255. var paramsMap, outputParams, paramsDependencies;
  256. paramsMap = vc.getDefaultsAndDependencyMap( tag );
  257. outputParams = {};
  258. // Make all values extended from default
  259. values = _.extend( {}, paramsMap.defaults, values );
  260. paramsDependencies = _.extend( {}, paramsMap.dependencyMap );
  261. _.each( values, function ( value, key ) {
  262. if ( 'content' !== key ) {
  263. var paramSettings;
  264. // checking dependency
  265. if ( ! _.isUndefined( paramsDependencies[ key ] ) ) {
  266. // now we know that param has dependency, so we must check is it satisfy a statement
  267. if ( ! _.isUndefined( paramsDependencies[ paramsDependencies[ key ].element ] ) && _.isBoolean( paramsDependencies[ paramsDependencies[ key ].element ].failed ) && true === paramsDependencies[ paramsDependencies[ key ].element ].failed ) {
  268. paramsDependencies[ key ].failed = true;
  269. return; // in case if we already failed a dependency (a-b-c)
  270. }
  271. var rules, isDependedEmpty, dependedElement, dependedValue;
  272. dependedElement = paramsDependencies[ key ].element;
  273. dependedValue = values[ dependedElement ];
  274. isDependedEmpty = _.isEmpty( dependedValue );
  275. rules = _.omit( paramsDependencies[ key ], 'element' );
  276. if (
  277. (
  278. // check rule 'not_empty'
  279. _.isBoolean( rules.not_empty ) && true === rules.not_empty && isDependedEmpty
  280. ) ||
  281. (
  282. // check rule 'is_empty'
  283. _.isBoolean( rules.is_empty ) && true === rules.is_empty && ! isDependedEmpty
  284. ) ||
  285. (
  286. // check rule 'value'
  287. rules.value && ! _.intersection( (
  288. _.isArray( rules.value ) ? rules.value : [ rules.value ]),
  289. (_.isArray( dependedValue ) ? dependedValue : [ dependedValue ] )
  290. ).length
  291. ) ||
  292. (
  293. // check rule 'value_not_equal_to'
  294. rules.value_not_equal_to && _.intersection( (
  295. _.isArray( rules.value_not_equal_to ) ? rules.value_not_equal_to : [ rules.value_not_equal_to ] ),
  296. (_.isArray( dependedValue ) ? dependedValue : [ dependedValue ])
  297. ).length
  298. )
  299. ) {
  300. paramsDependencies[ key ].failed = true;
  301. return; // some of these rules doesn't satisfy so just exit
  302. }
  303. }
  304. // now check for defaults if not deleted already
  305. paramSettings = vc.getParamSettings( tag, key );
  306. if ( _.isUndefined( paramSettings ) ) {
  307. outputParams[ key ] = value;
  308. // this means that param is not mapped
  309. // so maybe it is can be used somewhere in other place.
  310. // We need to save it anyway. #93627986
  311. } else if (
  312. ( // add value if it is not same as default
  313. ! _.isUndefined( paramsMap.defaults[ key ] ) && paramsMap.defaults[ key ] !== value
  314. ) || (
  315. // or if no defaults exists -> add value if it is not empty
  316. _.isUndefined( paramsMap.defaults[ key ] ) && '' !== value
  317. ) || (
  318. // Or it is required to save always
  319. ! _.isUndefined( paramSettings.save_always ) && true === paramSettings.save_always )
  320. ) {
  321. outputParams[ key ] = value;
  322. }
  323. }
  324. } );
  325. return outputParams;
  326. };
  327. vc.getParamSettings = vc.memoizeWrapper( function ( tag, paramName ) {
  328. var params, paramSettings;
  329. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  330. paramSettings = _.find( params, function ( settings ) {
  331. return _.isObject( settings ) && settings.param_name === paramName;
  332. }, this );
  333. return paramSettings;
  334. }, function () {
  335. return arguments[ 0 ] + ',' + arguments[ 1 ];
  336. } );
  337. vc.getParamSettingsByType = vc.memoizeWrapper( function ( tag, paramType ) {
  338. var params, paramSettings;
  339. params = _.isObject( vc.map[ tag ] ) && _.isArray( vc.map[ tag ].params ) ? vc.map[ tag ].params : [];
  340. paramSettings = _.find( params, function ( settings ) {
  341. return _.isObject( settings ) && settings.type === paramType;
  342. }, this );
  343. return paramSettings;
  344. }, function () {
  345. return arguments[ 0 ] + ',' + arguments[ 1 ];
  346. } );
  347. /**
  348. * Checks if given shortcode has el_id param
  349. */
  350. vc.shortcodeHasIdParam = vc.memoizeWrapper( function ( tag ) {
  351. return vc.getParamSettingsByType( tag, 'el_id' );
  352. } );
  353. })( window.jQuery );