| @@ -1,54 +1,131 @@ | ||
| 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 | + // helper function for setting CSS styles | |
| 11 | + function css(element, styles) { | |
| 12 | + if( styles.background_color ) { | |
| 13 | + element.style.background = styles.background_color; | |
| 14 | + } | |
| 14 | 15 | |
| 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; | |
| 16 | + if( styles.color ) { | |
| 17 | + element.style.color = styles.color; | |
| 18 | + } | |
| 20 | 19 | |
| 21 | - // create box | |
| 22 | - var box = Boxzilla.create( boxOpts.id, boxOpts); | |
| 23 | - | |
| 24 | - // add custom css to box | |
| 25 | - css(box.element, boxOpts.css); | |
| 26 | -} | |
| 20 | + if( styles.border_color ) { | |
| 21 | + element.style.borderColor = styles.border_color; | |
| 22 | + } | |
| 27 | 23 | |
| 28 | -function css(element, styles) { | |
| 29 | - if( styles.background_color ) { | |
| 30 | - element.style.background = styles.background_color; | |
| 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 | + } | |
| 31 | 35 | } |
| 32 | 36 | |
| 33 | - if( styles.color ) { | |
| 34 | - element.style.color = styles.color; | |
| 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 i=0; i < options.boxes.length; i++ ) { | |
| 46 | + // get opts | |
| 47 | + var boxOpts = options.boxes[i]; | |
| 48 | + boxOpts.testMode = isLoggedIn && options.testMode; | |
| 49 | + | |
| 50 | + // set box content element | |
| 51 | + boxOpts.content = document.getElementById('boxzilla-box-'+ boxOpts.id +'-content'); | |
| 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 | + box.element.firstChild.firstChild.className += " first-child"; | |
| 63 | + box.element.firstChild.lastChild.className += " last-child"; | |
| 64 | + | |
| 65 | + // maybe show box right away | |
| 66 | + if( box.fits() && locationHashRefersBox(box) ) { | |
| 67 | + box.show(); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + // set flag to prevent initialising twice | |
| 72 | + options.inited = true; | |
| 73 | + | |
| 74 | + // trigger "done" event. | |
| 75 | + Boxzilla.trigger('done'); | |
| 76 | + | |
| 77 | + // maybe open box with MC4WP form in it | |
| 78 | + maybeOpenMailChimpForWordPressBox(); | |
| 35 | 79 | } |
| 36 | 80 | |
| 37 | - if( styles.border_color ) { | |
| 38 | - element.style.borderColor = styles.border_color; | |
| 81 | + function locationHashRefersBox(box) { | |
| 82 | + if( ! window.location.hash || 0 === window.location.hash.length ) { | |
| 83 | + return false; | |
| 84 | + } | |
| 85 | + | |
| 86 | + var elementId = window.location.hash.substring(1); | |
| 87 | + | |
| 88 | + // only attempt on strings looking like an ID | |
| 89 | + var regex = /^[a-zA-Z\-\_0-9]+$/; | |
| 90 | + if( ! regex.test(elementId) ) { | |
| 91 | + return false; | |
| 92 | + } | |
| 93 | + | |
| 94 | + if( elementId === box.element.id ) { | |
| 95 | + return true; | |
| 96 | + } else if( box.element.querySelector('#' + elementId) ) { | |
| 97 | + return true; | |
| 98 | + } | |
| 99 | + | |
| 100 | + return false; | |
| 39 | 101 | } |
| 40 | 102 | |
| 41 | - if( styles.border_width ) { | |
| 42 | - element.style.borderWidth = parseInt(styles.border_width) + "px"; | |
| 43 | - } | |
| 103 | + function maybeOpenMailChimpForWordPressBox() { | |
| 104 | + if( typeof(window.mc4wp_forms_config) !== "object" || ! window.mc4wp_forms_config.submitted_form ) { | |
| 105 | + return; | |
| 106 | + } | |
| 44 | 107 | |
| 45 | - if( styles.border_style ) { | |
| 46 | - element.style.borderStyle = styles.border_style; | |
| 108 | + var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id; | |
| 109 | + var boxes = Boxzilla.boxes; | |
| 110 | + for( var boxId in boxes ) { | |
| 111 | + if(!boxes.hasOwnProperty(boxId)) { continue; } | |
| 112 | + var box = boxes[boxId]; | |
| 113 | + if( box.element.querySelector(selector)) { | |
| 114 | + box.show(); | |
| 115 | + return; | |
| 116 | + } | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + // print message when test mode is enabled | |
| 121 | + var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 122 | + if( isLoggedIn && options.testMode ) { | |
| 123 | + console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' ); | |
| 47 | 124 | } |
| 48 | 125 | |
| 49 | - if( styles.width ) { | |
| 50 | - element.style.maxWidth = parseInt(styles.width) + "px"; | |
| 51 | - } | |
| 52 | -} | |
| 126 | + // init boxzilla | |
| 127 | + Boxzilla.init(); | |
| 53 | 128 | |
| 54 | -window.Boxzilla = Boxzilla; | |
| 129 | + // on window.load, create DOM elements for boxes | |
| 130 | + window.addEventListener('load', createBoxesFromConfig); | |
| 131 | +})(); | |