resize-event.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Modified from https://github.com/sdecima/javascript-detect-element-resize
  2. * version: 0.5.3
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013 Sebastián Décima
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  20. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. const isServer = typeof window === 'undefined';
  27. /* istanbul ignore next */
  28. const requestFrame = (function() {
  29. if (isServer) return;
  30. const raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame ||
  31. function(fn) {
  32. return window.setTimeout(fn, 20);
  33. };
  34. return function(fn) {
  35. return raf(fn);
  36. };
  37. })();
  38. /* istanbul ignore next */
  39. const cancelFrame = (function() {
  40. if (isServer) return;
  41. const cancel = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame || window.clearTimeout;
  42. return function(id) {
  43. return cancel(id);
  44. };
  45. })();
  46. /* istanbul ignore next */
  47. const resetTrigger = function(element) {
  48. const trigger = element.__resizeTrigger__;
  49. const expand = trigger.firstElementChild;
  50. const contract = trigger.lastElementChild;
  51. const expandChild = expand.firstElementChild;
  52. contract.scrollLeft = contract.scrollWidth;
  53. contract.scrollTop = contract.scrollHeight;
  54. expandChild.style.width = expand.offsetWidth + 1 + 'px';
  55. expandChild.style.height = expand.offsetHeight + 1 + 'px';
  56. expand.scrollLeft = expand.scrollWidth;
  57. expand.scrollTop = expand.scrollHeight;
  58. };
  59. /* istanbul ignore next */
  60. const checkTriggers = function(element) {
  61. return element.offsetWidth !== element.__resizeLast__.width || element.offsetHeight !== element.__resizeLast__.height;
  62. };
  63. /* istanbul ignore next */
  64. const scrollListener = function(event) {
  65. resetTrigger(this);
  66. if (this.__resizeRAF__) cancelFrame(this.__resizeRAF__);
  67. this.__resizeRAF__ = requestFrame(() => {
  68. if (checkTriggers(this)) {
  69. this.__resizeLast__.width = this.offsetWidth;
  70. this.__resizeLast__.height = this.offsetHeight;
  71. this.__resizeListeners__.forEach((fn) => {
  72. fn.call(this, event);
  73. });
  74. }
  75. });
  76. };
  77. /* Detect CSS Animations support to detect element display/re-attach */
  78. const attachEvent = isServer ? {} : document.attachEvent;
  79. const DOM_PREFIXES = 'Webkit Moz O ms'.split(' ');
  80. const START_EVENTS = 'webkitAnimationStart animationstart oAnimationStart MSAnimationStart'.split(' ');
  81. const RESIZE_ANIMATION_NAME = 'resizeanim';
  82. let animation = false;
  83. let keyFramePrefix = '';
  84. let animationStartEvent = 'animationstart';
  85. /* istanbul ignore next */
  86. if (!attachEvent && !isServer) {
  87. const testElement = document.createElement('fakeelement');
  88. if (testElement.style.animationName !== undefined) {
  89. animation = true;
  90. }
  91. if (animation === false) {
  92. let prefix = '';
  93. for (var i = 0; i < DOM_PREFIXES.length; i++) {
  94. if (testElement.style[DOM_PREFIXES[i] + 'AnimationName'] !== undefined) {
  95. prefix = DOM_PREFIXES[i];
  96. keyFramePrefix = '-' + prefix.toLowerCase() + '-';
  97. animationStartEvent = START_EVENTS[i];
  98. animation = true;
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. let stylesCreated = false;
  105. /* istanbul ignore next */
  106. const createStyles = function() {
  107. if (!stylesCreated && !isServer) {
  108. const animationKeyframes = `@${keyFramePrefix}keyframes ${RESIZE_ANIMATION_NAME} { from { opacity: 0; } to { opacity: 0; } } `;
  109. const animationStyle = `${keyFramePrefix}animation: 1ms ${RESIZE_ANIMATION_NAME};`;
  110. // opacity: 0 works around a chrome bug https://code.google.com/p/chromium/issues/detail?id=286360
  111. const css = `${animationKeyframes}
  112. .resize-triggers { ${animationStyle} visibility: hidden; opacity: 0; }
  113. .resize-triggers, .resize-triggers > div, .contract-trigger:before { content: \" \"; display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; }
  114. .resize-triggers > div { background: #eee; overflow: auto; }
  115. .contract-trigger:before { width: 200%; height: 200%; }`;
  116. const head = document.head || document.getElementsByTagName('head')[0];
  117. const style = document.createElement('style');
  118. style.type = 'text/css';
  119. if (style.styleSheet) {
  120. style.styleSheet.cssText = css;
  121. } else {
  122. style.appendChild(document.createTextNode(css));
  123. }
  124. head.appendChild(style);
  125. stylesCreated = true;
  126. }
  127. };
  128. /* istanbul ignore next */
  129. export const addResizeListener = function(element, fn) {
  130. if (isServer) return;
  131. if (attachEvent) {
  132. element.attachEvent('onresize', fn);
  133. } else {
  134. if (!element.__resizeTrigger__) {
  135. if (getComputedStyle(element).position === 'static') {
  136. element.style.position = 'relative';
  137. }
  138. createStyles();
  139. element.__resizeLast__ = {};
  140. element.__resizeListeners__ = [];
  141. const resizeTrigger = element.__resizeTrigger__ = document.createElement('div');
  142. resizeTrigger.className = 'resize-triggers';
  143. resizeTrigger.innerHTML = '<div class="expand-trigger"><div></div></div><div class="contract-trigger"></div>';
  144. element.appendChild(resizeTrigger);
  145. resetTrigger(element);
  146. element.addEventListener('scroll', scrollListener, true);
  147. /* Listen for a css animation to detect element display/re-attach */
  148. if (animationStartEvent) {
  149. resizeTrigger.addEventListener(animationStartEvent, function(event) {
  150. if (event.animationName === RESIZE_ANIMATION_NAME) {
  151. resetTrigger(element);
  152. }
  153. });
  154. }
  155. }
  156. element.__resizeListeners__.push(fn);
  157. }
  158. };
  159. /* istanbul ignore next */
  160. export const removeResizeListener = function(element, fn) {
  161. if (attachEvent) {
  162. element.detachEvent('onresize', fn);
  163. } else {
  164. element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
  165. if (!element.__resizeListeners__.length) {
  166. element.removeEventListener('scroll', scrollListener);
  167. element.__resizeTrigger__ = !element.removeChild(element.__resizeTrigger__);
  168. }
  169. }
  170. };