// https://stackoverflow.com/questions/27078285/simple-throttle-in-js // Returns a function, that, when invoked, will only be triggered at most once // during a given window of time. Normally, the throttled function will run // as much as it can, without ever going more than once per `wait` duration; // but if you'd like to disable the execution on the leading edge, pass // `{leading: false}`. To disable execution on the trailing edge, ditto. function throttle(func, wait, options) { var context, args, result; var timeout = null; var previous = 0; if (!options) options = {}; var later = function() { previous = options.leading === false ? 0 : Date.now(); timeout = null; result = func.apply(context, args); if (!timeout) context = args = null; }; return function() { var now = Date.now(); if (!previous && options.leading === false) previous = now; var remaining = wait - (now - previous); context = this; args = arguments; if (remaining <= 0 || remaining > wait) { if (timeout) { clearTimeout(timeout); timeout = null; } previous = now; result = func.apply(context, args); if (!timeout) context = args = null; } else if (!timeout && options.trailing !== false) { timeout = setTimeout(later, remaining); } return result; }; }; class Grip { constructor(opt) { if(opt.element === undefined) { throw new Error("Grip: no element provided.") } this.element = opt.element; this.color = opt.color || "primary"; this.offsetX = opt.offsetX || opt.offset || 0; this.offsetY = opt.offsetY || opt.offset || 0; this.spreadX = opt.spreadX || opt.spread || 24; this.spreadY = opt.spreadY || opt.spread || 24; this.animating = false; } trigger() { if(this.animating) return; this.animating = true; const top = this.element.offsetTop - 3 - this.offsetY; const left = this.element.offsetLeft - 3 - this.offsetX; const width = this.element.offsetWidth + 2 * (3 + this.offsetX); const height = this.element.offsetHeight + 2 * (3 + this.offsetY); const i = Grip.iterator++; const animName = `anim--grip--i_${i}`; const style = document.createElement("style"); style.textContent = ` @keyframes ${animName} { from { top: ${top}px; left: ${left}px; width: ${width}px; height: ${height}px; opacity: 1; } to { top: ${top - this.spreadY}px; left: ${left - this.spreadX}px; width: ${width + 2 * this.spreadX}px; height: ${height + 2 * this.spreadY}px; opacity: 0; } } `; document.body.appendChild(style) const grip = document.createElement("div"); grip.className = `grip grip--color-${this.color}`; grip.style.animationName = animName; document.body.appendChild(grip) setTimeout(() => { document.body.removeChild(style) document.body.removeChild(grip) this.animating = false; }, 1010) } } Grip.iterator = 0; function scrollElementIntoView(element, opt, thr = 100) { element.scrollIntoView(opt) return new Promise((resolve, reject) => { if(resolve !== undefined) { let prevScroll = -1; const f = () => { const nextScroll = document.scrollingElement.scrollTop; if(prevScroll == nextScroll) { clearInterval(interval) resolve() } else { prevScroll = nextScroll; } }; const interval = setInterval(f, 100); f() } }) } const $NAV = { el: { parent: document.getElementById("navbar"), bars: document.getElementById("navbar-bars"), times: document.getElementById("navbar-times"), slide: document.getElementById("navbar-slide"), }, opened: false, open() { if(this.opened) return; this.opened = true; this.el.slide.dataset.opened = true; window.history.pushState({ navbar: true, }, null) }, close() { if(!this.opened) return; this.opened = false; this.el.slide.dataset.opened = false; }, onScroll() { this.el.parent.dataset.scrolled = document.scrollingElement.scrollTop != 0; }, scrollToContact() { this.close() scrollElementIntoView(this.c.element, { behavior: "smooth", block: "center", }) .then(() => { this.c.grip.trigger() }) }, ini() { this.el.bars.addEventListener("click", this.open.bind(this)) this.el.times.addEventListener("click", () => { window.history.back() }) window.addEventListener("scroll", throttle(this.onScroll.bind(this), 500)) const fc = document.getElementById("footer-contact"); this.c = { element: fc, grip: new Grip({ element: fc, color: "white", offsetX: 4, }), }; document.querySelectorAll("[data-contact]").forEach(el => { el.addEventListener("click", this.scrollToContact.bind(this)) }) window.history.replaceState({ ...window.history.state, navbar: false, }, null) window.addEventListener("popstate", () => { if(window.history.state !== null) { if(!window.history.state.navbar) { this.close() } } }) }, }; $NAV.ini() class ScrollHighlight { constructor(offset, thr) { this.offset = offset || 60; this.thr = thr || 500; this.items = []; document.querySelectorAll("[data-sh]").forEach(el => { this.items.push({ grip: new Grip({ element: el, }), top: el.offsetTop, height: el.offsetHeight, highlighted: false, timeout: null, }) }) this._onScroll = throttle(this.onScroll.bind(this), this.thr); if(this.items.length != 0) { window.addEventListener("scroll", this._onScroll) this._onScroll() } } onScroll() { const s = document.scrollingElement.scrollTop; const h = window.innerHeight; const from = s + this.offset; const to = s + h - this.offset; this.items.forEach((item, i) => { if(item.grip.animating) return; if(from <= item.top && to - item.height >= item.top) { if(item.highlighted) return; item.highlighted = true; item.grip.trigger(from, to) } else { item.highlighted = false; } }) } } new ScrollHighlight() function copyToClipboard(textToCopy, event) { const top = event === undefined ? 0 : event.currentTarget.getBoundingClientRect().top; let b = document.createElement('textarea'); b.style.position = "fixed"; b.style.top = top+"px"; b.style.left = "-100px"; b.style.width = "50px"; b.style.height = "50px"; b.style.fontSize = "20px"; let v = new DOMParser().parseFromString(textToCopy, "text/html"); b.value = v.documentElement.textContent; document.body.appendChild(b) if(!!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)) { b.contentEditable = true; let range = document.createRange(); let s = window.getSelection(); s.removeAllRanges() s.addRange(range) b.setSelectionRange(0, textToCopy.length) } else { b.select() } let copied = false; try { document.execCommand("copy") copied = true; } catch (d) { copied = window.prompt("Zkopírujte si odkaz do schránky a stiskněte \"OK\".", textToCopy) !== null; } document.body.removeChild(b); return copied; } class ShareDialog extends React.Component { constructor(props) { super(props) this.state = { openedOnce: false, opened: false, copied: false, }; this._close = this.close.bind(this); // this._shareOnFacebook = this.shareOnFacebook.bind(this); this._copy = this.copy.bind(this); this._shareMore = this.shareMore.bind(this); } open() { this.setState({ openedOnce: true, opened: true, copied: false, }) } close() { this.setState({ opened: false, }) } // shareOnFacebook() { // const url = `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(this.props.link)}"e=${encodeURIComponent(this.props.text)}`; // window.open(url, "Sdílet přes Facebook", "width=600,height=600,left=100,top=100") // } copy(e) { const copied = copyToClipboard(`${this.props.text} ${this.props.link}`); if(copied) { this.setState({ copied: true, }) } } shareMore() { window.navigator.share({ text: this.props.text, url: this.props.link, }) } render() { return