PageSourceSearch

https://direktvertrieb.de/assets/scripts/utils.js?v=2412031737

js direktvertrieb.de collected 2026-09-24 18:34:37 UTC 5,067 bytes, 165 lines download raw bytes

1/**
2 * Author and copyright: Stefan Haack (https://shaack.com)
3 * License: UNLICENSED, all rights reserved
4 */
5
6function scrollSmoothTo($element, offset) {
7    console.log("scrollSmoothTo", $element, offset)
8    if ($element) {
9        if (!offset) {
10            offset = 0
11        }
12        $('html, body').animate({
13            scrollTop: ($element.offset().top - offset)
14        }, 500)
15    }
16}
17
18function shuffle(array) {
19    array.sort(function () {
20        return Math.random() - 0.5
21    })
22}
23
24function openExternalLinksInNewTab() {
25    var links = document.links
26    for (var i = 0, linksLength = links.length; i < linksLength; i++) {
27        if (links[i].hostname !== window.location.hostname && links[i].hostname) {
28            links[i].target = '_blank'
29        }
30    }
31}
32
33function scrollAboveAnchor() {
34    function offsetAnchor() {
35        if (location.hash.length !== 0) {
36            window.scrollTo(window.scrollX, window.scrollY - 100)
37        }
38    }
39
40    $(document).on('click', 'a[href^="#"]', function (event) {
41        window.setTimeout(function () {
42            offsetAnchor()
43        })
44    })
45
46// Set the offset when entering page with hash present in the url
47    window.setTimeout(offsetAnchor, 0)
48}
49
50
51function SizeDetect(onChange) {
52
53    var self = this
54    var state = {
55        lastSize: null
56    }
57
58    this.getCurrentSize = function () {
59        return window.getComputedStyle(document.querySelector('body'), ':before')
60            .getPropertyValue('content')
61            .replace(/"/g, '')
62    }
63
64    window.addEventListener("resize", function () {
65        var currentSize = self.getCurrentSize()
66        if (currentSize !== state.lastSize) {
67            state.lastSize = currentSize
68            onChange(currentSize)
69        }
70    })
71
72    state.lastSize = this.getCurrentSize()
73}
74
75function ScrollDetect(onChange) {
76    var self = this
77    var state = {
78        lastScrolled: null
79    }
80
81    this.scrolled = function () {
82        return window.scrollY > 10
83    }
84
85    window.addEventListener("scroll", function () {
86        var currentScrolled = self.scrolled()
87        if (currentScrolled !== state.lastScrolled) {
88            state.lastScrolled = currentScrolled
89            onChange(currentScrolled)
90        }
91    })
92
93    state.lastScrolled = this.scrolled()
94}
95
96// https://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433
97
98function loadLazy(elements, scrollingContainer = undefined) {
99    for (const element of elements) {
100        $(window).on("resize", () => {
101            const attrWidth = parseFloat(element.getAttribute("width"))
102            const attrHeight = parseFloat(element.getAttribute("height"))
103            const ratio = attrWidth / attrHeight
104            // console.log(attrWidth, attrHeight, ratio)
105            const height = element.offsetHeight
106            element.style.width = height * ratio + "px"
107        })
108        const attrWidth = parseFloat(element.getAttribute("width"))
109        const attrHeight = parseFloat(element.getAttribute("height"))
110        const ratio = attrWidth / attrHeight
111        // console.log(attrWidth, attrHeight, ratio)
112        const height = element.offsetHeight
113        element.style.width = height * ratio + "px"
114        new VisibilityObserver(element, function (element, visible) {
115            if (visible) {
116                if (element.getAttribute("src") !== element.dataset.src) {
117                    element.setAttribute("src", element.dataset.src)
118                }
119            }
120        }, 500, scrollingContainer)
121    }
122}
123
124function VisibilityObserver(element, callback, offset = 0, scrollingContainers = undefined) {
125    function isElementInViewport(el) {
126        var rect = el.getBoundingClientRect()
127        return (
128            rect.top >= 0 - (rect.height + offset) &&
129            rect.left >= 0 - (rect.width + offset) &&
130            rect.bottom <= window.innerHeight + rect.height + offset &&
131            rect.right <= window.innerWidth + rect.width + offset
132        )
133    }
134
135    function onVisibilityChange(element, callback) {
136        var old_visible
137        return function () {
138            var visible = isElementInViewport(element)
139            if (visible !== old_visible) {
140                old_visible = visible
141                callback(element, visible)
142            }
143        }
144    }
145
146    var handler = onVisibilityChange(element, function (element, visible) {
147        callback(element, visible)
148    })
149    $(window).on('DOMContentLoaded load resize scroll', handler)
150    if (scrollingContainers) {
151        for (const scrollingContainer of scrollingContainers) {
152            $(scrollingContainer).on('load resize scroll', handler)
153        }
154    }
155}
156
157function handleNewsClick() {
158    $(document).on('shown.bs.collapse', '.news-item-text.collapse', function (event) {
159        const id = event.target.dataset.itemId
160        scrollSmoothTo($("#item-anchor-" + id), 110)
161    })
162}
163
164loadLazy(document.querySelectorAll("img[data-src]"), document.querySelectorAll(".scrolling-container"))
165handleNewsClick()

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.