| 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 |
box.element.firstChild.firstChild.className += " first-child"; |
| 70 |
box.element.firstChild.lastChild.className += " last-child"; |
| 71 |
|
| 72 |
// maybe show box right away |
| 73 |
if( box.fits() && locationHashRefersBox(box) ) { |
| 74 |
box.show(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// set flag to prevent initialising twice |
| 79 |
options.inited = true; |
| 80 |
|
| 81 |
// trigger "done" event. |
| 82 |
Boxzilla.trigger('done'); |
| 83 |
|
| 84 |
// maybe open box with MC4WP form in it |
| 85 |
maybeOpenMailChimpForWordPressBox(); |
| 86 |
} |
| 87 |
|
| 88 |
function locationHashRefersBox(box) { |
| 89 |
if( ! window.location.hash || 0 === window.location.hash.length ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
var elementId = window.location.hash.substring(1); |
| 94 |
|
| 95 |
// only attempt on strings looking like an ID |
| 96 |
var regex = /^[a-zA-Z\-\_0-9]+$/; |
| 97 |
if( ! regex.test(elementId) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
if( elementId === box.element.id ) { |
| 102 |
return true; |
| 103 |
} else if( box.element.querySelector('#' + elementId) ) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
function maybeOpenMailChimpForWordPressBox() { |
| 111 |
if( typeof(window.mc4wp_forms_config) !== "object" || ! window.mc4wp_forms_config.submitted_form ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id; |
| 116 |
var boxes = Boxzilla.boxes; |
| 117 |
for( var boxId in boxes ) { |
| 118 |
if(!boxes.hasOwnProperty(boxId)) { continue; } |
| 119 |
var box = boxes[boxId]; |
| 120 |
if( box.element.querySelector(selector)) { |
| 121 |
box.show(); |
| 122 |
return; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// print message when test mode is enabled |
| 128 |
var isLoggedIn = document.body.className.indexOf('logged-in') > -1; |
| 129 |
if( isLoggedIn && options.testMode ) { |
| 130 |
console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' ); |
| 131 |
} |
| 132 |
|
| 133 |
// init boxzilla |
| 134 |
Boxzilla.init(); |
| 135 |
|
| 136 |
// on window.load, create DOM elements for boxes |
| 137 |
window.addEventListener('load', createBoxesFromConfig); |
| 138 |
})(); |
| 139 |
|