PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 4.7.1.1
Hustle – Email Marketing, Lead Generation, Optins, Popups v4.7.1.1
7.8.14.2 7.8.14 7.8.14.1 7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / js / public.js
wordpress-popup / js Last commit date
ace.js 11 years ago ace.min.js 11 years ago popup-admin.js 11 years ago popup-admin.min.js 11 years ago public.js 11 years ago public.min.js 11 years ago theme-chrome.js 11 years ago theme-chrome.min.js 11 years ago worker-css.js 11 years ago worker-css.min.js 11 years ago
public.js
993 lines
1 /*! PopUp Free - v4.7.11
2 * https://wordpress.org/plugins/wordpress-popup/
3 * Copyright (c) 2015; * Licensed GPLv2+ */
4 /*global window:false */
5 /*global document:false */
6 /*global _popup_data:false */
7 /*jslint evil: true */ // Allows us to keep the `fn = new Function()` line
8
9 ;(function () {
10 var recent_ajax_calls = [],
11 doing_ajax = false;
12
13 var Popup = function( _options ) {
14
15 var me = this,
16 $doc = jQuery( document ),
17 $win = jQuery( window ),
18 $po_div = null,
19 $po_msg = null,
20 $po_close = null,
21 $po_hide = null,
22 $po_move = null,
23 $po_resize = null,
24 $po_img = null,
25 $po_back = null
26 ;
27
28 this.data = {};
29 this.have_popup = false;
30 this.ajax_data = {};
31 this.opened = 0;
32
33 /**
34 * Close PopUp and set the "never see again" flag.
35 */
36 this.close_forever = function close_forever() {
37 var expiry = me.data.expiry || 365;
38
39 me.close_popup();
40 if ( _options['preview'] ) { return false; }
41
42 me.set_cookie( 'po_h', 1, expiry );
43 return false;
44 };
45
46 /**
47 * Close PopUp.
48 * Depending on the "multi_open" flag it can be opened again.
49 */
50 this.close_popup = function close_popup() {
51 jQuery( 'html' ).removeClass( 'has-popup' );
52
53 function close_it() {
54 if ( me.data.display_data['click_multi'] ) {
55 $po_back.hide();
56 $po_div.hide();
57 } else {
58 $po_back.remove();
59 $po_div.remove();
60
61 me.have_popup = false;
62 }
63
64 $doc.trigger( 'popup-closed' );
65 // Legacy trigger.
66 $doc.trigger( 'popover-closed' );
67 }
68
69 if ( me.data.animation_out ) {
70 $po_msg.addClass( me.data.animation_out + ' animated' );
71 $po_msg.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
72 $po_msg.removeClass( 'animated' );
73 $po_msg.removeClass( me.data.animation_out );
74 close_it();
75 });
76 } else {
77 close_it();
78 }
79
80 popup_close( me );
81 return false;
82 };
83
84 /**
85 * When user clicked on the background-layer.
86 */
87 this.background_clicked = function background_clicked( ev ) {
88 var el = jQuery( ev.target );
89
90 if ( el.hasClass( 'wdpu-background' ) ) {
91 if ( ! me.data.overlay_close ) { return; }
92
93 if ( me.data.close_hide ) {
94 me.close_forever();
95 } else {
96 me.close_popup();
97 }
98 }
99 };
100
101 /**
102 * Resize and move the PopUp. Triggered when PopUp is loaded and
103 * window is resized.
104 */
105 this.move_popup = function move_popup() {
106 var new_width, new_height, reduce_el, reduce_w_by = 0, reduce_h_by = 0;
107
108 // Resize, if custom-size is active.
109 if ( me.data.custom_size ) {
110 if ( me.data.height && ! isNaN( me.data.height ) ) {
111 if ( $po_resize.data( 'reduce-height' ) ) {
112 reduce_el = jQuery( $po_resize.data( 'reduce-height' ) );
113 reduce_h_by = reduce_el.outerHeight();
114 }
115 new_height = me.data.height - reduce_h_by;
116 if ( new_height < 100 ) { new_height = 100; }
117 $po_resize.height( new_height );
118 }
119
120 if ( me.data.width && ! isNaN( me.data.width ) ) {
121 if ( $po_resize.data( 'reduce-width' ) ) {
122 reduce_el = jQuery( $po_resize.data( 'reduce-width' ) );
123 reduce_w_by = reduce_el.outerWidth();
124 }
125 new_width = me.data.width - reduce_w_by;
126 if ( new_width < 100 ) { new_width = 100; }
127 $po_resize.width( new_width );
128 }
129 }
130
131 // This function centers the PopUp and the featured image.
132 var update_position = function update_position() {
133 if ( ! $po_move.hasClass( 'no-move-x' ) ) {
134 var win_width = $win.width(),
135 msg_width = $po_msg.outerWidth(),
136 msg_left = (win_width - msg_width) / 2;
137
138 // Move window horizontally.
139 if ( msg_left < 10 ) { msg_left = 10; }
140 $po_move.css({ 'left': msg_left });
141 }
142
143 if ( ! $po_move.hasClass( 'no-move-y' ) ) {
144 var win_height = $win.height(),
145 msg_height = $po_msg.outerHeight(),
146 msg_top = (win_height - msg_height) / 2;
147
148 // Move window vertically.
149 if ( msg_top < 10 ) { msg_top = 10; }
150 $po_move.css({ 'top': msg_top });
151 }
152
153 // Move the image.
154 if ( $po_img.length ) {
155 var offset_x, offset_y,
156 img_width = $po_img.width(),
157 img_height = $po_img.height(),
158 box_width = $po_img.parent().width(),
159 box_height = $po_img.parent().height();
160
161 // Center horizontally.
162 if ( img_width > box_width ) {
163 // Center image.
164 offset_x = (box_width - img_width) / 2;
165 $po_img.css({ 'margin-left': offset_x });
166 } else {
167 // Align image according to layout.
168 $po_img.css({ 'margin-left': 0 });
169 }
170
171 // Center vertially.
172 if ( img_height > box_height ) {
173 // Center image.
174 offset_y = (box_height - img_height) / 2;
175 $po_img.css({ 'margin-top': offset_y });
176 } else {
177 // Align image according to layout.
178 $po_img.css({ 'margin-top': 0 });
179 }
180 }
181 };
182
183 // Short delay before positioning the popup to give the browser time
184 // to show/resize the popup (20ms ~ 1 screen refresh)
185 window.setTimeout(update_position, 20);
186 update_position();
187 };
188
189 /**
190 * Reject the current PopUp: Do not display it.
191 */
192 this.reject = function reject() {
193 me.have_popup = false;
194 me.data = {};
195 };
196
197 /**
198 * Check if the PopUp is ready to be displayed.
199 * If it is ready then it is displayed.
200 */
201 this.prepare = function prepare() {
202 me.fetch_dom();
203
204 // Move the PopUp out of the viewport but make it visible.
205 // This way the browser will start to render the contents and there
206 // will be no delay when the PopUp is made visible later.
207 $po_div.css({
208 'opacity': 0,
209 'z-index': -1,
210 'position': 'fixed',
211 'left': -1000,
212 'width': 100,
213 'right': 'auto',
214 'top': -1000,
215 'height': 100,
216 'bottom': 'auto'
217 }).show();
218
219 $doc.trigger( 'popup-init', [me, me.data] );
220
221 if ( me.have_popup ) {
222 switch ( me.data.display ) {
223 case 'scroll':
224 $win.on( 'scroll', me.show_at_position );
225 break;
226
227 case 'anchor':
228 $win.on( 'scroll', me.show_at_element );
229 break;
230
231 case 'delay':
232 var delay = me.data.display_data.delay * 1000;
233 if ( 'm' === me.data.display_data.delay_type ) {
234 delay *= 60;
235 }
236
237 window.setTimeout( function() {
238 popup_open( me );
239 }, delay );
240 break;
241
242 default:
243 // A custom action will show the PopUp (e.g. click/leave)
244 window.setTimeout(function() {
245 if ( 'function' === typeof me.custom_handler ) {
246 me.custom_handler( me );
247 }
248 }, 20);
249 }
250
251 } else {
252 // PopUp was rejected during popup-init event. Do not display.
253 }
254 };
255
256 /**
257 * Observe the scroll-top to trigger the PopUp.
258 */
259 this.show_at_position = function show_at_position( ev ) {
260 var height, perc,
261 el = jQuery( this ),
262 top = el.scrollTop();
263
264 if ( 'px' === me.data.display_data.scroll_type ) {
265 if ( top >= me.data.display_data.scroll ) {
266 $win.off( 'scroll', me.show_at_position );
267 popup_open( me );
268 }
269 } else { // this handles '%'
270 height = $doc.height() - $win.height();
271 perc = 100 * top / height;
272
273 if ( perc >= me.data.display_data.scroll ) {
274 $win.off( 'scroll', me.show_at_position );
275 popup_open( me );
276 }
277 }
278 };
279
280 /**
281 * Tests if a specific HTML element is visible to trigger the PopUp.
282 * We intentionally calculate el_top every time this function is called
283 * because the element may be hidden or not present at page load.
284 */
285 this.show_at_element = function show_at_element( ev ) {
286 var anchor = jQuery( me.data.display_data.anchor ),
287 view_top = $win.scrollTop(),
288 view_bottom = view_top + $win.height(),
289 el_top = anchor.offset().top,
290 offset = view_bottom - el_top;
291
292 // When 10px of the element are visible show the PopUp.
293 if ( offset > 10 ) {
294 $win.off( 'scroll', me.show_at_element );
295 popup_open( me );
296 }
297 };
298
299 /**
300 * Can be used from custom_handler() to make a Popup visible.
301 */
302 this.show_popup = function show_popup() {
303 popup_open( me );
304
305 // Prevent default action when user attached this to a click event.
306 return false;
307 };
308
309 /**
310 * Display the PopUp!
311 * This function is called by popup_open() below!!!
312 */
313 this._show = function _show() {
314 var count;
315
316 // If for some reason the popup container is missing then exit.
317 if ( ! $po_div.length ) {
318 return false;
319 }
320
321 count = parseInt( me.get_cookie('po_c'), 10 );
322 if ( isNaN( count ) ) { count = 0; }
323 me.set_cookie( 'po_c', count + 1, 365 );
324
325 me.opened += 1;
326 $po_back.on( 'click', me.background_clicked );
327
328 $win.off("resize.popup")
329 .on("resize.popup", function () { me.move_popup(me.data); });
330
331 $po_div.removeAttr( 'style' ).show();
332 $po_back.show();
333
334 if ( me.data.scroll_body ) {
335 jQuery( 'html' ).addClass( 'has-popup can-scroll' );
336 } else {
337 jQuery( 'html' ).addClass( 'has-popup no-scroll' );
338 }
339
340 // Fix issue where Buttons are not available in Chrome
341 // https://app.asana.com/0/11388810124414/18688920614102
342 $po_msg.hide();
343 window.setTimeout(function() {
344 // The timer is so short that the element will *not* be hidden
345 // but webkit will still redraw the element.
346 $po_msg.show();
347 }, 2);
348
349 me.move_popup(me.data);
350 me.setup_popup();
351
352 // Disables the CSS animation is browser does not support them.
353 me.prepare_animation();
354
355 if ( me.data.animation_in ) {
356 $po_msg.addClass( me.data.animation_in + ' animated' );
357 $po_msg.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
358 $po_msg.removeClass( 'animated' );
359 $po_msg.removeClass( me.data.animation_in );
360 });
361 }
362
363 return true;
364 };
365
366 this.prepare_animation = function prepare_animation() {
367 var can_animate = false,
368 domPrefixes = 'Webkit Moz O ms Khtml'.split(' ');
369
370 if ( $po_msg[0].style.animationName !== undefined ) { can_animate = true; }
371
372 if ( can_animate === false ) {
373 for ( var i = 0; i < domPrefixes.length; i++ ) {
374 if ( $po_msg[0].style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
375 can_animate = true;
376 break;
377 }
378 }
379 }
380
381 if ( ! can_animate ) {
382 // Sorry guys, CSS animations are not supported...
383 me.data.animation_in = '';
384 me.data.animation_out = '';
385 }
386 };
387
388 /**
389 * Add event handlers to the PopUp controls.
390 */
391 this.setup_popup = function setup_popup() {
392 $po_hide.off( 'click', me.close_forever )
393 .on( 'click', me.close_forever );
394
395 if ( me.data && me.data.close_hide ) {
396 $po_close.off( 'click', me.close_forever )
397 .on( 'click', me.close_forever );
398
399 $po_msg.off( 'click', '.close', me.close_forever )
400 .on( 'click', '.close', me.close_forever );
401 } else {
402 $po_close.off( 'click', me.close_popup )
403 .on( 'click', me.close_popup );
404
405 $po_msg.off( 'click', '.close', me.close_popup )
406 .on( 'click', '.close', me.close_popup );
407 }
408
409 $po_msg.hover(function() {
410 jQuery( '.claimbutton' ).removeClass( 'hide' );
411 }, function() {
412 jQuery( '.claimbutton' ).addClass( 'hide' );
413 });
414
415 $doc.trigger( 'popup-displayed', [me.data, me] );
416 // Legacy trigger.
417 $doc.trigger( 'popover-displayed', [me.data, me] );
418
419 $po_div.off( 'submit', 'form', me.form_submit )
420 .on( 'submit', 'form', me.form_submit );
421 };
422
423
424 /*----- Dynamically load PopUps ------*/
425
426
427 /**
428 * Finds the PopUp DOM elements and stores them in protected member
429 * variables for easy access.
430 */
431 this.fetch_dom = function fetch_dom() {
432 // The top container of the PopUp.
433 $po_div = jQuery( '#' + me.data['html_id'] );
434
435 // Reject this PopUp if the HTML element is missing.
436 if ( ! $po_div.length ) { me.reject(); }
437
438 // The container that should be resized (custom size).
439 $po_resize = $po_div.find( '.resize' );
440
441 // The container that should be moved (centered on screen).
442 $po_move = $po_div.find( '.move' );
443
444 // The container that holds the message:
445 // For new styles this is same as $po_resize.
446 // For old popup styles this is a different contianer...
447 $po_msg = $po_div.find( '.wdpu-msg' );
448
449 // Close button.
450 $po_close = $po_div.find( '.wdpu-close' );
451
452 // Hide forever button.
453 $po_hide = $po_div.find( '.wdpu-hide-forever' );
454
455 // Featured image.
456 $po_img = $po_div.find( '.wdpu-image > img' );
457
458 // The modal background.
459 if ( $po_div.hasClass( 'wdpu-background' ) ) {
460 $po_back = $po_div;
461 } else {
462 $po_back = $po_div.find( '.wdpu-background' );
463
464 if ( ! $po_back.length ) {
465 $po_back = $po_div.parents( '.wdpu-background' );
466 }
467 }
468
469 if ( ! $po_move.length ) { $po_move = $po_div; }
470 if ( ! $po_resize.length ) { $po_resize = $po_div; }
471 };
472
473 /**
474 * Load popup data via ajax.
475 */
476 this.load_popup = function load_popup( id, data ) {
477 if ( undefined === id && _options.preview ) { return; }
478 me.have_popup = false; // This object cannot display a PopUp...
479 load_popups( _options, id, data );
480 };
481
482 /**
483 * A form inside the PopUp is submitted.
484 */
485 this.form_submit = function form_submit( ev ) {
486 var tmr_check, duration, frame, form = jQuery( this ),
487 popup = form.parents( '.wdpu-container' ).first(),
488 msg = popup.find( '.wdpu-msg' ),
489 inp_popup = jQuery( '<input type="hidden" name="_po_method_" />' ),
490 po_id = '.wdpu-' + me.data.popup_id,
491 iteration;
492
493 if ( ! popup.length ) { return true; }
494
495 // Frequently checks the loading state of the hidden iframe.
496 function check_state() {
497 var is_done = false;
498
499 if ( 'complete' === frame[0].contentDocument.readyState &&
500 ! doing_ajax
501 ) {
502 is_done = true;
503 } else {
504 iteration += 1; // 20 iterations is equal to 1 second.
505
506 // 200 iterations are 10 seconds.
507 if ( iteration > 200 ) {
508 $doc.trigger( 'popup-submit-timeout', [me, me.data] );
509 is_done = true;
510 }
511 }
512
513 if ( is_done ) {
514 window.clearInterval( tmr_check );
515 process_document();
516 }
517 }
518
519 // Closes the popup
520 function do_close_popup( close_it ) {
521 if ( undefined !== close_it ) {
522 me.data.close_popup = close_it;
523 }
524
525 if ( recent_ajax_calls ) {
526 me.data.ajax_history = recent_ajax_calls;
527
528 if ( recent_ajax_calls.length ) {
529 me.data.last_ajax = recent_ajax_calls[0];
530 }
531 } else {
532 me.data.ajax_history = [];
533 me.data.last_ajax = {};
534 }
535
536 $doc.trigger( 'popup-submit-done', [me, me.data] );
537
538 if ( me.data.close_popup ) {
539 me.close_popup();
540 return true;
541 } else {
542 return false;
543 }
544 }
545
546 // Replaces the popup contents with some new contents.
547 function do_replace_contents( contents, title, subtitle ) {
548 var new_content = contents,
549 el_inner = popup.find( '.wdpu-msg-inner' ),
550 el_title = popup.find( '.wdpu-title' ),
551 el_subtitle = popup.find( '.wdpu-subtitle' );
552
553 if ( ! ( new_content instanceof jQuery ) ) {
554 new_content = jQuery( '<div></div>' ).html( contents );
555 }
556
557 if ( new_content instanceof jQuery ) {
558 if ( new_content.hasClass( 'wdpu-msg-inner' ) ) {
559 el_inner.replaceWith( new_content );
560 } else {
561 el_inner.find( '.wdpu-content' )
562 .empty()
563 .append( new_content );
564 }
565 }
566
567 if ( undefined !== title ) {
568 el_title.html( title );
569 }
570 if ( undefined !== subtitle ) {
571 el_subtitle.html( subtitle );
572 }
573
574 me.move_popup();
575 me.setup_popup();
576
577 do_close_popup();
578
579 me.fetch_dom();
580 me.setup_popup();
581
582 // Re-initialize the local DOM cache.
583 $doc.trigger( 'popup-init', [me, me.data] );
584 }
585
586 // Executed once the iframe is fully loaded.
587 // This will remove the loading animation and update the popup
588 // contents if required.
589 function process_document() {
590 var inner_new, inner_old, html, external, close_on_fail;
591
592 // Allow other javascript functions to pre-process the event.
593 $doc.trigger( 'popup-submit-process', [frame, me, me.data] );
594
595 /*
596 * Use the event jQuery('document').on('popup-submit-process')
597 * to set `data.form_submit = false` to prevent form handling.
598 */
599 if ( ! me.data.form_submit ) { return false; }
600
601 if ( 'ignore' === me.data.form_submit ) {
602 close_on_fail = false;
603 } else {
604 close_on_fail = true;
605 }
606
607 try {
608 // grab the HTML from the body, using the raw DOM node (frame[0])
609 // and more specifically, it's `contentDocument` property.
610 html = jQuery( po_id, frame[0].contentDocument );
611 external = me.data.did_ajax;
612 } catch ( err ) {
613 // In case the iframe link was an external website the above
614 // line will most likely cause a security issue.
615 html = jQuery( '<html></html>' );
616 external = true;
617 }
618
619 me.data.close_popup = false;
620 msg.removeClass( 'wdpu-loading' );
621
622 // Get the new and old Popup Contents.
623 inner_new = html.find( '.wdpu-msg-inner' );
624 inner_old = popup.find( '.wdpu-msg-inner' );
625
626 // remove the temporary iframe.
627 jQuery( "#wdpu-frame" ).remove();
628 me.data.last_ajax = undefined;
629
630 if ( 'close' === me.data.form_submit ) {
631 // =========================================================
632 // Admin defined to close this popup after every form submit
633
634 do_close_popup( true );
635
636 } else if ( me.data.new_content ) {
637 // =========================================================
638 // Popup contents were explicitely defined by the javascript
639 // event 'popup-submit-process'
640
641 do_replace_contents(
642 me.data.new_content,
643 me.data.new_title,
644 me.data.new_subtitle
645 );
646
647 } else if ( external ) {
648 // =========================================================
649 // For external pages we have no access to the response:
650 // Close the popup!
651
652 // E.g. Contact Form 7
653
654 do_close_popup( close_on_fail );
655
656 } else if ( ! html.length || ! html.html().length ) {
657 // =========================================================
658 // The PopUp is completely empty, possibly another
659 // ajax-handler did process the form. Keep the popup open.
660
661 // E.g. Gravity Forms
662
663 do_close_popup( true );
664
665 } else if ( ! inner_old.length || ! inner_new.length || ! inner_new.text().length ) {
666 // =========================================================
667 // A new page was loaded that does not contain new content
668 // for the current PopUp. Close the popup!
669
670 do_close_popup( close_on_fail );
671
672 } else {
673 // =========================================================
674 // Update the Popup contents.
675
676 do_replace_contents( inner_new );
677
678 }
679 }
680 // end of process_document()
681
682 if ( 'redirect' !== me.data.form_submit ) {
683 // Only change the form target when NOT redirecting
684 frame = jQuery( '<iframe id="wdpu-frame" name="wdpu-frame"></iframe>' )
685 .hide()
686 .appendTo( 'body' );
687
688 // Set form target to the hidden frame.
689 form.attr( 'target', 'wdpu-frame' );
690 inp_popup.appendTo( form ).val( 'raw' );
691 }
692
693 msg.addClass( 'wdpu-loading' );
694
695 if ( 'redirect' === me.data.form_submit ) {
696 /**
697 * When redirecting always close the popup
698 */
699 window.setTimeout(function() {
700 me.close_popup();
701 }, 10);
702 } else {
703 if ( doing_ajax ) {
704 // E.g. Contact Form 7
705 me.data.did_ajax = true;
706 iteration = 0;
707 tmr_check = window.setInterval( check_state, 50 );
708 } else {
709 // E.g. Gravity Forms
710 me.data.did_ajax = false;
711 frame.load( process_document );
712 }
713 }
714
715 return true;
716 };
717
718
719 /*----- Init ------*/
720
721
722 this.init = function init() {
723 if ( ! _options['popup'] ) {
724 me.load_popup();
725 } else {
726 me.have_popup = true;
727 me.data = _options['popup'];
728 me.exec_scripts();
729 me.prepare();
730 }
731 };
732
733 /**
734 * If the popup provides custom javascript code we execute it here.
735 * Custom script might be provided by rules that are executed in JS such
736 * as the window-width rule or on-click behavior.
737 */
738 this.exec_scripts = function exec_scripts() {
739 var fn;
740 if ( undefined !== me.data.script ) {
741 fn = new Function( 'me', me.data.script );
742 fn( me );
743 }
744 };
745
746
747 /*======================================*\
748 ==========================================
749 == ==
750 == HELPER FUNCTIONS ==
751 == ==
752 ==========================================
753 \*======================================*/
754
755
756 // Get a cookie value.
757 this.get_cookie = function get_cookie( name ) {
758 var i, c, cookie_name, value,
759 ca = document.cookie.split( ';' );
760
761 if ( me.data && me.data.popup_id ) {
762 cookie_name = name + '-' + me.data.popup_id + "=";
763 } else {
764 cookie_name = name + "=";
765 }
766
767 for ( i = 0; i < ca.length; i += 1 ) {
768 c = ca[i];
769 while ( c.charAt(0) === ' ' ) {
770 c = c.substring( 1, c.length );
771 }
772 if (c.indexOf( cookie_name ) === 0 ) {
773 return c.substring( cookie_name.length, c.length );
774 }
775 }
776 return null;
777 };
778
779 // Saves the value into a cookie.
780 this.set_cookie = function set_cookie( name, value, days ) {
781 var date, expires, cookie_name;
782
783 if ( _options['preview'] ) { return; }
784
785 if ( ! isNaN( days ) ) {
786 date = new Date();
787 date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
788 expires = "; expires=" + date.toGMTString();
789 } else {
790 expires = "";
791 }
792
793 if ( me.data && me.data.popup_id ) {
794 cookie_name = name + '-' + me.data.popup_id;
795 } else {
796 cookie_name = name;
797 }
798
799 document.cookie = cookie_name + "=" + value + expires + "; path=/";
800 };
801
802 /*----- Finished ------*/
803
804 // Only expose the "init" and "load" functions of the PopUp.
805 return {
806 init: me.init,
807 load: me.load_popup,
808 extend: me
809 };
810 };
811
812 // Local variables used by popup_open() and popup_close()
813 var po_status = 'closed',
814 po_queue = [],
815 po_current = null;
816
817 /**
818 * Either opens the PopUp or enqueues it to be opened as soon as the current
819 * PopUp is closed.
820 */
821 function popup_open( popup ) {
822 if ( 'closed' === po_status ) {
823 if ( popup._show() ) {
824 po_current = popup;
825 po_status = 'open';
826 } else {
827 // The PopUp could not be opened due to some error...
828 popup_close( popup );
829 }
830 } else {
831 po_queue[po_queue.length] = popup;
832 }
833 }
834
835 /**
836 * Marks the specified PopUp as closed and opens the next enqueued PopUp.
837 */
838 function popup_close( popup ) {
839 po_status = 'closed';
840 po_current = null;
841
842 // PopUp was closed, check if there is another PopUp in open-queue.
843 if ( po_queue.length > 0 ) {
844 var item = po_queue.shift();
845 popup_open( item );
846 }
847 }
848
849 /**
850 * Load popup data via ajax.
851 * This function will create new Popup objects.
852 */
853 function load_popups( options, id, data ) {
854 var ajax_args, ajax_data,
855 po_id = 0,
856 thefrom = str_reverse( window.location.toString() ),
857 thereferrer = str_reverse( document.referrer.toString() ),
858 the_data = null;
859
860 var handle_done = function handle_done( data ) {
861 the_data = jQuery.extend( {}, options );
862 the_data.popup = data;
863
864 // This will create new Popup objects.
865 initialize( the_data );
866 };
867
868 // Legacy: force_popover = load a popup_id by ID.
869 if ( window.force_popover !== undefined ) {
870 po_id = window.force_popover.toString();
871 }
872
873 // New way of specifying popup ID is via param: load(id)
874 if ( id !== undefined ) {
875 po_id = id.toString();
876 }
877
878 options['ajax_data'] = options['ajax_data'] || {};
879 ajax_data = jQuery.extend( {}, options['ajax_data'] );
880
881 ajax_data['action'] = 'inc_popup';
882 ajax_data['do'] = options['do'];
883 ajax_data['thefrom'] = thefrom;
884 ajax_data['thereferrer'] = thereferrer;
885
886 if ( po_id ) { ajax_data['po_id'] = po_id; }
887 if ( data ) { ajax_data['data'] = data; }
888 if ( options['preview'] ) { ajax_data['preview'] = true; }
889
890 ajax_args = {
891 url: options['ajaxurl'],
892 dataType: 'jsonp',
893 jsonpCallback: 'po_data',
894 data: ajax_data,
895 success: function( data ) {
896 handle_done( data );
897 },
898 complete: function() {
899 jQuery( document ).trigger( 'popup-load-done', [the_data] );
900 }
901 };
902
903 return jQuery.ajax(ajax_args);
904 }
905
906 /**
907 * Create and initialize new Popup objects
908 */
909 function initialize( popup_data ) {
910 if ( undefined === popup_data ) { return; }
911
912 // Initlize a single PopUp.
913 var spawn_popup = function spawn_popup( data ) {
914 if ( undefined === data ) { return; }
915
916 // Insert the PopUp CSS and HTML as hidden elements into the DOM.
917 if ( undefined !== data.popup && undefined !== data.popup['html'] ) {
918 jQuery( '<style type="text/css">' + data.popup['styles'] + '</style>' )
919 .appendTo('head');
920
921 jQuery( data.popup['html'] )
922 .appendTo('body')
923 .hide();
924 }
925
926 // inc_popup .. the most recently created Popup object.
927 window.inc_popup = new Popup( data );
928
929 // inc_popups .. collection of all Popup objects on current page.
930 window.inc_popups[window.inc_popups.length] = window.inc_popup;
931
932 jQuery( document ).trigger( 'popup-initialized', [window.inc_popup] );
933
934 if ( data['noinit'] || data['preview'] ) { return; }
935 window.inc_popup.init();
936 };
937
938 // Initialize a single or multiple PopUps, depending on provided data.
939 if ( popup_data.popup instanceof Array ) {
940 for ( var i = 0; i < popup_data.popup.length; i += 1 ) {
941 var data = jQuery.extend( {}, popup_data );
942 data.popup = popup_data.popup[i];
943 spawn_popup( data );
944 }
945 } else if ( popup_data instanceof Object ) {
946 spawn_popup( popup_data );
947 }
948 }
949
950 // Reverse a string preserving utf characters
951 function str_reverse( str ) {
952 var charArray = [];
953
954 for (var i = 0; i < str.length; i++) {
955 if ( i + 1 < str.length ) {
956 var value = str.charCodeAt( i );
957 var nextValue = str.charCodeAt(i+1);
958 if ( ( value >= 0xD800 && value <= 0xDBFF &&
959 (nextValue & 0xFC00) === 0xDC00) || // Surrogate pair
960 (nextValue >= 0x0300 && nextValue <= 0x036F) // Combining marks
961 ) {
962 charArray.unshift( str.substring(i, i+2) );
963 i++; // Skip the other half
964 continue;
965 }
966 }
967
968 // Otherwise we just have a rogue surrogate marker or a plain old character.
969 charArray.unshift( str[i] );
970 }
971
972 return charArray.join( '' );
973 }
974
975 // Store flag whether ajax request is processed
976 jQuery( document ).ajaxStart(function(ev) {
977 doing_ajax = true;
978 });
979
980 // Store all Ajax responses
981 jQuery( document ).ajaxComplete(function(ev, jqXHR, settings) {
982 doing_ajax = false;
983 recent_ajax_calls.unshift( jqXHR );
984 });
985
986 // Initialize the PopUp one the page is loaded.
987 jQuery(function() {
988 window.inc_popups = [];
989 initialize( _popup_data );
990 });
991
992 })();
993