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

/wp-content/plugins/divi-builder/framework/scripts/builder.js

https://gitlab.com/gregtyka/lfmawordpress
JavaScript | 1499 lines | 1465 code | 23 blank | 11 comment | 5 complexity | 5392186c0ecde27c83b306d76758c103 MD5 | raw file
  1. var ET_PageBuilder = ET_PageBuilder || {};
  2. window.wp = window.wp || {};
  3. ( function($) {
  4. $( document ).ready( function() {
  5. // Explicitly define ERB-style template delimiters to prevent
  6. // template delimiters being overwritten by 3rd party plugin
  7. _.templateSettings = {
  8. evaluate : /<%([\s\S]+?)%>/g,
  9. interpolate: /<%=([\s\S]+?)%>/g,
  10. escape : /<%-([\s\S]+?)%>/g
  11. };
  12. // Models
  13. ET_PageBuilder.Module = Backbone.Model.extend( {
  14. defaults: {
  15. type : 'element'
  16. }
  17. } );
  18. ET_PageBuilder.SavedTemplate = Backbone.Model.extend( {
  19. defaults: {
  20. title : 'template',
  21. ID : 0,
  22. shortcode : '',
  23. is_global : 'false',
  24. layout_type : '',
  25. module_type : '',
  26. categories : []
  27. }
  28. } );
  29. ET_PageBuilder.History = Backbone.Model.extend( {
  30. defaults : {
  31. timestamp : _.now(),
  32. shortcode : '',
  33. current_active_history : false,
  34. verb : 'did',
  35. noun : 'something'
  36. },
  37. max_history_limit : 100,
  38. validate : function( attributes, options ) {
  39. var histories_count = options.collection.length,
  40. active_history_model = options.collection.findWhere({ current_active_history : true }),
  41. shortcode = attributes.shortcode,
  42. last_model = _.isUndefined( active_history_model ) ? options.collection.at( ( options.collection.length - 1 ) ) : active_history_model,
  43. last_shortcode = _.isUndefined( last_model ) ? false : last_model.get( 'shortcode' ),
  44. previous_active_histories;
  45. if ( shortcode === last_shortcode ) {
  46. return 'duplicate';
  47. }
  48. // Turn history tracking off
  49. ET_PageBuilder_App.enable_history = false;
  50. // Limit number of history limit
  51. var histories_count = options.collection.models.length,
  52. remove_limit = histories_count - ( this.max_history_limit - 1 ),
  53. ranges,
  54. deleted_model;
  55. // Some models are need to be removed
  56. if ( remove_limit > 0 ) {
  57. // Loop and shift (remove first model in collection) n-times
  58. for (var i = 1; i <= remove_limit; i++) {
  59. options.collection.shift();
  60. };
  61. }
  62. }
  63. } );
  64. // helper module
  65. ET_PageBuilder.Layout = Backbone.Model.extend( {
  66. defaults: {
  67. moduleNumber : 0,
  68. forceRemove : false,
  69. modules : $.parseJSON( et_pb_options.et_builder_modules ),
  70. views : [
  71. ]
  72. },
  73. initialize : function() {
  74. // Single and double quotes are replaced with %% in et_builder_modules
  75. // to avoid js conflicts.
  76. // Replace them with appropriate signs.
  77. _.each( this.get( 'modules' ), function( module ) {
  78. module['title'] = module['title'].replace( /%%/g, '"' );
  79. module['title'] = module['title'].replace( /\|\|/g, "'" );
  80. } );
  81. },
  82. addView : function( module_cid, view ) {
  83. var views = this.get( 'views' );
  84. views[module_cid] = view;
  85. this.set( { 'views' : views } );
  86. },
  87. getView : function( cid ) {
  88. return this.get( 'views' )[cid];
  89. },
  90. getChildViews : function( parent_id ) {
  91. var views = this.get( 'views' ),
  92. child_views = {};
  93. _.each( views, function( view, key ) {
  94. if ( view['model']['attributes']['parent'] === parent_id )
  95. child_views[key] = view;
  96. } );
  97. return child_views;
  98. },
  99. getChildrenViews : function( parent_id ) {
  100. var this_el = this,
  101. views = this_el.get( 'views' ),
  102. child_views = {},
  103. grand_children;
  104. _.each( views, function( view, key ) {
  105. if ( view['model']['attributes']['parent'] === parent_id ) {
  106. grand_children = this_el.getChildrenViews( view['model']['attributes']['cid'] );
  107. if ( ! _.isEmpty( grand_children ) ) {
  108. _.extend( child_views, grand_children );
  109. }
  110. child_views[key] = view;
  111. }
  112. } );
  113. return child_views;
  114. },
  115. getParentViews : function( parent_cid ) {
  116. var parent_view = this.getView( parent_cid ),
  117. parent_views = {};
  118. while( ! _.isUndefined( parent_view ) ) {
  119. parent_views[parent_view['model']['attributes']['cid']] = parent_view;
  120. parent_view = this.getView( parent_view['model']['attributes']['parent'] );
  121. }
  122. return parent_views;
  123. },
  124. getSectionView : function( parent_cid ) {
  125. var views = this.getParentViews( parent_cid ),
  126. section_view;
  127. section_view = _.filter( views, function( item ) {
  128. if ( item.model.attributes.type === "section" ) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. } );
  134. if ( _.isUndefined( section_view[0] ) ) {
  135. return false;
  136. } else {
  137. return section_view[0];
  138. }
  139. },
  140. setNewParentID : function( cid, new_parent_id ) {
  141. var views = this.get( 'views' );
  142. views[cid]['model']['attributes']['parent'] = new_parent_id;
  143. this.set( { 'views' : views } );
  144. },
  145. removeView : function( cid ) {
  146. var views = this.get( 'views' ),
  147. new_views = {};
  148. _.each( views, function( value, key ) {
  149. if ( key != cid )
  150. new_views[key] = value;
  151. } );
  152. this.set( { 'views' : new_views } );
  153. },
  154. generateNewId : function() {
  155. var moduleNumber = this.get( 'moduleNumber' ) + 1;
  156. this.set( { 'moduleNumber' : moduleNumber } );
  157. return moduleNumber;
  158. },
  159. generateTemplateName : function( name ) {
  160. var default_elements = [ 'row', 'row_inner', 'section', 'column', 'column_inner'];
  161. if ( -1 !== $.inArray( name, default_elements ) ) {
  162. name = 'et_pb_' + name;
  163. }
  164. return '#et-builder-' + name + '-module-template';
  165. },
  166. getModuleOptionsNames : function( module_type ) {
  167. var modules = this.get('modules');
  168. return this.addAdminLabel( _.findWhere( modules, { label : module_type } )['options'] );
  169. },
  170. getNumberOf : function( element_name, module_cid ) {
  171. var views = this.get( 'views' ),
  172. num = 0;
  173. _.each( views, function( view ) {
  174. var type = view['model']['attributes']['type'];
  175. if ( view['model']['attributes']['parent'] === module_cid && ( type === element_name || type === ( element_name + '_inner' ) ) )
  176. num++;
  177. } );
  178. return num;
  179. },
  180. getNumberOfModules : function( module_name ) {
  181. var views = this.get( 'views' ),
  182. num = 0;
  183. _.each( views, function( view ) {
  184. if ( view['model']['attributes']['type'] === module_name )
  185. num++;
  186. } );
  187. return num;
  188. },
  189. getTitleByShortcodeTag : function ( tag ) {
  190. var modules = this.get('modules');
  191. return _.findWhere( modules, { label : tag } )['title'];
  192. },
  193. isModuleFullwidth : function ( module_type ) {
  194. var modules = this.get('modules');
  195. return 'on' === _.findWhere( modules, { label : module_type } )['fullwidth_only'] ? true : false;
  196. },
  197. isChildrenLocked : function ( module_cid ) {
  198. var children_views = this.getChildrenViews( module_cid ),
  199. children_locked = false;
  200. _.each( children_views, function( child ) {
  201. if ( child.model.get( 'et_pb_locked' ) === 'on' || child.model.get( 'et_pb_parent_locked' ) === 'on' ) {
  202. children_locked = true;
  203. }
  204. } );
  205. return children_locked;
  206. },
  207. addAdminLabel : function ( optionsNames ) {
  208. return _.union( optionsNames, ['admin_label'] );
  209. }
  210. } );
  211. // Collections
  212. ET_PageBuilder.Modules = Backbone.Collection.extend( {
  213. model : ET_PageBuilder.Module
  214. } );
  215. ET_PageBuilder.SavedTemplates = Backbone.Collection.extend( {
  216. model : ET_PageBuilder.SavedTemplate
  217. } );
  218. ET_PageBuilder.Histories = Backbone.Collection.extend( {
  219. model : ET_PageBuilder.History
  220. } );
  221. //Views
  222. ET_PageBuilder.TemplatesView = window.wp.Backbone.View.extend( {
  223. className : 'et_pb_saved_layouts_list',
  224. tagName : 'ul',
  225. render: function() {
  226. var global_class = '',
  227. layout_category = typeof this.options.category === 'undefined' ? 'all' : this.options.category;
  228. this.collection.each( function( single_template ) {
  229. if ( 'all' === layout_category || ( -1 !== $.inArray( layout_category, single_template.get( 'categories' ) ) ) ) {
  230. var single_template_view = new ET_PageBuilder.SingleTemplateView( { model: single_template } );
  231. this.$el.append( single_template_view.el );
  232. global_class = typeof single_template_view.model.get( 'is_global' ) !== 'undefined' && 'global' === single_template_view.model.get( 'is_global' ) ? 'global' : '';
  233. }
  234. }, this );
  235. if ( 'global' === global_class ) {
  236. this.$el.addClass( 'et_pb_global' );
  237. }
  238. return this;
  239. }
  240. } );
  241. ET_PageBuilder.SingleTemplateView = window.wp.Backbone.View.extend( {
  242. tagName : 'li',
  243. template: _.template( $( '#et-builder-saved-entry' ).html() ),
  244. events: {
  245. 'click' : 'insertSection',
  246. },
  247. initialize: function(){
  248. this.render();
  249. },
  250. render: function() {
  251. this.$el.html( this.template( this.model.toJSON() ) );
  252. if ( typeof this.model.get( 'module_type' ) !== 'undefined' && '' !== this.model.get( 'module_type' ) && 'module' === this.model.get( 'layout_type' ) ) {
  253. this.$el.addClass( this.model.get( 'module_type' ) );
  254. }
  255. },
  256. insertSection : function( event ) {
  257. var clicked_button = $( event.target ),
  258. parent_id = typeof clicked_button.closest( '.et_pb_modal_settings' ).data( 'parent_cid' ) !== 'undefined' ? clicked_button.closest( '.et_pb_modal_settings' ).data( 'parent_cid' ) : '',
  259. current_row = typeof $( '.et-pb-settings-heading' ).data( 'current_row' ) !== 'undefined' ? $( '.et-pb-settings-heading' ).data( 'current_row' ) : '',
  260. global_id = 'global' === this.model.get( 'is_global' ) ? this.model.get( 'ID' ) : '',
  261. specialty_row = typeof $( '.et-pb-saved-modules-switcher' ).data( 'specialty_columns' ) !== 'undefined' ? 'on' : 'off',
  262. shortcode = this.model.get( 'shortcode' ),
  263. update_global = false,
  264. global_holder_id = 'row' === this.model.get( 'layout_type' ) ? current_row : parent_id,
  265. global_holder_view = ET_PageBuilder_Layout.getView( global_holder_id ),
  266. history_noun = this.options.model.get( 'layout_type' ) === 'row_inner' ? 'saved_row' : 'saved_' + this.options.model.get( 'layout_type' );
  267. if ( 'on' === specialty_row ) {
  268. global_holder_id = global_holder_view.model.get( 'parent' );
  269. global_holder_view = ET_PageBuilder_Layout.getView( global_holder_id );
  270. }
  271. if ( 'section' !== this.model.get( 'layout_type' ) && ( ( typeof global_holder_view.model.get( 'global_parent_cid' ) !== 'undefined' && '' !== global_holder_view.model.get( 'global_parent_cid' ) ) || ( typeof global_holder_view.model.get( 'et_pb_global_module' ) !== 'undefined' && '' !== global_holder_view.model.get( 'et_pb_global_module' ) ) ) ) {
  272. update_global = true;
  273. }
  274. // Enable history saving and set meta for history
  275. ET_PageBuilder_App.allowHistorySaving( 'added', history_noun );
  276. event.preventDefault();
  277. ET_PageBuilder_App.createLayoutFromContent( shortcode , parent_id, '', { ignore_template_tag : 'ignore_template', current_row_cid : current_row, global_id : global_id, after_section : parent_id, is_reinit : 'reinit' } );
  278. et_reinitialize_builder_layout();
  279. if ( true === update_global ) {
  280. global_module_cid = typeof global_holder_view.model.get( 'global_parent_cid' ) !== 'undefined' ? global_holder_view.model.get( 'global_parent_cid' ) : global_holder_id;
  281. et_pb_update_global_template( global_module_cid );
  282. }
  283. }
  284. } );
  285. ET_PageBuilder.TemplatesModal = window.wp.Backbone.View.extend( {
  286. className : 'et_pb_modal_settings',
  287. template : _.template( $( '#et-builder-load_layout-template' ).html() ),
  288. events : {
  289. 'click .et-pb-options-tabs-links li a' : 'switchTab'
  290. },
  291. render: function() {
  292. this.$el.html( this.template( { "display_switcher" : "off" } ) );
  293. this.$el.addClass( 'et_pb_modal_no_tabs' );
  294. return this;
  295. },
  296. switchTab: function( event ) {
  297. var $this_el = $( event.currentTarget ).parent();
  298. event.preventDefault();
  299. et_handle_templates_switching( $this_el, 'section', '' );
  300. }
  301. } );
  302. ET_PageBuilder.SectionView = window.wp.Backbone.View.extend( {
  303. className : 'et_pb_section',
  304. template : _.template( $('#et-builder-section-template').html() ),
  305. events: {
  306. 'click .et-pb-settings-section' : 'showSettings',
  307. 'click .et-pb-clone-section' : 'cloneSection',
  308. 'click .et-pb-remove-section' : 'removeSection',
  309. 'click .et-pb-section-add-main' : 'addSection',
  310. 'click .et-pb-section-add-fullwidth' : 'addFullwidthSection',
  311. 'click .et-pb-section-add-specialty' : 'addSpecialtySection',
  312. 'click .et-pb-section-add-saved' : 'addSavedSection',
  313. 'click .et-pb-expand' : 'expandSection',
  314. 'contextmenu .et-pb-section-add' : 'showRightClickOptions',
  315. 'click.et_pb_section > .et-pb-controls .et-pb-unlock' : 'unlockSection',
  316. 'contextmenu.et_pb_section > .et-pb-controls' : 'showRightClickOptions',
  317. 'contextmenu.et_pb_row > .et-pb-right-click-trigger-overlay' : 'showRightClickOptions',
  318. 'click.et_pb_section > .et-pb-controls' : 'hideRightClickOptions',
  319. 'click.et_pb_row > .et-pb-right-click-trigger-overlay' : 'hideRightClickOptions',
  320. 'click > .et-pb-locked-overlay' : 'showRightClickOptions',
  321. 'contextmenu > .et-pb-locked-overlay' : 'showRightClickOptions',
  322. },
  323. initialize : function() {
  324. this.child_views = [];
  325. this.listenTo( this.model, 'change:admin_label', this.renameModule );
  326. },
  327. render : function() {
  328. this.$el.html( this.template( this.model.toJSON() ) );
  329. if ( this.model.get( 'et_pb_specialty' ) === 'on' ) {
  330. this.$el.addClass( 'et_pb_section_specialty' );
  331. if ( this.model.get( 'et_pb_specialty_placeholder' ) === 'true' ) {
  332. this.$el.addClass( 'et_pb_section_placeholder' );
  333. }
  334. }
  335. if ( typeof this.model.get( 'et_pb_global_module' ) !== 'undefined' || ( typeof this.model.get( 'et_pb_template_type' ) !== 'undefined' && 'section' === this.model.get( 'et_pb_template_type' ) && 'global' === et_pb_options.is_global_template ) ) {
  336. this.$el.addClass( 'et_pb_global' );
  337. }
  338. if ( typeof this.model.get( 'et_pb_disabled' ) !== 'undefined' && this.model.get( 'et_pb_disabled' ) === 'on' ) {
  339. this.$el.addClass( 'et_pb_disabled' );
  340. }
  341. if ( typeof this.model.get( 'et_pb_locked' ) !== 'undefined' && this.model.get( 'et_pb_locked' ) === 'on' ) {
  342. this.$el.addClass( 'et_pb_locked' );
  343. }
  344. if ( typeof this.model.get( 'et_pb_collapsed' ) !== 'undefined' && this.model.get( 'et_pb_collapsed' ) === 'on' ) {
  345. this.$el.addClass( 'et_pb_collapsed' );
  346. }
  347. this.makeRowsSortable();
  348. return this;
  349. },
  350. showSettings : function( event ) {
  351. var $current_target = $( event.currentTarget ),
  352. modal_view,
  353. view_settings = {
  354. model : this.model,
  355. collection : this.collection,
  356. attributes : {
  357. 'data-open_view' : 'module_settings'
  358. },
  359. triggered_by_right_click : this.triggered_by_right_click,
  360. do_preview : this.do_preview
  361. };
  362. event.preventDefault();
  363. if ( this.isSectionLocked() ) {
  364. return;
  365. }
  366. if ( $current_target.closest( '.et_pb_section_specialty' ).length ) {
  367. var $specialty_section_columns = $current_target.closest( '.et_pb_section_specialty' ).find( '.et-pb-section-content > .et-pb-column' ),
  368. columns_layout = '';
  369. if ( $specialty_section_columns.length ) {
  370. $specialty_section_columns.each( function() {
  371. columns_layout += '' === columns_layout ? '1_1' : ',1_1';
  372. });
  373. }
  374. view_settings.model.attributes.columns_layout = columns_layout;
  375. }
  376. modal_view = new ET_PageBuilder.ModalView( view_settings );
  377. $('body').append( modal_view.render().el );
  378. if ( ( typeof modal_view.model.get( 'et_pb_global_module' ) !== 'undefined' && '' !== modal_view.model.get( 'et_pb_global_module' ) ) || ( typeof this.model.get( 'et_pb_template_type' ) !== 'undefined' && 'section' === this.model.get( 'et_pb_template_type' ) && 'global' === et_pb_options.is_global_template ) ) {
  379. $( '.et_pb_modal_settings_container' ).addClass( 'et_pb_saved_global_modal' );
  380. var saved_tabs = [ 'general', 'advanced', 'custom_css' ];
  381. _.each( saved_tabs, function( tab_name ) {
  382. $( '.et_pb_options_tab_' + tab_name ).addClass( 'et_pb_saved_global_tab' );
  383. });
  384. }
  385. if ( typeof this.model.get( 'et_pb_specialty' ) === 'undefined' || 'on' !== this.model.get( 'et_pb_specialty' ) ) {
  386. $( '.et_pb_modal_settings_container' ).addClass( 'et_pb_hide_advanced_tab' );
  387. }
  388. et_pb_open_current_tab();
  389. },
  390. addSection : function( event ) {
  391. var module_id = ET_PageBuilder_Layout.generateNewId();
  392. event.preventDefault();
  393. et_pb_close_all_right_click_options();
  394. // Enable history saving and set meta for history
  395. ET_PageBuilder_App.allowHistorySaving( 'added', 'section' );
  396. this.collection.add( [ {
  397. type : 'section',
  398. module_type : 'section',
  399. et_pb_fullwidth : 'off',
  400. et_pb_specialty : 'off',
  401. cid : module_id,
  402. view : this,
  403. created : 'auto',
  404. admin_label : et_pb_options.noun['section']
  405. } ] );
  406. },
  407. addFullwidthSection : function( event ) {
  408. var module_id = ET_PageBuilder_Layout.generateNewId();
  409. event.preventDefault();
  410. et_pb_close_all_right_click_options();
  411. // Enable history saving and set meta for history
  412. ET_PageBuilder_App.allowHistorySaving( 'added', 'fullwidth_section' );
  413. this.collection.add( [ {
  414. type : 'section',
  415. module_type : 'section',
  416. et_pb_fullwidth : 'on',
  417. et_pb_specialty : 'off',
  418. cid : module_id,
  419. view : this,
  420. created : 'auto',
  421. admin_label : et_pb_options.noun['section']
  422. } ] );
  423. },
  424. addSpecialtySection : function( event ) {
  425. var module_id = ET_PageBuilder_Layout.generateNewId(),
  426. $event_target = $(event.target),
  427. template_type = typeof $event_target !== 'undefined' && typeof $event_target.data( 'is_template' ) !== 'undefined' ? 'section' : '';
  428. event.preventDefault();
  429. et_pb_close_all_right_click_options();
  430. // Enable history saving and set meta for history
  431. ET_PageBuilder_App.allowHistorySaving( 'added', 'specialty_section' );
  432. this.collection.add( [ {
  433. type : 'section',
  434. module_type : 'section',
  435. et_pb_fullwidth : 'off',
  436. et_pb_specialty : 'on',
  437. cid : module_id,
  438. template_type : template_type,
  439. view : this,
  440. created : 'auto',
  441. admin_label : et_pb_options.noun['section']
  442. } ] );
  443. },
  444. addSavedSection : function( event ) {
  445. var parent_cid = this.model.get( 'cid' ),
  446. view_settings = {
  447. attributes : {
  448. 'data-open_view' : 'saved_templates',
  449. 'data-parent_cid' : parent_cid
  450. },
  451. view : this
  452. },
  453. main_view = new ET_PageBuilder.ModalView( view_settings );
  454. et_pb_close_all_right_click_options();
  455. $( 'body' ).append( main_view.render().el );
  456. generate_templates_view( 'include_global', '', 'section', $( '.et-pb-saved-modules-tab' ), 'regular', 0, 'all' );
  457. event.preventDefault();
  458. },
  459. expandSection : function( event ) {
  460. event.preventDefault();
  461. var $parent = this.$el.closest('.et_pb_section');
  462. $parent.removeClass('et_pb_collapsed');
  463. // Add attribute to shortcode
  464. this.options.model.attributes.et_pb_collapsed = 'off';
  465. // Enable history saving and set meta for history
  466. ET_PageBuilder_App.allowHistorySaving( 'expanded', 'section' );
  467. // Rebuild shortcodes
  468. ET_PageBuilder_App.saveAsShortcode();
  469. },
  470. unlockSection : function( event ) {
  471. event.preventDefault();
  472. var this_el = this,
  473. $parent = this_el.$el.closest('.et_pb_section'),
  474. request = et_pb_user_lock_permissions(),
  475. children_views;
  476. request.done( function ( response ) {
  477. if ( true === response ) {
  478. $parent.removeClass('et_pb_locked');
  479. // Add attribute to shortcode
  480. this_el.options.model.attributes.et_pb_locked = 'off';
  481. children_views = ET_PageBuilder_Layout.getChildrenViews( this_el.model.get('cid') );
  482. _.each( children_views, function( view, key ) {
  483. view.$el.removeClass('et_pb_parent_locked');
  484. view.model.set( 'et_pb_parent_locked', 'off', { silent : true } );
  485. } );
  486. // Enable history saving and set meta for history
  487. ET_PageBuilder_App.allowHistorySaving( 'unlocked', 'section' );
  488. // Rebuild shortcodes
  489. ET_PageBuilder_App.saveAsShortcode();
  490. } else {
  491. alert( et_pb_options.locked_section_permission_alert );
  492. }
  493. });
  494. },
  495. addRow : function( appendAfter ) {
  496. var module_id = ET_PageBuilder_Layout.generateNewId(),
  497. global_parent = typeof this.model.get( 'et_pb_global_module' ) !== 'undefined' && '' !== this.model.get( 'et_pb_global_module' ) ? this.model.get( 'et_pb_global_module' ) : '',
  498. global_parent_cid = '' !== global_parent ? this.model.get( 'cid' ) : '',
  499. new_row_view;
  500. this.collection.add( [ {
  501. type : 'row',
  502. module_type : 'row',
  503. cid : module_id,
  504. parent : this.model.get( 'cid' ),
  505. view : this,
  506. appendAfter : appendAfter,
  507. et_pb_global_parent : global_parent,
  508. global_parent_cid : global_parent_cid,
  509. admin_label : et_pb_options.noun['row']
  510. } ] );
  511. new_row_view = ET_PageBuilder_Layout.getView( module_id );
  512. new_row_view.displayColumnsOptions();
  513. },
  514. cloneSection : function( event ) {
  515. event.preventDefault();
  516. if ( this.isSectionLocked() ) {
  517. return;
  518. }
  519. var $cloned_element = this.$el.clone(),
  520. content,
  521. clone_section,
  522. view_settings = {
  523. model : this.model,
  524. view : this.$el,
  525. view_event : event
  526. };
  527. clone_section = new ET_PageBuilder.RightClickOptionsView( view_settings, true );
  528. // Enable history saving and set meta for history
  529. ET_PageBuilder_App.allowHistorySaving( 'cloned', 'section' );
  530. clone_section.copy( event );
  531. clone_section.pasteAfter( event );
  532. },
  533. makeRowsSortable : function() {
  534. var this_el = this,
  535. sortable_el = this_el.model.get( 'et_pb_fullwidth' ) !== 'on'
  536. ? '.et-pb-section-content'
  537. : '.et_pb_fullwidth_sortable_area',
  538. connectWith = ':not(.et_pb_locked) > ' + sortable_el;
  539. if ( this_el.model.get( 'et_pb_specialty' ) === 'on' ) {
  540. return;
  541. }
  542. this_el.$el.find( sortable_el ).sortable( {
  543. connectWith: connectWith,
  544. cancel : '.et-pb-settings, .et-pb-clone, .et-pb-remove, .et-pb-row-add, .et-pb-insert-module, .et-pb-insert-column, .et_pb_locked, .et-pb-disable-sort',
  545. update : function( event, ui ) {
  546. if ( ! $( ui.item ).closest( event.target ).length ) {
  547. // don't allow to move the row to another section if the section has only one row
  548. if ( ! $( event.target ).find( '.et_pb_row' ).length ) {
  549. $(this).sortable( 'cancel' );
  550. alert( et_pb_options.section_only_row_dragged_away );
  551. }
  552. // do not allow to drag rows into sections where sorting is disabled
  553. if ( $( ui.item ).closest( '.et-pb-disable-sort').length ) {
  554. $( event.target ).sortable( 'cancel' );
  555. }
  556. // makes sure the code runs one time, if row is dragged into another section
  557. return;
  558. }
  559. if ( $( ui.item ).closest( '.et_pb_section.et_pb_global' ).length && $( ui.item ).hasClass( 'et_pb_global' ) ) {
  560. $( ui.sender ).sortable( 'cancel' );
  561. alert( et_pb_options.global_row_alert );
  562. } else if ( ( $( ui.item ).closest( '.et_pb_section.et_pb_global' ).length || $( ui.sender ).closest( '.et_pb_section.et_pb_global' ).length ) && '' === et_pb_options.template_post_id ) {
  563. var module_cid = ui.item.data( 'cid' ),
  564. model,
  565. global_module_cid,
  566. $moving_from,
  567. $moving_to;
  568. $moving_from = $( ui.sender ).closest( '.et_pb_section.et_pb_global' );
  569. $moving_to = $( ui.item ).closest( '.et_pb_section.et_pb_global' );
  570. if ( $moving_from === $moving_to ) {
  571. model = this_el.collection.find( function( model ) {
  572. return model.get('cid') == module_cid;
  573. } );
  574. global_module_cid = model.get( 'global_parent_cid' );
  575. et_pb_update_global_template( global_module_cid );
  576. et_reinitialize_builder_layout();
  577. } else {
  578. var $global_element = $moving_from;
  579. for ( var i = 1; i <= 2; i++ ) {
  580. global_module_cid = $global_element.find( '.et-pb-section-content' ).data( 'cid' );
  581. if ( typeof global_module_cid !== 'undefined' && '' !== global_module_cid ) {
  582. et_pb_update_global_template( global_module_cid );
  583. et_reinitialize_builder_layout();
  584. }
  585. $global_element = $moving_to;
  586. };
  587. }
  588. }
  589. ET_PageBuilder_Layout.setNewParentID( ui.item.find( '.et-pb-row-content' ).data( 'cid' ), this_el.model.attributes.cid );
  590. // Enable history saving and set meta for history
  591. ET_PageBuilder_App.allowHistorySaving( 'moved', 'row' );
  592. ET_PageBuilder_Events.trigger( 'et-sortable:update' );
  593. // Prepare collection sorting based on layout position
  594. var section_cid = parseInt( $(this).attr( 'data-cid') ),
  595. sibling_row_index = 0;
  596. // Loop row block based on DOM position to ensure its index order
  597. $(this).find('.et-pb-row-content').each(function(){
  598. sibling_row_index++;
  599. var sibling_row_cid = parseInt( $(this).data('cid') ),
  600. layout_index = section_cid + sibling_row_index,
  601. sibling_model = ET_PageBuilder_Modules.findWhere({ cid : sibling_row_cid });
  602. // Set layout_index
  603. sibling_model.set({ layout_index : layout_index });
  604. });
  605. // Sort collection based on layout_index
  606. ET_PageBuilder_Modules.comparator = 'layout_index';
  607. ET_PageBuilder_Modules.sort();
  608. },
  609. start : function( event, ui ) {
  610. et_pb_close_all_right_click_options();
  611. }
  612. } );
  613. },
  614. addChildView : function( view ) {
  615. this.child_views.push( view );
  616. },
  617. removeChildViews : function() {
  618. _.each( this.child_views, function( view ) {
  619. if ( typeof view.model !== 'undefined' )
  620. view.model.destroy();
  621. view.remove();
  622. } );
  623. },
  624. removeSection : function( event, remove_all ) {
  625. var rows,
  626. remove_last_specialty_section = false;
  627. if ( event ) event.preventDefault();
  628. if ( this.isSectionLocked() || ET_PageBuilder_Layout.isChildrenLocked( this.model.get( 'cid' ) ) ) {
  629. return;
  630. }
  631. if ( this.model.get( 'et_pb_fullwidth' ) === 'on' ) {
  632. this.removeChildViews();
  633. } else {
  634. rows = ET_PageBuilder_Layout.getChildViews( this.model.get('cid') );
  635. _.each( rows, function( row ) {
  636. if ( row.model.get( 'type' ) === 'column' ) {
  637. // remove column in specialty section
  638. row.removeColumn();
  639. } else {
  640. row.removeRow();
  641. }
  642. } );
  643. }
  644. // the only section left is specialty or fullwidth section
  645. if ( ! ET_PageBuilder_Layout.get( 'forceRemove' ) && ( this.model.get( 'et_pb_specialty' ) === 'on' || this.model.get( 'et_pb_fullwidth' ) === 'on' ) && ET_PageBuilder_Layout.getNumberOfModules( 'section' ) === 1 ) {
  646. remove_last_specialty_section = true;
  647. }
  648. // if there is only one section, don't remove it
  649. // allow to remove all sections if removeSection function is called directly
  650. // remove the specialty section even if it's the last one on the page
  651. if ( ET_PageBuilder_Layout.get( 'forceRemove' ) || remove_last_specialty_section || ET_PageBuilder_Layout.getNumberOfModules( 'section' ) > 1 ) {
  652. this.model.destroy();
  653. ET_PageBuilder_Layout.removeView( this.model.get('cid') );
  654. this.remove();
  655. }
  656. // start with the clean layout if the user removed the last specialty section on the page
  657. if ( remove_last_specialty_section ) {
  658. ET_PageBuilder_App.removeAllSections( true );
  659. return;
  660. }
  661. // Enable history saving and set meta for history
  662. if ( _.isUndefined( remove_all ) ) {
  663. ET_PageBuilder_App.allowHistorySaving( 'removed', 'section' );
  664. } else {
  665. ET_PageBuilder_App.allowHistorySaving( 'cleared', 'layout' );
  666. }
  667. // trigger remove event if the row was removed manually ( using a button )
  668. if ( event ) {
  669. ET_PageBuilder_Events.trigger( 'et-module:removed' );
  670. }
  671. },
  672. isSectionLocked : function() {
  673. if ( 'on' === this.model.get( 'et_pb_locked' ) ) {
  674. return true;
  675. }
  676. return false;
  677. },
  678. showRightClickOptions : function( event ) {
  679. event.preventDefault();
  680. var et_right_click_options_view,
  681. view_settings = {
  682. model : this.model,
  683. view : this.$el,
  684. view_event : event
  685. };
  686. et_right_click_options_view = new ET_PageBuilder.RightClickOptionsView( view_settings );
  687. },
  688. hideRightClickOptions : function( event ) {
  689. event.preventDefault();
  690. et_pb_close_all_right_click_options();
  691. },
  692. renameModule : function() {
  693. this.$( '.et-pb-section-title' ).html( this.model.get( 'admin_label' ) );
  694. }
  695. } );
  696. ET_PageBuilder.RowView = window.wp.Backbone.View.extend( {
  697. className : 'et_pb_row',
  698. template : _.template( $('#et-builder-row-template').html() ),
  699. events : {
  700. 'click .et-pb-settings-row' : 'showSettings',
  701. 'click .et-pb-insert-column' : 'displayColumnsOptions',
  702. 'click .et-pb-clone-row' : 'cloneRow',
  703. 'click .et-pb-row-add' : 'addNewRow',
  704. 'click .et-pb-remove-row' : 'removeRow',
  705. 'click .et-pb-change-structure' : 'changeStructure',
  706. 'click .et-pb-expand' : 'expandRow',
  707. 'contextmenu .et-pb-row-add' : 'showRightClickOptions',
  708. 'click.et_pb_row > .et-pb-controls .et-pb-unlock' : 'unlockRow',
  709. 'contextmenu.et_pb_row > .et-pb-controls' : 'showRightClickOptions',
  710. 'contextmenu.et_pb_row > .et-pb-right-click-trigger-overlay' : 'showRightClickOptions',
  711. 'contextmenu .et-pb-column' : 'showRightClickOptions',
  712. 'click.et_pb_row > .et-pb-controls' : 'hideRightClickOptions',
  713. 'click.et_pb_row > .et-pb-right-click-trigger-overlay' : 'hideRightClickOptions',
  714. 'click > .et-pb-locked-overlay' : 'showRightClickOptions',
  715. 'contextmenu > .et-pb-locked-overlay' : 'showRightClickOptions',
  716. },
  717. initialize : function() {
  718. this.listenTo( ET_PageBuilder_Events, 'et-add:columns', this.toggleInsertColumnButton );
  719. this.listenTo( this.model, 'change:admin_label', this.renameModule );
  720. },
  721. render : function() {
  722. var parent_views = ET_PageBuilder_Layout.getParentViews( this.model.get( 'parent' ) );
  723. if ( typeof this.model.get( 'view' ) !== 'undefined' && typeof this.model.get( 'view' ).model.get( 'layout_specialty' ) !== 'undefined' ) {
  724. this.model.set( 'specialty_row', '1', { silent : true } );
  725. }
  726. this.$el.html( this.template( this.model.toJSON() ) );
  727. if ( typeof this.model.get( 'et_pb_global_module' ) !== 'undefined' || ( typeof this.model.get( 'et_pb_template_type' ) !== 'undefined' && 'row' === this.model.get( 'et_pb_template_type' ) && 'global' === et_pb_options.is_global_template ) ) {
  728. this.$el.addClass( 'et_pb_global' );
  729. }
  730. if ( typeof this.model.get( 'et_pb_disabled' ) !== 'undefined' && this.model.get( 'et_pb_disabled' ) === 'on' ) {
  731. this.$el.addClass( 'et_pb_disabled' );
  732. }
  733. if ( typeof this.model.get( 'et_pb_locked' ) !== 'undefined' && this.model.get( 'et_pb_locked' ) === 'on' ) {
  734. this.$el.addClass( 'et_pb_locked' );
  735. _.each( parent_views, function( parent ) {
  736. parent.$el.addClass( 'et_pb_children_locked' );
  737. } );
  738. }
  739. if ( typeof this.model.get( 'et_pb_parent_locked' ) !== 'undefined' && this.model.get( 'et_pb_parent_locked' ) === 'on' ) {
  740. this.$el.addClass( 'et_pb_parent_locked' );
  741. }
  742. if ( typeof this.model.get( 'et_pb_collapsed' ) !== 'undefined' && this.model.get( 'et_pb_collapsed' ) === 'on' ) {
  743. this.$el.addClass( 'et_pb_collapsed' );
  744. }
  745. return this;
  746. },
  747. showSettings : function( event ) {
  748. var modal_view,
  749. view_settings = {
  750. model : this.model,
  751. collection : this.collection,
  752. attributes : {
  753. 'data-open_view' : 'module_settings'
  754. },
  755. triggered_by_right_click : this.triggered_by_right_click,
  756. do_preview : this.do_preview
  757. };
  758. event.preventDefault();
  759. if ( this.isRowLocked() ) {
  760. return;
  761. }
  762. modal_view = new ET_PageBuilder.ModalView( view_settings );
  763. $('body').append( modal_view.render().el );
  764. if ( ( typeof modal_view.model.get( 'et_pb_global_module' ) !== 'undefined' && '' !== modal_view.model.get( 'et_pb_global_module' ) ) || ( ET_PageBuilder_Layout.getView( modal_view.model.get('cid') ).$el.closest( '.et_pb_global' ).length ) || ( typeof this.model.get( 'et_pb_template_type' ) !== 'undefined' && 'row' === this.model.get( 'et_pb_template_type' ) && 'global' === et_pb_options.is_global_template ) ) {
  765. $( '.et_pb_modal_settings_container' ).addClass( 'et_pb_saved_global_modal' );
  766. var saved_tabs = [ 'general', 'advanced', 'custom_css' ];
  767. _.each( saved_tabs, function( tab_name ) {
  768. $( '.et_pb_options_tab_' + tab_name ).addClass( 'et_pb_saved_global_tab' );
  769. });
  770. }
  771. },
  772. displayColumnsOptions : function( event ) {
  773. if ( event ) {
  774. event.preventDefault();
  775. }
  776. if ( this.isRowLocked() ) {
  777. return;
  778. }
  779. var view,
  780. this_view = this;
  781. this.model.set( 'open_view', 'column_settings', { silent : true } );
  782. view = new ET_PageBuilder.ModalView( {
  783. model : this.model,
  784. collection : this.collection,
  785. attributes : {
  786. 'data-open_view' : 'column_settings'
  787. },
  788. view : this_view
  789. } );
  790. $('body').append( view.render().el );
  791. this.toggleInsertColumnButton();
  792. },
  793. changeStructure : function( event ) {
  794. event.preventDefault();
  795. var view,
  796. this_view = this;
  797. if ( this.isRowLocked() ) {
  798. return;
  799. }
  800. this.model.set( 'change_structure', 'true', { silent : true } );
  801. this.model.set( 'open_view', 'column_settings', { silent : true } );
  802. ET_PageBuilder.Events = ET_PageBuilder_Events;
  803. view = new ET_PageBuilder.ModalView( {
  804. model : this.model,
  805. collection : this.collection,
  806. attributes : {
  807. 'data-open_view' : 'column_settings'
  808. },
  809. view : this_view
  810. } );
  811. $('body').append( view.render().el );
  812. },
  813. expandRow : function( event ) {
  814. event.preventDefault();
  815. var $parent = this.$el.closest('.et_pb_row');
  816. $parent.removeClass('et_pb_collapsed');
  817. // Add attribute to shortcode
  818. this.options.model.attributes.et_pb_collapsed = 'off';
  819. // Enable history saving and set meta for history
  820. ET_PageBuilder_App.allowHistorySaving( 'expanded', 'row' );
  821. // Rebuild shortcodes
  822. ET_PageBuilder_App.saveAsShortcode();
  823. },
  824. unlockRow : function( event ) {
  825. event.preventDefault();
  826. var this_el = this,
  827. $parent = this_el.$el.closest('.et_pb_row'),
  828. request = et_pb_user_lock_permissions(),
  829. children_views,
  830. parent_views;
  831. request.done( function ( response ) {
  832. if ( true === response ) {
  833. $parent.removeClass('et_pb_locked');
  834. // Add attribute to shortcode
  835. this_el.options.model.attributes.et_pb_locked = 'off';
  836. children_views = ET_PageBuilder_Layout.getChildrenViews( this_el.model.get('cid') );
  837. _.each( children_views, function( view, key ) {
  838. view.$el.removeClass('et_pb_parent_locked');
  839. view.model.set( 'et_pb_parent_locked', 'off', { silent : true } );
  840. } );
  841. parent_views = ET_PageBuilder_Layout.getParentViews( this_el.model.get('parent') );
  842. _.each( parent_views, function( view, key ) {
  843. if ( ! ET_PageBuilder_Layout.isChildrenLocked( view.model.get( 'cid' ) ) ) {
  844. view.$el.removeClass('et_pb_children_locked');
  845. }
  846. } );
  847. // Enable history saving and set meta for history
  848. ET_PageBuilder_App.allowHistorySaving( 'unlocked', 'row' );
  849. // Rebuild shortcodes
  850. ET_PageBuilder_App.saveAsShortcode();
  851. } else {
  852. alert( et_pb_options.locked_row_permission_alert );
  853. }
  854. });
  855. },
  856. toggleInsertColumnButton : function() {
  857. var model_id = this.model.get( 'cid' ),
  858. columnsInRow;
  859. // check if the current row has at least one column
  860. columnsInRow = this.collection.find( function( model ) {
  861. return ( model.get( 'type' ) === 'column' || model.get( 'type' ) === 'column_inner' ) && model.get( 'parent' ) === model_id;
  862. } );
  863. if ( ! _.isUndefined( columnsInRow ) ) {
  864. this.$( '.et-pb-insert-column' ).hide();
  865. // show "change columns structure" icon, if current row's column layout is set
  866. this.$( '.et-pb-change-structure' ).show();
  867. }
  868. },
  869. addNewRow : function( event ) {
  870. var $parent_section = this.$el.closest( '.et-pb-section-content' ),
  871. $current_target = $( event.currentTarget ),
  872. parent_view_cid = $current_target.closest( '.et-pb-column-specialty' ).length ? $current_target.closest( '.et-pb-column-specialty' ).data( 'cid' ) : $parent_section.data( 'cid' ),
  873. parent_view = ET_PageBuilder_Layout.getView( parent_view_cid );
  874. event.preventDefault();
  875. et_pb_close_all_right_click_options();
  876. if ( 'on' === this.model.get( 'et_pb_parent_locked' ) ) {
  877. return;
  878. }
  879. // Enable history saving and set meta for history
  880. ET_PageBuilder_App.allowHistorySaving( 'added', 'row' );
  881. parent_view.addRow( this.$el );
  882. },
  883. cloneRow : function( event ) {
  884. var global_module_cid = '',
  885. parent_view = ET_PageBuilder_Layout.getView( this.model.get( 'parent' ) ),
  886. clone_row,
  887. view_settings = {
  888. model : this.model,
  889. view : this.$el,
  890. view_event : event
  891. };
  892. event.preventDefault();
  893. if ( this.isRowLocked() ) {
  894. return;
  895. }
  896. if ( this.$el.closest( '.et_pb_section.et_pb_global' ).length && typeof parent_view.model.get( 'et_pb_template_type' ) === 'undefined' ) {
  897. global_module_cid = this.model.get( 'global_parent_cid' );
  898. }
  899. clone_row = new ET_PageBuilder.RightClickOptionsView( view_settings, true );
  900. // Enable history saving and set meta for history
  901. ET_PageBuilder_App.allowHistorySaving( 'cloned', 'row' );
  902. clone_row.copy( event );
  903. clone_row.pasteAfter( event );
  904. if ( '' !== global_module_cid ) {
  905. et_pb_update_global_template( global_module_cid );
  906. }
  907. },
  908. removeRow : function( event, force ) {
  909. var columns,
  910. global_module_cid = '',
  911. parent_view = ET_PageBuilder_Layout.getView( this.model.get( 'parent' ) );
  912. if ( this.isRowLocked() || ET_PageBuilder_Layout.isChildrenLocked( this.model.get( 'cid' ) ) ) {
  913. return;
  914. }
  915. if ( event ) {
  916. event.preventDefault();
  917. // don't allow to remove a specialty section, even if there is only one row in it
  918. if ( this.$el.closest( '.et-pb-column-specialty' ).length ) {
  919. event.stopPropagation();
  920. }
  921. if ( this.$el.closest( '.et_pb_section.et_pb_global' ).length && typeof parent_view.model.get( 'et_pb_template_type' ) === 'undefined' ) {
  922. global_module_cid = this.model.get( 'global_parent_cid' );
  923. }
  924. }
  925. columns = ET_PageBuilder_Layout.getChildViews( this.model.get('cid') );
  926. _.each( columns, function( column ) {
  927. column.removeColumn();
  928. } );
  929. // if there is only one row in the section, don't remove it
  930. if ( ET_PageBuilder_Layout.get( 'forceRemove' ) || ET_PageBuilder_Layout.getNumberOf( 'row', this.model.get('parent') ) > 1 ) {
  931. this.model.destroy();
  932. ET_PageBuilder_Layout.removeView( this.model.get('cid') );
  933. this.remove();
  934. } else {
  935. this.$( '.et-pb-insert-column' ).show();
  936. // hide "change columns structure" icon, column layout can be re-applied using "Insert column(s)" button
  937. this.$( '.et-pb-change-structure' ).hide();
  938. }
  939. // Enable history saving and set meta for history
  940. ET_PageBuilder_App.allowHistorySaving( 'removed', 'row' );
  941. // trigger remove event if the row was removed manually ( using a button )
  942. if ( event ) {
  943. ET_PageBuilder_Events.trigger( 'et-module:removed' );
  944. }
  945. if ( '' !== global_module_cid ) {
  946. et_pb_update_global_template( global_module_cid );
  947. }
  948. },
  949. isRowLocked : function() {
  950. if ( 'on' === this.model.get( 'et_pb_locked' ) || 'on' === this.model.get( 'et_pb_parent_locked' ) ) {
  951. return true;
  952. }
  953. return false;
  954. },
  955. showRightClickOptions : function( event ) {
  956. event.preventDefault();
  957. var $event_target = $( event.target ),
  958. et_right_click_options_view,
  959. view_settings;
  960. // Do nothing if Module or "Insert Module" clicked
  961. if ( $event_target.closest( '.et-pb-insert-module' ).length || $event_target.hasClass( 'et_pb_module_block' ) || $event_target.closest( '.et_pb_module_block' ).length ) {
  962. return;
  963. }
  964. et_right_click_options_view,
  965. view_settings = {
  966. model : this.model,
  967. view : this.$el,
  968. view_event : event
  969. };
  970. et_right_click_options_view = new ET_PageBuilder.RightClickOptionsView( view_settings );
  971. },
  972. hideRightClickOptions : function( event ) {
  973. event.preventDefault();
  974. et_pb_close_all_right_click_options();
  975. },
  976. renameModule : function() {
  977. this.$( '.et-pb-row-title' ).html( this.model.get( 'admin_label' ) );
  978. }
  979. } );
  980. ET_PageBuilder.ModalView = window.wp.Backbone.View.extend( {
  981. className : 'et_pb_modal_settings_container',
  982. template : _.template( $('#et-builder-modal-template').html() ),
  983. events : {
  984. 'click .et-pb-modal-save' : 'saveSettings',
  985. 'click .et-pb-modal-preview-template' : 'preview',
  986. 'click .et-pb-preview-mobile' : 'resizePreviewScreen',
  987. 'click .et-pb-preview-tablet' : 'resizePreviewScreen',
  988. 'click .et-pb-preview-desktop' : 'resizePreviewScreen',
  989. 'click .et-pb-modal-close' : 'closeModal',
  990. 'click .et-pb-modal-save-template' : 'saveTemplate',
  991. 'change #et_pb_select_category' : 'applyFilter'
  992. },
  993. initialize : function( attributes ) {
  994. this.listenTo( ET_PageBuilder_Events, 'et-add:columns', this.removeView );
  995. // listen to module settings box that is created after the user selects new module to add
  996. this.listenTo( ET_PageBuilder_Events, 'et-new_module:show_settings', this.removeView );
  997. this.listenTo( ET_PageBuilder_Events, 'et-saved_layout:loaded', this.removeView );
  998. this.options = attributes;
  999. },
  1000. render : function() {
  1001. var view,
  1002. view_settings = {
  1003. model : this.model,
  1004. collection : this.collection,
  1005. view : this.options.view
  1006. },
  1007. fake_value = false;
  1008. this.$el.attr( 'tabindex', 0 ); // set tabindex to make the div focusable
  1009. // update the row view if it has been dragged into another column
  1010. if ( typeof this.model !== 'undefined' && typeof this.model.get( 'view' ) !== 'undefined' && ( this.model.get( 'module_type' ) === 'row_inner' || this.model.get( 'module_type' ) === 'row' ) && this.model.get( 'parent' ) !== this.model.get( 'view' ).$el.data( 'cid' ) ) {
  1011. this.model.set( 'view', ET_PageBuilder_Layout.getView( this.model.get( 'parent' ) ), { silent : true } );
  1012. }
  1013. if ( this.attributes['data-open_view'] === 'all_modules' && this.model.get( 'module_type' ) === 'section' && this.model.get( 'et_pb_fullwidth' ) === 'on' ) {
  1014. this.model.set( 'type', 'column', { silent : true } );
  1015. fake_value = true;
  1016. }
  1017. if ( typeof this.model !== 'undefined' ) {
  1018. var this_parent_view = ET_PageBuilder_Layout.getView( this.model.get( 'parent' ) ),
  1019. this_template_type = typeof this.model.get( 'et_pb_template_type' ) !== 'undefined' && 'module' === this.model.get( 'et_pb_template_type' ) || typeof this.model.get( 'template_type' ) !== 'undefined' && 'module' === this.model.get( 'template_type' ),
  1020. saved_tabs = typeof this.model.get( 'et_pb_saved_tabs' ) !== 'undefined' && 'all' !== this.model.get( 'et_pb_saved_tabs' ) || typeof this_parent_view !== 'undefined' && typeof this_parent_view.model.get( 'et_pb_saved_tabs' ) !== 'undefined' && 'all' !== this_parent_view.model.get( 'et_pb_saved_tabs' )
  1021. if ( this.attributes['data-open_view'] === 'column_specialty_settings' ) {
  1022. this.model.set( 'open_view', 'column_specialty_settings', { silent : true } );
  1023. }
  1024. this.$el.html( this.template( this.model.toJSON() ) );
  1025. if ( this.attributes['data-open_view'] === 'column_specialty_settings' ) {
  1026. this.model.unset( 'open_view', 'column_specialty_settings', { silent : true } );
  1027. }
  1028. if ( this_template_type && saved_tabs ) {
  1029. var selected_tabs = typeof this.model.get( 'et_pb_saved_tabs' ) !== 'undefined' ? this.model.get( 'et_pb_saved_tabs' ) : this_parent_view.model.get( 'et_pb_saved_tabs' ) ,
  1030. selected_tabs_array = selected_tabs.split( ',' ),
  1031. possible_tabs_array = [ 'general', 'advanced', 'css' ],
  1032. css_class = '',
  1033. start_from_tab = '';
  1034. if ( selected_tabs_array[0] !== 'all' ) {
  1035. _.each( possible_tabs_array, function ( tab ) {
  1036. if ( -1 === $.inArray( tab, selected_tabs_array ) ) {
  1037. css_class += ' et_pb_hide_' + tab + '_tab';
  1038. } else {
  1039. start_from_tab = '' === start_from_tab ? tab : start_from_tab;
  1040. }
  1041. } );
  1042. start_from_tab = 'css' === start_from_tab ? 'custom_css' : start_from_tab;
  1043. }
  1044. this.$el.addClass( css_class );
  1045. if ( typeof this.model.get( 'et_pb_saved_tabs' ) === 'undefined' ) {
  1046. this.model.set( 'et_pb_saved_tabs', selected_tabs, { silent : true } );
  1047. }
  1048. }
  1049. }
  1050. else
  1051. this.$el.html( this.template() );
  1052. if ( fake_value )
  1053. this.model.set( 'type', 'section', { silent : true } );
  1054. this.container = this.$('.et-pb-modal-container');
  1055. if ( this.attributes['data-open_view'] === 'column_settings' ) {
  1056. view = new ET_PageBuilder.ColumnSettingsView( view_settings );
  1057. } else if ( this.attributes['data-open_view'] === 'all_modules' ) {
  1058. view_settings['attributes'] = {
  1059. 'data-parent_cid' : this.model.get( 'cid' )
  1060. }
  1061. view = new ET_PageBuilder.ModulesView( view_settings );
  1062. } else if ( this.attributes['data-open_view'] === 'module_settings' ) {
  1063. view_settings['attributes'] = {
  1064. 'data-module_type' : this.model.get( 'module_type' )
  1065. }
  1066. view_settings['view'] = this;
  1067. view = new ET_PageBuilder.ModuleSettingsView( view_settings );
  1068. } else if ( this.attributes['data-open_view'] === 'save_layout' ) {
  1069. view = new ET_PageBuilder.SaveLayoutSettingsView( view_settings );
  1070. } else if ( this.attributes['data-open_view'] === 'column_specialty_settings' ) {
  1071. view = new ET_PageBuilder.ColumnSettingsView( view_settings );
  1072. } else if ( this.attributes['data-open_view'] === 'saved_templates' ) {
  1073. view = new ET_PageBuilder.TemplatesModal( { attributes: { 'data-parent_cid' : this.attributes['data-parent_cid'] } } );
  1074. }
  1075. this.container.append( view.render().el );
  1076. if ( this.attributes['data-open_view'] === 'column_settings' ) {
  1077. // if column settings layout was generated, remove open_view attribute from a row
  1078. // the row module modal window shouldn't have this attribute attached
  1079. this.model.unset( 'open_view', { silent : true } );
  1080. }
  1081. // show only modules that the current element can contain
  1082. if ( this.attributes['data-open_view'] === 'all_modules' ) {
  1083. if ( this.model.get( 'module_type' ) === 'section' && typeof( this.model.get( 'et_pb_fullwidth' ) !== 'undefined' ) && this.model.get( 'et_pb_fullwidth' ) === 'on' ) {
  1084. $( view.render().el ).find( '.et-pb-all-modules li:not(.et_pb_fullwidth_only_module)' ).remove();
  1085. } else {
  1086. $( view.render().el ).find( 'li.et_pb_fullwidth_only_module' ).remove();
  1087. }
  1088. }
  1089. if ( $( '.et_pb_modal_overlay' ).length ) {
  1090. $( '.et_pb_modal_overlay' ).remove();
  1091. $( 'body' ).removeClass( 'et_pb_stop_scroll' );
  1092. }
  1093. $( 'body' ).addClass( 'et_pb_stop_scroll' ).append( '<div class="et_pb_modal_overlay"></div>' );
  1094. return this;
  1095. },
  1096. closeModal : function( event ) {
  1097. event.preventDefault();
  1098. if ( $( '.et_modal_on_top' ).length ) {
  1099. $( '.et_modal_on_top' ).remove();
  1100. } else {
  1101. this.removeOverlay();
  1102. if ( typeof this.model !== 'undefined' && this.model.get( 'type' ) === 'module' && this.$( '#et_pb_content_new' ).length )
  1103. et_pb_tinymce_remove_control( 'et_pb_content_new' );
  1104. et_pb_hide_active_color_picker( this );
  1105. this.remove();
  1106. ET_PageBuilder_Events.trigger( 'et-modal-view-removed' );
  1107. }
  1108. },
  1109. removeView : function() {
  1110. this.removeOverlay();
  1111. if ( typeof this.model === 'undefined' || ( this.model.get( 'type' ) === 'row' || this.model.get( 'type' ) === 'column' || this.model.get( 'type' ) === 'row_inner' || this.model.get( 'type' ) === 'column_inner' || ( this.model.get( 'type' ) === 'section' && ( this.model.get( 'et_pb_fullwidth' ) === 'on' || this.model.get( 'et_pb_specialty' ) === 'on' ) ) ) ) {
  1112. this.remove();
  1113. }
  1114. },
  1115. saveSettings : function( event, close_modal ) {
  1116. var that = this,
  1117. global_module_cid = '',
  1118. this_parent_view = typeof that.model.get( 'parent' ) !== 'undefined' ? ET_PageBuilder_Layout.getView( that.model.get( 'parent' ) ) : '',
  1119. global_holder_view = '' !== this_parent_view && ( typeof that.model.get( 'et_pb_global_module' ) === 'undefined' || '' === that.model.get( 'et_pb_global_module' ) ) ? this_parent_view : ET_PageBuilder_Layout.getView( that.model.get( 'cid' ) ),
  1120. update_template_only = false,
  1121. close_modal = _.isUndefined( close_modal ) ? true : close_modal;
  1122. event.preventDefault();
  1123. // Disabling state and mark it. It takes a while for generating shortcode,
  1124. // so ensure that user doesn't update the page before shortcode generation has completed
  1125. $('#publish').addClass( 'disabled' );
  1126. ET_PageBuilder_App.disable_publish = true;
  1127. if ( ( typeof global_holder_view.model.get( 'global_parent_cid' ) !== 'undefined' && '' !== global_holder_view.model.get( 'global_parent_cid' ) ) || ( typeof global_holder_view.model.get( 'et_pb_global_module' ) !== 'undefined' && '' !== global_holder_view.model.get( 'et_pb_global_module' ) ) ) {
  1128. global_mo