UNPKG

18.4 kBSource Map (JSON)View Raw
1{"version":3,"file":"component.esm.js","sources":["../src/utils.ts","../src/ActualComponentEvent.ts","../src/Component.ts","../src/ComponentEvent.ts","../src/index.ts"],"sourcesContent":["/*\n * Copyright (c) 2015 NAVER Corp.\n * egjs projects are licensed under the MIT license\n */\nexport const isUndefined = (value: any): value is undefined => typeof value === \"undefined\";\n","import { DefaultProps } from \"./types\";\n\n// This class name is not matched to file name intentionally\n/**\n * Event class to provide additional properties\n * @ko Component에서 추가적인 프로퍼티를 제공하는 이벤트 클래스\n */\nclass ComponentEvent<PROPS extends Record<string, any>, TYPE extends string = string, THIS = any> implements DefaultProps<TYPE, THIS> {\n /**\n * A Component instance that triggered event.\n * @type Component\n * @ko 이벤트를 트리거한 Component 인스턴스.\n * @example\n * ```ts\n * class ExtendedClass extends Component<{\n * someEvent: ComponentEvent<{ foo: number; bar: string }>\n * }> {}\n *\n * new ExtendedClass().on(\"someEvent\", e => {\n * e.currentTarget; // ExtendedClass\n * });\n * ```\n */\n public currentTarget: THIS;\n\n /**\n * The name of the event.\n * @type string\n * @ko 이벤트 이름.\n * @example\n * ```ts\n * class ExtendedClass extends Component<{\n * someEvent: ComponentEvent\n * }> {}\n *\n * new ExtendedClass().on(\"someEvent\", e => {\n * e.eventType; // \"someEvent\"\n * });\n * ```\n */\n public eventType: TYPE;\n\n private _canceled: boolean;\n\n /**\n * Create a new instance of ComponentEvent.\n * @ko ComponentEvent의 새로운 인스턴스를 생성한다.\n * @param eventType The name of the event.<ko>이벤트 이름.</ko>\n * @param props An object that contains additional event properties.<ko>추가적인 이벤트 프로퍼티 오브젝트.</ko>\n */\n public constructor(\n eventType: TYPE,\n props: PROPS\n ) {\n this.eventType = eventType;\n this._canceled = false;\n\n if (!props) return;\n\n for (const key of Object.keys(props as Record<string, any>)) {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n this[key] = props[key];\n }\n }\n\n /**\n * Stop the event. {@link ComponentEvent#isCanceled} will return `true` after.\n * @ko 이벤트를 중단한다. 이후 {@link ComponentEvent#isCanceled}가 `true`를 반환한다.\n */\n public stop() {\n this._canceled = true;\n }\n\n /**\n * Returns a boolean value that indicates whether {@link ComponentEvent#stop} is called before.\n * @ko {@link ComponentEvent#stop}이 호출되었는지 여부를 반환한다.\n * @return {boolean} A boolean value that indicates whether {@link ComponentEvent#stop} is called before.<ko>이전에 {@link ComponentEvent#stop}이 불려졌는지 여부를 반환한다.</ko>\n */\n public isCanceled() {\n return this._canceled;\n }\n}\n\nexport default ComponentEvent;\n","/*\n * Copyright (c) 2015 NAVER Corp.\n * egjs projects are licensed under the MIT license\n */\nimport { isUndefined } from \"./utils\";\nimport { EventCallback, EventHash, EventKey, EventMap, EventTriggerParams } from \"./types\";\nimport ComponentEvent from \"./ComponentEvent\";\nimport ActualComponentEvent from \"./ActualComponentEvent\";\n\n/**\n * A class used to manage events in a component\n * @ko 컴포넌트의 이벤트을 관리할 수 있게 하는 클래스\n */\nclass Component<T extends EventMap> {\n /**\n * Version info string\n * @ko 버전정보 문자열\n * @name VERSION\n * @static\n * @example\n * Component.VERSION; // ex) 3.0.0\n * @memberof Component\n */\n public static VERSION: string = \"#__VERSION__#\";\n\n private _eventHandler: { [keys: string]: Array<(...args: any[]) => any> };\n\n /**\n * @support {\"ie\": \"7+\", \"ch\" : \"latest\", \"ff\" : \"latest\", \"sf\" : \"latest\", \"edge\" : \"latest\", \"ios\" : \"7+\", \"an\" : \"2.1+ (except 3.x)\"}\n */\n public constructor() {\n this._eventHandler = {};\n }\n\n public trigger<K extends EventKey<T>>(event: ComponentEvent<T[K], K, this> & T[K]): this;\n public trigger<K extends EventKey<T>>(event: K, ...params: EventTriggerParams<T, K>): this;\n /**\n * Trigger a custom event.\n * @ko 커스텀 이벤트를 발생시킨다\n * @param {string | ComponentEvent} event The name of the custom event to be triggered or an instance of the ComponentEvent<ko>발생할 커스텀 이벤트의 이름 또는 ComponentEvent의 인스턴스</ko>\n * @param {any[]} params Event data to be sent when triggering a custom event <ko>커스텀 이벤트가 발생할 때 전달할 데이터</ko>\n * @return An instance of the component itself<ko>컴포넌트 자신의 인스턴스</ko>\n * @example\n * ```ts\n * import Component, { ComponentEvent } from \"@egjs/component\";\n *\n * class Some extends Component<{\n * beforeHi: ComponentEvent<{ foo: number; bar: string }>;\n * hi: { foo: { a: number; b: boolean } };\n * someEvent: (foo: number, bar: string) => void;\n * someOtherEvent: void; // When there's no event argument\n * }> {\n * some(){\n * if(this.trigger(\"beforeHi\")){ // When event call to stop return false.\n * this.trigger(\"hi\");// fire hi event.\n * }\n * }\n * }\n *\n * const some = new Some();\n * some.on(\"beforeHi\", e => {\n * if(condition){\n * e.stop(); // When event call to stop, `hi` event not call.\n * }\n * // `currentTarget` is component instance.\n * console.log(some === e.currentTarget); // true\n *\n * typeof e.foo; // number\n * typeof e.bar; // string\n * });\n * some.on(\"hi\", e => {\n * typeof e.foo.b; // boolean\n * });\n * // If you want to more know event design. You can see article.\n * // https://github.com/naver/egjs-component/wiki/How-to-make-Component-event-design%3F\n * ```\n */\n public trigger<K extends EventKey<T>>(event: K | ComponentEvent<T[K], K, this>, ...params: EventTriggerParams<T, K> | void[]): this {\n const eventName = (event as any) instanceof ActualComponentEvent\n ? (event as ActualComponentEvent<T[K]>).eventType\n : event as K;\n\n const handlers = [...(this._eventHandler[eventName] || [])];\n\n if (handlers.length <= 0) {\n return this;\n }\n\n if ((event as any) instanceof ActualComponentEvent) {\n (event as ActualComponentEvent<T[K]>).currentTarget = this;\n\n handlers.forEach((handler: (event: ComponentEvent<T[K], K, this>) => any) => {\n handler(event as ComponentEvent<T[K], K, this>);\n });\n } else {\n handlers.forEach(handler => {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n handler(...params);\n });\n }\n\n return this;\n }\n\n public once<K extends EventKey<T>>(eventName: K, handlerToAttach: EventCallback<T, K, this>): this;\n public once(eventHash: EventHash<T, this>): this;\n /**\n * Executed event just one time.\n * @ko 이벤트가 한번만 실행된다.\n * @param {string} eventName The name of the event to be attached or an event name - event handler mapped object.<ko>등록할 이벤트의 이름 또는 이벤트 이름-핸들러 오브젝트</ko>\n * @param {function} handlerToAttach The handler function of the event to be attached <ko>등록할 이벤트의 핸들러 함수</ko>\n * @return An instance of the component itself<ko>컴포넌트 자신의 인스턴스</ko>\n * @example\n * ```ts\n * import Component, { ComponentEvent } from \"@egjs/component\";\n *\n * class Some extends Component<{\n * hi: ComponentEvent;\n * }> {\n * hi() {\n * alert(\"hi\");\n * }\n * thing() {\n * this.once(\"hi\", this.hi);\n * }\n * }\n *\n * var some = new Some();\n * some.thing();\n * some.trigger(new ComponentEvent(\"hi\"));\n * // fire alert(\"hi\");\n * some.trigger(new ComponentEvent(\"hi\"));\n * // Nothing happens\n * ```\n */\n public once<K extends EventKey<T>>(eventName: K | EventHash<T, this>, handlerToAttach?: EventCallback<T, K, this>): this {\n if (typeof eventName === \"object\" && isUndefined(handlerToAttach)) {\n const eventHash = eventName;\n\n for (const key in eventHash) {\n this.once((key as K), eventHash[key] as EventCallback<T, K, this>);\n }\n return this;\n } else if (typeof eventName === \"string\" && typeof handlerToAttach === \"function\") {\n const listener: any = (...args: any[]) => {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n handlerToAttach(...args);\n this.off(eventName, listener);\n };\n\n this.on(eventName, listener);\n }\n\n return this;\n }\n\n /**\n * Checks whether an event has been attached to a component.\n * @ko 컴포넌트에 이벤트가 등록됐는지 확인한다.\n * @param {string} eventName The name of the event to be attached <ko>등록 여부를 확인할 이벤트의 이름</ko>\n * @return {boolean} Indicates whether the event is attached. <ko>이벤트 등록 여부</ko>\n * @example\n * ```ts\n * import Component from \"@egjs/component\";\n *\n * class Some extends Component<{\n * hi: void;\n * }> {\n * some() {\n * this.hasOn(\"hi\");// check hi event.\n * }\n * }\n * ```\n */\n public hasOn<K extends EventKey<T>>(eventName: K): boolean {\n return !!this._eventHandler[eventName];\n }\n\n public on<K extends EventKey<T>>(eventName: K, handlerToAttach: EventCallback<T, K, this>): this;\n public on(eventHash: EventHash<T, this>): this;\n /**\n * Attaches an event to a component.\n * @ko 컴포넌트에 이벤트를 등록한다.\n * @param {string} eventName The name of the event to be attached or an event name - event handler mapped object.<ko>등록할 이벤트의 이름 또는 이벤트 이름-핸들러 오브젝트</ko>\n * @param {function} handlerToAttach The handler function of the event to be attached <ko>등록할 이벤트의 핸들러 함수</ko>\n * @return An instance of a component itself<ko>컴포넌트 자신의 인스턴스</ko>\n * @example\n * ```ts\n * import Component, { ComponentEvent } from \"@egjs/component\";\n *\n * class Some extends Component<{\n * hi: void;\n * }> {\n * hi() {\n * console.log(\"hi\");\n * }\n * some() {\n * this.on(\"hi\",this.hi); //attach event\n * }\n * }\n * ```\n */\n public on<K extends EventKey<T>>(eventName: K | EventHash<T, this>, handlerToAttach?: EventCallback<T, K, this>): this {\n if (typeof eventName === \"object\" && isUndefined(handlerToAttach)) {\n const eventHash = eventName;\n\n for (const name in eventHash) {\n this.on(name, eventHash[name] as any);\n }\n\n return this;\n } else if (typeof eventName === \"string\" &&\n typeof handlerToAttach === \"function\") {\n let handlerList = this._eventHandler[eventName];\n\n if (isUndefined(handlerList)) {\n this._eventHandler[eventName] = [];\n handlerList = this._eventHandler[eventName];\n }\n\n handlerList.push(handlerToAttach as EventCallback<T, EventKey<T>, this>);\n }\n\n return this;\n }\n\n public off(eventHash?: EventHash<T, this>): this;\n public off<K extends EventKey<T>>(eventName: K, handlerToDetach?: EventCallback<T, K, this>): this;\n /**\n * Detaches an event from the component.<br/>If the `eventName` is not given this will detach all event handlers attached.<br/>If the `handlerToDetach` is not given, this will detach all event handlers for `eventName`.\n * @ko 컴포넌트에 등록된 이벤트를 해제한다.<br/>`eventName`이 주어지지 않았을 경우 모든 이벤트 핸들러를 제거한다.<br/>`handlerToAttach`가 주어지지 않았을 경우 `eventName`에 해당하는 모든 이벤트 핸들러를 제거한다.\n * @param {string?} eventName The name of the event to be detached <ko>해제할 이벤트의 이름</ko>\n * @param {function?} handlerToDetach The handler function of the event to be detached <ko>해제할 이벤트의 핸들러 함수</ko>\n * @return An instance of a component itself <ko>컴포넌트 자신의 인스턴스</ko>\n * @example\n * ```ts\n * import Component, { ComponentEvent } from \"@egjs/component\";\n *\n * class Some extends Component<{\n * hi: void;\n * }> {\n * hi() {\n * console.log(\"hi\");\n * }\n * some() {\n * this.off(\"hi\",this.hi); //detach event\n * }\n * }\n * ```\n */\n public off<K extends EventKey<T>>(eventName?: K | EventHash<T, this>, handlerToDetach?: EventCallback<T, K, this>): this {\n // Detach all event handlers.\n if (isUndefined(eventName)) {\n this._eventHandler = {};\n return this;\n }\n\n // Detach all handlers for eventname or detach event handlers by object.\n if (isUndefined(handlerToDetach)) {\n if (typeof eventName === \"string\") {\n delete this._eventHandler[eventName];\n return this;\n } else {\n const eventHash = eventName;\n\n for (const name in eventHash) {\n this.off(name, eventHash[name] as any);\n }\n return this;\n }\n }\n\n // Detach single event handler\n const handlerList = this._eventHandler[eventName as K];\n\n if (handlerList) {\n let idx = 0;\n for (const handlerFunction of handlerList) {\n if (handlerFunction === handlerToDetach) {\n handlerList.splice(idx, 1);\n\n if (handlerList.length <= 0) {\n delete this._eventHandler[eventName as K];\n }\n\n break;\n }\n idx++;\n }\n }\n\n return this;\n }\n}\n\nexport default Component;\n","/*\n * Copyright (c) 2015 NAVER Corp.\n * egjs projects are licensed under the MIT license\n */\nimport ActualComponentEvent from \"./ActualComponentEvent\";\nimport { ComponentEventConstructor, DefaultProps } from \"./types\";\n\n// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unsafe-assignment\nconst ComponentEvent = ActualComponentEvent as ComponentEventConstructor;\n\n// eslint-disable-next-line @typescript-eslint/ban-types\ntype ComponentEvent<PROPS = {}, TYPE extends string = string, THIS = any> = DefaultProps<TYPE, THIS> & PROPS;\n\nexport default ComponentEvent;\n","/*\n * Copyright (c) 2015 NAVER Corp.\n * egjs projects are licensed under the MIT license\n */\nimport Component from \"./Component\";\nimport ComponentEvent from \"./ComponentEvent\";\n\nexport { ComponentEvent };\nexport default Component;\n"],"names":["isUndefined","value","eventType","props","_canceled","_b","__values","Object","keys","key","_eventHandler","event","_i","params","eventName","ActualComponentEvent","handlers","length","currentTarget","forEach","handler","handlerToAttach","eventHash","once","listener_1","args","_this","off","on","name","handlerList","push","handlerToDetach","idx","handlerList_1","handlerFunction","splice","Component","ComponentEvent"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;AAIA,AAAO,IAAMA,WAAW,GAAG,UAACC,KAAD;SAAoC,OAAOA,KAAP,KAAiB,WAAjB;CAAxD;;ACDP;;;;;AAIA;;;;;;;;;yBA2CE,CACEC,SADF,EAEEC,KAFF;;;SAIOD,SAAL,GAAiBA,SAAjB;SACKE,SAAL,GAAiB,KAAjB;QAEI,CAACD,KAAL,EAAY;;;WAEM,IAAAE,KAAAC,SAAAC,MAAM,CAACC,IAAP,CAAYL,KAAZ,EAAA,gBAAlB,UAAA,gBAAA,EAA6D;YAAlDM,GAAG,WAAT,CAAwD;;aAEtDA,GAAL,IAAYN,KAAK,CAACM,GAAD,CAAjB;;;;;;;;;;;;;;;;;;;;;;cAQG,GAAP;SACOL,SAAL,GAAiB,IAAjB;GADK;;;;;;;;oBASA,GAAP;WACS,KAAKA,SAAZ;GADK;;uBAGT;GA1EA;;ACEA;;;;;AAIA;;;;;;oBAiBE;SACOM,aAAL,GAAqB,EAArB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8CK,GAAP,UAAsCC,KAAtC;mBAAgF;;SAAA,YAAAC,uBAAAA;MAAAC,cAAA,gBAAA;;;QACxEC,SAAS,GAAIH,KAAa,YAAYI,cAAzB,GACdJ,KAAoC,CAACT,SADvB,GAEfS,KAFJ;;QAIMK,QAAQ,YAAQ,KAAKN,aAAL,CAAmBI,SAAnB,KAAiC,GAAvD;;QAEIE,QAAQ,CAACC,MAAT,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