| @@ -34,23 +34,14 @@ | ||
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | function createBoxesFromConfig() { |
| 38 | - var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 39 | - | |
| 38 | + | |
| 40 | 39 | // failsafe against including script twice. |
| 41 | 40 | if( options.inited ) { |
| 42 | 41 | return; |
| 43 | 42 | } |
| 44 | 43 | |
| 45 | - // print message when test mode is enabled | |
| 46 | - if( isLoggedIn && options.testMode ) { | |
| 47 | - console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' ); | |
| 48 | - } | |
| 49 | - | |
| 50 | - // init boxzilla | |
| 51 | - Boxzilla.init(); | |
| 52 | - | |
| 53 | 44 | // create boxes from options |
| 54 | 45 | for( var i=0; i < options.boxes.length; i++ ) { |
| 55 | 46 | // get opts |
| 56 | 47 | var boxOpts = options.boxes[i]; |
| @@ -55,15 +46,17 @@ | ||
| 55 | 46 | // get opts |
| 56 | 47 | var boxOpts = options.boxes[i]; |
| 57 | 48 | boxOpts.testMode = isLoggedIn && options.testMode; |
| 58 | 49 | |
| 59 | - // fix http:// links in box content.... | |
| 60 | - if( window.location.protocol === "https:" && window.location.host ) { | |
| 61 | - var o = "http://" + window.location.host; | |
| 62 | - var n = o.replace('http://', 'https://'); | |
| 63 | - boxOpts.content = boxOpts.content.replace(o, n); | |
| 50 | + // find box content element, bail if not found | |
| 51 | + var boxContentElement = document.getElementById('boxzilla-box-'+ boxOpts.id +'-content'); | |
| 52 | + if( ! boxContentElement) { | |
| 53 | + continue; | |
| 64 | 54 | } |
| 65 | 55 | |
| 56 | + // use element as content option | |
| 57 | + boxOpts.content = boxContentElement; | |
| 58 | + | |
| 66 | 59 | // create box |
| 67 | 60 | var box = Boxzilla.create(boxOpts.id, boxOpts); |
| 68 | 61 | |
| 69 | 62 | // add box slug to box element as classname |
| @@ -73,8 +66,13 @@ | ||
| 73 | 66 | css(box.element, boxOpts.css); |
| 74 | 67 | |
| 75 | 68 | box.element.firstChild.firstChild.className += " first-child"; |
| 76 | 69 | box.element.firstChild.lastChild.className += " last-child"; |
| 70 | + | |
| 71 | + // maybe show box right away | |
| 72 | + if( box.fits() && locationHashRefersBox(box) ) { | |
| 73 | + box.show(); | |
| 74 | + } | |
| 77 | 75 | } |
| 78 | 76 | |
| 79 | 77 | // set flag to prevent initialising twice |
| 80 | 78 | options.inited = true; |
| @@ -80,24 +78,60 @@ | ||
| 80 | 78 | options.inited = true; |
| 81 | 79 | |
| 82 | 80 | // trigger "done" event. |
| 83 | 81 | Boxzilla.trigger('done'); |
| 82 | + | |
| 83 | + // maybe open box with MC4WP form in it | |
| 84 | + maybeOpenMailChimpForWordPressBox(); | |
| 84 | 85 | } |
| 85 | 86 | |
| 86 | - function openMailChimpForWordPressBox() { | |
| 87 | - if( typeof(window.mc4wp_forms_config) === "object" && window.mc4wp_forms_config.submitted_form ) { | |
| 88 | - var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id; | |
| 89 | - var boxes = Boxzilla.boxes; | |
| 90 | - for( var boxId in boxes ) { | |
| 91 | - if(!boxes.hasOwnProperty(boxId)) { continue; } | |
| 92 | - var box = boxes[boxId]; | |
| 93 | - if( box.element.querySelector(selector)) { | |
| 94 | - box.show(); | |
| 95 | - return; | |
| 96 | - } | |
| 87 | + function locationHashRefersBox(box) { | |
| 88 | + if( ! window.location.hash || 0 === window.location.hash.length ) { | |
| 89 | + return false; | |
| 90 | + } | |
| 91 | + | |
| 92 | + var elementId = window.location.hash.substring(1); | |
| 93 | + | |
| 94 | + // only attempt on strings looking like an ID | |
| 95 | + var regex = /^[a-zA-Z\-\_0-9]+$/; | |
| 96 | + if( ! regex.test(elementId) ) { | |
| 97 | + return false; | |
| 98 | + } | |
| 99 | + | |
| 100 | + if( elementId === box.element.id ) { | |
| 101 | + return true; | |
| 102 | + } else if( box.element.querySelector('#' + elementId) ) { | |
| 103 | + return true; | |
| 104 | + } | |
| 105 | + | |
| 106 | + return false; | |
| 107 | + } | |
| 108 | + | |
| 109 | + function maybeOpenMailChimpForWordPressBox() { | |
| 110 | + if( typeof(window.mc4wp_forms_config) !== "object" || ! window.mc4wp_forms_config.submitted_form ) { | |
| 111 | + return; | |
| 112 | + } | |
| 113 | + | |
| 114 | + var selector = '#' + window.mc4wp_forms_config.submitted_form.element_id; | |
| 115 | + var boxes = Boxzilla.boxes; | |
| 116 | + for( var boxId in boxes ) { | |
| 117 | + if(!boxes.hasOwnProperty(boxId)) { continue; } | |
| 118 | + var box = boxes[boxId]; | |
| 119 | + if( box.element.querySelector(selector)) { | |
| 120 | + box.show(); | |
| 121 | + return; | |
| 97 | 122 | } |
| 98 | 123 | } |
| 124 | + } | |
| 125 | + | |
| 126 | + // print message when test mode is enabled | |
| 127 | + var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 128 | + if( isLoggedIn && options.testMode ) { | |
| 129 | + console.log( 'Boxzilla: Test mode is enabled. Please disable test mode if you\'re done testing.' ); | |
| 99 | 130 | } |
| 100 | 131 | |
| 101 | - window.addEventListener('load', openMailChimpForWordPressBox); | |
| 102 | - createBoxesFromConfig(); | |
| 132 | + // init boxzilla | |
| 133 | + Boxzilla.init(); | |
| 134 | + | |
| 135 | + // on window.load, create DOM elements for boxes | |
| 136 | + window.addEventListener('load', createBoxesFromConfig); | |
| 103 | 137 | })(); |