319 lines
11 KiB
TypeScript
319 lines
11 KiB
TypeScript
export type SlideOptions = {
|
|
slideSpeed?: number | undefined;
|
|
direction?: "up" | "down" | undefined;
|
|
easing?:
|
|
| "linear"
|
|
| "ease"
|
|
| "ease-in"
|
|
| "ease-in-out"
|
|
| "ease-out"
|
|
| undefined;
|
|
delay?: number | undefined;
|
|
visibleDisplayValue?:
|
|
| "block"
|
|
| "inline"
|
|
| "run-in"
|
|
| "flow"
|
|
| "flow-root"
|
|
| "table"
|
|
| "flex"
|
|
| "grid"
|
|
| "ruby"
|
|
| "list-item"
|
|
| "contents"
|
|
| "none"
|
|
| "inline-block"
|
|
| "inherit"
|
|
| "initial"
|
|
| "inherit"
|
|
| "unset"
|
|
| undefined;
|
|
};
|
|
|
|
type SlideQueueItem = {
|
|
element: HTMLElement;
|
|
options: SlideOptions;
|
|
promise: Promise<HTMLElement>;
|
|
};
|
|
|
|
export default {
|
|
methods: {
|
|
sliderInitied: false,
|
|
styleCache: {} as Partial<Record<string, CSSStyleDeclaration | {}>>,
|
|
queue: [] as Array<SlideQueueItem>,
|
|
/**
|
|
* Slide down an element
|
|
*
|
|
* @param el Selector or a domElement
|
|
* @param options SlideOptions as optional arguments
|
|
* @returns
|
|
*/
|
|
slideDown(
|
|
el: string | HTMLElement,
|
|
options: SlideOptions | null = null
|
|
): Promise<HTMLElement> {
|
|
const domEl = this.fetchEl(el);
|
|
|
|
if (!domEl) {
|
|
throw Error(`'${el}' selector not found`);
|
|
}
|
|
|
|
const slideSpeed = options?.slideSpeed,
|
|
easing = options?.easing,
|
|
delay = options?.delay,
|
|
visibleDisplayValue = options?.visibleDisplayValue;
|
|
return this.slide(domEl, {
|
|
slideSpeed: slideSpeed,
|
|
direction: "down",
|
|
easing: easing,
|
|
delay: delay,
|
|
visibleDisplayValue: visibleDisplayValue,
|
|
});
|
|
},
|
|
/**
|
|
* Slide up an element
|
|
*
|
|
* @param el Selector or a domElement
|
|
* @param options SlideOptions as optional arguments
|
|
* @returns
|
|
*/
|
|
slideUp(
|
|
el: string | HTMLElement,
|
|
options: SlideOptions | null = null
|
|
): Promise<HTMLElement> {
|
|
const domEl = this.fetchEl(el);
|
|
|
|
if (!domEl) {
|
|
throw Error(`'${el}' selector not found`);
|
|
}
|
|
|
|
const slideSpeed = options?.slideSpeed,
|
|
easing = options?.easing,
|
|
delay = options?.delay,
|
|
visibleDisplayValue = options?.visibleDisplayValue;
|
|
return this.slide(domEl, {
|
|
slideSpeed: slideSpeed,
|
|
direction: "up",
|
|
easing: easing,
|
|
delay: delay,
|
|
visibleDisplayValue: visibleDisplayValue,
|
|
});
|
|
},
|
|
/**
|
|
* Slide toggle an element
|
|
*
|
|
* @param el Selector or a domElement
|
|
* @param options SlideOptions as optional arguments
|
|
* @returns
|
|
*/
|
|
slideToggle(
|
|
el: string | HTMLElement,
|
|
options: SlideOptions | null = null
|
|
): Promise<HTMLElement> {
|
|
const domEl = this.fetchEl(el);
|
|
|
|
if (!domEl) {
|
|
throw Error(`'${el}' selector not found`);
|
|
}
|
|
|
|
const slideSpeed = options?.slideSpeed,
|
|
easing = options?.easing,
|
|
delay = options?.delay,
|
|
visibleDisplayValue = options?.visibleDisplayValue;
|
|
return this.slide(domEl, {
|
|
slideSpeed: slideSpeed,
|
|
easing: easing,
|
|
direction: undefined,
|
|
delay: delay,
|
|
visibleDisplayValue: visibleDisplayValue,
|
|
});
|
|
},
|
|
slide(
|
|
element: HTMLElement,
|
|
options: SlideOptions
|
|
): Promise<HTMLElement> {
|
|
const slideSpeed = options.slideSpeed ?? 300,
|
|
direction = options.direction,
|
|
easing = options.easing ?? "ease",
|
|
_ref4$delay = options.delay ?? 0,
|
|
delay = _ref4$delay,
|
|
_ref4$visibleDisplayV = options.visibleDisplayValue,
|
|
visibleDisplayValue =
|
|
_ref4$visibleDisplayV === void 0
|
|
? "block"
|
|
: _ref4$visibleDisplayV;
|
|
const domSliderId =
|
|
element.dataset.domSliderId ||
|
|
(Date.now() * Math.random()).toFixed(0);
|
|
|
|
if (!element.dataset.domSliderId) {
|
|
element.dataset.domSliderId = domSliderId;
|
|
}
|
|
|
|
if (!this.styleCache[domSliderId]) {
|
|
this.styleCache[domSliderId] = {};
|
|
}
|
|
|
|
const cachedStyle = this.styleCache[
|
|
domSliderId
|
|
] as CSSStyleDeclaration;
|
|
const computedStyle = window.getComputedStyle(element);
|
|
const isDisplayNoneByDefault =
|
|
computedStyle.getPropertyValue("display") === "none";
|
|
const slideDirection =
|
|
direction ||
|
|
(isDisplayNoneByDefault ||
|
|
element.classList.contains("DOM-slider-hidden")
|
|
? "down"
|
|
: "up");
|
|
const speed = slideSpeed;
|
|
const boxSizing = computedStyle.getPropertyValue("box-sizing");
|
|
const paddingTop = parseInt(
|
|
computedStyle.getPropertyValue("padding-top").split("px")[0]
|
|
);
|
|
const paddingBottom = parseInt(
|
|
computedStyle.getPropertyValue("padding-bottom").split("px")[0]
|
|
);
|
|
let contentHeight = Math.max(
|
|
element.scrollHeight - paddingTop - paddingBottom,
|
|
0
|
|
);
|
|
|
|
if (boxSizing === "border-box") {
|
|
contentHeight = Math.max(element.scrollHeight, 0);
|
|
}
|
|
|
|
if (element.dataset.sliding) {
|
|
const promise = new Promise(
|
|
(resolve: (value: HTMLElement) => void) => resolve(element)
|
|
);
|
|
this.queue.push({
|
|
promise: promise,
|
|
element: element,
|
|
options: options,
|
|
});
|
|
return promise;
|
|
}
|
|
|
|
if (
|
|
slideDirection === "down" &&
|
|
!isDisplayNoneByDefault &&
|
|
!element.classList.contains("DOM-slider-hidden")
|
|
) {
|
|
return Promise.resolve(element);
|
|
}
|
|
|
|
if (
|
|
slideDirection === "up" &&
|
|
element.classList.contains("DOM-slider-hidden")
|
|
) {
|
|
return Promise.resolve(element);
|
|
}
|
|
|
|
element.dataset.sliding = String(true);
|
|
element.setAttribute(
|
|
"aria-hidden",
|
|
slideDirection === "down" ? "false" : "true"
|
|
);
|
|
|
|
if (slideDirection === "down" && isDisplayNoneByDefault) {
|
|
element.classList.add("DOM-slider-hidden");
|
|
element.style.display = visibleDisplayValue;
|
|
contentHeight = element.scrollHeight;
|
|
} // a fixed height is required in order to animate the height
|
|
|
|
element.style.height = "".concat(
|
|
cachedStyle.height ? cachedStyle.height : String(contentHeight),
|
|
"px"
|
|
);
|
|
element.style.transition = "all "
|
|
.concat(String(speed), "ms ")
|
|
.concat(String(easing) || "");
|
|
element.style.overflow = "hidden";
|
|
return new Promise((resolve) => {
|
|
setTimeout(
|
|
() => {
|
|
// begin the animation
|
|
element.classList.toggle("DOM-slider-hidden");
|
|
resolve(element);
|
|
},
|
|
+delay > 20 ? +delay : 20
|
|
);
|
|
}).then(() => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
element.style.removeProperty("height");
|
|
element.style.removeProperty("transition");
|
|
element.style.removeProperty("overflow");
|
|
element.removeAttribute("data-sliding");
|
|
const style = this.styleCache[
|
|
domSliderId
|
|
] as CSSStyleDeclaration;
|
|
style.height = String(contentHeight);
|
|
this.styleCache[domSliderId] = style;
|
|
resolve(element);
|
|
if (this.queue.length) {
|
|
const queueItem = this.queue.pop();
|
|
if (queueItem) {
|
|
this.slide(
|
|
queueItem.element,
|
|
queueItem.options
|
|
);
|
|
}
|
|
}
|
|
}, speed);
|
|
});
|
|
});
|
|
},
|
|
initDomSlider(): void {
|
|
this.sliderInitied = true;
|
|
const sheet = document.createElement("style");
|
|
sheet.id = "dom-slider";
|
|
sheet.innerHTML =
|
|
"\n .DOM-slider-hidden {\n height: 0 !important;\n padding-top: 0 !important;\n padding-bottom: 0 !important;\n border-top-width: 0 !important;\n border-bottom-width: 0 !important;\n margin-top: 0 !important;\n margin-bottom: 0 !important;\n overflow: hidden !important;\n }\n ";
|
|
document.head.appendChild(sheet);
|
|
},
|
|
fetchEl(el: string | HTMLElement): HTMLElement | null {
|
|
const domEl =
|
|
typeof el === "string"
|
|
? (document.querySelector(el) as HTMLElement | null)
|
|
: el;
|
|
if (!domEl) {
|
|
if (window.vueDebug) {
|
|
console.warn(`'${el}' selector not found`);
|
|
}
|
|
}
|
|
if (!this.sliderInitied) {
|
|
this.initDomSlider();
|
|
}
|
|
return domEl;
|
|
},
|
|
initPrintStyles() {
|
|
let hiddenElements: NodeListOf<HTMLElement>;
|
|
|
|
function showContent() {
|
|
hiddenElements =
|
|
document.querySelectorAll(".DOM-slider-hidden");
|
|
hiddenElements.forEach(function (element) {
|
|
element.classList.remove("DOM-slider-hidden");
|
|
});
|
|
}
|
|
|
|
function hideContent() {
|
|
hiddenElements.forEach(function (element) {
|
|
element.classList.add("DOM-slider-hidden");
|
|
});
|
|
}
|
|
|
|
window.onbeforeprint = showContent;
|
|
window.onafterprint = hideContent;
|
|
const mediaQueryList = window.matchMedia("print");
|
|
mediaQueryList.addEventListener("change", function (mql) {
|
|
if (mql.matches) {
|
|
showContent();
|
|
setTimeout(hideContent, 500);
|
|
}
|
|
});
|
|
},
|
|
},
|
|
};
|