function Util() {}; Util.hasClass = function(el, className) { if (el.classList) return el.classList.contains(className); else return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)')); }; Util.addClass = function(el, className) { var classList = className.split(' '); if (el.classList) el.classList.add(classList[0]); else if (!Util.hasClass(el, classList[0])) el.className += " " + classList[0]; if (classList.length > 1) Util.addClass(el, classList.slice(1).join(' ')); }; Util.removeClass = function(el, className) { var classList = className.split(' '); if (el.classList) el.classList.remove(classList[0]); else if (Util.hasClass(el, classList[0])) { var reg = new RegExp('(\\s|^)' + classList[0] + '(\\s|$)'); el.className = el.className.replace(reg, ' '); } if (classList.length > 1) Util.removeClass(el, classList.slice(1).join(' ')); }; Util.toggleClass = function(el, className, bool) { if (bool) Util.addClass(el, className); else Util.removeClass(el, className); }; Util.setAttributes = function(el, attrs) { for (var key in attrs) { el.setAttribute(key, attrs[key]); } }; Util.getChildrenByClassName = function(el, className) { var children = el.children, childrenByClass = []; for (var i = 0; i < el.children.length; i++) { if (Util.hasClass(el.children[i], className)) childrenByClass.push(el.children[i]); } return childrenByClass; }; Util.is = function(elem, selector) { if (selector.nodeType) { return elem === selector; } var qa = (typeof(selector) === 'string' ? document.querySelectorAll(selector) : selector), length = qa.length, returnArr = []; while (length--) { if (qa[length] === elem) { return true; } } return false; }; Util.setHeight = function(start, to, element, duration, cb) { var change = to - start, currentTime = null; var animateHeight = function(timestamp) { if (!currentTime) currentTime = timestamp; var progress = timestamp - currentTime; var val = parseInt((progress / duration) * change + start); element.style.height = val + "px"; if (progress < duration) { window.requestAnimationFrame(animateHeight); } else { cb(); } }; element.style.height = start + "px"; window.requestAnimationFrame(animateHeight); }; Util.scrollTo = function(final, duration, cb) { var start = window.scrollY || document.documentElement.scrollTop, currentTime = null; var animateScroll = function(timestamp) { if (!currentTime) currentTime = timestamp; var progress = timestamp - currentTime; if (progress > duration) progress = duration; var val = Math.easeInOutQuad(progress, start, final - start, duration); window.scrollTo(0, val); if (progress < duration) { window.requestAnimationFrame(animateScroll); } else { cb && cb(); } }; window.requestAnimationFrame(animateScroll); }; Util.moveFocus = function(element) { if (!element) element = document.getElementsByTagName("body")[0]; element.focus(); if (document.activeElement !== element) { element.setAttribute('tabindex', '-1'); element.focus(); } }; Util.getIndexInArray = function(array, el) { return Array.prototype.indexOf.call(array, el); }; Util.cssSupports = function(property, value) { if ('CSS' in window) { return CSS.supports(property, value); } else { var jsProperty = property.replace(/-([a-z])/g, function(g) { return g[1].toUpperCase(); }); return jsProperty in document.body.style; } }; Util.extend = function() { var extended = {}; var deep = false; var i = 0; var length = arguments.length; if (Object.prototype.toString.call(arguments[0]) === '[object Boolean]') { deep = arguments[0]; i++; } var merge = function(obj) { for (var prop in obj) { if (Object.prototype.hasOwnProperty.call(obj, prop)) { if (deep && Object.prototype.toString.call(obj[prop]) === '[object Object]') { extended[prop] = extend(true, extended[prop], obj[prop]); } else { extended[prop] = obj[prop]; } } } }; for (; i < length; i++) { var obj = arguments[i]; merge(obj); } return extended; }; if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; } if (!Element.prototype.closest) { Element.prototype.closest = function(s) { var el = this; if (!document.documentElement.contains(el)) return null; do { if (el.matches(s)) return el; el = el.parentElement || el.parentNode; } while (el !== null && el.nodeType === 1); return null; }; } if (typeof window.CustomEvent !== "function") { function CustomEvent(event, params) { params = params || { bubbles: false, cancelable: false, detail: undefined }; var evt = document.createEvent('CustomEvent'); evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); return evt; } CustomEvent.prototype = window.Event.prototype; window.CustomEvent = CustomEvent; } Math.easeInOutQuad = function(t, b, c, d) { t /= d / 2; if (t < 1) return c / 2 * t * t + b; t--; return -c / 2 * (t * (t - 2) - 1) + b; }; /**/ (function() { var LanguagePicker = function(element) { this.element = element; this.select = this.element.getElementsByTagName('select')[0]; this.options = this.select.getElementsByTagName('option'); this.selectedOption = getSelectedOptionText(this); this.pickerId = this.select.getAttribute('id'); this.trigger = false; this.dropdown = false; this.firstLanguage = false; // dropdown arrow inside the button element this.svgPath = ''; initLanguagePicker(this); initLanguagePickerEvents(this); }; function initLanguagePicker(picker) { // create the HTML for the custom dropdown element picker.element.insertAdjacentHTML('beforeend', initButtonPicker(picker) + initListPicker(picker)); // save picker elements picker.dropdown = picker.element.getElementsByClassName('language-picker__dropdown')[0]; picker.firstLanguage = picker.dropdown.getElementsByClassName('language-picker__item')[0]; picker.trigger = picker.element.getElementsByClassName('language-picker__button')[0]; }; function initLanguagePickerEvents(picker) { // make sure to add the icon class to the arrow dropdown inside the button element Util.addClass(picker.trigger.getElementsByTagName('svg')[0], 'icon'); // language selection in dropdown // ⚠️ Important: you need to modify this function in production initLanguageSelection(picker); // click events picker.trigger.addEventListener('click', function(){ toggleLanguagePicker(picker, false); }); }; function toggleLanguagePicker(picker, bool) { var ariaExpanded; if(bool) { ariaExpanded = bool; } else { ariaExpanded = picker.trigger.getAttribute('aria-expanded') == 'true' ? 'false' : 'true'; } picker.trigger.setAttribute('aria-expanded', ariaExpanded); if(ariaExpanded == 'true') { picker.firstLanguage.focus(); // fallback if transition is not supported picker.dropdown.addEventListener('transitionend', function cb(){ picker.firstLanguage.focus(); picker.dropdown.removeEventListener('transitionend', cb); }); } }; function checkLanguagePickerClick(picker, target) { // if user clicks outside the language picker -> close it if( !picker.element.contains(target) ) toggleLanguagePicker(picker, 'false'); }; function moveFocusToPickerTrigger(picker) { if(picker.trigger.getAttribute('aria-expanded') == 'false') return; if(document.activeElement.closest('.language-picker__dropdown') == picker.dropdown) picker.trigger.focus(); }; function initButtonPicker(picker) { // create the button element -> picker trigger // check if we need to add custom classes to the button trigger var customClasses = picker.element.getAttribute('data-trigger-class') ? ' '+picker.element.getAttribute('data-trigger-class') : ''; var button = ''; }; function initListPicker(picker) { // create language picker dropdown var list = '
'+picker.element.getElementsByTagName('label')[0].innerText+'
'; list = list + '