fl-lightbox.js
11.3 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
(function($){
/**
* Custom lightbox for builder popups.
*
* @class FLLightbox
* @since 1.0
*/
FLLightbox = function(settings)
{
this._init(settings);
this._render();
this._bind();
};
/**
* Closes the lightbox of a child element that
* is passed to this method.
*
* @since 1.0
* @static
* @method closeParent
* @param {Object} child An HTML element or jQuery reference to an element.
*/
FLLightbox.closeParent = function(child)
{
var instanceId = $(child).closest('.fl-lightbox-wrap').attr('data-instance-id');
FLLightbox._instances[instanceId].close();
};
/**
* An object that stores a reference to each
* lightbox instance that is created.
*
* @since 1.0
* @static
* @access private
* @property {Object} _instances
*/
FLLightbox._instances = {};
/**
* Prototype for new instances.
*
* @since 1.0
* @property {Object} prototype
*/
FLLightbox.prototype = {
/**
* A unique ID for this instance that's used to store
* it in the static _instances object.
*
* @since 1.0
* @access private
* @property {String} _id
*/
_id: null,
/**
* A jQuery reference to the main wrapper div.
*
* @since 1.0
* @access private
* @property {Object} _node
*/
_node: null,
/**
* Flag for whether the lightbox is visible or not.
*
* @since 1.0
* @access private
* @property {Boolean} _visible
*/
_visible: false,
/**
* A timeout used to throttle the resize event.
*
* @since 1.0
* @access private
* @property {Object} _resizeTimer
*/
_resizeTimer: null,
/**
* A flag for whether this instance should be
* draggable or not.
*
* @since 1.0
* @access private
* @property {Boolean} _draggable
*/
_draggable: false,
/**
* Default config object.
*
* @since 1.0
* @access private
* @property {Object} _defaults
* @property {String} _defaults.className - A custom classname to add to the wrapper div.
* @property {Boolean} _defaults.destroyOnClose - Flag for whether the instance should be destroyed when closed.
*/
_defaults: {
className: '',
destroyOnClose: false
},
/**
* Opens the lightbox. You can pass new content to this method.
* If no content is passed, the previous content will be shown.
*
* @since 1.0
* @method open
* @param {String} content HTML content to add to the lightbox.
*/
open: function(content)
{
this._node.find('.fl-lightbox').attr( 'style', '' );
this._node.show();
this._visible = true;
if(typeof content !== 'undefined') {
this.setContent(content);
}
else {
this._resize();
}
this.trigger('open');
},
/**
* Closes the lightbox.
*
* @since 1.0
* @method close
*/
close: function()
{
// Temporary fix for link editor not closing since WP 4.5
if ( 'undefined' != typeof tinymce && 'undefined' != typeof tinymce.EditorManager.activeEditor && tinymce.EditorManager.activeEditor != null ) {
tinymce.EditorManager.activeEditor.hide();
}
this._node.hide();
this._visible = false;
this.trigger('close');
if(this._defaults.destroyOnClose) {
this.destroy();
}
},
/**
* Adds HTML content to the lightbox replacing any
* previously added content.
*
* @since 1.0
* @method setContent
* @param {String} content HTML content to add to the lightbox.
*/
setContent: function(content)
{
this._node.find('.fl-lightbox-content').html(content);
this._resize();
},
/**
* Uses the jQuery empty function to remove lightbox
* content and any related events.
*
* @since 1.0
* @method empty
*/
empty: function()
{
this._node.find('.fl-lightbox-content').empty();
this._node.find('.fl-lightbox').removeClass('fl-lightbox-expanded');
},
/**
* Bind an event to the lightbox.
*
* @since 1.0
* @method on
* @param {String} event The type of event to bind.
* @param {Function} callback A callback to fire when the event is triggered.
*/
on: function(event, callback)
{
this._node.on(event, callback);
},
/**
* Unbind an event from the lightbox.
*
* @since 1.0
* @method off
* @param {String} event The type of event to unbind.
*/
off: function(event)
{
this._node.off(event);
},
/**
* Trigger an event on the lightbox.
*
* @since 1.0
* @method trigger
* @param {String} event The type of event to trigger.
* @param {Object} params Additional parameters to pass to the event.
*/
trigger: function(event, params)
{
this._node.trigger(event, params);
},
/**
* Enable or disable dragging for the lightbox.
*
* @since 1.0
* @method draggable
* @param {Boolean} toggle Whether the lightbox should be draggable or not.
*/
draggable: function(toggle)
{
var mask = this._node.find('.fl-lightbox-mask'),
lightbox = this._node.find('.fl-lightbox');
toggle = typeof toggle === 'undefined' ? false : toggle;
if(this._draggable) {
lightbox.draggable('destroy');
}
if(toggle) {
this._unbind();
this._draggable = true;
mask.hide();
lightbox.draggable({
cursor: 'move',
handle: toggle.handle || ''
});
}
else {
mask.show();
this._bind();
this._draggable = false;
}
this._resize();
},
/**
* Destroy the lightbox by removing all elements, events
* and object references.
*
* @since 1.0
* @method destroy
*/
destroy: function()
{
$(window).off('resize.fl-lightbox-' + this._id);
this._node.empty();
this._node.remove();
FLLightbox._instances[this._id] = 'undefined';
try{ delete FLLightbox._instances[this._id]; } catch(e){}
},
/**
* Render the expand/contract of lightbox
*
* @method renderResize
* @param {String}
*/
renderResize: function(method)
{
if(typeof method !== 'undefined') {
var activeNode = this._getActiveNode();
lightbox = activeNode.find('.fl-lightbox'),
boxFields = lightbox.find('.fl-builder-settings-fields'),
win = $(window),
winHeight = win.height(),
winWidth = win.width(),
boxHeaderHeight = lightbox.find('.fl-lightbox-header').height(),
boxTabsHeight = lightbox.find('.fl-builder-settings-tabs').height(),
boxFooterHeight = lightbox.find('.fl-lightbox-footer').height(),
boxFieldHeight = (winHeight - (boxHeaderHeight + boxTabsHeight + boxFooterHeight + 103)),
editor = typeof tinymce !== 'undefined' && tinymce.EditorManager.activeEditor ? tinymce : null,
editorId = editor ? editor.EditorManager.activeEditor.id : 'flhiddeneditor',
editorIframeEl = lightbox.find('#'+ editorId +'_ifr'),
editorTextarea = lightbox.find('#'+ editorId),
codeField = lightbox.find('.fl-code-field .ace_editor');
if(method == 'expand' || method == 'window_resize') {
if(method == 'window_resize' && !lightbox.hasClass('fl-lightbox-expanded')) {
return false;
}
boxFields.css('height', boxFieldHeight + 'px');
if(method == 'expand') {
lightbox.addClass('fl-lightbox-expanded');
lightbox.draggable('disable');
}
if(editorIframeEl.length > 0) {
editorIframeEl.css('height', (boxFieldHeight - 145) + 'px');
}
if(editorTextarea.length > 0) {
editorTextarea.css('height', (boxFieldHeight - 145) + 'px');
}
if(codeField.length > 0) {
codeField.css('height', (boxFieldHeight - 30) + 'px');
}
}
else {
// Contract lightbox
setTimeout($.proxy(this._resize, this), 250);
lightbox.removeClass('fl-lightbox-expanded');
boxFields.removeAttr('style');
if(editorId !== null) {
editorIframeEl.css('height', '232px');
editorTextarea.css('height', '232px');
}
if(codeField.length > 0) {
codeField.css('height', '380px');
}
lightbox.draggable('enable');
}
}
},
/**
* Initialize this lightbox instance.
*
* @since 1.0
* @access private
* @method _init
* @param {Object} settings A setting object for this instance.
*/
_init: function(settings)
{
var i = 0,
prop = null;
for(prop in FLLightbox._instances) {
i++;
}
this._defaults = $.extend({}, this._defaults, settings);
this._id = new Date().getTime() + i;
FLLightbox._instances[this._id] = this;
},
/**
* Renders the main wrapper.
*
* @since 1.0
* @access private
* @method _render
*/
_render: function()
{
this._node = $('<div class="fl-lightbox-wrap" data-instance-id="'+ this._id +'"><div class="fl-lightbox-mask"></div><div class="fl-lightbox"><div class="fl-lightbox-content-wrap"><div class="fl-lightbox-content"></div></div></div></div>');
this._node.addClass(this._defaults.className);
$('body').append(this._node);
},
/**
* Binds events for this instance.
*
* @since 1.0
* @access private
* @method _bind
*/
_bind: function()
{
$(window).on('resize.fl-lightbox-' + this._id, $.proxy(this._delayedResize, this));
},
/**
* Unbinds events for this instance.
*
* @since 1.0
* @access private
* @method _unbind
*/
_unbind: function()
{
$(window).off('resize.fl-lightbox-' + this._id);
},
/**
* Resizes the lightbox after a delay.
*
* @since 1.0
* @access private
* @method _delayedResize
*/
_delayedResize: function()
{
clearTimeout(this._resizeTimer);
this._resizeTimer = setTimeout($.proxy(this._resize, this), 250);
this.renderResize('window_resize');
},
/**
* Resizes the lightbox.
*
* @since 1.0
* @access private
* @method _resize
*/
_resize: function()
{
if(this._visible) {
var lightbox = this._node.find('.fl-lightbox'),
boxHeight = lightbox.height(),
boxWidth = lightbox.width(),
win = $(window),
winHeight = win.height(),
winWidth = win.width(),
top = '0px',
left = ((winWidth - boxWidth)/2 - 30) + 'px';
// Zero out margins and position.
lightbox.css({
'margin' : '0px',
'top' : 'auto',
'left' : 'auto'
});
// Get the top position.
if(winHeight - 80 > boxHeight) {
top = ((winHeight - boxHeight)/2 - 40) + 'px';
}
// Set the positions.
if(this._draggable) {
lightbox.css('top', top);
lightbox.css('left', FLBuilderConfig.isRtl ? '-' + left : left);
}
else {
lightbox.css('margin-top', top);
lightbox.css('margin-left', 'auto');
lightbox.css('margin-right', 'auto');
}
}
},
/**
* Closes the lightbox when the esc key is pressed.
* Currently not in use.
*
* @since 1.0
* @access private
* @method _onKeypress
* @param {Object} e An event object.
*/
_onKeypress: function(e)
{
if(e.which == 27 && this._visible) {
this.close();
}
},
/**
* Get the current active lightbox from multiple instances.
*
* @since 1.0
* @access private
* @method _getActiveNode
* @return {object} Current node
*/
_getActiveNode: function()
{
var activeNode = this._node;
$.each(FLLightbox._instances, function(i, obj){
if($(obj._node).is(':visible')) {
activeNode = $(obj._node);
}
});
return activeNode;
}
};
})(jQuery);