// IIFE - see http://benalman.com/news/2010/11/immediately-invoked-function-expression/#iife ;(function (win, doc, undefined) { // Declare some variable names we'll use. win.FlipWidget = function (options) { var flipWrapper, widgetHeading, backButton, flippedClass, enabledClass, disabledClass, flipElement, cardA, cardB, prefixes, defaultOpts, opts; // Quit early CSS 3D transforms and the classList API // (as reported by Modernizr) are unsupported. if (!(Modernizr.csstransforms3d && Modernizr.classlist)) { return; } // Small helper function to extend objects - // see https://gist.github.com/toddmotto/49c16d931a7380a1e661 function extend (target, source) { var a = Object.create(target); Object.keys(source).map(function (prop) { prop in a && (a[prop] = source[prop]); }); return a; } // Start with default options, but extend them based on the passed // in options object above and the extend function. defaultOpts = { wrapperSel: '.flip-wrapper', flipElementSel: '.flip-wrapper', widgetHeadingSel: '.menu-heading', backButtonSel: '.menu-save', flippedClass: 'is-flipped', enabledClass: 'flip-enabled', disabledClass: 'flip-disabled', sideASel: '.flip-a', sideBSel: '.flip-b', prefixes: ['webkit', 'MS', 'moz', ''] }; options = options || {}; opts = extend(defaultOpts, options); // Initialize variables now that we now we're actually doing this. flipWrapper = doc.querySelector(opts.wrapperSel); widgetHeading = flipWrapper.querySelector(opts.widgetHeadingSel); flipElement = flipWrapper.querySelector(opts.flipElementSel) || flipWrapper; backButton = flipWrapper.querySelector(opts.backButtonSel); sideA = flipWrapper.querySelector(opts.sideASel); sideB = flipWrapper.querySelector(opts.sideBSel); // Create the "show filters" button var trigger = doc.createElement('button'), triggerText = doc.createTextNode('Show filters'); trigger.appendChild(triggerText); // Give the trigger a class name. trigger.className += ' flip-trigger'; // Add the trigger to the menu heading. widgetHeading.appendChild(trigger); // A small function to toggle the class name "is-flipped" when clicked: function toggleCard() { flipWrapper.classList.toggle(opts.flippedClass); } // Helper function to generate prefixed versions of the event handlers. function prefixedEvent(addOrRemove, prefixes, element, evt, callback) { var l = prefixes.length; // Loop over all the prefixes to support and run either // element.addEventListener or element.removeEventListener, based // on the value of the addOrRemove argument. for (var current=0; current