2024-07-30 00:16:42 +00:00
|
|
|
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);
|
|
|
|
};
|
2024-07-30 21:24:20 +00:00
|
|
|
const setSimpleData = (icon, text) => {
|
2024-07-30 00:16:42 +00:00
|
|
|
if(hasSimpleElement) {
|
2024-07-30 21:24:20 +00:00
|
|
|
for(const className of simpleElementIcon.classList)
|
|
|
|
if(className.startsWith('oauth2-simplehead-icon--'))
|
|
|
|
simpleElementIcon.classList.remove(className);
|
|
|
|
|
|
|
|
simpleElementIcon.classList.add(`oauth2-simplehead-icon--${icon}`);
|
2024-07-30 00:16:42 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
};
|