flipwidget.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // IIFE - see http://benalman.com/news/2010/11/immediately-invoked-function-expression/#iife
  2. ;(function (win, doc, undefined) {
  3. // Declare some variable names we'll use.
  4. win.FlipWidget = function (options) {
  5. var flipWrapper,
  6. widgetHeading,
  7. backButton,
  8. flippedClass,
  9. enabledClass,
  10. disabledClass,
  11. flipElement,
  12. cardA,
  13. cardB,
  14. prefixes,
  15. defaultOpts,
  16. opts;
  17. // Quit early CSS 3D transforms and the classList API
  18. // (as reported by Modernizr) are unsupported.
  19. if (!(Modernizr.csstransforms3d && Modernizr.classlist)) {
  20. return;
  21. }
  22. // Small helper function to extend objects -
  23. // see https://gist.github.com/toddmotto/49c16d931a7380a1e661
  24. function extend (target, source) {
  25. var a = Object.create(target);
  26. Object.keys(source).map(function (prop) {
  27. prop in a && (a[prop] = source[prop]);
  28. });
  29. return a;
  30. }
  31. // Start with default options, but extend them based on the passed
  32. // in options object above and the extend function.
  33. defaultOpts = {
  34. wrapperSel: '.flip-wrapper',
  35. flipElementSel: '.flip-wrapper',
  36. widgetHeadingSel: '.menu-heading',
  37. backButtonSel: '.menu-save',
  38. flippedClass: 'is-flipped',
  39. enabledClass: 'flip-enabled',
  40. disabledClass: 'flip-disabled',
  41. sideASel: '.flip-a',
  42. sideBSel: '.flip-b',
  43. prefixes: ['webkit', 'MS', 'moz', '']
  44. };
  45. options = options || {};
  46. opts = extend(defaultOpts, options);
  47. // Initialize variables now that we now we're actually doing this.
  48. flipWrapper = doc.querySelector(opts.wrapperSel);
  49. widgetHeading = flipWrapper.querySelector(opts.widgetHeadingSel);
  50. flipElement = flipWrapper.querySelector(opts.flipElementSel) || flipWrapper;
  51. backButton = flipWrapper.querySelector(opts.backButtonSel);
  52. sideA = flipWrapper.querySelector(opts.sideASel);
  53. sideB = flipWrapper.querySelector(opts.sideBSel);
  54. // Create the "show filters" button
  55. var trigger = doc.createElement('button'),
  56. triggerText = doc.createTextNode('Show filters');
  57. trigger.appendChild(triggerText);
  58. // Give the trigger a class name.
  59. trigger.className += ' flip-trigger';
  60. // Add the trigger to the menu heading.
  61. widgetHeading.appendChild(trigger);
  62. // A small function to toggle the class name "is-flipped" when clicked:
  63. function toggleCard() {
  64. flipWrapper.classList.toggle(opts.flippedClass);
  65. }
  66. // Helper function to generate prefixed versions of the event handlers.
  67. function prefixedEvent(addOrRemove, prefixes, element, evt, callback) {
  68. var l = prefixes.length;
  69. // Loop over all the prefixes to support and run either
  70. // element.addEventListener or element.removeEventListener, based
  71. // on the value of the addOrRemove argument.
  72. for (var current=0; current<l; current++) {
  73. // set the event name to the lowercased standard version if no prefix.
  74. if (prefixes[current] === "") {
  75. evt = evt.toLowerCase();
  76. }
  77. element[addOrRemove + 'EventListener'](prefixes[current]+evt, callback, false);
  78. }
  79. }
  80. // Helper function to switch classes using the classList API.
  81. function switchClass(el, cls1, cls2) {
  82. el.classList.remove(cls1);
  83. el.classList.add(cls2);
  84. }
  85. function hideSide(side) {
  86. switchClass(side, opts.enabledClass, opts.disabledClass);
  87. side.setAttribute('aria-hidden', 'true');
  88. }
  89. function showSide(side) {
  90. switchClass(side, opts.disabledClass, opts.enabledClass);
  91. side.setAttribute('aria-hidden', 'false');
  92. }
  93. // Function to completely hide A if B is displayed and vice versa
  94. // This is to make the widget keyboard accessible, so that it is not
  95. // possible to e.g. tab into the hidden part of the widget using
  96. // the keyboard.
  97. function handleDisplay(e) {
  98. var toShow, toHide, rAF;
  99. // A very crude "polyfill" for the requestAnimationFrame API -
  100. // see usage below.
  101. rAF = win.requestAnimationFrame ||
  102. win.webkitRequestAnimationFrame ||
  103. win.mozRequestAnimationFrame ||
  104. function (callback) {return setTimeout(callback, 17);};
  105. if (flipWrapper.classList.contains(opts.flippedClass)) {
  106. toShow = sideA;
  107. toHide = sideB;
  108. } else {
  109. toShow = sideB;
  110. toHide = sideA;
  111. }
  112. showSide(toShow);
  113. // Stop the event from bubbling & stop it from submitting the form:
  114. if (e && 'preventDefault' in e) {
  115. //e.stopPropagation();
  116. e.preventDefault();
  117. }
  118. // After a transition on a side is done, stop listening to
  119. // transitionend, and hide the side that was to be hidden.
  120. function removeTransitionListener(e) {
  121. // Return if not transitioning on the flipElement.
  122. if (e && e.target != flipElement) {
  123. return;
  124. }
  125. hideSide(toHide);
  126. prefixedEvent('remove', opts.prefixes, flipElement, 'TransitionEnd', removeTransitionListener);
  127. }
  128. // Listen to the transition end event, and then hide the other.
  129. prefixedEvent('add', opts.prefixes, flipElement, 'TransitionEnd', removeTransitionListener);
  130. // Since we're setting the visibility property, we don't want to
  131. // run the toggle animation in the same frame. Thus, we're using
  132. // the requestAnimationFrame API to wait until the next available
  133. // animation frame, when DOM operations are done.
  134. rAF(function () {
  135. toggleCard();
  136. // When going back to side A, focus the trigger button again.
  137. if (e.target == backButton) {
  138. // This is a very hacky solution, but some browsers don't
  139. // seem to respect setting the focus to an element mid-transition.
  140. // Therefore we wait until well after the transition to move
  141. // keyboard focus to the button.
  142. setTimeout(function () {
  143. trigger.focus();
  144. }, 300);
  145. }
  146. });
  147. }
  148. // Run the setup once to set the initial states:
  149. hideSide(sideB);
  150. // Hook up the buttons to the event handler:
  151. trigger.addEventListener('click', handleDisplay);
  152. backButton.addEventListener('click', handleDisplay);
  153. };
  154. // Note on the pattern below: this self-invoking function is called with
  155. // the window and document host objects as arguments.
  156. }(window, document));