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