| @@ -1,80 +1,117 @@ | ||
| 1 | -'use strict'; | |
| 1 | +(function() { | |
| 2 | + 'use strict'; | |
| 2 | 3 | |
| 3 | -var Boxzilla = require('boxzilla'); | |
| 4 | -var options = window.boxzilla_options; | |
| 5 | -var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 4 | + var Boxzilla = require('boxzilla'); | |
| 5 | + var options = window.boxzilla_options; | |
| 6 | 6 | |
| 7 | -// print message when test mode is enabled | |
| 8 | -if( isLoggedIn && options.testMode ) { | |
| 9 | - console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' ); | |
| 10 | -} | |
| 7 | + // expose Boxzilla object to window | |
| 8 | + window.Boxzilla = Boxzilla; | |
| 11 | 9 | |
| 12 | -// init boxzilla | |
| 13 | -Boxzilla.init(); | |
| 10 | + function ready(fn) { | |
| 11 | + if (document.readyState != 'loading'){ | |
| 12 | + fn(); | |
| 13 | + } else { | |
| 14 | + document.addEventListener('DOMContentLoaded', fn); | |
| 15 | + } | |
| 16 | + } | |
| 14 | 17 | |
| 15 | -// create boxes from options | |
| 16 | -for( var i=0; i < options.boxes.length; i++ ) { | |
| 17 | - // get opts | |
| 18 | - var boxOpts = options.boxes[i]; | |
| 19 | - boxOpts.testMode = isLoggedIn && options.testMode; | |
| 18 | + // helper function for setting CSS styles | |
| 19 | + function css(element, styles) { | |
| 20 | + if( styles.background_color ) { | |
| 21 | + element.style.background = styles.background_color; | |
| 22 | + } | |
| 20 | 23 | |
| 21 | - // fix http:// links in box content.... | |
| 22 | - if( window.location.origin.substring(0, 5) === "https" ) { | |
| 23 | - boxOpts.content = boxOpts.content.replace(window.location.origin.replace("https", "http"), window.location.origin); | |
| 24 | + if( styles.color ) { | |
| 25 | + element.style.color = styles.color; | |
| 26 | + } | |
| 27 | + | |
| 28 | + if( styles.border_color ) { | |
| 29 | + element.style.borderColor = styles.border_color; | |
| 30 | + } | |
| 31 | + | |
| 32 | + if( styles.border_width ) { | |
| 33 | + element.style.borderWidth = parseInt(styles.border_width) + "px"; | |
| 34 | + } | |
| 35 | + | |
| 36 | + if( styles.border_style ) { | |
| 37 | + element.style.borderStyle = styles.border_style; | |
| 38 | + } | |
| 39 | + | |
| 40 | + if( styles.width ) { | |
| 41 | + element.style.maxWidth = parseInt(styles.width) + "px"; | |
| 42 | + } | |
| 24 | 43 | } |
| 25 | 44 | |
| 26 | - // create box | |
| 27 | - var box = Boxzilla.create( boxOpts.id, boxOpts); | |
| 28 | - | |
| 29 | - // add custom css to box | |
| 30 | - css(box.element, boxOpts.css); | |
| 31 | -} | |
| 45 | + function createBoxesFromConfig() { | |
| 46 | + var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 32 | 47 | |
| 33 | -// helper function for setting CSS styles | |
| 34 | -function css(element, styles) { | |
| 35 | - if( styles.background_color ) { | |
| 36 | - element.style.background = styles.background_color; | |
| 37 | - } | |
| 48 | + // failsafe against including script twice. | |
| 49 | + if( options.inited ) { | |
| 50 | + return; | |
| 51 | + } | |
| 38 | 52 | |
| 39 | - if( styles.color ) { | |
| 40 | - element.style.color = styles.color; | |
| 41 | - } | |
| 53 | + // print message when test mode is enabled | |
| 54 | + if( isLoggedIn && options.testMode ) { | |
| 55 | + console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' ); | |
| 56 | + } | |
| 42 | 57 | |
| 43 | - if( styles.border_color ) { | |
| 44 | - element.style.borderColor = styles.border_color; | |
| 45 | - } | |
| 58 | + // init boxzilla | |
| 59 | + Boxzilla.init(); | |
| 46 | 60 | |
| 47 | - if( styles.border_width ) { | |
| 48 | - element.style.borderWidth = parseInt(styles.border_width) + "px"; | |
| 49 | - } | |
| 61 | + // create boxes from options | |
| 62 | + for( var i=0; i < options.boxes.length; i++ ) { | |
| 63 | + // get opts | |
| 64 | + var boxOpts = options.boxes[i]; | |
| 65 | + boxOpts.testMode = isLoggedIn && options.testMode; | |
| 50 | 66 | |
| 51 | - if( styles.border_style ) { | |
| 52 | - element.style.borderStyle = styles.border_style; | |
| 53 | - } | |
| 67 | + // fix http:// links in box content.... | |
| 68 | + if( window.location.protocol === "https:" && window.location.host ) { | |
| 69 | + var o = "http://" + window.location.host; | |
| 70 | + var n = o.replace('http://', 'https://'); | |
| 71 | + boxOpts.content = boxOpts.content.replace(o, n); | |
| 72 | + } | |
| 54 | 73 | |
| 55 | - if( styles.width ) { | |
| 56 | - element.style.maxWidth = parseInt(styles.width) + "px"; | |
| 74 | + // create box | |
| 75 | + var box = Boxzilla.create(boxOpts.id, boxOpts); | |
| 76 | + | |
| 77 | + // add box slug to box element as classname | |
| 78 | + box.element.className = box.element.className + ' boxzilla-' + boxOpts.post.slug; | |
| 79 | + | |
| 80 | + // add custom css to box | |
| 81 | + css(box.element, boxOpts.css); | |
| 82 | + | |
| 83 | + box.element.firstChild.firstChild.className += " first-child"; | |
| 84 | + box.element.firstChild.lastChild.className += " last-child"; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * If a MailChimp for WordPress form was submitted, open the box containing that form (if any) | |
| 89 | + * | |
| 90 | + * TODO: Just set location hash from MailChimp for WP? | |
| 91 | + */ | |
| 92 | + window.addEventListener('load', openMailChimpForWordPressBox); | |
| 93 | + | |
| 94 | + options.inited = true; | |
| 95 | + | |
| 96 | + // trigger "done" event. | |
| 97 | + Boxzilla.trigger('done'); | |
| 57 | 98 | } |
| 58 | -} | |
| 59 | 99 | |
| 60 | -/** | |
| 61 | - * If a MailChimp for WordPress form was submitted, open the box containing that form (if any) | |
| 62 | - * | |
| 63 | - * TODO: Just set location hash from MailChimp for WP? | |
| 64 | - */ | |
| 65 | -window.addEventListener('load', function() { | |
| 66 | - if( typeof(window.mc4wp_forms_config) === "object" && window.mc4wp_forms_config.submitted_form ) { | |
| 67 | - var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id; | |
| 68 | - var boxes = Boxzilla.boxes; | |
| 69 | - for( var boxId in boxes ) { | |
| 70 | - if(!boxes.hasOwnProperty(boxId)) { continue; } | |
| 71 | - var box = boxes[boxId]; | |
| 72 | - if( box.element.querySelector(selector)) { | |
| 73 | - box.show(); | |
| 74 | - return; | |
| 100 | + function openMailChimpForWordPressBox() { | |
| 101 | + if( typeof(window.mc4wp_forms_config) === "object" && window.mc4wp_forms_config.submitted_form ) { | |
| 102 | + var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id; | |
| 103 | + var boxes = Boxzilla.boxes; | |
| 104 | + for( var boxId in boxes ) { | |
| 105 | + if(!boxes.hasOwnProperty(boxId)) { continue; } | |
| 106 | + var box = boxes[boxId]; | |
| 107 | + if( box.element.querySelector(selector)) { | |
| 108 | + box.show(); | |
| 109 | + return; | |
| 110 | + } | |
| 75 | 111 | } |
| 76 | 112 | } |
| 77 | 113 | } |
| 78 | -}); | |
| 79 | 114 | |
| 80 | -window.Boxzilla = Boxzilla; | |
| 115 | + // create boxes as soon as document.ready fires | |
| 116 | + ready(createBoxesFromConfig); | |
| 117 | +})(); | |