feat: Upgrade template based on projects made with it
Some checks failed
Build, check & Test / run (push) Failing after 42s
Some checks failed
Build, check & Test / run (push) Failing after 42s
Signed-off-by: Florian Bouillon <f.bouillon@aptatio.com>
This commit is contained in:
53
src/libs/Component.ts
Normal file
53
src/libs/Component.ts
Normal file
@ -0,0 +1,53 @@
|
||||
type Fn<T extends HTMLElement> = (el: Component<T>) => void | Promise<void>
|
||||
|
||||
/**
|
||||
* Component client side initialisation class
|
||||
*/
|
||||
export default class Component<T extends HTMLElement> {
|
||||
private constructor(
|
||||
public element: T
|
||||
) {}
|
||||
|
||||
public handled(value: boolean): this
|
||||
public handled(): boolean
|
||||
public handled(value?: boolean): this | boolean {
|
||||
if (typeof value === 'undefined') {
|
||||
return typeof this.element.dataset.handled === 'string'
|
||||
}
|
||||
this.element.dataset.handled = ''
|
||||
return this
|
||||
}
|
||||
|
||||
public init(fn: (el: Component<T>) => void | Promise<void>) {
|
||||
if (this.handled()) {
|
||||
return
|
||||
}
|
||||
fn(this)
|
||||
this.handled(true)
|
||||
}
|
||||
|
||||
public child<El extends HTMLElement>(query: string, fn: Fn<El>) {
|
||||
this.element.querySelectorAll<El>(query).forEach((it) => {
|
||||
const cp = new Component(it)
|
||||
cp.init(fn)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* start handling an element
|
||||
* @param query the query to get the element
|
||||
* @param fn the function that is run ONCE per elements
|
||||
*/
|
||||
public static handle<T extends HTMLElement>(query: string, fn: (el: T) => void | Promise<void>) {
|
||||
document.querySelectorAll<T>(query).forEach((it) => {
|
||||
const cp = new Component(it)
|
||||
cp.init((it) => fn(it.element))
|
||||
})
|
||||
document.addEventListener('astro:page-load', () => {
|
||||
document.querySelectorAll<T>(query).forEach((it) => {
|
||||
const cp = new Component(it)
|
||||
cp.init((it) => fn(it.element))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
@ -1,10 +1,18 @@
|
||||
import { objectLoop } from '@dzeio/object-util'
|
||||
import StatusCode from './HTTP/StatusCode'
|
||||
|
||||
/**
|
||||
* Simple builde to create a new Response object
|
||||
*/
|
||||
export default class ResponseBuilder {
|
||||
|
||||
public static redirect(location: string, statusCode: number = StatusCode.FOUND) {
|
||||
const resp = new ResponseBuilder()
|
||||
resp.addHeader('Location', location)
|
||||
resp.status(statusCode)
|
||||
return resp.build()
|
||||
}
|
||||
|
||||
private _body: BodyInit | null | undefined
|
||||
public body(body: string | Buffer | object | null | undefined) {
|
||||
if (typeof body === 'object' && !(body instanceof Buffer)) {
|
||||
@ -42,7 +50,7 @@ export default class ResponseBuilder {
|
||||
}
|
||||
|
||||
private _status?: number
|
||||
public status(status: number) {
|
||||
public status(status: StatusCode | number) {
|
||||
this._status = status
|
||||
return this
|
||||
}
|
||||
|
Reference in New Issue
Block a user