|
| 1 | +function HandleElementCSSBoxFeature(context) { |
| 2 | + var postMessage = context.postMessage; |
| 3 | + var options = context.options || {}; |
| 4 | + var target = options.target; |
| 5 | + var shouldThrowWhenNotFound = options.shouldThrowWhenNotFound === true; |
| 6 | + |
| 7 | + if (!target || (typeof target !== 'string' && typeof target !== 'object')) { |
| 8 | + throw new Error('Missing or wrong type for required target option'); |
| 9 | + } |
| 10 | + |
| 11 | + var postSize = context.makeCallbackSafe(function () { |
| 12 | + var element = context.getDOMSelection(target, false); |
| 13 | + if (element != null) { |
| 14 | + var clientRect = element.getBoundingClientRect(), |
| 15 | + styles = getComputedStyle(element), |
| 16 | + paddingLeft = context.numericFromPxString(styles.paddingLeft), |
| 17 | + paddingRight = context.numericFromPxString(styles.paddingRight), |
| 18 | + paddingTop = context.numericFromPxString(styles.paddingTop), |
| 19 | + paddingBottom = context.numericFromPxString(styles.paddingBottom), |
| 20 | + borderLeftWidth = context.numericFromPxString(styles.borderLeftWidth), |
| 21 | + borderRightWidth = context.numericFromPxString(styles.borderRightWidth), |
| 22 | + borderTopWidth = context.numericFromPxString(styles.borderTopWidth), |
| 23 | + borderBottomWidth = context.numericFromPxString( |
| 24 | + styles.borderBottomWidth |
| 25 | + ), |
| 26 | + marginLeft = context.numericFromPxString(styles.marginLeft), |
| 27 | + marginRight = context.numericFromPxString(styles.marginRight), |
| 28 | + marginTop = context.numericFromPxString(styles.marginTop), |
| 29 | + marginBottom = context.numericFromPxString(styles.marginBottom), |
| 30 | + borderBoxWidth = clientRect.width, |
| 31 | + borderBoxHeight = clientRect.height, |
| 32 | + horizontalScrollbarWidth = element.offsetHeight - element.clientHeight, |
| 33 | + verticalScrollbarWidth = element.offsetWidth - element.clientWidth, |
| 34 | + scrollBoxWidth = element.scrollWidth, |
| 35 | + scrollBoxHeight = element.scrollHeight; |
| 36 | + var dimensions = { |
| 37 | + scrollBox: { |
| 38 | + width: scrollBoxWidth, |
| 39 | + height: scrollBoxHeight |
| 40 | + }, |
| 41 | + borderBox: { |
| 42 | + width: borderBoxWidth, |
| 43 | + height: borderBoxHeight |
| 44 | + }, |
| 45 | + computedStyle: { |
| 46 | + paddingTop: paddingTop, |
| 47 | + paddingBottom: paddingBottom, |
| 48 | + paddingLeft: paddingLeft, |
| 49 | + paddingRight: paddingRight, |
| 50 | + borderTopWidth: borderTopWidth, |
| 51 | + borderBottomWidth: borderBottomWidth, |
| 52 | + borderLeftWidth: borderLeftWidth, |
| 53 | + borderRightWidth: borderRightWidth, |
| 54 | + marginTop: marginTop, |
| 55 | + marginBottom: marginBottom, |
| 56 | + marginLeft: marginLeft, |
| 57 | + marginRight: marginRight |
| 58 | + }, |
| 59 | + horizontalScrollbarWidth: horizontalScrollbarWidth, |
| 60 | + verticalScrollbarWidth: verticalScrollbarWidth |
| 61 | + }; |
| 62 | + postMessage(dimensions); |
| 63 | + } else if (shouldThrowWhenNotFound) { |
| 64 | + throw new Error( |
| 65 | + "Couldn't find an element for target " + JSON.stringify(target) |
| 66 | + ); |
| 67 | + } |
| 68 | + }); |
| 69 | + postSize(); |
| 70 | + window.addEventListener('resize', postSize); |
| 71 | + // trigger when DOM changes |
| 72 | + var MutationObserver = |
| 73 | + window['MutationObserver'] || window['WebKitMutationObserver']; |
| 74 | + if (MutationObserver) { |
| 75 | + var observer = new MutationObserver(postSize); |
| 76 | + observer.observe(document, { |
| 77 | + subtree: true, |
| 78 | + attributes: true |
| 79 | + }); |
| 80 | + } else { |
| 81 | + // That is a last resort fallback for older browsers |
| 82 | + context.warn( |
| 83 | + "This browser doesn't support MutationObserver. Falling back to an interval." |
| 84 | + ); |
| 85 | + setInterval(postSize, 200); |
| 86 | + } |
| 87 | +} |
0 commit comments