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