|
| 1 | +var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; |
| 2 | + |
| 3 | +function createCommonjsModule(fn, basedir, module) { |
| 4 | + return module = { |
| 5 | + path: basedir, |
| 6 | + exports: {}, |
| 7 | + require: function (path, base) { |
| 8 | + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); |
| 9 | + } |
| 10 | + }, fn(module, module.exports), module.exports; |
| 11 | +} |
| 12 | + |
| 13 | +function commonjsRequire () { |
| 14 | + throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); |
| 15 | +} |
| 16 | + |
| 17 | +var ResizeSensor = createCommonjsModule(function (module, exports) { |
| 18 | + |
| 19 | +/** |
| 20 | + * Copyright Marc J. Schmidt. See the LICENSE file at the top-level |
| 21 | + * directory of this distribution and at |
| 22 | + * https://github.com/marcj/css-element-queries/blob/master/LICENSE. |
| 23 | + */ |
| 24 | +(function (root, factory) { |
| 25 | + { |
| 26 | + module.exports = factory(); |
| 27 | + } |
| 28 | +}(typeof window !== 'undefined' ? window : commonjsGlobal, function () { |
| 29 | + |
| 30 | + // Make sure it does not throw in a SSR (Server Side Rendering) situation |
| 31 | + if (typeof window === "undefined") { |
| 32 | + return null; |
| 33 | + } |
| 34 | + // https://github.com/Semantic-Org/Semantic-UI/issues/3855 |
| 35 | + // https://github.com/marcj/css-element-queries/issues/257 |
| 36 | + var globalWindow = typeof window != 'undefined' && window.Math == Math |
| 37 | + ? window |
| 38 | + : typeof self != 'undefined' && self.Math == Math |
| 39 | + ? self |
| 40 | + : Function('return this')(); |
| 41 | + // Only used for the dirty checking, so the event callback count is limited to max 1 call per fps per sensor. |
| 42 | + // In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and |
| 43 | + // would generate too many unnecessary events. |
| 44 | + var requestAnimationFrame = globalWindow.requestAnimationFrame || |
| 45 | + globalWindow.mozRequestAnimationFrame || |
| 46 | + globalWindow.webkitRequestAnimationFrame || |
| 47 | + function (fn) { |
| 48 | + return globalWindow.setTimeout(fn, 20); |
| 49 | + }; |
| 50 | + |
| 51 | + var cancelAnimationFrame = globalWindow.cancelAnimationFrame || |
| 52 | + globalWindow.mozCancelAnimationFrame || |
| 53 | + globalWindow.webkitCancelAnimationFrame || |
| 54 | + function (timer) { |
| 55 | + globalWindow.clearTimeout(timer); |
| 56 | + }; |
| 57 | + |
| 58 | + /** |
| 59 | + * Iterate over each of the provided element(s). |
| 60 | + * |
| 61 | + * @param {HTMLElement|HTMLElement[]} elements |
| 62 | + * @param {Function} callback |
| 63 | + */ |
| 64 | + function forEachElement(elements, callback){ |
| 65 | + var elementsType = Object.prototype.toString.call(elements); |
| 66 | + var isCollectionTyped = ('[object Array]' === elementsType |
| 67 | + || ('[object NodeList]' === elementsType) |
| 68 | + || ('[object HTMLCollection]' === elementsType) |
| 69 | + || ('[object Object]' === elementsType) |
| 70 | + || ('undefined' !== typeof jQuery && elements instanceof jQuery) //jquery |
| 71 | + || ('undefined' !== typeof Elements && elements instanceof Elements) //mootools |
| 72 | + ); |
| 73 | + var i = 0, j = elements.length; |
| 74 | + if (isCollectionTyped) { |
| 75 | + for (; i < j; i++) { |
| 76 | + callback(elements[i]); |
| 77 | + } |
| 78 | + } else { |
| 79 | + callback(elements); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get element size |
| 85 | + * @param {HTMLElement} element |
| 86 | + * @returns {Object} {width, height} |
| 87 | + */ |
| 88 | + function getElementSize(element) { |
| 89 | + if (!element.getBoundingClientRect) { |
| 90 | + return { |
| 91 | + width: element.offsetWidth, |
| 92 | + height: element.offsetHeight |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + var rect = element.getBoundingClientRect(); |
| 97 | + return { |
| 98 | + width: Math.round(rect.width), |
| 99 | + height: Math.round(rect.height) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Apply CSS styles to element. |
| 105 | + * |
| 106 | + * @param {HTMLElement} element |
| 107 | + * @param {Object} style |
| 108 | + */ |
| 109 | + function setStyle(element, style) { |
| 110 | + Object.keys(style).forEach(function(key) { |
| 111 | + element.style[key] = style[key]; |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Class for dimension change detection. |
| 117 | + * |
| 118 | + * @param {Element|Element[]|Elements|jQuery} element |
| 119 | + * @param {Function} callback |
| 120 | + * |
| 121 | + * @constructor |
| 122 | + */ |
| 123 | + var ResizeSensor = function(element, callback) { |
| 124 | + //Is used when checking in reset() only for invisible elements |
| 125 | + var lastAnimationFrameForInvisibleCheck = 0; |
| 126 | + |
| 127 | + /** |
| 128 | + * |
| 129 | + * @constructor |
| 130 | + */ |
| 131 | + function EventQueue() { |
| 132 | + var q = []; |
| 133 | + this.add = function(ev) { |
| 134 | + q.push(ev); |
| 135 | + }; |
| 136 | + |
| 137 | + var i, j; |
| 138 | + this.call = function(sizeInfo) { |
| 139 | + for (i = 0, j = q.length; i < j; i++) { |
| 140 | + q[i].call(this, sizeInfo); |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + this.remove = function(ev) { |
| 145 | + var newQueue = []; |
| 146 | + for(i = 0, j = q.length; i < j; i++) { |
| 147 | + if(q[i] !== ev) newQueue.push(q[i]); |
| 148 | + } |
| 149 | + q = newQueue; |
| 150 | + }; |
| 151 | + |
| 152 | + this.length = function() { |
| 153 | + return q.length; |
| 154 | + }; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * |
| 159 | + * @param {HTMLElement} element |
| 160 | + * @param {Function} resized |
| 161 | + */ |
| 162 | + function attachResizeEvent(element, resized) { |
| 163 | + if (!element) return; |
| 164 | + if (element.resizedAttached) { |
| 165 | + element.resizedAttached.add(resized); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + element.resizedAttached = new EventQueue(); |
| 170 | + element.resizedAttached.add(resized); |
| 171 | + |
| 172 | + element.resizeSensor = document.createElement('div'); |
| 173 | + element.resizeSensor.dir = 'ltr'; |
| 174 | + element.resizeSensor.className = 'resize-sensor'; |
| 175 | + |
| 176 | + var style = { |
| 177 | + pointerEvents: 'none', |
| 178 | + position: 'absolute', |
| 179 | + left: '0px', |
| 180 | + top: '0px', |
| 181 | + right: '0px', |
| 182 | + bottom: '0px', |
| 183 | + overflow: 'hidden', |
| 184 | + zIndex: '-1', |
| 185 | + visibility: 'hidden', |
| 186 | + maxWidth: '100%' |
| 187 | + }; |
| 188 | + var styleChild = { |
| 189 | + position: 'absolute', |
| 190 | + left: '0px', |
| 191 | + top: '0px', |
| 192 | + transition: '0s', |
| 193 | + }; |
| 194 | + |
| 195 | + setStyle(element.resizeSensor, style); |
| 196 | + |
| 197 | + var expand = document.createElement('div'); |
| 198 | + expand.className = 'resize-sensor-expand'; |
| 199 | + setStyle(expand, style); |
| 200 | + |
| 201 | + var expandChild = document.createElement('div'); |
| 202 | + setStyle(expandChild, styleChild); |
| 203 | + expand.appendChild(expandChild); |
| 204 | + |
| 205 | + var shrink = document.createElement('div'); |
| 206 | + shrink.className = 'resize-sensor-shrink'; |
| 207 | + setStyle(shrink, style); |
| 208 | + |
| 209 | + var shrinkChild = document.createElement('div'); |
| 210 | + setStyle(shrinkChild, styleChild); |
| 211 | + setStyle(shrinkChild, { width: '200%', height: '200%' }); |
| 212 | + shrink.appendChild(shrinkChild); |
| 213 | + |
| 214 | + element.resizeSensor.appendChild(expand); |
| 215 | + element.resizeSensor.appendChild(shrink); |
| 216 | + element.appendChild(element.resizeSensor); |
| 217 | + |
| 218 | + var computedStyle = window.getComputedStyle(element); |
| 219 | + var position = computedStyle ? computedStyle.getPropertyValue('position') : null; |
| 220 | + if ('absolute' !== position && 'relative' !== position && 'fixed' !== position && 'sticky' !== position) { |
| 221 | + element.style.position = 'relative'; |
| 222 | + } |
| 223 | + |
| 224 | + var dirty = false; |
| 225 | + |
| 226 | + //last request animation frame id used in onscroll event |
| 227 | + var rafId = 0; |
| 228 | + var size = getElementSize(element); |
| 229 | + var lastWidth = 0; |
| 230 | + var lastHeight = 0; |
| 231 | + var initialHiddenCheck = true; |
| 232 | + lastAnimationFrameForInvisibleCheck = 0; |
| 233 | + |
| 234 | + var resetExpandShrink = function () { |
| 235 | + var width = element.offsetWidth; |
| 236 | + var height = element.offsetHeight; |
| 237 | + |
| 238 | + expandChild.style.width = (width + 10) + 'px'; |
| 239 | + expandChild.style.height = (height + 10) + 'px'; |
| 240 | + |
| 241 | + expand.scrollLeft = width + 10; |
| 242 | + expand.scrollTop = height + 10; |
| 243 | + |
| 244 | + shrink.scrollLeft = width + 10; |
| 245 | + shrink.scrollTop = height + 10; |
| 246 | + }; |
| 247 | + |
| 248 | + var reset = function() { |
| 249 | + // Check if element is hidden |
| 250 | + if (initialHiddenCheck) { |
| 251 | + var invisible = element.offsetWidth === 0 && element.offsetHeight === 0; |
| 252 | + if (invisible) { |
| 253 | + // Check in next frame |
| 254 | + if (!lastAnimationFrameForInvisibleCheck){ |
| 255 | + lastAnimationFrameForInvisibleCheck = requestAnimationFrame(function(){ |
| 256 | + lastAnimationFrameForInvisibleCheck = 0; |
| 257 | + reset(); |
| 258 | + }); |
| 259 | + } |
| 260 | + |
| 261 | + return; |
| 262 | + } else { |
| 263 | + // Stop checking |
| 264 | + initialHiddenCheck = false; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + resetExpandShrink(); |
| 269 | + }; |
| 270 | + element.resizeSensor.resetSensor = reset; |
| 271 | + |
| 272 | + var onResized = function() { |
| 273 | + rafId = 0; |
| 274 | + |
| 275 | + if (!dirty) return; |
| 276 | + |
| 277 | + lastWidth = size.width; |
| 278 | + lastHeight = size.height; |
| 279 | + |
| 280 | + if (element.resizedAttached) { |
| 281 | + element.resizedAttached.call(size); |
| 282 | + } |
| 283 | + }; |
| 284 | + |
| 285 | + var onScroll = function() { |
| 286 | + size = getElementSize(element); |
| 287 | + dirty = size.width !== lastWidth || size.height !== lastHeight; |
| 288 | + |
| 289 | + if (dirty && !rafId) { |
| 290 | + rafId = requestAnimationFrame(onResized); |
| 291 | + } |
| 292 | + |
| 293 | + reset(); |
| 294 | + }; |
| 295 | + |
| 296 | + var addEvent = function(el, name, cb) { |
| 297 | + if (el.attachEvent) { |
| 298 | + el.attachEvent('on' + name, cb); |
| 299 | + } else { |
| 300 | + el.addEventListener(name, cb); |
| 301 | + } |
| 302 | + }; |
| 303 | + |
| 304 | + addEvent(expand, 'scroll', onScroll); |
| 305 | + addEvent(shrink, 'scroll', onScroll); |
| 306 | + |
| 307 | + // Fix for custom Elements and invisible elements |
| 308 | + lastAnimationFrameForInvisibleCheck = requestAnimationFrame(function(){ |
| 309 | + lastAnimationFrameForInvisibleCheck = 0; |
| 310 | + reset(); |
| 311 | + }); |
| 312 | + } |
| 313 | + |
| 314 | + forEachElement(element, function(elem){ |
| 315 | + attachResizeEvent(elem, callback); |
| 316 | + }); |
| 317 | + |
| 318 | + this.detach = function(ev) { |
| 319 | + // clean up the unfinished animation frame to prevent a potential endless requestAnimationFrame of reset |
| 320 | + if (lastAnimationFrameForInvisibleCheck) { |
| 321 | + cancelAnimationFrame(lastAnimationFrameForInvisibleCheck); |
| 322 | + lastAnimationFrameForInvisibleCheck = 0; |
| 323 | + } |
| 324 | + ResizeSensor.detach(element, ev); |
| 325 | + }; |
| 326 | + |
| 327 | + this.reset = function() { |
| 328 | + //To prevent invoking element.resizeSensor.resetSensor if it's undefined |
| 329 | + if (element.resizeSensor.resetSensor) { |
| 330 | + element.resizeSensor.resetSensor(); |
| 331 | + } |
| 332 | + }; |
| 333 | + }; |
| 334 | + |
| 335 | + ResizeSensor.reset = function(element) { |
| 336 | + forEachElement(element, function(elem){ |
| 337 | + //To prevent invoking element.resizeSensor.resetSensor if it's undefined |
| 338 | + if (element.resizeSensor.resetSensor) { |
| 339 | + elem.resizeSensor.resetSensor(); |
| 340 | + } |
| 341 | + }); |
| 342 | + }; |
| 343 | + |
| 344 | + ResizeSensor.detach = function(element, ev) { |
| 345 | + forEachElement(element, function(elem){ |
| 346 | + if (!elem) return; |
| 347 | + if(elem.resizedAttached && typeof ev === "function"){ |
| 348 | + elem.resizedAttached.remove(ev); |
| 349 | + if(elem.resizedAttached.length()) return; |
| 350 | + } |
| 351 | + if (elem.resizeSensor) { |
| 352 | + if (elem.contains(elem.resizeSensor)) { |
| 353 | + elem.removeChild(elem.resizeSensor); |
| 354 | + } |
| 355 | + delete elem.resizeSensor; |
| 356 | + delete elem.resizedAttached; |
| 357 | + } |
| 358 | + }); |
| 359 | + }; |
| 360 | + |
| 361 | + if (typeof MutationObserver !== "undefined") { |
| 362 | + var observer = new MutationObserver(function (mutations) { |
| 363 | + for (var i in mutations) { |
| 364 | + if (mutations.hasOwnProperty(i)) { |
| 365 | + var items = mutations[i].addedNodes; |
| 366 | + for (var j = 0; j < items.length; j++) { |
| 367 | + if (items[j].resizeSensor) { |
| 368 | + ResizeSensor.reset(items[j]); |
| 369 | + } |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | + }); |
| 374 | + |
| 375 | + document.addEventListener("DOMContentLoaded", function (event) { |
| 376 | + observer.observe(document.body, { |
| 377 | + childList: true, |
| 378 | + subtree: true, |
| 379 | + }); |
| 380 | + }); |
| 381 | + } |
| 382 | + |
| 383 | + return ResizeSensor; |
| 384 | + |
| 385 | +})); |
| 386 | +}); |
| 387 | + |
| 388 | +export default ResizeSensor; |
0 commit comments