controller.js
364 lines
| 1 | /** |
| 2 | * Component Controller |
| 3 | * |
| 4 | * Version 1.2 |
| 5 | * For Strong Testimonials version 2.31 |
| 6 | * |
| 7 | * @namespace window.strongControllerParms |
| 8 | */ |
| 9 | |
| 10 | 'use strict'; |
| 11 | |
| 12 | var debugit = false; |
| 13 | |
| 14 | var strongController = { |
| 15 | |
| 16 | grids: {}, |
| 17 | |
| 18 | iframes: {}, |
| 19 | |
| 20 | defaults: { |
| 21 | initializeOn: 'windowLoad', |
| 22 | method: '', |
| 23 | universalTimer: 500, |
| 24 | observerTimer: 500, |
| 25 | containerId: 'page', // = what we listen to (try page > content > primary) |
| 26 | addedNodeId: 'content', // = what we listen for |
| 27 | event: '', |
| 28 | script: '', |
| 29 | debug: false |
| 30 | }, |
| 31 | |
| 32 | config: {}, |
| 33 | |
| 34 | setup: function (settings) { |
| 35 | // Convert strings to integers |
| 36 | settings.universalTimer = parseInt(settings.universalTimer); |
| 37 | settings.observerTimer = parseInt(settings.observerTimer); |
| 38 | // Convert strings to booleans |
| 39 | settings.debug = !!settings.debug; |
| 40 | debugit = settings.debug; |
| 41 | this.config = jQuery.extend({}, this.defaults, settings); |
| 42 | }, |
| 43 | |
| 44 | mutationObserver: window.MutationObserver || window.WebKitMutationObserver, |
| 45 | |
| 46 | eventListenerSupported: window.addEventListener, |
| 47 | |
| 48 | checkInit: function () { |
| 49 | return jQuery('.strong-view[data-state="idle"]').length; |
| 50 | }, |
| 51 | |
| 52 | /** |
| 53 | * Initialize sliders. |
| 54 | */ |
| 55 | initSliders: function () { |
| 56 | var sliders = jQuery('.strong-view.slider-container[data-state="idle"]'); |
| 57 | if (debugit) console.log('sliders found:', sliders.length); |
| 58 | if (sliders.length) { |
| 59 | // Initialize independently |
| 60 | sliders.each(function () { |
| 61 | jQuery(this).strongSlider(); |
| 62 | }); |
| 63 | } |
| 64 | }, |
| 65 | |
| 66 | /** |
| 67 | * Initialize paginated views. |
| 68 | */ |
| 69 | initPagers: function () { |
| 70 | var pagers = jQuery('.strong-pager[data-state="idle"]'); |
| 71 | if (debugit) console.log('pagers found:', pagers.length); |
| 72 | if (pagers.length) { |
| 73 | pagers.each(function () { |
| 74 | jQuery(this).strongPager(); |
| 75 | }); |
| 76 | } |
| 77 | }, |
| 78 | |
| 79 | /** |
| 80 | * Initialize layouts. |
| 81 | */ |
| 82 | initLayouts: function () { |
| 83 | /* |
| 84 | * Masonry |
| 85 | */ |
| 86 | this.grids = jQuery('.strong-view[data-state="idle"] .strong-masonry'); |
| 87 | if (debugit) console.log('Masonry found:', this.grids.length); |
| 88 | if (this.grids.length) { |
| 89 | // Add our element sizing. |
| 90 | this.grids.prepend('<div class="grid-sizer"></div><div class="gutter-sizer"></div>'); |
| 91 | |
| 92 | // Initialize Masonry after images are loaded. |
| 93 | this.grids.imagesLoaded(function () { |
| 94 | strongController.grids.masonry({ |
| 95 | columnWidth: '.grid-sizer', |
| 96 | gutter: '.gutter-sizer', |
| 97 | itemSelector: '.testimonial', |
| 98 | percentPosition: true |
| 99 | }); |
| 100 | |
| 101 | strongController.grids.closest('.strong-view').attr('data-state', 'init'); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | }, |
| 106 | |
| 107 | /** |
| 108 | * Initialize form validation. |
| 109 | */ |
| 110 | initForm: function () { |
| 111 | var forms = jQuery('.strong-form[data-state="idle"]'); |
| 112 | var messages = jQuery('.testimonial-success'); |
| 113 | if (debugit) console.log('forms found:', forms.length); |
| 114 | if (debugit) console.log('messages found:', messages.length); |
| 115 | if (forms.length || messages.length) { |
| 116 | strongValidation.init(); |
| 117 | // initialize Captcha plugins here |
| 118 | } |
| 119 | }, |
| 120 | |
| 121 | /** |
| 122 | * Look for iframes. |
| 123 | */ |
| 124 | initIframes: function () { |
| 125 | this.iframes = jQuery('iframe'); |
| 126 | }, |
| 127 | |
| 128 | /** |
| 129 | * Listen for custom events from other scripts. |
| 130 | */ |
| 131 | customEvents: function () { |
| 132 | addEventListener( 'toggleFullContent', function (event) { |
| 133 | if (strongController.grids.length) { |
| 134 | strongController.grids.masonry(); |
| 135 | } |
| 136 | }); |
| 137 | }, |
| 138 | |
| 139 | /** |
| 140 | * Create observer that reacts to nodes added or removed. |
| 141 | * |
| 142 | * https://stackoverflow.com/a/14570614/51600 |
| 143 | */ |
| 144 | observer: function (obj, callback) { |
| 145 | if (this.mutationObserver) { |
| 146 | |
| 147 | // Define a new observer |
| 148 | var obs = new this.mutationObserver(function (mutations) { |
| 149 | // Loop through mutations |
| 150 | for (var i = 0; i < mutations.length; i++) { |
| 151 | if (mutations[i].addedNodes.length) { |
| 152 | if (debugit) console.log('mutation observed', mutations); |
| 153 | // Loop through added nodes |
| 154 | for (var j = 0; j < mutations[i].addedNodes.length; j++) { |
| 155 | if (mutations[i].addedNodes[j].id === strongController.config.containerId) { |
| 156 | if (debugit) console.log('+', strongController.config.containerId); |
| 157 | callback(); |
| 158 | return; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | }); |
| 164 | // Have the observer observe obj for changes |
| 165 | obs.observe(obj, {childList: true, subtree: true}); |
| 166 | |
| 167 | } else if (this.eventListenerSupported) { |
| 168 | |
| 169 | obj.addEventListener('DOMNodeInserted', function (e) { |
| 170 | /** currentTarget **/ |
| 171 | if (e.currentTarget.id === obj.id) { |
| 172 | if (debugit) console.log('DOMNodeInserted:', e.currentTarget.id); |
| 173 | callback(); |
| 174 | } |
| 175 | }, false); |
| 176 | |
| 177 | } |
| 178 | }, |
| 179 | |
| 180 | /** |
| 181 | * Timer variables |
| 182 | */ |
| 183 | intervalId: null, |
| 184 | timeoutId: null, |
| 185 | |
| 186 | /** |
| 187 | * Set up interval |
| 188 | */ |
| 189 | newInterval: function () { |
| 190 | strongController.intervalId = setInterval(function tick () { |
| 191 | if (debugit) console.log('tick > checkInit', strongController.checkInit()); |
| 192 | |
| 193 | // Check for uninitialized components (sliders, paginated, layouts) |
| 194 | if (strongController.checkInit()) { |
| 195 | strongController.start(); |
| 196 | } |
| 197 | }, strongController.config.universalTimer); |
| 198 | }, |
| 199 | |
| 200 | /** |
| 201 | * Set up timeout |
| 202 | */ |
| 203 | newTimeout: function () { |
| 204 | strongController.timeoutId = setTimeout(function tick () { |
| 205 | if (debugit) console.log('tick > checkInit', strongController.checkInit()); |
| 206 | |
| 207 | // Check for uninitialized components (sliders, paginated, layouts) |
| 208 | if (strongController.checkInit()) { |
| 209 | strongController.start(); |
| 210 | } |
| 211 | }, strongController.config.observerTimer); |
| 212 | }, |
| 213 | |
| 214 | /** |
| 215 | * Initialize controller. |
| 216 | */ |
| 217 | init: function () { |
| 218 | if (debugit) console.log('strongController init'); |
| 219 | |
| 220 | // Get settings |
| 221 | var settings = {}; |
| 222 | if (typeof window.strongControllerParms !== 'undefined') { |
| 223 | settings = window.strongControllerParms; |
| 224 | } else { |
| 225 | if (debugit) console.log('settings not found'); |
| 226 | } |
| 227 | |
| 228 | // Configure |
| 229 | this.setup(settings); |
| 230 | if (debugit) console.log('config', this.config); |
| 231 | |
| 232 | /* |
| 233 | * Start on specific event |
| 234 | */ |
| 235 | if ('documentReady' === this.config.initializeOn) { |
| 236 | |
| 237 | jQuery(document).ready(function () { |
| 238 | if (debugit) console.log('document ready'); |
| 239 | // Start components. |
| 240 | strongController.start(); |
| 241 | // Listen. |
| 242 | strongController.listen(); |
| 243 | }); |
| 244 | |
| 245 | } else { // Fail-safe |
| 246 | |
| 247 | jQuery(window).on('load', function () { |
| 248 | if (debugit) console.log('window load'); |
| 249 | // Start components. |
| 250 | strongController.start(); |
| 251 | // Listen. |
| 252 | strongController.listen(); |
| 253 | }); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | // Regardless of initializeOn setting, check for embeds in Masonry on window load. |
| 258 | jQuery(window).on('load', function () { |
| 259 | strongController.listenForIframeReady(); |
| 260 | }); |
| 261 | |
| 262 | }, |
| 263 | |
| 264 | /** |
| 265 | * Start components. |
| 266 | */ |
| 267 | start: function () { |
| 268 | if (debugit) console.log('start'); |
| 269 | strongController.initSliders(); |
| 270 | strongController.initPagers(); |
| 271 | strongController.initLayouts(); |
| 272 | strongController.initForm(); |
| 273 | strongController.initIframes(); |
| 274 | strongController.customEvents(); |
| 275 | }, |
| 276 | |
| 277 | /** |
| 278 | * Listen. |
| 279 | */ |
| 280 | listen: function () { |
| 281 | if (debugit) console.log('listen'); |
| 282 | |
| 283 | switch (this.config.method) { |
| 284 | case 'universal': |
| 285 | // Set a timer to check for idle components. |
| 286 | this.newInterval(); |
| 287 | break; |
| 288 | |
| 289 | case 'observer': |
| 290 | // Observe a specific DOM element on a timer. |
| 291 | // Calling start() here is too soon; the transition is not complete yet. |
| 292 | this.observer(document.getElementById(this.config.containerId), this.newTimeout); |
| 293 | break; |
| 294 | |
| 295 | case 'event': |
| 296 | // The theme/plugin uses an event emitter. |
| 297 | |
| 298 | // jQuery Pjax -!- Not working in any theme tested yet -!- |
| 299 | // event name = pjax:end |
| 300 | |
| 301 | // Pjax by MoOx |
| 302 | // @link https://github.com/MoOx/pjax |
| 303 | // event name = pjax:success |
| 304 | |
| 305 | // Ajax Pagination and Infinite Scroll by Malinky |
| 306 | // @link https://wordpress.org/plugins/malinky-ajax-pagination/ |
| 307 | // event name = malinkyLoadPostsComplete |
| 308 | |
| 309 | document.addEventListener(this.config.event, this.start); |
| 310 | break; |
| 311 | |
| 312 | case 'script': |
| 313 | // The theme/plugin uses a dispatcher. |
| 314 | |
| 315 | switch (this.config.script) { |
| 316 | case 'barba': |
| 317 | // Barba |
| 318 | // @link http://barbajs.org/ |
| 319 | if (typeof Barba === 'object' && Barba.hasOwnProperty('Dispatcher')) { |
| 320 | Barba.Dispatcher.on('transitionCompleted', this.start); |
| 321 | } |
| 322 | break; |
| 323 | default: |
| 324 | } |
| 325 | break; |
| 326 | |
| 327 | default: |
| 328 | // no Pjax support |
| 329 | } |
| 330 | }, |
| 331 | |
| 332 | /** |
| 333 | * Listen. |
| 334 | */ |
| 335 | listenForIframeReady: function () { |
| 336 | if (debugit) console.log('listenForIframeReady'); |
| 337 | |
| 338 | if (strongController.iframes.length && strongController.grids.length) { |
| 339 | |
| 340 | strongController.iframes.ready(function () { |
| 341 | // still needs a moment to render |
| 342 | setTimeout(function () { |
| 343 | strongController.grids.masonry(); |
| 344 | if (debugit) console.log('listenForIframeReady', 'timeout 1'); |
| 345 | }, 1000); |
| 346 | // just in case |
| 347 | setTimeout(function () { |
| 348 | strongController.grids.masonry(); |
| 349 | if (debugit) console.log('listenForIframeReady', 'timeout 2'); |
| 350 | }, 2000); |
| 351 | }); |
| 352 | |
| 353 | } else { |
| 354 | |
| 355 | if (debugit) console.log('listenForIframeReady', 'no iframes or Masonry found'); |
| 356 | |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | }; |
| 361 | |
| 362 | // Initialize controller. |
| 363 | strongController.init(); |
| 364 |