| @@ -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,14 +46,10 @@ | ||
| 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); | |
| 64 | - } | |
| 50 | + // set box content element | |
| 51 | + boxOpts.content = document.getElementById('boxzilla-box-'+ boxOpts.id +'-content'); | |
| 65 | 52 | |
| 66 | 53 | // create box |
| 67 | 54 | var box = Boxzilla.create(boxOpts.id, boxOpts); |
| 68 | 55 | |
| @@ -73,8 +60,13 @@ | ||
| 73 | 60 | css(box.element, boxOpts.css); |
| 74 | 61 | |
| 75 | 62 | box.element.firstChild.firstChild.className += " first-child"; |
| 76 | 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 | + } | |
| 77 | 69 | } |
| 78 | 70 | |
| 79 | 71 | // set flag to prevent initialising twice |
| 80 | 72 | options.inited = true; |
| @@ -80,24 +72,60 @@ | ||
| 80 | 72 | options.inited = true; |
| 81 | 73 | |
| 82 | 74 | // trigger "done" event. |
| 83 | 75 | Boxzilla.trigger('done'); |
| 76 | + | |
| 77 | + // maybe open box with MC4WP form in it | |
| 78 | + maybeOpenMailChimpForWordPressBox(); | |
| 84 | 79 | } |
| 85 | 80 | |
| 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 | - } | |
| 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; | |
| 101 | + } | |
| 102 | + | |
| 103 | + function maybeOpenMailChimpForWordPressBox() { | |
| 104 | + if( typeof(window.mc4wp_forms_config) !== "object" || ! window.mc4wp_forms_config.submitted_form ) { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 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; | |
| 97 | 116 | } |
| 98 | 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.' ); | |
| 99 | 124 | } |
| 100 | 125 | |
| 101 | - window.addEventListener('load', openMailChimpForWordPressBox); | |
| 102 | - createBoxesFromConfig(); | |
| 126 | + // init boxzilla | |
| 127 | + Boxzilla.init(); | |
| 128 | + | |
| 129 | + // on window.load, create DOM elements for boxes | |
| 130 | + window.addEventListener('load', createBoxesFromConfig); | |
| 103 | 131 | })(); |