PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.14
Boxzilla – WordPress Popup Builder v3.2.14
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / assets / browserify / script.js

script.js in Boxzilla – WordPress Popup Builder 3.2.14, at assets/browserify/script.js

140 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function() {
2 'use strict';
3
4 var Boxzilla = require('boxzilla');
5 var options = window.boxzilla_options;
6
7 // expose Boxzilla object to window
8 window.Boxzilla = Boxzilla;
9
10 // helper function for setting CSS styles
11 function css(element, styles) {
12 if( styles.background_color ) {
13 element.style.background = styles.background_color;
14 }
15
16 if( styles.color ) {
17 element.style.color = styles.color;
18 }
19
20 if( styles.border_color ) {
21 element.style.borderColor = styles.border_color;
22 }
23
24 if( styles.border_width ) {
25 element.style.borderWidth = parseInt(styles.border_width) + "px";
26 }
27
28 if( styles.border_style ) {
29 element.style.borderStyle = styles.border_style;
30 }
31
32 if( styles.width ) {
33 element.style.maxWidth = parseInt(styles.width) + "px";
34 }
35 }
36
37 function createBoxesFromConfig() {
38
39 // failsafe against including script twice.
40 if( options.inited ) {
41 return;
42 }
43
44 // create boxes from options
45 for(var key in options.boxes) {
46
47 // get opts
48 var boxOpts = options.boxes[key];
49 boxOpts.testMode = isLoggedIn && options.testMode;
50
51 // find box content element, bail if not found
52 var boxContentElement = document.getElementById('boxzilla-box-'+ boxOpts.id +'-content');
53 if( ! boxContentElement) {
54 continue;
55 }
56
57 // use element as content option
58 boxOpts.content = boxContentElement;
59
60 // create box
61 var box = Boxzilla.create(boxOpts.id, boxOpts);
62
63 // add box slug to box element as classname
64 box.element.className = box.element.className + ' boxzilla-' + boxOpts.post.slug;
65
66 // add custom css to box
67 css(box.element, boxOpts.css);
68
69 try {
70 box.element.firstChild.firstChild.className += " first-child";
71 box.element.firstChild.lastChild.className += " last-child";
72 } catch(e) {}
73
74 // maybe show box right away
75 if( box.fits() && locationHashRefersBox(box) ) {
76 box.show();
77 }
78 }
79
80 // set flag to prevent initialising twice
81 options.inited = true;
82
83 // trigger "done" event.
84 Boxzilla.trigger('done');
85
86 // maybe open box with MC4WP form in it
87 maybeOpenMailChimpForWordPressBox();
88 }
89
90 function locationHashRefersBox(box) {
91 if( ! window.location.hash || 0 === window.location.hash.length ) {
92 return false;
93 }
94
95 // parse "boxzilla-{id}" from location hash
96 const match = window.location.hash.match(/[#&](boxzilla-\d+)/);
97 if (!match || typeof(match) !== "object" || match.length < 2) {
98 return false;
99 }
100
101 const elementId = match[1];
102 if( elementId === box.element.id ) {
103 return true;
104 } else if( box.element.querySelector('#' + elementId) ) {
105 return true;
106 }
107
108 return false;
109 }
110
111 function maybeOpenMailChimpForWordPressBox() {
112 if( typeof(window.mc4wp_forms_config) !== "object" || ! window.mc4wp_forms_config.submitted_form ) {
113 return;
114 }
115
116 var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id;
117 var boxes = Boxzilla.boxes;
118 for( var boxId in boxes ) {
119 if(!boxes.hasOwnProperty(boxId)) { continue; }
120 var box = boxes[boxId];
121 if( box.element.querySelector(selector)) {
122 box.show();
123 return;
124 }
125 }
126 }
127
128 // print message when test mode is enabled
129 var isLoggedIn = document.body.className.indexOf('logged-in') > -1;
130 if( isLoggedIn && options.testMode ) {
131 console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' );
132 }
133
134 // init boxzilla
135 Boxzilla.init();
136
137 // on window.load, create DOM elements for boxes
138 window.addEventListener('load', createBoxesFromConfig);
139 })();
140