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