UNPKG

16.7 kBSource Map (JSON)View Raw
1{"version":3,"file":"component.min.js","sources":["../src/utils.ts","../src/ActualComponentEvent.ts","../src/Component.ts","../src/index.umd.ts","../src/ComponentEvent.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 */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n\nimport Component from \"./Component\";\nimport ComponentEvent from \"./ComponentEvent\";\n\n(Component as any).ComponentEvent = ComponentEvent;\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"],"names":["isUndefined","value","eventType","props","_canceled","_b","__values","Object","keys","key","this","_eventHandler","event","_i","params","eventName","ActualComponentEvent","handlers","length","currentTarget","forEach","handler","handlerToAttach","listener_1","eventHash","once","args","_this","off","on","name","handlerList","push","handlerToDetach","idx","handlerList_1","splice","Component","ComponentEvent"],"mappings":";;;;;;;;+0BAI2B,SAAdA,EAAeC,eAAoD,IAAVA,EAA/D,4BC+CHC,EACAC,mBAEKD,UAAYA,OACZE,WAAY,EAEZD,UAEa,IAAAE,EAAAC,EAAAC,OAAOC,KAAKL,kCAA+B,KAAlDM,eAEJA,GAAON,EAAMM,sIAQtB,gBACOL,WAAY,gBAQnB,kBACSM,KAAKN,+CChDPO,cAAgB,sCA8CvB,SAAsCC,oBAA0CC,mBAAAA,IAAAC,wBACxEC,EAAaH,aAAyBI,EACvCJ,EAAqCV,UACtCU,EAEEK,IAAgBP,KAAKC,cAAcI,IAAc,WAEnDE,EAASC,QAAU,IAIlBN,aAAyBI,GAC3BJ,EAAqCO,cAAgBT,KAEtDO,EAASG,QAAQ,SAACC,GAChBA,EAAQT,MAGVK,EAASG,QAAQ,SAAAC,GAEfA,iBAAWP,OAZNJ,aAkDX,SAAmCK,EAAmCO,OAS5DC,YARiB,iBAAdR,GAA0Bf,EAAYsB,GAAkB,KAGtDb,EAFLe,EAAYT,MAEPN,KAAOe,OACXC,KAAMhB,EAAWe,EAAUf,WAE3BC,WACuB,iBAAdK,GAAqD,mBAApBO,IAC3CC,EAAgB,4BAACV,mBAAAA,IAAAa,kBAErBJ,iBAAmBI,IACnBC,EAAKC,IAAIb,EAAWQ,SAGjBM,GAAGd,EAAWQ,IAGdb,cAqBT,SAAoCK,WACzBL,KAAKC,cAAcI,SA2B9B,SAAiCA,EAAmCO,MACzC,iBAAdP,GAA0Bf,EAAYsB,GAAkB,KAGtDQ,EAFLN,EAAYT,MAEPe,KAAQN,OACZK,GAAGC,EAAMN,EAAUM,WAGnBpB,KACF,IAEDqB,QAF0B,iBAAdhB,GACW,mBAApBO,IACHS,EAAcrB,KAAKC,cAAcI,GAEjCf,EAAY+B,UACTpB,cAAcI,GAAa,GAChCgB,EAAcrB,KAAKC,cAAcI,IAGnCgB,EAAYC,KAAKV,IAGZZ,YA2BT,SAAkCK,EAAoCkB,cAEhEjC,EAAYe,eACTJ,cAAgB,GACdD,QAILV,EAAYiC,GAAkB,IACP,iBAAdlB,gBACFL,KAAKC,cAAcI,GACnBL,SAIIoB,EAFLN,EAAYT,MAEPe,KAAQN,OACZI,IAAIE,EAAMN,EAAUM,WAEpBpB,SAKLqB,EAAcrB,KAAKC,cAAcI,MAEnCgB,EAAa,KACXG,EAAM,UACoB,IAAAC,EAAA7B,EAAAyB,iCAAa,cACjBE,EAAiB,CACvCF,EAAYK,OAAOF,EAAK,GAEpBH,EAAYb,QAAU,UACjBR,KAAKC,cAAcI,SAK9BmB,8GAIGxB,MA5QK2B,UAAkB,oBCdjCA,EAAkBC,eCDItB"}
\No newline at end of file