| @@ -1,103 +1,80 @@ | ||
| 1 | -(function() { | |
| 2 | - 'use strict'; | |
| 1 | +'use strict'; | |
| 3 | 2 | |
| 4 | - var Boxzilla = require('boxzilla'); | |
| 5 | - var options = window.boxzilla_options; | |
| 3 | +var Boxzilla = require('boxzilla'); | |
| 4 | +var options = window.boxzilla_options; | |
| 5 | +var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 6 | 6 | |
| 7 | - // expose Boxzilla object to window | |
| 8 | - window.Boxzilla = Boxzilla; | |
| 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 | +} | |
| 9 | 11 | |
| 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 | - } | |
| 12 | +// init boxzilla | |
| 13 | +Boxzilla.init(); | |
| 15 | 14 | |
| 16 | - if( styles.color ) { | |
| 17 | - element.style.color = styles.color; | |
| 18 | - } | |
| 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; | |
| 19 | 20 | |
| 20 | - if( styles.border_color ) { | |
| 21 | - element.style.borderColor = styles.border_color; | |
| 22 | - } | |
| 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 | + } | |
| 23 | 25 | |
| 24 | - if( styles.border_width ) { | |
| 25 | - element.style.borderWidth = parseInt(styles.border_width) + "px"; | |
| 26 | - } | |
| 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 | +} | |
| 27 | 32 | |
| 28 | - if( styles.border_style ) { | |
| 29 | - element.style.borderStyle = styles.border_style; | |
| 30 | - } | |
| 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 | + } | |
| 31 | 38 | |
| 32 | - if( styles.width ) { | |
| 33 | - element.style.maxWidth = parseInt(styles.width) + "px"; | |
| 34 | - } | |
| 39 | + if( styles.color ) { | |
| 40 | + element.style.color = styles.color; | |
| 35 | 41 | } |
| 36 | 42 | |
| 37 | - function createBoxesFromConfig() { | |
| 38 | - var isLoggedIn = document.body.className.indexOf('logged-in') > -1; | |
| 43 | + if( styles.border_color ) { | |
| 44 | + element.style.borderColor = styles.border_color; | |
| 45 | + } | |
| 39 | 46 | |
| 40 | - // failsafe against including script twice. | |
| 41 | - if( options.inited ) { | |
| 42 | - return; | |
| 43 | - } | |
| 47 | + if( styles.border_width ) { | |
| 48 | + element.style.borderWidth = parseInt(styles.border_width) + "px"; | |
| 49 | + } | |
| 44 | 50 | |
| 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 | - } | |
| 51 | + if( styles.border_style ) { | |
| 52 | + element.style.borderStyle = styles.border_style; | |
| 53 | + } | |
| 49 | 54 | |
| 50 | - // init boxzilla | |
| 51 | - Boxzilla.init(); | |
| 52 | - | |
| 53 | - // create boxes from options | |
| 54 | - for( var i=0; i < options.boxes.length; i++ ) { | |
| 55 | - // get opts | |
| 56 | - var boxOpts = options.boxes[i]; | |
| 57 | - boxOpts.testMode = isLoggedIn && options.testMode; | |
| 58 | - | |
| 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 | - } | |
| 65 | - | |
| 66 | - // create box | |
| 67 | - var box = Boxzilla.create(boxOpts.id, boxOpts); | |
| 68 | - | |
| 69 | - // add box slug to box element as classname | |
| 70 | - box.element.className = box.element.className + ' boxzilla-' + boxOpts.post.slug; | |
| 71 | - | |
| 72 | - // add custom css to box | |
| 73 | - css(box.element, boxOpts.css); | |
| 74 | - | |
| 75 | - box.element.firstChild.firstChild.className += " first-child"; | |
| 76 | - box.element.firstChild.lastChild.className += " last-child"; | |
| 77 | - } | |
| 78 | - | |
| 79 | - // set flag to prevent initialising twice | |
| 80 | - options.inited = true; | |
| 81 | - | |
| 82 | - // trigger "done" event. | |
| 83 | - Boxzilla.trigger('done'); | |
| 55 | + if( styles.width ) { | |
| 56 | + element.style.maxWidth = parseInt(styles.width) + "px"; | |
| 84 | 57 | } |
| 58 | +} | |
| 85 | 59 | |
| 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 | - } | |
| 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; | |
| 97 | 75 | } |
| 98 | 76 | } |
| 99 | 77 | } |
| 78 | +}); | |
| 100 | 79 | |
| 101 | - window.addEventListener('load', openMailChimpForWordPressBox); | |
| 102 | - createBoxesFromConfig(); | |
| 103 | -})(); | |
| 80 | +window.Boxzilla = Boxzilla; | |