PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.21
Boxzilla – WordPress Popup Builder v3.2.21
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
← All changes | assets/js/script.js +919 -862 3.03.2.21 View file →
@@ -1,1065 +1,1122 @@
1 -(function () { var require = undefined; var define = undefined; (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2 -'use strict';
1 +(function () { var require = undefined; var module = undefined; var exports = undefined; var define = undefined; (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2 +"use strict";
3 3
4 -var Boxzilla = require('boxzilla');
5 -var options = window.boxzilla_options;
6 -var isLoggedIn = document.body.className.indexOf('logged-in') > -1;
4 +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
7 5
8 -// print message when test mode is enabled
9 -if( isLoggedIn && options.testMode ) {
10 - console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' );
11 -}
6 +(function () {
7 + var Boxzilla = require('boxzilla');
12 8
13 -// init boxzilla
14 -Boxzilla.init();
9 + var options = window.boxzilla_options; // helper function for setting CSS styles
15 10
16 -// create boxes from options
17 -for( var i=0; i < options.boxes.length; i++ ) {
18 - // get opts
19 - var boxOpts = options.boxes[i];
20 - boxOpts.testMode = isLoggedIn && options.testMode;
11 + function css(element, styles) {
12 + if (styles.background_color) {
13 + element.style.background = styles.background_color;
14 + }
21 15
22 - // create box
23 - var box = Boxzilla.create( boxOpts.id, boxOpts);
24 -
25 - // add custom css to box
26 - css(box.element, boxOpts.css);
27 -}
16 + if (styles.color) {
17 + element.style.color = styles.color;
18 + }
28 19
29 -function css(element, styles) {
30 - if( styles.background_color ) {
31 - element.style.background = styles.background_color;
20 + if (styles.border_color) {
21 + element.style.borderColor = styles.border_color;
32 22 }
33 23
34 - if( styles.color ) {
35 - element.style.color = styles.color;
24 + if (styles.border_width) {
25 + element.style.borderWidth = parseInt(styles.border_width) + 'px';
36 26 }
37 27
38 - if( styles.border_color ) {
39 - element.style.borderColor = styles.border_color;
28 + if (styles.border_style) {
29 + element.style.borderStyle = styles.border_style;
40 30 }
41 31
42 - if( styles.border_width ) {
43 - element.style.borderWidth = parseInt(styles.border_width) + "px";
32 + if (styles.width) {
33 + element.style.maxWidth = parseInt(styles.width) + 'px';
44 34 }
35 + }
45 36
46 - if( styles.border_style ) {
47 - element.style.borderStyle = styles.border_style;
37 + function createBoxesFromConfig() {
38 + // failsafe against including script twice.
39 + if (options.inited) {
40 + return;
41 + } // create boxes from options
42 +
43 +
44 + for (var key in options.boxes) {
45 + // get opts
46 + var boxOpts = options.boxes[key];
47 + boxOpts.testMode = isLoggedIn && options.testMode; // find box content element, bail if not found
48 +
49 + var boxContentElement = document.getElementById('boxzilla-box-' + boxOpts.id + '-content');
50 +
51 + if (!boxContentElement) {
52 + continue;
53 + } // use element as content option
54 +
55 +
56 + boxOpts.content = boxContentElement; // create box
57 +
58 + var box = Boxzilla.create(boxOpts.id, boxOpts); // add box slug to box element as classname
59 +
60 + box.element.className = box.element.className + ' boxzilla-' + boxOpts.post.slug; // add custom css to box
61 +
62 + css(box.element, boxOpts.css);
63 +
64 + try {
65 + box.element.firstChild.firstChild.className += ' first-child';
66 + box.element.firstChild.lastChild.className += ' last-child';
67 + } catch (e) {} // maybe show box right away
68 +
69 +
70 + if (box.fits() && locationHashRefersBox(box)) {
71 + box.show();
72 + }
73 + } // set flag to prevent initialising twice
74 +
75 +
76 + options.inited = true; // trigger "done" event.
77 +
78 + Boxzilla.trigger('done'); // maybe open box with MC4WP form in it
79 +
80 + maybeOpenMailChimpForWordPressBox();
81 + }
82 +
83 + function locationHashRefersBox(box) {
84 + if (!window.location.hash || window.location.hash.length === 0) {
85 + return false;
86 + } // parse "boxzilla-{id}" from location hash
87 +
88 +
89 + var match = window.location.hash.match(/[#&](boxzilla-\d+)/);
90 +
91 + if (!match || _typeof(match) !== 'object' || match.length < 2) {
92 + return false;
48 93 }
49 94
50 - if( styles.width ) {
51 - element.style.maxWidth = parseInt(styles.width) + "px";
95 + var elementId = match[1];
96 +
97 + if (elementId === box.element.id) {
98 + return true;
99 + } else if (box.element.querySelector('#' + elementId)) {
100 + return true;
52 101 }
102 +
103 + return false;
104 + }
105 +
106 + function maybeOpenMailChimpForWordPressBox() {
107 + if ((_typeof(window.mc4wp_forms_config) !== 'object' || !window.mc4wp_forms_config.submitted_form) && _typeof(window.mc4wp_submitted_form) !== 'object') {
108 + return;
109 + }
110 +
111 + var form = window.mc4wp_submitted_form || window.mc4wp_forms_config.submitted_form;
112 + var selector = '#' + form.element_id;
113 + Boxzilla.boxes.forEach(function (box) {
114 + if (box.element.querySelector(selector)) {
115 + box.show();
116 + }
117 + });
118 + } // print message when test mode is enabled
119 +
120 +
121 + var isLoggedIn = document.body.className.indexOf('logged-in') > -1;
122 +
123 + if (isLoggedIn && options.testMode) {
124 + console.log('Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.');
125 + } // init boxzilla
126 +
127 +
128 + Boxzilla.init(); // on window.load, create DOM elements for boxes
129 +
130 + window.addEventListener('load', createBoxesFromConfig);
131 +})();
132 +
133 +},{"boxzilla":4}],2:[function(require,module,exports){
134 +"use strict";
135 +
136 +var duration = 320;
137 +
138 +function css(element, styles) {
139 + for (var property in styles) {
140 + if (!styles.hasOwnProperty(property)) {
141 + continue;
142 + }
143 +
144 + element.style[property] = styles[property];
145 + }
53 146 }
54 147
55 -window.Boxzilla = Boxzilla;
56 -},{"boxzilla":3}],2:[function(require,module,exports){
57 -'use strict';
148 +function initObjectProperties(properties, value) {
149 + var newObject = {};
58 150
59 -var $ = window.jQuery,
60 - defaults = {
61 - 'animation': 'fade',
62 - 'rehide': false,
63 - 'content': '',
64 - 'cookieTime': 0,
65 - 'icon': '&times',
66 - 'minimumScreenWidth': 0,
67 - 'position': 'center',
68 - 'testMode': false,
69 - 'trigger': false,
70 - 'closable': true
71 - }, Boxzilla;
151 + for (var i = 0; i < properties.length; i++) {
152 + newObject[properties[i]] = value;
153 + }
72 154
155 + return newObject;
156 +}
157 +
158 +function copyObjectProperties(properties, object) {
159 + var newObject = {};
160 +
161 + for (var i = 0; i < properties.length; i++) {
162 + newObject[properties[i]] = object[properties[i]];
163 + }
164 +
165 + return newObject;
166 +}
73 167 /**
74 - * Merge 2 objects, values of the latter overwriting the former.
168 + * Checks if the given element is currently being animated.
75 169 *
76 - * @param obj1
77 - * @param obj2
78 - * @returns {*}
170 + * @param element
171 + * @returns {boolean}
79 172 */
80 -function merge( obj1, obj2 ) {
81 - var obj3 = {};
82 - for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
83 - for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
84 - return obj3;
173 +
174 +
175 +function animated(element) {
176 + return !!element.getAttribute('data-animated');
85 177 }
178 +/**
179 + * Toggles the element using the given animation.
180 + *
181 + * @param element
182 + * @param animation Either "fade" or "slide"
183 + * @param callbackFn
184 + */
86 185
87 -// Box Object
88 -var Box = function( id, config ) {
89 - this.id = id;
90 186
91 - // store config values
92 - this.config = merge(defaults, config);
187 +function toggle(element, animation, callbackFn) {
188 + var nowVisible = element.style.display !== 'none' || element.offsetLeft > 0; // create clone for reference
93 189
94 - // store ref to overlay
95 - this.overlay = document.getElementById('boxzilla-overlay');
190 + var clone = element.cloneNode(true);
96 191
97 - // state
98 - this.visible = false;
99 - this.closed = false;
100 - this.triggered = false;
101 - this.triggerHeight = 0;
102 - this.cookieSet = false;
192 + var cleanup = function cleanup() {
193 + element.removeAttribute('data-animated');
194 + element.setAttribute('style', clone.getAttribute('style'));
195 + element.style.display = nowVisible ? 'none' : '';
103 196
104 - // if a trigger was given, calculate values once and store
105 - if( this.config.trigger ) {
106 - if( this.config.trigger.method === 'percentage' || this.config.trigger.method === 'element' ) {
107 - this.triggerHeight = this.calculateTriggerHeight();
108 - }
197 + if (callbackFn) {
198 + callbackFn();
199 + }
200 + }; // store attribute so everyone knows we're animating this element
109 201
110 - this.cookieSet = this.isCookieSet();
111 - }
112 202
113 - // create dom element for this box
114 - this.element = this.dom();
115 - this.$element = $(this.element);
203 + element.setAttribute('data-animated', 'true'); // toggle element visiblity right away if we're making something visible
116 204
117 - // further initialise the box
118 - this.events();
119 -};
205 + if (!nowVisible) {
206 + element.style.display = '';
207 + }
120 208
121 -// initialise the box
122 -Box.prototype.events = function() {
123 - var box = this;
209 + var hiddenStyles;
210 + var visibleStyles; // animate properties
124 211
125 - // attach event to "close" icon inside box
126 - this.$element.find('.boxzilla-close-icon').click(box.dismiss.bind(this));
212 + if (animation === 'slide') {
213 + hiddenStyles = initObjectProperties(['height', 'borderTopWidth', 'borderBottomWidth', 'paddingTop', 'paddingBottom'], 0);
214 + visibleStyles = {};
127 215
128 - this.$element.on('click', 'a', function(e) {
129 - Boxzilla.trigger('box.interactions.link', [ box, e.target ] );
130 - });
216 + if (!nowVisible) {
217 + var computedStyles = window.getComputedStyle(element);
218 + visibleStyles = copyObjectProperties(['height', 'borderTopWidth', 'borderBottomWidth', 'paddingTop', 'paddingBottom'], computedStyles); // in some browsers, getComputedStyle returns "auto" value. this falls back to getBoundingClientRect() in those browsers since we need an actual height.
131 219
132 - this.$element.on('submit', 'form', function(e) {
133 - box.setCookie();
134 - Boxzilla.trigger('box.interactions.form', [ box, e.target ]);
135 - });
220 + if (!isFinite(visibleStyles.height)) {
221 + var clientRect = element.getBoundingClientRect();
222 + visibleStyles.height = clientRect.height;
223 + }
136 224
137 - // attach event to all links referring #boxzilla-{box_id}
138 - $(document.body).on('click', 'a[href="#boxzilla-' + box.id + '"]', function() {
139 - box.toggle();
140 - return false;
141 - });
225 + css(element, hiddenStyles);
226 + } // don't show a scrollbar during animation
142 227
143 - // maybe show box right away
144 - if( this.config.trigger.method === "time_on_page" && this.mayAutoShow() ) {
145 - window.setTimeout(this.trigger.bind(this), this.config.trigger.value * 1000 );
146 - // auto-show the box if box is referenced from URL
147 - } else if( this.fits() && this.locationHashRefersBox() ) {
148 - $(window).load(this.show.bind(this));
228 +
229 + element.style.overflowY = 'hidden';
230 + animate(element, nowVisible ? hiddenStyles : visibleStyles, cleanup);
231 + } else {
232 + hiddenStyles = {
233 + opacity: 0
234 + };
235 + visibleStyles = {
236 + opacity: 1
237 + };
238 +
239 + if (!nowVisible) {
240 + css(element, hiddenStyles);
149 241 }
150 242
151 -};
243 + animate(element, nowVisible ? hiddenStyles : visibleStyles, cleanup);
244 + }
245 +}
152 246
153 -// generate dom elements for this box
154 -Box.prototype.dom = function() {
247 +function animate(element, targetStyles, fn) {
248 + var last = +new Date();
249 + var initialStyles = window.getComputedStyle(element);
250 + var currentStyles = {};
251 + var propSteps = {};
155 252
156 - var wrapper = document.createElement('div');
157 - wrapper.className = 'boxzilla-container boxzilla-' + this.config.position + '-container';
253 + for (var property in targetStyles) {
254 + if (!targetStyles.hasOwnProperty(property)) {
255 + continue;
256 + } // make sure we have an object filled with floats
158 257
159 - var box = document.createElement('div');
160 - box.className = 'boxzilla boxzilla-' + this.id + ' boxzilla-' + this.config.position;
161 - box.style.display = 'none';
162 - wrapper.appendChild(box);
163 258
164 - var content = document.createElement('div');
165 - content.className = 'boxzilla-content';
166 - content.innerHTML = this.config.content;
167 - box.appendChild(content);
259 + targetStyles[property] = parseFloat(targetStyles[property]); // calculate step size & current value
168 260
169 - if( this.config.closable && this.config.icon ) {
170 - var icon = document.createElement('span');
171 - icon.className = "boxzilla-close-icon";
172 - icon.innerHTML = this.config.icon;
173 - box.appendChild(icon);
261 + var to = targetStyles[property];
262 + var current = parseFloat(initialStyles[property]); // is there something to do?
263 +
264 + if (current === to) {
265 + delete targetStyles[property];
266 + continue;
174 267 }
175 268
176 - document.body.appendChild(wrapper);
269 + propSteps[property] = (to - current) / duration; // points per second
177 270
178 - return box;
179 -};
271 + currentStyles[property] = current;
272 + }
180 273
181 -// set (calculate) custom box styling depending on box options
182 -Box.prototype.setCustomBoxStyling = function() {
274 + var tick = function tick() {
275 + var now = +new Date();
276 + var timeSinceLastTick = now - last;
277 + var done = true;
278 + var step, to, increment, newValue;
183 279
184 - // reset element to its initial state
185 - this.element.style.overflowY = 'auto';
186 - this.element.style.maxHeight = 'none';
280 + for (var _property in targetStyles) {
281 + if (!targetStyles.hasOwnProperty(_property)) {
282 + continue;
283 + }
187 284
188 - // get new dimensions
189 - var windowHeight = window.innerHeight;
190 - var boxHeight = this.$element.outerHeight();
285 + step = propSteps[_property];
286 + to = targetStyles[_property];
287 + increment = step * timeSinceLastTick;
288 + newValue = currentStyles[_property] + increment;
191 289
192 - // add scrollbar to box and limit height
193 - if( boxHeight > windowHeight ) {
194 - this.element.style.maxHeight = windowHeight + "px";
195 - this.element.style.overflowY = 'scroll';
290 + if (step > 0 && newValue >= to || step < 0 && newValue <= to) {
291 + newValue = to;
292 + } else {
293 + done = false;
294 + } // store new value
295 +
296 +
297 + currentStyles[_property] = newValue;
298 + element.style[_property] = _property !== 'opacity' ? newValue + 'px' : newValue;
196 299 }
197 300
198 - // set new top margin for boxes which are centered
199 - if( this.config.position === 'center' ) {
200 - var newTopMargin = ( ( windowHeight - boxHeight ) / 2 );
201 - newTopMargin = newTopMargin >= 0 ? newTopMargin : 0;
202 - this.element.style.marginTop = newTopMargin + "px";
301 + last = +new Date();
302 +
303 + if (!done) {
304 + // keep going until we're done for all props
305 + window.requestAnimationFrame(tick);
306 + } else {
307 + // call callback
308 + fn && fn();
203 309 }
310 + };
204 311
312 + tick();
313 +}
314 +
315 +module.exports = {
316 + toggle: toggle,
317 + animate: animate,
318 + animated: animated
205 319 };
206 320
207 -// toggle visibility of the box
208 -Box.prototype.toggle = function(show) {
321 +},{}],3:[function(require,module,exports){
322 +"use strict";
209 323
210 - // revert visibility if no explicit argument is given
211 - if( typeof( show ) === "undefined" ) {
212 - show = ! this.visible;
324 +var defaults = {
325 + animation: 'fade',
326 + rehide: false,
327 + content: '',
328 + cookie: null,
329 + icon: '&times',
330 + screenWidthCondition: null,
331 + position: 'center',
332 + testMode: false,
333 + trigger: false,
334 + closable: true
335 +};
336 +
337 +var Animator = require('./animator.js');
338 +/**
339 + * Merge 2 objects, values of the latter overwriting the former.
340 + *
341 + * @param obj1
342 + * @param obj2
343 + * @returns {*}
344 + */
345 +
346 +
347 +function merge(obj1, obj2) {
348 + var obj3 = {}; // add obj1 to obj3
349 +
350 + for (var attrname in obj1) {
351 + if (obj1.hasOwnProperty(attrname)) {
352 + obj3[attrname] = obj1[attrname];
213 353 }
354 + } // add obj2 to obj3
214 355
215 - // do nothing if element is being animated
216 - if( this.$element.is(':animated') ) {
217 - return false;
356 +
357 + for (var _attrname in obj2) {
358 + if (obj2.hasOwnProperty(_attrname)) {
359 + obj3[_attrname] = obj2[_attrname];
218 360 }
361 + }
219 362
220 - // is box already at desired visibility?
221 - if( show === this.visible ) {
222 - return false;
363 + return obj3;
364 +}
365 +/**
366 + * Get the real height of entire document.
367 + * @returns {number}
368 + */
369 +
370 +
371 +function getDocumentHeight() {
372 + var body = document.body;
373 + var html = document.documentElement;
374 + return Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
375 +} // Box Object
376 +
377 +
378 +function Box(id, config, fireEvent) {
379 + this.id = id;
380 + this.fireEvent = fireEvent; // store config values
381 +
382 + this.config = merge(defaults, config); // add overlay element to dom and store ref to overlay
383 +
384 + this.overlay = document.createElement('div');
385 + this.overlay.style.display = 'none';
386 + this.overlay.id = 'boxzilla-overlay-' + this.id;
387 + this.overlay.classList.add('boxzilla-overlay');
388 + document.body.appendChild(this.overlay); // state
389 +
390 + this.visible = false;
391 + this.dismissed = false;
392 + this.triggered = false;
393 + this.triggerHeight = this.calculateTriggerHeight();
394 + this.cookieSet = this.isCookieSet();
395 + this.element = null;
396 + this.contentElement = null;
397 + this.closeIcon = null; // create dom elements for this box
398 +
399 + this.dom(); // further initialise the box
400 +
401 + this.events();
402 +} // initialise the box
403 +
404 +
405 +Box.prototype.events = function () {
406 + var box = this; // attach event to "close" icon inside box
407 +
408 + if (this.closeIcon) {
409 + this.closeIcon.addEventListener('click', function (evt) {
410 + evt.preventDefault();
411 + box.dismiss();
412 + });
413 + }
414 +
415 + this.element.addEventListener('click', function (evt) {
416 + if (evt.target.tagName === 'A') {
417 + this.fireEvent('box.interactions.link', [box, evt.target]);
223 418 }
419 + }, false);
420 + this.element.addEventListener('submit', function (evt) {
421 + box.setCookie();
422 + this.fireEvent('box.interactions.form', [box, evt.target]);
423 + }, false);
424 + this.overlay.addEventListener('click', function (e) {
425 + var x = e.offsetX;
426 + var y = e.offsetY; // calculate if click was less than 40px outside box to avoid closing it by accident
224 427
225 - // if box should be hidden but is not closable, bail.
226 - if( ! show && ! this.config.closable ) {
227 - return false;
428 + var rect = box.element.getBoundingClientRect();
429 + var margin = 40; // if click was not anywhere near box, dismiss it.
430 +
431 + if (x < rect.left - margin || x > rect.right + margin || y < rect.top - margin || y > rect.bottom + margin) {
432 + box.dismiss();
228 433 }
434 + });
435 +}; // generate dom elements for this box
229 436
230 - // set new visibility status
231 - this.visible = show;
232 437
233 - // calculate custom styling for which CSS is "too stupid"
234 - this.setCustomBoxStyling();
438 +Box.prototype.dom = function () {
439 + var wrapper = document.createElement('div');
440 + wrapper.className = 'boxzilla-container boxzilla-' + this.config.position + '-container';
441 + var box = document.createElement('div');
442 + box.id = 'boxzilla-' + this.id;
443 + box.className = 'boxzilla boxzilla-' + this.id + ' boxzilla-' + this.config.position;
444 + box.style.display = 'none';
445 + wrapper.appendChild(box);
446 + var content;
235 447
236 - // fadein / fadeout the overlay if position is "center"
237 - if( this.config.position === 'center' ) {
238 - $(this.overlay).fadeToggle('slow');
239 - }
448 + if (typeof this.config.content === 'string') {
449 + content = document.createElement('div');
450 + content.innerHTML = this.config.content;
451 + } else {
452 + content = this.config.content; // make sure element is visible
240 453
241 - // trigger event
242 - Boxzilla.trigger('box.' + ( show ? 'show' : 'hide' ), [ this ] );
454 + content.style.display = '';
455 + }
243 456
244 - // show or hide box using selected animation
245 - if( this.config.animation === 'fade' ) {
246 - this.$element.fadeToggle( 'slow' );
457 + content.className = 'boxzilla-content';
458 + box.appendChild(content);
459 +
460 + if (this.config.closable && this.config.icon) {
461 + var closeIcon = document.createElement('span');
462 + closeIcon.className = 'boxzilla-close-icon';
463 + closeIcon.innerHTML = this.config.icon;
464 + box.appendChild(closeIcon);
465 + this.closeIcon = closeIcon;
466 + }
467 +
468 + document.body.appendChild(wrapper);
469 + this.contentElement = content;
470 + this.element = box;
471 +}; // set (calculate) custom box styling depending on box options
472 +
473 +
474 +Box.prototype.setCustomBoxStyling = function () {
475 + // reset element to its initial state
476 + var origDisplay = this.element.style.display;
477 + this.element.style.display = '';
478 + this.element.style.overflowY = '';
479 + this.element.style.maxHeight = ''; // get new dimensions
480 +
481 + var windowHeight = window.innerHeight;
482 + var boxHeight = this.element.clientHeight; // add scrollbar to box and limit height
483 +
484 + if (boxHeight > windowHeight) {
485 + this.element.style.maxHeight = windowHeight + 'px';
486 + this.element.style.overflowY = 'scroll';
487 + } // set new top margin for boxes which are centered
488 +
489 +
490 + if (this.config.position === 'center') {
491 + var newTopMargin = (windowHeight - boxHeight) / 2;
492 + newTopMargin = newTopMargin >= 0 ? newTopMargin : 0;
493 + this.element.style.marginTop = newTopMargin + 'px';
494 + }
495 +
496 + this.element.style.display = origDisplay;
497 +}; // toggle visibility of the box
498 +
499 +
500 +Box.prototype.toggle = function (show, animate) {
501 + show = typeof show === 'undefined' ? !this.visible : show;
502 + animate = typeof animate === 'undefined' ? true : animate; // is box already at desired visibility?
503 +
504 + if (show === this.visible) {
505 + return false;
506 + } // is box being animated?
507 +
508 +
509 + if (Animator.animated(this.element)) {
510 + return false;
511 + } // if box should be hidden but is not closable, bail.
512 +
513 +
514 + if (!show && !this.config.closable) {
515 + return false;
516 + } // set new visibility status
517 +
518 +
519 + this.visible = show; // calculate new styling rules
520 +
521 + this.setCustomBoxStyling(); // trigger event
522 +
523 + this.fireEvent('box.' + (show ? 'show' : 'hide'), [this]); // show or hide box using selected animation
524 +
525 + if (this.config.position === 'center') {
526 + this.overlay.classList.toggle('boxzilla-' + this.id + '-overlay');
527 +
528 + if (animate) {
529 + Animator.toggle(this.overlay, 'fade');
247 530 } else {
248 - this.$element.slideToggle( 'slow' );
531 + this.overlay.style.display = show ? '' : 'none';
249 532 }
533 + }
250 534
251 - // // focus on first input field in box
252 - // this.$element.find('input').first().focus();
535 + if (animate) {
536 + Animator.toggle(this.element, this.config.animation, function () {
537 + if (this.visible) {
538 + return;
539 + }
253 540
254 - return true;
255 -};
541 + this.contentElement.innerHTML = this.contentElement.innerHTML + '';
542 + }.bind(this));
543 + } else {
544 + this.element.style.display = show ? '' : 'none';
545 + }
256 546
257 -// show the box
258 -Box.prototype.show = function() {
259 - return this.toggle(true);
260 -};
547 + return true;
548 +}; // show the box
261 549
262 -// hide the box
263 -Box.prototype.hide = function() {
264 - return this.toggle(false);
265 -};
266 550
267 -// calculate trigger height
268 -Box.prototype.calculateTriggerHeight = function() {
269 - var triggerHeight = 0;
551 +Box.prototype.show = function (animate) {
552 + return this.toggle(true, animate);
553 +}; // hide the box
270 554
271 - if( this.config.trigger.method === 'element' ) {
272 - var $triggerElement = $(this.config.trigger.value).first();
273 - triggerHeight = ( $triggerElement.length > 0 ) ? $triggerElement.offset().top : 0;
274 - } else if( this.config.trigger.method === 'percentage' ) {
275 - triggerHeight = ( this.config.trigger.value / 100 * $(document).height() );
555 +
556 +Box.prototype.hide = function (animate) {
557 + return this.toggle(false, animate);
558 +}; // calculate trigger height
559 +
560 +
561 +Box.prototype.calculateTriggerHeight = function () {
562 + var triggerHeight = 0;
563 +
564 + if (this.config.trigger) {
565 + if (this.config.trigger.method === 'element') {
566 + var triggerElement = document.body.querySelector(this.config.trigger.value);
567 +
568 + if (triggerElement) {
569 + var offset = triggerElement.getBoundingClientRect();
570 + triggerHeight = offset.top;
571 + }
572 + } else if (this.config.trigger.method === 'percentage') {
573 + triggerHeight = this.config.trigger.value / 100 * getDocumentHeight();
276 574 }
575 + }
277 576
278 - return triggerHeight;
577 + return triggerHeight;
279 578 };
280 579
281 -// set cookie that disables automatically showing the box
282 -Box.prototype.setCookie = function() {
283 - // do nothing if cookieTime evaluates to false
284 - if(! this.config.cookieTime) {
285 - return;
286 - }
580 +Box.prototype.fits = function () {
581 + if (!this.config.screenWidthCondition || !this.config.screenWidthCondition.value) {
582 + return true;
583 + }
287 584
288 - var expiryDate = new Date();
289 - expiryDate.setDate( expiryDate.getDate() + this.config.cookieTime );
290 - document.cookie = 'boxzilla_box_'+ this.id + '=true; expires='+ expiryDate.toUTCString() +'; path=/';
585 + switch (this.config.screenWidthCondition.condition) {
586 + case 'larger':
587 + return window.innerWidth > this.config.screenWidthCondition.value;
588 +
589 + case 'smaller':
590 + return window.innerWidth < this.config.screenWidthCondition.value;
591 + } // meh.. condition should be "smaller" or "larger", just return true.
592 +
593 +
594 + return true;
291 595 };
292 596
293 -// checks whether window.location.hash equals the box element ID or that of any element inside the box
294 -Box.prototype.locationHashRefersBox = function() {
597 +Box.prototype.onResize = function () {
598 + this.triggerHeight = this.calculateTriggerHeight();
599 + this.setCustomBoxStyling();
600 +}; // is this box enabled?
295 601
296 - if( ! window.location.hash || 0 === window.location.hash.length ) {
297 - return false;
298 - }
299 602
300 - var elementId = window.location.hash.substring(1);
301 - if( elementId === this.element.id ) {
302 - return true;
303 - } else if( this.element.querySelector('#' + elementId) ) {
304 - return true;
305 - }
603 +Box.prototype.mayAutoShow = function () {
604 + if (this.dismissed) {
605 + return false;
606 + } // check if box fits on given minimum screen width
306 607
608 +
609 + if (!this.fits()) {
307 610 return false;
611 + } // if trigger empty or error in calculating triggerHeight, return false
612 +
613 +
614 + if (!this.config.trigger) {
615 + return false;
616 + } // rely on cookie value (show if not set, don't show if set)
617 +
618 +
619 + return !this.cookieSet;
308 620 };
309 621
310 -Box.prototype.fits = function() {
311 - if( this.config.minimumScreenWidth <= 0 ) {
312 - return true;
313 - }
314 -
315 - return window.innerWidth > this.config.minimumScreenWidth
622 +Box.prototype.mayRehide = function () {
623 + return this.config.rehide && this.triggered;
316 624 };
317 625
318 -// is this box enabled?
319 -Box.prototype.mayAutoShow = function() {
626 +Box.prototype.isCookieSet = function () {
627 + // always show on test mode or when no auto-trigger is configured
628 + if (this.config.testMode || !this.config.trigger) {
629 + return false;
630 + } // if either cookie is null or trigger & dismiss are both falsey, don't bother checking.
320 631
321 - // don't show if box was closed (dismissed) before
322 - if( this.closed ) {
323 - return false;
324 - }
325 632
326 - // check if box fits on given minimum screen width
327 - if( ! this.fits() ) {
328 - return false;
329 - }
633 + if (!this.config.cookie || !this.config.cookie.triggered && !this.config.cookie.dismissed) {
634 + return false;
635 + }
330 636
331 - // if trigger empty or error in calculating triggerHeight, return false
332 - if( ! this.config.trigger ) {
333 - return false;
334 - }
637 + return document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + 'boxzilla_box_' + this.id + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1') === 'true';
638 +}; // set cookie that disables automatically showing the box
335 639
336 - // rely on cookie value (show if not set, don't show if set)
337 - return ! this.cookieSet;
338 -};
339 640
340 -Box.prototype.mayRehide = function() {
341 - return this.config.rehide && this.triggered;
641 +Box.prototype.setCookie = function (hours) {
642 + var expiryDate = new Date();
643 + expiryDate.setHours(expiryDate.getHours() + hours);
644 + document.cookie = 'boxzilla_box_' + this.id + '=true; expires=' + expiryDate.toUTCString() + '; path=/';
342 645 };
343 646
344 -Box.prototype.isCookieSet = function() {
345 - // always show on test mode
346 - if( this.config.testMode ) {
347 - return false;
348 - }
647 +Box.prototype.trigger = function () {
648 + var shown = this.show();
349 649
350 - // check for cookie
351 - if( ! this.config.cookieTime ) {
352 - return false;
353 - }
650 + if (!shown) {
651 + return;
652 + }
354 653
355 - var cookieSet = document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + 'boxzilla_box_' + this.id + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1") === "true";
356 - return cookieSet;
654 + this.triggered = true;
357 655
656 + if (this.config.cookie && this.config.cookie.triggered) {
657 + this.setCookie(this.config.cookie.triggered);
658 + }
358 659 };
660 +/**
661 + * Dismisses the box and optionally sets a cookie.
662 + * @param animate
663 + * @returns {boolean}
664 + */
359 665
360 -Box.prototype.trigger = function() {
361 - var shown = this.show();
362 - if( shown ) {
363 - this.triggered = true;
364 - }
365 -};
366 666
367 -Box.prototype.dismiss = function() {
368 - this.hide();
369 - this.setCookie();
370 - this.closed = true;
371 - Boxzilla.trigger('box.dismiss', [ this ]);
372 -};
667 +Box.prototype.dismiss = function (animate) {
668 + // only dismiss box if it's currently open.
669 + if (!this.visible) {
670 + return false;
671 + } // hide box element
373 672
374 -module.exports = function(_Boxzilla) {
375 - Boxzilla = _Boxzilla;
376 - return Box;
673 +
674 + this.hide(animate); // set cookie
675 +
676 + if (this.config.cookie && this.config.cookie.dismissed) {
677 + this.setCookie(this.config.cookie.dismissed);
678 + }
679 +
680 + this.dismissed = true;
681 + this.fireEvent('box.dismiss', [this]);
682 + return true;
377 683 };
378 -},{}],3:[function(require,module,exports){
379 -'use strict';
380 684
381 -var $ = window.jQuery,
382 - EventEmitter = require('wolfy87-eventemitter'),
383 - Boxzilla = Object.create(EventEmitter.prototype),
384 - Box = require('./Box.js')(Boxzilla),
385 - boxes = {},
386 - windowHeight = window.innerHeight,
387 - overlay = document.createElement('div'),
388 - exitIntentDelayTimer,
389 - exitIntentTriggered;
685 +module.exports = Box;
390 686
391 -function each( obj, callback ) {
392 - for( var key in obj ) {
393 - if(! obj.hasOwnProperty(key)) continue;
394 - callback(obj[key]);
395 - }
396 -}
687 +},{"./animator.js":2}],4:[function(require,module,exports){
688 +"use strict";
397 689
398 -function throttle(fn, threshhold, scope) {
399 - threshhold || (threshhold = 250);
400 - var last,
401 - deferTimer;
402 - return function () {
403 - var context = scope || this;
690 +var Box = require('./box.js');
404 691
405 - var now = +new Date,
406 - args = arguments;
407 - if (last && now < last + threshhold) {
408 - // hold on to it
409 - clearTimeout(deferTimer);
410 - deferTimer = setTimeout(function () {
411 - last = now;
412 - fn.apply(context, args);
413 - }, threshhold);
414 - } else {
415 - last = now;
416 - fn.apply(context, args);
417 - }
418 - };
419 -}
692 +var throttle = require('./util.js').throttle;
420 693
421 -// "keyup" listener
422 -function onKeyUp(e) {
423 - if (e.keyCode == 27) {
424 - Boxzilla.dismiss();
425 - }
426 -}
694 +var styles = require('./styles.js');
427 695
428 -function checkTimeCriteria() {
429 - var start = sessionStorage.getItem('boxzilla_start_time');
430 - var now = Date.now();
431 - var timeOnSite = ( now - start ) / 1000;
696 +var ExitIntent = require('./triggers/exit-intent.js');
432 697
433 - each(boxes, function(box) {
434 - if( ! box.mayAutoShow() || box.config.trigger.method !== 'time_on_site' ) {
435 - return;
436 - }
698 +var Scroll = require('./triggers/scroll.js');
437 699
438 - if( timeOnSite > box.config.trigger.value ) {
439 - box.trigger();
440 - }
441 - });
442 -}
700 +var Pageviews = require('./triggers/pageviews.js');
443 701
444 -// check triggerHeight criteria for all boxes
445 -function checkHeightCriteria() {
446 - var scrollY = window.scrollY;
447 - var scrollHeight = scrollY + ( windowHeight * 0.9 );
702 +var Time = require('./triggers/time.js');
448 703
449 - each(boxes, function(box) {
450 - if( ! box.mayAutoShow() || box.triggerHeight <= 0 ) {
451 - return;
452 - }
704 +var initialised = false;
705 +var boxes = [];
706 +var listeners = {};
453 707
454 - if( scrollHeight > box.triggerHeight ) {
455 - box.trigger();
456 - } else if( box.mayRehide() ) {
457 - box.hide();
458 - }
459 - });
708 +function onKeyUp(evt) {
709 + if (evt.key === 'Escape' || evt.key === 'Esc') {
710 + dismiss();
711 + }
460 712 }
461 713
462 -// recalculate heights and variables based on height
463 714 function recalculateHeights() {
464 - windowHeight = window.innerHeight;
465 -
466 - each(boxes, function(box) {
467 - box.setCustomBoxStyling();
468 - });
715 + boxes.forEach(function (box) {
716 + return box.onResize();
717 + });
469 718 }
470 719
471 -function onOverlayClick(e) {
472 - var x = e.offsetX;
473 - var y = e.offsetY;
720 +function onElementClick(evt) {
721 + // bubble up to <a> element
722 + var el = evt.target;
474 723
475 - // calculate if click was near a box to avoid closing it (click error margin)
476 - each(boxes, function(box) {
477 - var rect = box.element.getBoundingClientRect();
478 - var margin = 100 + ( window.innerWidth * 0.05 );
724 + for (var i = 0; i <= 3; i++) {
725 + if (!el || el.tagName === 'A') {
726 + break;
727 + }
479 728
480 - // if click was not anywhere near box, dismiss it.
481 - if( x < ( rect.left - margin ) || x > ( rect.right + margin ) || y < ( rect.top - margin ) || y > ( rect.bottom + margin ) ) {
482 - box.dismiss();
483 - }
484 - });
485 -}
729 + el = el.parentElement;
730 + }
486 731
487 -function triggerExitIntent() {
488 - if(exitIntentTriggered) return;
732 + if (!el || el.tagName !== 'A' || !el.href) {
733 + return;
734 + }
489 735
490 - each(boxes, function(box) {
491 - if(box.mayAutoShow() && box.config.trigger.method === 'exit_intent' ) {
492 - box.trigger();
493 - }
494 - });
736 + var match = el.href.match(/[#&]boxzilla-(.+)/i);
495 737
496 - exitIntentTriggered = true;
738 + if (match && match.length > 1) {
739 + toggle(match[1]);
740 + }
497 741 }
498 742
499 -function onMouseLeave(e) {
500 - // did mouse leave at top of window?
501 - if( e.clientY < 0 ) {
502 - exitIntentDelayTimer = window.setTimeout(triggerExitIntent, 1000);
503 - }
743 +function trigger(event, args) {
744 + listeners[event] && listeners[event].forEach(function (f) {
745 + return f.apply(null, args);
746 + });
504 747 }
505 748
506 -function onMouseEnter() {
507 - if( exitIntentDelayTimer ) {
508 - window.clearInterval(exitIntentDelayTimer);
509 - exitIntentDelayTimer = null;
510 - }
749 +function on(event, fn) {
750 + listeners[event] = listeners[event] || [];
751 + listeners[event].push(fn);
511 752 }
512 753
513 -// initialise & add event listeners
514 -Boxzilla.init = function() {
515 - // add overlay element to dom
516 - overlay.id = 'boxzilla-overlay';
517 - document.body.appendChild(overlay);
754 +function off(event, fn) {
755 + listeners[event] && listeners[event].filter(function (f) {
756 + return f !== fn;
757 + });
758 +} // initialise & add event listeners
518 759
519 - // event binds
520 - $(window).on('scroll', throttle(checkHeightCriteria));
521 - $(window).on('resize', throttle(recalculateHeights));
522 - $(window).on('load', recalculateHeights );
523 - $(document).on('mouseleave', onMouseLeave);
524 - $(document).on('mouseenter', onMouseEnter);
525 - $(document).on('keyup', onKeyUp);
526 - $(overlay).click(onOverlayClick);
527 - window.setInterval(checkTimeCriteria, 1000);
528 760
529 - if(! sessionStorage.getItem('boxzilla_start_time')) {
530 - sessionStorage.setItem('boxzilla_start_time', Date.now());
531 - }
761 +function init() {
762 + if (initialised) {
763 + return;
764 + } // insert styles into DOM
532 765
533 - Boxzilla.trigger('ready');
534 -};
535 766
536 -/**
537 - * Create a new Box
538 - *
539 - * @param string id
540 - * @param object opts
541 - *
542 - * @returns Box
543 - */
544 -Boxzilla.create = function(id, opts) {
545 - boxes[id] = new Box(id, opts);
546 - return boxes[id];
547 -};
767 + var styleElement = document.createElement('style');
768 + styleElement.innerHTML = styles;
769 + document.head.appendChild(styleElement); // init triggers
548 770
549 -// dismiss a single box (or all by omitting id param)
550 -Boxzilla.dismiss = function(id) {
551 - // if no id given, dismiss all current open boxes
552 - if( typeof(id) === "undefined" ) {
553 - each(boxes, function(box) { box.dismiss(); });
554 - } else if( typeof( boxes[id] ) === "object" ) {
555 - boxes[id].dismiss();
556 - }
557 -};
771 + ExitIntent(boxes);
772 + Pageviews(boxes);
773 + Scroll(boxes);
774 + Time(boxes);
775 + document.body.addEventListener('click', onElementClick, true);
776 + window.addEventListener('resize', throttle(recalculateHeights));
777 + window.addEventListener('load', recalculateHeights);
778 + document.addEventListener('keyup', onKeyUp);
779 + trigger('ready');
780 + initialised = true; // ensure this function doesn't run again
781 +}
558 782
559 -Boxzilla.hide = function(id) {
560 - if( typeof(id) === "undefined" ) {
561 - each(boxes, function(box) { box.hide(); });
562 - } else if( typeof( boxes[id] ) === "object" ) {
563 - boxes[id].hide();
783 +function create(id, opts) {
784 + // preserve backwards compat for minimumScreenWidth option
785 + if (typeof opts.minimumScreenWidth !== 'undefined') {
786 + opts.screenWidthCondition = {
787 + condition: 'larger',
788 + value: opts.minimumScreenWidth
789 + };
790 + }
791 +
792 + id = String(id);
793 + var box = new Box(id, opts, trigger);
794 + boxes.push(box);
795 + return box;
796 +}
797 +
798 +function get(id) {
799 + id = String(id);
800 +
801 + for (var i = 0; i < boxes.length; i++) {
802 + if (boxes[i].id === id) {
803 + return boxes[i];
564 804 }
565 -};
805 + }
566 806
567 -Boxzilla.show = function(id) {
568 - if( typeof(id) === "undefined" ) {
569 - each(boxes, function(box) { box.show(); });
570 - } else if( typeof( boxes[id] ) === "object" ) {
571 - boxes[id].show();
572 - }
573 -};
807 + throw new Error('No box exists with ID ' + id);
808 +} // dismiss a single box (or all by omitting id param)
574 809
575 -Boxzilla.toggle = function(id) {
576 - if( typeof(id) === "undefined" ) {
577 - each(boxes, function(box) { box.toggle(); });
578 - } else if( typeof( boxes[id] ) === "object" ) {
579 - boxes[id].toggle();
580 - }
581 -};
582 810
583 -if ( typeof module !== 'undefined' && module.exports ) {
584 - module.exports = Boxzilla;
585 -} else {
586 - this.Boxzilla = Boxzilla;
811 +function dismiss(id, animate) {
812 + if (id) {
813 + get(id).dismiss(animate);
814 + } else {
815 + boxes.forEach(function (box) {
816 + return box.dismiss(animate);
817 + });
818 + }
587 819 }
588 -},{"./Box.js":2,"wolfy87-eventemitter":4}],4:[function(require,module,exports){
589 -/*!
590 - * EventEmitter v4.2.11 - git.io/ee
591 - * Unlicense - http://unlicense.org/
592 - * Oliver Caldwell - http://oli.me.uk/
593 - * @preserve
594 - */
595 820
596 -;(function () {
597 - 'use strict';
821 +function hide(id, animate) {
822 + if (id) {
823 + get(id).hide(animate);
824 + } else {
825 + boxes.forEach(function (box) {
826 + return box.hide(animate);
827 + });
828 + }
829 +}
598 830
599 - /**
600 - * Class for managing events.
601 - * Can be extended to provide event functionality in other classes.
602 - *
603 - * @class EventEmitter Manages event registering and emitting.
604 - */
605 - function EventEmitter() {}
831 +function show(id, animate) {
832 + if (id) {
833 + get(id).show(animate);
834 + } else {
835 + boxes.forEach(function (box) {
836 + return box.show(animate);
837 + });
838 + }
839 +}
606 840
607 - // Shortcuts to improve speed and size
608 - var proto = EventEmitter.prototype;
609 - var exports = this;
610 - var originalGlobalValue = exports.EventEmitter;
841 +function toggle(id, animate) {
842 + if (id) {
843 + get(id).toggle(animate);
844 + } else {
845 + boxes.forEach(function (box) {
846 + return box.toggle(animate);
847 + });
848 + }
849 +} // expose boxzilla object
611 850
612 - /**
613 - * Finds the index of the listener for the event in its storage array.
614 - *
615 - * @param {Function[]} listeners Array of listeners to search through.
616 - * @param {Function} listener Method to look for.
617 - * @return {Number} Index of the specified listener, -1 if not found
618 - * @api private
619 - */
620 - function indexOfListener(listeners, listener) {
621 - var i = listeners.length;
622 - while (i--) {
623 - if (listeners[i].listener === listener) {
624 - return i;
625 - }
626 - }
627 851
628 - return -1;
629 - }
852 +var Boxzilla = {
853 + off: off,
854 + on: on,
855 + get: get,
856 + init: init,
857 + create: create,
858 + trigger: trigger,
859 + show: show,
860 + hide: hide,
861 + dismiss: dismiss,
862 + toggle: toggle,
863 + boxes: boxes
864 +};
865 +window.Boxzilla = Boxzilla;
630 866
631 - /**
632 - * Alias a method while keeping the context correct, to allow for overwriting of target method.
633 - *
634 - * @param {String} name The name of the target method.
635 - * @return {Function} The aliased method
636 - * @api private
637 - */
638 - function alias(name) {
639 - return function aliasClosure() {
640 - return this[name].apply(this, arguments);
641 - };
642 - }
867 +if (typeof module !== 'undefined' && module.exports) {
868 + module.exports = Boxzilla;
869 +}
643 870
644 - /**
645 - * Returns the listener array for the specified event.
646 - * Will initialise the event object and listener arrays if required.
647 - * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
648 - * Each property in the object response is an array of listener functions.
649 - *
650 - * @param {String|RegExp} evt Name of the event to return the listeners from.
651 - * @return {Function[]|Object} All listener functions for the event.
652 - */
653 - proto.getListeners = function getListeners(evt) {
654 - var events = this._getEvents();
655 - var response;
656 - var key;
871 +},{"./box.js":3,"./styles.js":5,"./triggers/exit-intent.js":7,"./triggers/pageviews.js":8,"./triggers/scroll.js":9,"./triggers/time.js":10,"./util.js":11}],5:[function(require,module,exports){
872 +"use strict";
657 873
658 - // Return a concatenated array of all matching events if
659 - // the selector is a regular expression.
660 - if (evt instanceof RegExp) {
661 - response = {};
662 - for (key in events) {
663 - if (events.hasOwnProperty(key) && evt.test(key)) {
664 - response[key] = events[key];
665 - }
666 - }
667 - }
668 - else {
669 - response = events[evt] || (events[evt] = []);
670 - }
874 +var styles = '#boxzilla-overlay,.boxzilla-overlay{position:fixed;background:rgba(0,0,0,.65);width:100%;height:100%;left:0;top:0;z-index:10000}.boxzilla-center-container{position:fixed;top:0;left:0;right:0;height:0;text-align:center;z-index:11000;line-height:0}.boxzilla-center-container .boxzilla{display:inline-block;text-align:left;position:relative;line-height:normal}.boxzilla{position:fixed;z-index:12000;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;background:#fff;padding:25px}.boxzilla.boxzilla-top-left{top:0;left:0}.boxzilla.boxzilla-top-right{top:0;right:0}.boxzilla.boxzilla-bottom-left{bottom:0;left:0}.boxzilla.boxzilla-bottom-right{bottom:0;right:0}.boxzilla-content>:first-child{margin-top:0;padding-top:0}.boxzilla-content>:last-child{margin-bottom:0;padding-bottom:0}.boxzilla-close-icon{position:absolute;right:0;top:0;text-align:center;padding:6px;cursor:pointer;-webkit-appearance:none;font-size:28px;font-weight:700;line-height:20px;color:#000;opacity:.5}.boxzilla-close-icon:focus,.boxzilla-close-icon:hover{opacity:.8}';
875 +module.exports = styles;
671 876
672 - return response;
673 - };
877 +},{}],6:[function(require,module,exports){
878 +"use strict";
674 879
675 - /**
676 - * Takes a list of listener objects and flattens it into a list of listener functions.
677 - *
678 - * @param {Object[]} listeners Raw listener objects.
679 - * @return {Function[]} Just the listener functions.
680 - */
681 - proto.flattenListeners = function flattenListeners(listeners) {
682 - var flatListeners = [];
683 - var i;
880 +var Timer = function Timer() {
881 + this.time = 0;
882 + this.interval = 0;
883 +};
684 884
685 - for (i = 0; i < listeners.length; i += 1) {
686 - flatListeners.push(listeners[i].listener);
687 - }
885 +Timer.prototype.tick = function () {
886 + this.time++;
887 +};
688 888
689 - return flatListeners;
690 - };
889 +Timer.prototype.start = function () {
890 + if (!this.interval) {
891 + this.interval = window.setInterval(this.tick.bind(this), 1000);
892 + }
893 +};
691 894
692 - /**
693 - * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
694 - *
695 - * @param {String|RegExp} evt Name of the event to return the listeners from.
696 - * @return {Object} All listener functions for an event in an object.
697 - */
698 - proto.getListenersAsObject = function getListenersAsObject(evt) {
699 - var listeners = this.getListeners(evt);
700 - var response;
895 +Timer.prototype.stop = function () {
896 + if (this.interval) {
897 + window.clearInterval(this.interval);
898 + this.interval = 0;
899 + }
900 +};
701 901
702 - if (listeners instanceof Array) {
703 - response = {};
704 - response[evt] = listeners;
705 - }
902 +module.exports = Timer;
706 903
707 - return response || listeners;
708 - };
904 +},{}],7:[function(require,module,exports){
905 +"use strict";
709 906
710 - /**
711 - * Adds a listener function to the specified event.
712 - * The listener will not be added if it is a duplicate.
713 - * If the listener returns true then it will be removed after it is called.
714 - * If you pass a regular expression as the event name then the listener will be added to all events that match it.
715 - *
716 - * @param {String|RegExp} evt Name of the event to attach the listener to.
717 - * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
718 - * @return {Object} Current instance of EventEmitter for chaining.
719 - */
720 - proto.addListener = function addListener(evt, listener) {
721 - var listeners = this.getListenersAsObject(evt);
722 - var listenerIsWrapped = typeof listener === 'object';
723 - var key;
907 +module.exports = function (boxes) {
908 + var timeout = null;
909 + var touchStart = {};
724 910
725 - for (key in listeners) {
726 - if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
727 - listeners[key].push(listenerIsWrapped ? listener : {
728 - listener: listener,
729 - once: false
730 - });
731 - }
732 - }
911 + function trigger() {
912 + document.documentElement.removeEventListener('mouseleave', onMouseLeave);
913 + document.documentElement.removeEventListener('mouseenter', onMouseEnter);
914 + document.documentElement.removeEventListener('click', clearTimeout);
915 + window.removeEventListener('touchstart', onTouchStart);
916 + window.removeEventListener('touchend', onTouchEnd); // show boxes with exit intent trigger
733 917
734 - return this;
735 - };
918 + boxes.forEach(function (box) {
919 + if (box.mayAutoShow() && box.config.trigger.method === 'exit_intent') {
920 + box.trigger();
921 + }
922 + });
923 + }
736 924
737 - /**
738 - * Alias of addListener
739 - */
740 - proto.on = alias('addListener');
925 + function clearTimeout() {
926 + if (timeout === null) {
927 + return;
928 + }
741 929
742 - /**
743 - * Semi-alias of addListener. It will add a listener that will be
744 - * automatically removed after its first execution.
745 - *
746 - * @param {String|RegExp} evt Name of the event to attach the listener to.
747 - * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
748 - * @return {Object} Current instance of EventEmitter for chaining.
749 - */
750 - proto.addOnceListener = function addOnceListener(evt, listener) {
751 - return this.addListener(evt, {
752 - listener: listener,
753 - once: true
754 - });
755 - };
930 + window.clearTimeout(timeout);
931 + timeout = null;
932 + }
756 933
757 - /**
758 - * Alias of addOnceListener.
759 - */
760 - proto.once = alias('addOnceListener');
934 + function onMouseEnter() {
935 + clearTimeout();
936 + }
761 937
762 - /**
763 - * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
764 - * You need to tell it what event names should be matched by a regex.
765 - *
766 - * @param {String} evt Name of the event to create.
767 - * @return {Object} Current instance of EventEmitter for chaining.
768 - */
769 - proto.defineEvent = function defineEvent(evt) {
770 - this.getListeners(evt);
771 - return this;
772 - };
938 + function getAddressBarY() {
939 + if (document.documentMode || /Edge\//.test(navigator.userAgent)) {
940 + return 5;
941 + }
773 942
774 - /**
775 - * Uses defineEvent to define multiple events.
776 - *
777 - * @param {String[]} evts An array of event names to define.
778 - * @return {Object} Current instance of EventEmitter for chaining.
779 - */
780 - proto.defineEvents = function defineEvents(evts) {
781 - for (var i = 0; i < evts.length; i += 1) {
782 - this.defineEvent(evts[i]);
783 - }
784 - return this;
785 - };
943 + return 0;
944 + }
786 945
787 - /**
788 - * Removes a listener function from the specified event.
789 - * When passed a regular expression as the event name, it will remove the listener from all events that match it.
790 - *
791 - * @param {String|RegExp} evt Name of the event to remove the listener from.
792 - * @param {Function} listener Method to remove from the event.
793 - * @return {Object} Current instance of EventEmitter for chaining.
794 - */
795 - proto.removeListener = function removeListener(evt, listener) {
796 - var listeners = this.getListenersAsObject(evt);
797 - var index;
798 - var key;
946 + function onMouseLeave(evt) {
947 + clearTimeout(); // did mouse leave at top of window?
948 + // add small exception space in the top-right corner
799 949
800 - for (key in listeners) {
801 - if (listeners.hasOwnProperty(key)) {
802 - index = indexOfListener(listeners[key], listener);
950 + if (evt.clientY <= getAddressBarY() && evt.clientX < 0.8 * window.innerWidth) {
951 + timeout = window.setTimeout(trigger, 600);
952 + }
953 + }
803 954
804 - if (index !== -1) {
805 - listeners[key].splice(index, 1);
806 - }
807 - }
808 - }
809 -
810 - return this;
955 + function onTouchStart() {
956 + clearTimeout();
957 + touchStart = {
958 + timestamp: performance.now(),
959 + scrollY: window.scrollY,
960 + windowHeight: window.innerHeight
811 961 };
962 + }
812 963
813 - /**
814 - * Alias of removeListener
815 - */
816 - proto.off = alias('removeListener');
964 + function onTouchEnd(evt) {
965 + clearTimeout(); // did address bar appear?
817 966
818 - /**
819 - * Adds listeners in bulk using the manipulateListeners method.
820 - * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added.
821 - * You can also pass it a regular expression to add the array of listeners to all events that match it.
822 - * Yeah, this function does quite a bit. That's probably a bad thing.
823 - *
824 - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
825 - * @param {Function[]} [listeners] An optional array of listener functions to add.
826 - * @return {Object} Current instance of EventEmitter for chaining.
827 - */
828 - proto.addListeners = function addListeners(evt, listeners) {
829 - // Pass through to manipulateListeners
830 - return this.manipulateListeners(false, evt, listeners);
831 - };
967 + if (window.innerHeight > touchStart.windowHeight) {
968 + return;
969 + } // allow a tiny tiny margin for error, to not fire on clicks
832 970
833 - /**
834 - * Removes listeners in bulk using the manipulateListeners method.
835 - * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
836 - * You can also pass it an event name and an array of listeners to be removed.
837 - * You can also pass it a regular expression to remove the listeners from all events that match it.
838 - *
839 - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
840 - * @param {Function[]} [listeners] An optional array of listener functions to remove.
841 - * @return {Object} Current instance of EventEmitter for chaining.
842 - */
843 - proto.removeListeners = function removeListeners(evt, listeners) {
844 - // Pass through to manipulateListeners
845 - return this.manipulateListeners(true, evt, listeners);
846 - };
847 971
848 - /**
849 - * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level.
850 - * The first argument will determine if the listeners are removed (true) or added (false).
851 - * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
852 - * You can also pass it an event name and an array of listeners to be added/removed.
853 - * You can also pass it a regular expression to manipulate the listeners of all events that match it.
854 - *
855 - * @param {Boolean} remove True if you want to remove listeners, false if you want to add.
856 - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once.
857 - * @param {Function[]} [listeners] An optional array of listener functions to add/remove.
858 - * @return {Object} Current instance of EventEmitter for chaining.
859 - */
860 - proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
861 - var i;
862 - var value;
863 - var single = remove ? this.removeListener : this.addListener;
864 - var multiple = remove ? this.removeListeners : this.addListeners;
972 + if (window.scrollY + 20 > touchStart.scrollY) {
973 + return;
974 + }
865 975
866 - // If evt is an object then pass each of its properties to this method
867 - if (typeof evt === 'object' && !(evt instanceof RegExp)) {
868 - for (i in evt) {
869 - if (evt.hasOwnProperty(i) && (value = evt[i])) {
870 - // Pass the single listener straight through to the singular method
871 - if (typeof value === 'function') {
872 - single.call(this, i, value);
873 - }
874 - else {
875 - // Otherwise pass back to the multiple function
876 - multiple.call(this, i, value);
877 - }
878 - }
879 - }
880 - }
881 - else {
882 - // So evt must be a string
883 - // And listeners must be an array of listeners
884 - // Loop over it and pass each one to the multiple method
885 - i = listeners.length;
886 - while (i--) {
887 - single.call(this, evt, listeners[i]);
888 - }
889 - }
976 + if (performance.now() - touchStart.timestamp > 300) {
977 + return;
978 + }
890 979
891 - return this;
892 - };
980 + if (['A', 'INPUT', 'BUTTON'].indexOf(evt.target.tagName) > -1) {
981 + return;
982 + }
893 983
894 - /**
895 - * Removes all listeners from a specified event.
896 - * If you do not specify an event then all listeners will be removed.
897 - * That means every event will be emptied.
898 - * You can also pass a regex to remove all events that match it.
899 - *
900 - * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
901 - * @return {Object} Current instance of EventEmitter for chaining.
902 - */
903 - proto.removeEvent = function removeEvent(evt) {
904 - var type = typeof evt;
905 - var events = this._getEvents();
906 - var key;
984 + timeout = window.setTimeout(trigger, 800);
985 + }
907 986
908 - // Remove different things depending on the state of evt
909 - if (type === 'string') {
910 - // Remove all listeners for the specified event
911 - delete events[evt];
912 - }
913 - else if (evt instanceof RegExp) {
914 - // Remove all events matching the regex.
915 - for (key in events) {
916 - if (events.hasOwnProperty(key) && evt.test(key)) {
917 - delete events[key];
918 - }
919 - }
920 - }
921 - else {
922 - // Remove all listeners in all events
923 - delete this._events;
924 - }
987 + window.addEventListener('touchstart', onTouchStart);
988 + window.addEventListener('touchend', onTouchEnd);
989 + document.documentElement.addEventListener('mouseenter', onMouseEnter);
990 + document.documentElement.addEventListener('mouseleave', onMouseLeave);
991 + document.documentElement.addEventListener('click', clearTimeout);
992 +};
925 993
926 - return this;
927 - };
994 +},{}],8:[function(require,module,exports){
995 +"use strict";
928 996
929 - /**
930 - * Alias of removeEvent.
931 - *
932 - * Added to mirror the node API.
933 - */
934 - proto.removeAllListeners = alias('removeEvent');
997 +module.exports = function (boxes) {
998 + var pageviews;
935 999
936 - /**
937 - * Emits an event of your choice.
938 - * When emitted, every listener attached to that event will be executed.
939 - * If you pass the optional argument array then those arguments will be passed to every listener upon execution.
940 - * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
941 - * So they will not arrive within the array on the other side, they will be separate.
942 - * You can also pass a regular expression to emit to all events that match it.
943 - *
944 - * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
945 - * @param {Array} [args] Optional array of arguments to be passed to each listener.
946 - * @return {Object} Current instance of EventEmitter for chaining.
947 - */
948 - proto.emitEvent = function emitEvent(evt, args) {
949 - var listenersMap = this.getListenersAsObject(evt);
950 - var listeners;
951 - var listener;
952 - var i;
953 - var key;
954 - var response;
1000 + try {
1001 + pageviews = sessionStorage.getItem('boxzilla_pageviews') || 0;
1002 + sessionStorage.setItem('boxzilla_pageviews', ++pageviews);
1003 + } catch (e) {
1004 + pageviews = 0;
1005 + }
955 1006
956 - for (key in listenersMap) {
957 - if (listenersMap.hasOwnProperty(key)) {
958 - listeners = listenersMap[key].slice(0);
959 - i = listeners.length;
1007 + window.setTimeout(function () {
1008 + boxes.forEach(function (box) {
1009 + if (box.config.trigger.method === 'pageviews' && pageviews > box.config.trigger.value && box.mayAutoShow()) {
1010 + box.trigger();
1011 + }
1012 + });
1013 + }, 1000);
1014 +};
960 1015
961 - while (i--) {
962 - // If the listener returns true then it shall be removed from the event
963 - // The function is executed either with a basic call or an apply if there is an args array
964 - listener = listeners[i];
1016 +},{}],9:[function(require,module,exports){
1017 +"use strict";
965 1018
966 - if (listener.once === true) {
967 - this.removeListener(evt, listener.listener);
968 - }
1019 +var throttle = require('../util.js').throttle;
969 1020
970 - response = listener.listener.apply(this, args || []);
1021 +module.exports = function (boxes) {
1022 + // check triggerHeight criteria for all boxes
1023 + function checkHeightCriteria() {
1024 + var scrollY = window.hasOwnProperty('pageYOffset') ? window.pageYOffset : window.scrollTop;
1025 + scrollY = scrollY + window.innerHeight * 0.9;
1026 + boxes.forEach(function (box) {
1027 + if (!box.mayAutoShow() || box.triggerHeight <= 0) {
1028 + return;
1029 + }
971 1030
972 - if (response === this._getOnceReturnValue()) {
973 - this.removeListener(evt, listener.listener);
974 - }
975 - }
976 - }
977 - }
1031 + if (scrollY > box.triggerHeight) {
1032 + box.trigger();
1033 + } else if (box.mayRehide() && scrollY < box.triggerHeight - 5) {
1034 + // if box may auto-hide and scrollY is less than triggerHeight (with small margin of error), hide box
1035 + box.hide();
1036 + }
1037 + });
1038 + }
978 1039
979 - return this;
980 - };
1040 + window.addEventListener('touchstart', throttle(checkHeightCriteria), true);
1041 + window.addEventListener('scroll', throttle(checkHeightCriteria), true);
1042 +};
981 1043
982 - /**
983 - * Alias of emitEvent
984 - */
985 - proto.trigger = alias('emitEvent');
1044 +},{"../util.js":11}],10:[function(require,module,exports){
1045 +"use strict";
986 1046
987 - /**
988 - * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
989 - * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
990 - *
991 - * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
992 - * @param {...*} Optional additional arguments to be passed to each listener.
993 - * @return {Object} Current instance of EventEmitter for chaining.
994 - */
995 - proto.emit = function emit(evt) {
996 - var args = Array.prototype.slice.call(arguments, 1);
997 - return this.emitEvent(evt, args);
998 - };
1047 +var Timer = require('../timer.js');
999 1048
1000 - /**
1001 - * Sets the current value to check against when executing listeners. If a
1002 - * listeners return value matches the one set here then it will be removed
1003 - * after execution. This value defaults to true.
1004 - *
1005 - * @param {*} value The new value to check for when executing listeners.
1006 - * @return {Object} Current instance of EventEmitter for chaining.
1007 - */
1008 - proto.setOnceReturnValue = function setOnceReturnValue(value) {
1009 - this._onceReturnValue = value;
1010 - return this;
1011 - };
1049 +module.exports = function (boxes) {
1050 + var siteTimer = new Timer();
1051 + var pageTimer = new Timer();
1052 + var timers = {
1053 + start: function start() {
1054 + try {
1055 + var sessionTime = parseInt(sessionStorage.getItem('boxzilla_timer'));
1012 1056
1013 - /**
1014 - * Fetches the current value to check against when executing listeners. If
1015 - * the listeners return value matches this one then it should be removed
1016 - * automatically. It will return true by default.
1017 - *
1018 - * @return {*|Boolean} The current value to check for or the default, true.
1019 - * @api private
1020 - */
1021 - proto._getOnceReturnValue = function _getOnceReturnValue() {
1022 - if (this.hasOwnProperty('_onceReturnValue')) {
1023 - return this._onceReturnValue;
1057 + if (sessionTime) {
1058 + siteTimer.time = sessionTime;
1024 1059 }
1025 - else {
1026 - return true;
1027 - }
1028 - };
1060 + } catch (e) {}
1029 1061
1030 - /**
1031 - * Fetches the events object and creates one if required.
1032 - *
1033 - * @return {Object} The events storage object.
1034 - * @api private
1035 - */
1036 - proto._getEvents = function _getEvents() {
1037 - return this._events || (this._events = {});
1038 - };
1062 + siteTimer.start();
1063 + pageTimer.start();
1064 + },
1065 + stop: function stop() {
1066 + sessionStorage.setItem('boxzilla_timer', siteTimer.time);
1067 + siteTimer.stop();
1068 + pageTimer.stop();
1069 + }
1070 + }; // start timers
1039 1071
1040 - /**
1041 - * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version.
1042 - *
1043 - * @return {Function} Non conflicting EventEmitter class.
1044 - */
1045 - EventEmitter.noConflict = function noConflict() {
1046 - exports.EventEmitter = originalGlobalValue;
1047 - return EventEmitter;
1048 - };
1072 + timers.start(); // stop timers when leaving page or switching to other tab
1049 1073
1050 - // Expose the class either via AMD, CommonJS or the global object
1051 - if (typeof define === 'function' && define.amd) {
1052 - define(function () {
1053 - return EventEmitter;
1054 - });
1074 + document.addEventListener('visibilitychange', function () {
1075 + document.hidden ? timers.stop() : timers.start();
1076 + });
1077 + window.addEventListener('beforeunload', function () {
1078 + timers.stop();
1079 + });
1080 + window.setInterval(function () {
1081 + boxes.forEach(function (box) {
1082 + if (box.config.trigger.method === 'time_on_site' && siteTimer.time > box.config.trigger.value && box.mayAutoShow()) {
1083 + box.trigger();
1084 + } else if (box.config.trigger.method === 'time_on_page' && pageTimer.time > box.config.trigger.value && box.mayAutoShow()) {
1085 + box.trigger();
1086 + }
1087 + });
1088 + }, 1000);
1089 +};
1090 +
1091 +},{"../timer.js":6}],11:[function(require,module,exports){
1092 +"use strict";
1093 +
1094 +function throttle(fn, threshold, scope) {
1095 + threshold || (threshold = 800);
1096 + var last;
1097 + var deferTimer;
1098 + return function () {
1099 + var context = scope || this;
1100 + var now = +new Date();
1101 + var args = arguments;
1102 +
1103 + if (last && now < last + threshold) {
1104 + // hold on to it
1105 + clearTimeout(deferTimer);
1106 + deferTimer = setTimeout(function () {
1107 + last = now;
1108 + fn.apply(context, args);
1109 + }, threshold);
1110 + } else {
1111 + last = now;
1112 + fn.apply(context, args);
1055 1113 }
1056 - else if (typeof module === 'object' && module.exports){
1057 - module.exports = EventEmitter;
1058 - }
1059 - else {
1060 - exports.EventEmitter = EventEmitter;
1061 - }
1062 -}.call(this));
1114 + };
1115 +}
1063 1116
1117 +module.exports = {
1118 + throttle: throttle
1119 +};
1120 +
1064 1121 },{}]},{},[1]);
1065 - })();
1122 +; })();