1/*  DokuWiki MoaiEditor Scroll_tools.js file
2    Author  : MoaiTools <info@moaitools.org>
3    License : GPL 3 (http://www.gnu.org/licenses/gpl.html) */
4
5MoaiEditor.ScrollTools = class {
6
7
8    getMaxScrollY(element) {
9        const extraHeight = this.calcExtraHeight(element);
10        const bounds = element.getBoundingClientRect();
11        const height = bounds.height - extraHeight.border;
12        const maxScroll = Math.round(element.scrollHeight - height);
13        return maxScroll;
14    }
15
16    getMaxScrollX(element) {
17        const extraWidth = this.calcExtraWidth(element);
18        const bounds = element.getBoundingClientRect();
19        const width = bounds.width - extraWidth.total;
20        const maxScroll = Math.round(element.scrollWidth - width);
21        return maxScroll;
22    }
23
24    calcExtraHeight(element) {
25        const styles = window.getComputedStyle(element);
26        const parseValue = (v) => v.endsWith('px') ? parseInt(v.slice(0, -2), 10) : 0;
27        const paddingTop        = parseValue(styles.paddingTop);
28        const paddingBottom     = parseValue(styles.paddingBottom);
29        const borderTopWidth    = parseValue(styles.borderTopWidth);
30        const borderBottomWidth = parseValue(styles.borderBottomWidth);
31        return {
32            top     : paddingTop + borderTopWidth,
33            bottom  : paddingBottom + borderBottomWidth,
34            total   : paddingTop + paddingBottom + borderTopWidth + borderBottomWidth,
35            border  : borderTopWidth + borderBottomWidth
36        }
37    }
38
39    calcExtraWidth(element) {
40        const styles = window.getComputedStyle(element);
41        const parseValue = (v) => v.endsWith('px') ? parseInt(v.slice(0, -2), 10) : 0;
42        const paddingLeft       = parseValue(styles.paddingLeft);
43        const paddingRight      = parseValue(styles.paddingRight);
44        const borderLeftWidth   = parseValue(styles.borderLeftWidth);
45        const borderRightWidth  = parseValue(styles.borderRightWidth);
46        return {
47            left  : paddingLeft + borderLeftWidth,
48            right : paddingRight + borderRightWidth,
49            total : paddingLeft + paddingRight + borderLeftWidth + borderRightWidth
50        }
51    }
52
53    getRectRelativeToParent(element) {
54        const parent = element.parentElement;
55        const parentRect = parent.getBoundingClientRect();
56        const elementRect = element.getBoundingClientRect();
57        return {
58            top    : elementRect.top - parentRect.top + parent.scrollTop,
59            bottom : elementRect.bottom - parentRect.top + parent.scrollTop,
60            left   : elementRect.left - parentRect.left,
61            right  : elementRect.right - parentRect.left,
62            height : elementRect.bottom - elementRect.top,
63            width  : elementRect.right - elementRect.left,
64        };
65    }
66}; // End Class
67
68
69
70