import type { CSSClassName } from "../types/style"; import type { RMLTemplateExpressions, SinkBindingConfiguration } from "../types/internal"; import type { Sink, ExplicitSink } from "../types/sink"; import { SINK_TAG } from "../constants"; import { asap } from "../lib/drain"; import { ObjectMap } from "../lib/object-map"; export const TOGGLE_CLASS_SINK_TAG = 'ToggleClass'; export const CLASS_SINK_TAG = 'class'; // Keeping it same as 'class" attribute for now. Don't change yet... export type ClassRecord = Record; export type ClassName = string; export type ElementsSupportingClass = Record; export const ToggleClassSink = (className: ClassName): Sink => (node: HTMLElement | SVGElement) => node.classList.toggle.bind(node.classList, className) ; export const ClassNameSink: Sink = (node: HTMLElement | SVGElement) => (str: CSSClassName) => //@ts-ignore node.className = str ; export const ClassObjectSink: Sink = (node: Element) => { const cl = node.classList; const set = (str: string) => node.className = str; const add = cl.add.bind(cl); const remove = cl.remove.bind(cl); const toggle = cl.toggle.bind(cl); return (name: ClassName | ClassRecord | ((ClassName | ClassRecord)[])) => { // Do we really want to check for strings here?? typeof name == 'string' ? add(name) // FIXME: is it safe to assume it's an object, at this point? : (<(ClassName | ClassRecord)[]>[]).concat(name).forEach(obj => Object.entries(obj ?? {}) // TODO: support 3-state with toggle .forEach(([k, v]) => { // Use asap to handle both present and future values // For futures (Promise/Observable), it will wait for resolution // For present values, it will execute immediately asap((resolvedValue: any) => resolvedValue ? add(k) : remove(k), v); }) ) }; }; ////////////////////////// export const ExperimentalClassObjectSink: Sink = (node: Element) => { const cl = node.classList; const add = cl.add.bind(cl); const remove = cl.remove.bind(cl); const toggle = cl.toggle.bind(cl); // const actions = Record void>([ const actions = ObjectMap({ true: add, false: remove, undefined: remove, [-1]: remove, [0]: toggle, null: toggle, NaN: toggle, [1]: add, }); return (name: ClassName | ClassRecord) => { typeof name == 'string' ? add(name) // FIXME: is it safe to assume it's an object, at this point? : Object.entries(name ?? {}) .forEach(([k, v]) => actions[v]?.(k)); }; }; /** * A specialised sink to toggle individual classes on a given element * Will toggle the specified class name on the specified element, whenever the source emits. The actual value of the source will be ignored, as it's the emissions which will cause the class toggling. * @param source A present or future string * @param className The class name to toggle * @returns RMLTemplateExpression A template expression for the "className" attribute * @example
* @example
**/ export const ToggleClass: ExplicitSink<'class'> = (source: RMLTemplateExpressions.Any, className: CSSClassName) => >({ type: SINK_TAG, t: TOGGLE_CLASS_SINK_TAG, source, sink: ToggleClassSink(className), }) ; /** * A specialised sink for the "class" HTML attribute * Will set the whole className of an element to the string emitted by the source * @param source A present or future string * @returns RMLTemplateExpression A template expression for the "className" attribute * @example
* @example
* @example
**/ export const ClassName: ExplicitSink<'class'> = (source: RMLTemplateExpressions.ClassName) => >({ type: SINK_TAG, t: 'ClassName', source, sink: ClassNameSink, }) ;