const HanyuuOAuth2Header = function(element = '.js-oauth2-header', simpleElement = '.js-oauth2-header-simple') { if(typeof element === 'string') element = document.querySelector(element); if(!(element instanceof HTMLElement)) throw 'element must be a valid query selector or an instance of HTMLElement'; if(typeof simpleElement === 'string') simpleElement = element.querySelector(simpleElement); const simpleElementIcon = simpleElement?.querySelector('.js-oauth2-header-simple-icon'); const simpleElementText = simpleElement?.querySelector('.js-oauth2-header-simple-text'); const hasSimpleElement = simpleElement instanceof HTMLElement; const setSimpleVisible = state => { if(hasSimpleElement) simpleElement.classList.toggle('hidden', !state); }; const setSimpleData = (icon, text) => { if(hasSimpleElement) { for(const className of simpleElementIcon.classList) if(className.startsWith('oauth2-simplehead-icon--')) simpleElementIcon.classList.remove(className); simpleElementIcon.classList.add(`oauth2-simplehead-icon--${icon}`); simpleElementText.textContent = text; } }; const removeElement = (forceSimple = true) => { while(element.childElementCount > 1) element.lastElementChild.remove(); if(typeof forceSimple === 'boolean') setSimpleVisible(forceSimple); }; return { get element() { return element; }, get simpleVisible() { return hasSimpleElement && !simpleElement.classList.contains('hidden'); }, set simpleVisible(state) { setSimpleVisible(state); }, setSimpleData: setSimpleData, setElement: elementInfo => { removeElement(false); if(elementInfo instanceof Element) element.appendChild(elementInfo); else if('element' in elementInfo) element.appendChild(elementInfo.element); else throw 'elementInfo must be an instance of Element or contain an object with an element property'; }, removeElement: removeElement, }; };