fl-builder-ajax-layout.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. (function($){
  2. /**
  3. * Helper class for rendering layout changes via AJAX.
  4. *
  5. * @class FLBuilderAJAXLayout
  6. * @since 1.7
  7. */
  8. FLBuilderAJAXLayout = function( data, callback )
  9. {
  10. this._data = $.extend( {}, this._defaults, typeof data == 'string' ? JSON.parse( data ) : data );
  11. this._callback = callback;
  12. this._post = FLBuilderConfig.postId;
  13. this._head = $('head').eq(0);
  14. this._body = $('body').eq(0);
  15. // Setup the new CSS vars if we have new CSS.
  16. if ( this._data.css ) {
  17. this._loader = $('<img src="' + this._data.css + '" />');
  18. this._oldCss = $('link[href*="/cache/' + this._post + '-layout"]');
  19. this._newCss = $('<link rel="stylesheet" id="fl-builder-layout-' + this._post + '-css" href="'+ this._data.css +'" />');
  20. }
  21. // Setup partial refresh vars.
  22. if ( this._data.partial ) {
  23. if ( this._data.js ) {
  24. this._oldJs = $('#fl-builder-partial-refresh-js');
  25. this._newJs = $('<script type="text/javascript" id="fl-builder-partial-refresh-js">'+ this._data.js +'</script>');
  26. }
  27. if ( this._data.nodeId ) {
  28. if ( this._data.oldNodeId ) {
  29. this._oldScriptsStyles = $( '.fl-builder-node-scripts-styles[data-node="' + this._data.oldNodeId + '"]' );
  30. this._content = $( '.fl-node-' + this._data.oldNodeId );
  31. }
  32. else {
  33. this._oldScriptsStyles = $( '.fl-builder-node-scripts-styles[data-node="' + this._data.nodeId + '"]' );
  34. this._content = $( '.fl-node-' + this._data.nodeId ).eq(0);
  35. }
  36. }
  37. }
  38. // Setup full refresh vars.
  39. else {
  40. this._oldJs = $('script[src*="/cache/' + this._post + '"]');
  41. this._newJs = $('<script src="'+ this._data.js +'"></script>');
  42. this._oldScriptsStyles = $( '.fl-builder-layout-scripts-styles' );
  43. this._content = $( FLBuilder._contentClass );
  44. }
  45. this._init();
  46. };
  47. /**
  48. * Prototype for new instances.
  49. *
  50. * @since 1.7.
  51. * @property {Object} prototype
  52. */
  53. FLBuilderAJAXLayout.prototype = {
  54. /**
  55. * Defaults for the data sent from the server.
  56. *
  57. * @since 1.7
  58. * @access private
  59. * @property {Object} _defaults
  60. */
  61. _defaults : {
  62. partial : false,
  63. nodeId : null,
  64. nodeType : null,
  65. nodeParent : null,
  66. nodePosition : null,
  67. oldNodeId : null,
  68. html : null,
  69. scriptsStyles : null,
  70. css : null,
  71. js : null
  72. },
  73. /**
  74. * Data from the server for this render.
  75. *
  76. * @since 1.7
  77. * @access private
  78. * @property {Object} _data
  79. */
  80. _data : null,
  81. /**
  82. * A function to call when the render is complete.
  83. *
  84. * @since 1.7
  85. * @access private
  86. * @property {Function} _callback
  87. */
  88. _callback : function(){},
  89. /**
  90. * The ID of this post.
  91. *
  92. * @since 1.7
  93. * @access private
  94. * @property {Number} _post
  95. */
  96. _post : null,
  97. /**
  98. * A jQuery reference to the head element.
  99. *
  100. * @since 1.7
  101. * @access private
  102. * @property {Object} _head
  103. */
  104. _head : null,
  105. /**
  106. * A jQuery reference to the body element.
  107. *
  108. * @since 1.7
  109. * @access private
  110. * @property {Object} _body
  111. */
  112. _body : null,
  113. /**
  114. * An jQuery reference to an image element that is used
  115. * to preload the new CSS file using the onerror hack.
  116. *
  117. * @since 1.7
  118. * @access private
  119. * @property {Object} _loader
  120. */
  121. _loader : null,
  122. /**
  123. * An jQuery reference to the old CSS element.
  124. *
  125. * @since 1.7
  126. * @access private
  127. * @property {Object} _oldCss
  128. */
  129. _oldCss : null,
  130. /**
  131. * An jQuery reference to the new CSS element.
  132. *
  133. * @since 1.7
  134. * @access private
  135. * @property {Object} _newCss
  136. */
  137. _newCss : null,
  138. /**
  139. * An jQuery reference to the old JS element.
  140. *
  141. * @since 1.7
  142. * @access private
  143. * @property {Object} _oldJs
  144. */
  145. _oldJs : null,
  146. /**
  147. * An jQuery reference to the new JS element.
  148. *
  149. * @since 1.7
  150. * @access private
  151. * @property {Object} _newJs
  152. */
  153. _newJs : null,
  154. /**
  155. * An jQuery reference to the old div that holds scripts
  156. * and styles generated by widgets and shortcodes.
  157. *
  158. * @since 1.7
  159. * @access private
  160. * @property {Object} _oldScriptsStyles
  161. */
  162. _oldScriptsStyles : null,
  163. /**
  164. * An jQuery reference to the content element.
  165. *
  166. * @since 1.7
  167. * @access private
  168. * @property {Object} _content
  169. */
  170. _content : null,
  171. /**
  172. * Starts the render by loading the new CSS file.
  173. *
  174. * @since 1.7
  175. * @access private
  176. * @method _init
  177. */
  178. _init: function()
  179. {
  180. // Set the body height so the page doesn't scroll.
  181. this._body.height( this._body.height() );
  182. // Load the new CSS.
  183. if ( this._loader ) {
  184. // Load CSS using modern methods or fallback to the old way.
  185. if ( 'onload' in document.createElement( 'link' ) ) {
  186. this._newCss.on( 'load', $.proxy( this._finish, this ) );
  187. this._addNewCSS();
  188. } else {
  189. this._loader.on( 'error', $.proxy( this._loadNewCSSFallbackComplete, this ) );
  190. this._body.append( this._loader );
  191. }
  192. } else {
  193. // We don't have new CSS, finish the render.
  194. this._finish();
  195. }
  196. },
  197. /**
  198. * Removes the fallback loader, adds the new CSS once it has loaded,
  199. * and sets a quick timeout to finish the render.
  200. *
  201. * @since 1.7
  202. * @access private
  203. * @method _loadNewCSSFallbackComplete
  204. */
  205. _loadNewCSSFallbackComplete: function()
  206. {
  207. // Remove the loader.
  208. this._loader.remove();
  209. // Add the new layout css.
  210. this._addNewCSS();
  211. // Set a quick timeout to ensure the css has taken effect.
  212. setTimeout( $.proxy( this._finish, this ), 250 );
  213. },
  214. /**
  215. * Adds the new CSS once it has been loaded.
  216. *
  217. * @since 2.2
  218. * @access private
  219. * @method _addNewCSS
  220. */
  221. _addNewCSS: function()
  222. {
  223. if ( this._oldCss.length > 0 ) {
  224. this._oldCss.after( this._newCss );
  225. } else {
  226. this._head.append( this._newCss );
  227. }
  228. },
  229. /**
  230. * Finishes the render after the CSS has been loaded.
  231. *
  232. * @since 1.7
  233. * @access private
  234. * @method _finish
  235. */
  236. _finish: function()
  237. {
  238. // Remove the old content and assets.
  239. this._removeOldContentAndAssets();
  240. // Clean the new HTML.
  241. this._cleanNewHTML();
  242. // Clean up the new JS and CSS assets.
  243. this._cleanNewAssets();
  244. // Add the new HTML.
  245. this._addNewHTML();
  246. // Add widget/shortcode JS and CSS assets.
  247. this._addNewScriptsStyles();
  248. // Add the new layout JS.
  249. this._addNewJS();
  250. // Send the layout rendered event.
  251. $( FLBuilder._contentClass ).trigger( 'fl-builder.layout-rendered' );
  252. // Hide the loader.
  253. FLBuilder.hideAjaxLoader();
  254. // Run the callback.
  255. if ( typeof this._callback != 'undefined' ) {
  256. this._callback();
  257. }
  258. // Fire the complete hook.
  259. FLBuilder.triggerHook( 'didRenderLayoutComplete' );
  260. },
  261. /**
  262. * Removes old content and assets from the page.
  263. *
  264. * @since 1.7
  265. * @access private
  266. * @method _removeOldContentAndAssets
  267. */
  268. _removeOldContentAndAssets: function()
  269. {
  270. if ( this._content ) {
  271. this._content.empty();
  272. }
  273. if ( this._oldCss ) {
  274. this._oldCss.remove();
  275. }
  276. if ( this._oldJs ) {
  277. this._oldJs.remove();
  278. }
  279. if ( this._oldScriptsStyles ) {
  280. this._oldScriptsStyles.remove();
  281. }
  282. },
  283. /**
  284. * Removes scripts and styles from _data.html that have been added by
  285. * widgets and shortcodes and adds them to _data.scriptsStyles.
  286. *
  287. * @since 1.7
  288. * @access private
  289. * @method _cleanNewHTML
  290. */
  291. _cleanNewHTML: function()
  292. {
  293. // Only proceed if _data.scriptsStyles is set.
  294. if ( ! this._data.scriptsStyles ) {
  295. return;
  296. }
  297. // Setup vars.
  298. var html = $( '<div>' + this._data.html + '</div>' ),
  299. nodeClass = 'fl-row',
  300. scriptsStyles = this._data.scriptsStyles,
  301. removed = '';
  302. // Get the class of the nodes that should be in data.html.
  303. if ( this._data.partial ) {
  304. if ( 'column-group' == this._data.nodeType ) {
  305. nodeClass = 'fl-col-group';
  306. }
  307. else if ( 'column' == this._data.nodeType ) {
  308. nodeClass = 'fl-col';
  309. }
  310. else {
  311. nodeClass = 'fl-' + this._data.nodeType;
  312. }
  313. }
  314. // Remove elements that shouldn't be in data.html.
  315. html.find( '> *, script' ).each( function() {
  316. if ( ! $( this ).hasClass( nodeClass ) ) {
  317. removed = $( this ).remove();
  318. scriptsStyles += removed[0].outerHTML;
  319. }
  320. });
  321. // Wrap scriptsStyles if we have any content in it.
  322. if ( '' !== scriptsStyles ) {
  323. if ( this._data.partial ) {
  324. scriptsStyles = '<div class="fl-builder-node-scripts-styles" data-node="' + this._data.nodeId + '">' + scriptsStyles + '<div>';
  325. }
  326. else {
  327. scriptsStyles = '<div class="fl-builder-node-scripts-styles">' + scriptsStyles + '<div>';
  328. }
  329. }
  330. // Update the data object.
  331. this._data.html = html.html();
  332. this._data.scriptsStyles = scriptsStyles;
  333. },
  334. /**
  335. * Adds the new HTML to the page.
  336. *
  337. * @since 1.7
  338. * @access private
  339. * @method _addNewHTML
  340. */
  341. _addNewHTML: function()
  342. {
  343. var siblings;
  344. // Add HTML for a partial refresh.
  345. if ( this._data.partial ) {
  346. // If data.nodeParent is present, we have a new node.
  347. if ( this._data.nodeParent ) {
  348. // Get sibling rows.
  349. if ( this._data.nodeParent.hasClass( 'fl-builder-content' ) ) {
  350. siblings = this._data.nodeParent.find( ' > .fl-row' );
  351. }
  352. // Get sibling column groups.
  353. else if ( this._data.nodeParent.hasClass( 'fl-row-content' ) ) {
  354. siblings = this._data.nodeParent.find( ' > .fl-col-group' );
  355. }
  356. // Get sibling columns.
  357. else if ( this._data.nodeParent.hasClass( 'fl-col-group' ) ) {
  358. siblings = this._data.nodeParent.find( ' > .fl-col' );
  359. }
  360. // Get sibling modules.
  361. else {
  362. siblings = this._data.nodeParent.find( ' > .fl-col-group, > .fl-module' );
  363. }
  364. // Filter out any clones created by duplicating.
  365. siblings = siblings.filter( ':not(.fl-builder-node-clone)' );
  366. // Add the new node.
  367. if ( 0 === siblings.length || siblings.length == this._data.nodePosition ) {
  368. this._data.nodeParent.append( this._data.html );
  369. }
  370. else {
  371. siblings.eq( this._data.nodePosition ).before( this._data.html );
  372. }
  373. // Remove node loading placeholder in case we have one.
  374. if ( this._data.nodeId ) {
  375. FLBuilder._removeNodeLoadingPlaceholder( $( '.fl-node-' + this._data.nodeId ) );
  376. }
  377. }
  378. // We must be refreshing an existing node.
  379. else {
  380. this._content.after( this._data.html );
  381. this._content.remove();
  382. }
  383. }
  384. // Add HTML for a full refresh.
  385. else {
  386. this._content.append( this._data.html );
  387. }
  388. // Refresh preview HTML of nodes within other nodes (such as modules in a row) to ensure
  389. // any changes to the nested node are preserved after we've inserted the new HTML.
  390. if ( FLBuilder.preview && this._data.nodeId && this._data.nodeId != FLBuilder.preview.nodeId ) {
  391. var previewNode = $( FLBuilder.preview.classes.node );
  392. var isChild = previewNode.closest( '.fl-node-' + this._data.nodeId ).length;
  393. if ( isChild ) {
  394. previewNode.html( FLBuilder.preview.elements.node.html() );
  395. }
  396. }
  397. },
  398. /**
  399. * Removes unnecessary JS and CSS assets from the layout.
  400. *
  401. * @since 1.7
  402. * @access private
  403. * @method _cleanAssets
  404. */
  405. _cleanNewAssets: function()
  406. {
  407. var nodeId = null,
  408. self = this;
  409. // Remove duplicate assets from _data.html.
  410. this._data.html = this._removeDuplicateAssets( this._data.html );
  411. // Remove duplicate assets from _data.scriptsStyles.
  412. if ( this._data.scriptsStyles && '' !== this._data.scriptsStyles ) {
  413. this._data.scriptsStyles = this._removeDuplicateAssets( this._data.scriptsStyles );
  414. }
  415. // Remove all partial JS and CSS if this is a full render.
  416. if ( ! this._data.partial ) {
  417. $( '#fl-builder-partial-refresh-js' ).remove();
  418. $( '.fl-builder-node-scripts-styles' ).remove();
  419. }
  420. // Else, remove assets that aren't needed.
  421. else {
  422. $( '.fl-builder-node-scripts-styles' ).each( function() {
  423. if ( self._data.html.indexOf( 'fl-node-' + $( this ).data( 'node' ) ) > -1 ) {
  424. $( this ).remove();
  425. }
  426. } );
  427. }
  428. },
  429. /**
  430. * Removes JS and CSS that is already on the page
  431. * from the provided HTML content.
  432. *
  433. * @since 1.7
  434. * @access private
  435. * @method _removeDuplicateAssets
  436. * @param {String} html The HTML content to remove assets from.
  437. * @return {String} The cleaned HTML content.
  438. */
  439. _removeDuplicateAssets: function( html )
  440. {
  441. var cleaned = $( '<div>' + html + '</div>' ),
  442. src = '',
  443. script = null,
  444. href = '',
  445. link = null,
  446. loc = window.location,
  447. origin = loc.protocol + '//' + loc.hostname + ( loc.port ? ':' + loc.port : '' );
  448. // Remove duplicate scripts.
  449. cleaned.find( 'script' ).each( function() {
  450. src = $( this ).attr( 'src' );
  451. if ( 'undefined' != typeof src ) {
  452. src = src.replace( origin, '' );
  453. script = $( 'script[src*="' + src + '"]' );
  454. if ( script.length > 0 ) {
  455. $( this ).remove();
  456. }
  457. }
  458. });
  459. // Remove duplicate links.
  460. cleaned.find( 'link' ).each( function() {
  461. href = $( this ).attr( 'href' );
  462. if ( 'undefined' != typeof href ) {
  463. href = href.replace( origin, '' );
  464. link = $( 'link[href*="' + href + '"]' );
  465. if ( link.length > 0 ) {
  466. $( this ).remove();
  467. }
  468. }
  469. });
  470. return cleaned.html();
  471. },
  472. /**
  473. * Adds the new scripts and styles to the page.
  474. *
  475. * @since 1.7
  476. * @access private
  477. * @method _addNewScriptsStyles
  478. */
  479. _addNewScriptsStyles: function()
  480. {
  481. if ( this._data.scriptsStyles && '' !== this._data.scriptsStyles ) {
  482. this._body.append( this._data.scriptsStyles );
  483. }
  484. },
  485. /**
  486. * Adds the new layout JS to the page.
  487. *
  488. * @since 1.7
  489. * @access private
  490. * @method _addNewJS
  491. */
  492. _addNewJS: function()
  493. {
  494. setTimeout( $.proxy( function() {
  495. if ( this._newJs ) {
  496. this._head.append( this._newJs );
  497. }
  498. }, this ), 50 );
  499. },
  500. /**
  501. * Called when the render has been completed.
  502. *
  503. * @since 1.7
  504. * @access private
  505. * @method _complete
  506. */
  507. _complete: function()
  508. {
  509. FLBuilder._setupEmptyLayout();
  510. FLBuilder._highlightEmptyCols();
  511. FLBuilder._initDropTargets();
  512. FLBuilder._initSortables();
  513. FLBuilder._resizeLayout();
  514. FLBuilder._initMediaElements();
  515. FLBuilderLayout.init();
  516. FLBuilderResponsiveEditing.refreshPreview();
  517. this._body.height( 'auto' );
  518. }
  519. };
  520. })(jQuery);