1 line
18 KiB
Plaintext
1 line
18 KiB
Plaintext
|
{"version":3,"file":"fitty.min.js","sources":["../../src/util/fitty.js"],"sourcesContent":["/*eslint no-console: \"off\"*/\n/*\nCopyright (c) 2017-2021 Rik Schennink - All rights reserved.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software\nand associated documentation files (the \"Software\"), to deal in the Software without restriction,\nincluding without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,\nand/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies\nor substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,\nINCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR\nPURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE\nFOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\nARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n*/\nexport default ((w) => {\n // no window, early exit\n if (!w) { return; }\n\n // node list to array helper method\n const toArray = (nl) => [].slice.call(nl);\n\n // states\n const DrawState = {\n IDLE: 0,\n DIRTY_CONTENT: 1,\n DIRTY_LAYOUT: 2,\n DIRTY: 3,\n };\n\n // all active fitty elements\n let fitties = [];\n\n // group all redraw calls till next frame, we cancel each frame request when a new one comes in.\n // If no support for request animation frame, this is an empty function and supports for fitty stops.\n let redrawFrame = null;\n const requestRedraw =\n 'requestAnimationFrame' in w\n ? (options = { sync: false }) => {\n w.cancelAnimationFrame(redrawFrame);\n\n const redrawFn = () => redraw(fitties.filter((f) => f.dirty && f.active));\n\n if (options.sync) { return redrawFn(); }\n\n redrawFrame = w.requestAnimationFrame(redrawFn);\n }\n : () => {};\n\n // sets all fitties to dirty so they are redrawn on the next redraw loop, then calls redraw\n const redrawAll = (type) => (options) => {\n fitties.forEach((f) => (f.dirty = type));\n requestRedraw(options);\n };\n\n // redraws fitties so they nicely fit their parent container\n const redraw = (fitties) => {\n // getting info from the DOM at this point should not trigger a reflow,\n // let's gather as much intel as possible before triggering a reflow\n\n // check if styles of all fitties have been computed\n fitties\n .filter((f) => !f.styleComputed)\n .forEach((f) => {\n f.styleComputed = computeStyle(f);\n });\n\n // restyle elements that require pre-styling, this triggers a reflow, please try to prevent by adding CSS rules (see docs)\n fitties.filter(shouldPreStyle).forEach(applyStyle);\n\n // we now determine which fitties should be redrawn\n const fittiesToRedraw = fitties.filter(shouldRedraw);\n\n // we calculate final styles for these fitties\n fittiesToRedraw.forEach(calculateStyles);\n\n // now we apply the calculated styles from our previous loop\n fittiesToRedraw.forEach((f) => {\n applyStyle(f);\n markAsClean(f);\n });\n\n // now we dispatch events for all restyled fitties\n fittiesToRedraw.forEach(dispatchFitEvent);\n };\n\n const markAsClean = (f) => (f.dirty = DrawState.IDLE);\n\n const calculateStyles = (f) => {\n if (f.vertical) {\n // get available width from parent node\n f.availableHeight = f.element.parentNode.clientHeight;\n\n // the space our target element uses\n f.currentHeight = f.element.sc
|