All files decorators.ts

100% Statements 19/19
100% Branches 6/6
83.33% Functions 5/6
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 623x 3x 3x 4x 3x                                             3x 5x 5x 1x   5x 1x   5x                               3x 3x 3x 2x   3x          
import { EventMapListenAt } from "@domx/eventmap";
import { customDataElements, DataElement, DataElementCtor } from "./DataElement";
export { customDataElement, dataProperty }
export { event } from "@domx/eventmap/decorators";
export { EventMapListenAt }
 
 
interface CustomDataElementOptions {
    /** Sets which property is to be used as the stateId; default: stateId */
    stateIdProperty?: string,
    /**
     * Sets the default event listener element for events;
     * can be "self", "parent", or "window";
     * default: "self"
     */
    eventsListenAt?: EventMapListenAt|string
}
 
/**
 * A class decorator that defines the custom element with
 * `window.customElements.define` and tags the element name
 * for use in RootState.
 * 
 * Options allow for setting `stateIdProperty` and `eventsListenAt`.
 * @param elementName {string}
 * @param options {CustomDataElementOptions}
 */
const customDataElement = (elementName:string, options:CustomDataElementOptions={}) =>
    (ctor: CustomElementConstructor) => {
    if (options.stateIdProperty) {
        (ctor as any)["stateIdProperty"] = options.stateIdProperty;
    }
    if (options.eventsListenAt) {
        (ctor as any)["eventsListenAt"] = options.eventsListenAt;
    }
    customDataElements.define(elementName, ctor);
};
 
 
 
 
interface DataPropertyOptions {
    changeEvent:string
}
/**
 * A property decorator that tags a class property
 * as a state property.
 * 
 * Options allow for setting the change event name.
 * @param options 
 */
const dataProperty = (options?:DataPropertyOptions):any =>
    (prototype: any, propertyName: string) => {
        if (prototype.constructor.dataProperties === DataElement.dataProperties) {
            prototype.constructor.dataProperties = {};
        }
        (prototype.constructor as DataElementCtor).dataProperties[propertyName] = {
            changeEvent: options ? options.changeEvent : `${propertyName}-changed`
        };
    };