1 | /*
|
2 | Copyright (c) NAVER Corp.
|
3 | name: @egjs/component
|
4 | license: MIT
|
5 | author: NAVER Corp.
|
6 | repository: https://github.com/naver/egjs-component
|
7 | version: 3.0.1
|
8 | */
|
9 | (function (global, factory) {
|
10 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
11 | typeof define === 'function' && define.amd ? define(factory) :
|
12 | (global.Component = factory());
|
13 | }(this, (function () { 'use strict';
|
14 |
|
15 | /*! *****************************************************************************
|
16 | Copyright (c) Microsoft Corporation.
|
17 |
|
18 | Permission to use, copy, modify, and/or distribute this software for any
|
19 | purpose with or without fee is hereby granted.
|
20 |
|
21 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
22 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
23 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
24 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
25 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
26 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
27 | PERFORMANCE OF THIS SOFTWARE.
|
28 | ***************************************************************************** */
|
29 | function __values(o) {
|
30 | var s = typeof Symbol === "function" && Symbol.iterator,
|
31 | m = s && o[s],
|
32 | i = 0;
|
33 | if (m) return m.call(o);
|
34 | if (o && typeof o.length === "number") return {
|
35 | next: function () {
|
36 | if (o && i >= o.length) o = void 0;
|
37 | return {
|
38 | value: o && o[i++],
|
39 | done: !o
|
40 | };
|
41 | }
|
42 | };
|
43 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
44 | }
|
45 | function __read(o, n) {
|
46 | var m = typeof Symbol === "function" && o[Symbol.iterator];
|
47 | if (!m) return o;
|
48 | var i = m.call(o),
|
49 | r,
|
50 | ar = [],
|
51 | e;
|
52 |
|
53 | try {
|
54 | while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
55 | } catch (error) {
|
56 | e = {
|
57 | error: error
|
58 | };
|
59 | } finally {
|
60 | try {
|
61 | if (r && !r.done && (m = i["return"])) m.call(i);
|
62 | } finally {
|
63 | if (e) throw e.error;
|
64 | }
|
65 | }
|
66 |
|
67 | return ar;
|
68 | }
|
69 | function __spread() {
|
70 | for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
71 |
|
72 | return ar;
|
73 | }
|
74 |
|
75 | /*
|
76 | * Copyright (c) 2015 NAVER Corp.
|
77 | * egjs projects are licensed under the MIT license
|
78 | */
|
79 | var isUndefined = function (value) {
|
80 | return typeof value === "undefined";
|
81 | };
|
82 |
|
83 | /**
|
84 | * Event class to provide additional properties
|
85 | * @ko Component에서 추가적인 프로퍼티를 제공하는 이벤트 클래스
|
86 | */
|
87 |
|
88 | var ComponentEvent =
|
89 | /*#__PURE__*/
|
90 | function () {
|
91 | /**
|
92 | * Create a new instance of ComponentEvent.
|
93 | * @ko ComponentEvent의 새로운 인스턴스를 생성한다.
|
94 | * @param eventType The name of the event.<ko>이벤트 이름.</ko>
|
95 | * @param props An object that contains additional event properties.<ko>추가적인 이벤트 프로퍼티 오브젝트.</ko>
|
96 | */
|
97 | function ComponentEvent(eventType, props) {
|
98 | var e_1, _a;
|
99 |
|
100 | this.eventType = eventType;
|
101 | this._canceled = false;
|
102 | if (!props) return;
|
103 |
|
104 | try {
|
105 | for (var _b = __values(Object.keys(props)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
106 | var key = _c.value; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
107 |
|
108 | this[key] = props[key];
|
109 | }
|
110 | } catch (e_1_1) {
|
111 | e_1 = {
|
112 | error: e_1_1
|
113 | };
|
114 | } finally {
|
115 | try {
|
116 | if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
117 | } finally {
|
118 | if (e_1) throw e_1.error;
|
119 | }
|
120 | }
|
121 | }
|
122 | /**
|
123 | * Stop the event. {@link ComponentEvent#isCanceled} will return `true` after.
|
124 | * @ko 이벤트를 중단한다. 이후 {@link ComponentEvent#isCanceled}가 `true`를 반환한다.
|
125 | */
|
126 |
|
127 |
|
128 | var __proto = ComponentEvent.prototype;
|
129 |
|
130 | __proto.stop = function () {
|
131 | this._canceled = true;
|
132 | };
|
133 | /**
|
134 | * Returns a boolean value that indicates whether {@link ComponentEvent#stop} is called before.
|
135 | * @ko {@link ComponentEvent#stop}이 호출되었는지 여부를 반환한다.
|
136 | * @return {boolean} A boolean value that indicates whether {@link ComponentEvent#stop} is called before.<ko>이전에 {@link ComponentEvent#stop}이 불려졌는지 여부를 반환한다.</ko>
|
137 | */
|
138 |
|
139 |
|
140 | __proto.isCanceled = function () {
|
141 | return this._canceled;
|
142 | };
|
143 |
|
144 | return ComponentEvent;
|
145 | }();
|
146 |
|
147 | /**
|
148 | * A class used to manage events in a component
|
149 | * @ko 컴포넌트의 이벤트을 관리할 수 있게 하는 클래스
|
150 | */
|
151 |
|
152 | var Component =
|
153 | /*#__PURE__*/
|
154 | function () {
|
155 | /**
|
156 | * @support {"ie": "7+", "ch" : "latest", "ff" : "latest", "sf" : "latest", "edge" : "latest", "ios" : "7+", "an" : "2.1+ (except 3.x)"}
|
157 | */
|
158 | function Component() {
|
159 | this._eventHandler = {};
|
160 | }
|
161 | /**
|
162 | * Trigger a custom event.
|
163 | * @ko 커스텀 이벤트를 발생시킨다
|
164 | * @param {string | ComponentEvent} event The name of the custom event to be triggered or an instance of the ComponentEvent<ko>발생할 커스텀 이벤트의 이름 또는 ComponentEvent의 인스턴스</ko>
|
165 | * @param {any[]} params Event data to be sent when triggering a custom event <ko>커스텀 이벤트가 발생할 때 전달할 데이터</ko>
|
166 | * @return An instance of the component itself<ko>컴포넌트 자신의 인스턴스</ko>
|
167 | * @example
|
168 | * ```ts
|
169 | * import Component, { ComponentEvent } from "@egjs/component";
|
170 | *
|
171 | * class Some extends Component<{
|
172 | * beforeHi: ComponentEvent<{ foo: number; bar: string }>;
|
173 | * hi: { foo: { a: number; b: boolean } };
|
174 | * someEvent: (foo: number, bar: string) => void;
|
175 | * someOtherEvent: void; // When there's no event argument
|
176 | * }> {
|
177 | * some(){
|
178 | * if(this.trigger("beforeHi")){ // When event call to stop return false.
|
179 | * this.trigger("hi");// fire hi event.
|
180 | * }
|
181 | * }
|
182 | * }
|
183 | *
|
184 | * const some = new Some();
|
185 | * some.on("beforeHi", e => {
|
186 | * if(condition){
|
187 | * e.stop(); // When event call to stop, `hi` event not call.
|
188 | * }
|
189 | * // `currentTarget` is component instance.
|
190 | * console.log(some === e.currentTarget); // true
|
191 | *
|
192 | * typeof e.foo; // number
|
193 | * typeof e.bar; // string
|
194 | * });
|
195 | * some.on("hi", e => {
|
196 | * typeof e.foo.b; // boolean
|
197 | * });
|
198 | * // If you want to more know event design. You can see article.
|
199 | * // https://github.com/naver/egjs-component/wiki/How-to-make-Component-event-design%3F
|
200 | * ```
|
201 | */
|
202 |
|
203 |
|
204 | var __proto = Component.prototype;
|
205 |
|
206 | __proto.trigger = function (event) {
|
207 | var params = [];
|
208 |
|
209 | for (var _i = 1; _i < arguments.length; _i++) {
|
210 | params[_i - 1] = arguments[_i];
|
211 | }
|
212 |
|
213 | var eventName = event instanceof ComponentEvent ? event.eventType : event;
|
214 |
|
215 | var handlers = __spread(this._eventHandler[eventName] || []);
|
216 |
|
217 | if (handlers.length <= 0) {
|
218 | return this;
|
219 | }
|
220 |
|
221 | if (event instanceof ComponentEvent) {
|
222 | event.currentTarget = this;
|
223 | handlers.forEach(function (handler) {
|
224 | handler(event);
|
225 | });
|
226 | } else {
|
227 | handlers.forEach(function (handler) {
|
228 | // eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
229 | handler.apply(void 0, __spread(params));
|
230 | });
|
231 | }
|
232 |
|
233 | return this;
|
234 | };
|
235 | /**
|
236 | * Executed event just one time.
|
237 | * @ko 이벤트가 한번만 실행된다.
|
238 | * @param {string} eventName The name of the event to be attached or an event name - event handler mapped object.<ko>등록할 이벤트의 이름 또는 이벤트 이름-핸들러 오브젝트</ko>
|
239 | * @param {function} handlerToAttach The handler function of the event to be attached <ko>등록할 이벤트의 핸들러 함수</ko>
|
240 | * @return An instance of the component itself<ko>컴포넌트 자신의 인스턴스</ko>
|
241 | * @example
|
242 | * ```ts
|
243 | * import Component, { ComponentEvent } from "@egjs/component";
|
244 | *
|
245 | * class Some extends Component<{
|
246 | * hi: ComponentEvent;
|
247 | * }> {
|
248 | * hi() {
|
249 | * alert("hi");
|
250 | * }
|
251 | * thing() {
|
252 | * this.once("hi", this.hi);
|
253 | * }
|
254 | * }
|
255 | *
|
256 | * var some = new Some();
|
257 | * some.thing();
|
258 | * some.trigger(new ComponentEvent("hi"));
|
259 | * // fire alert("hi");
|
260 | * some.trigger(new ComponentEvent("hi"));
|
261 | * // Nothing happens
|
262 | * ```
|
263 | */
|
264 |
|
265 |
|
266 | __proto.once = function (eventName, handlerToAttach) {
|
267 | var _this = this;
|
268 |
|
269 | if (typeof eventName === "object" && isUndefined(handlerToAttach)) {
|
270 | var eventHash = eventName;
|
271 |
|
272 | for (var key in eventHash) {
|
273 | this.once(key, eventHash[key]);
|
274 | }
|
275 |
|
276 | return this;
|
277 | } else if (typeof eventName === "string" && typeof handlerToAttach === "function") {
|
278 | var listener_1 = function () {
|
279 | var args = [];
|
280 |
|
281 | for (var _i = 0; _i < arguments.length; _i++) {
|
282 | args[_i] = arguments[_i];
|
283 | } // eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
284 |
|
285 |
|
286 | handlerToAttach.apply(void 0, __spread(args));
|
287 |
|
288 | _this.off(eventName, listener_1);
|
289 | };
|
290 |
|
291 | this.on(eventName, listener_1);
|
292 | }
|
293 |
|
294 | return this;
|
295 | };
|
296 | /**
|
297 | * Checks whether an event has been attached to a component.
|
298 | * @ko 컴포넌트에 이벤트가 등록됐는지 확인한다.
|
299 | * @param {string} eventName The name of the event to be attached <ko>등록 여부를 확인할 이벤트의 이름</ko>
|
300 | * @return {boolean} Indicates whether the event is attached. <ko>이벤트 등록 여부</ko>
|
301 | * @example
|
302 | * ```ts
|
303 | * import Component from "@egjs/component";
|
304 | *
|
305 | * class Some extends Component<{
|
306 | * hi: void;
|
307 | * }> {
|
308 | * some() {
|
309 | * this.hasOn("hi");// check hi event.
|
310 | * }
|
311 | * }
|
312 | * ```
|
313 | */
|
314 |
|
315 |
|
316 | __proto.hasOn = function (eventName) {
|
317 | return !!this._eventHandler[eventName];
|
318 | };
|
319 | /**
|
320 | * Attaches an event to a component.
|
321 | * @ko 컴포넌트에 이벤트를 등록한다.
|
322 | * @param {string} eventName The name of the event to be attached or an event name - event handler mapped object.<ko>등록할 이벤트의 이름 또는 이벤트 이름-핸들러 오브젝트</ko>
|
323 | * @param {function} handlerToAttach The handler function of the event to be attached <ko>등록할 이벤트의 핸들러 함수</ko>
|
324 | * @return An instance of a component itself<ko>컴포넌트 자신의 인스턴스</ko>
|
325 | * @example
|
326 | * ```ts
|
327 | * import Component, { ComponentEvent } from "@egjs/component";
|
328 | *
|
329 | * class Some extends Component<{
|
330 | * hi: void;
|
331 | * }> {
|
332 | * hi() {
|
333 | * console.log("hi");
|
334 | * }
|
335 | * some() {
|
336 | * this.on("hi",this.hi); //attach event
|
337 | * }
|
338 | * }
|
339 | * ```
|
340 | */
|
341 |
|
342 |
|
343 | __proto.on = function (eventName, handlerToAttach) {
|
344 | if (typeof eventName === "object" && isUndefined(handlerToAttach)) {
|
345 | var eventHash = eventName;
|
346 |
|
347 | for (var name in eventHash) {
|
348 | this.on(name, eventHash[name]);
|
349 | }
|
350 |
|
351 | return this;
|
352 | } else if (typeof eventName === "string" && typeof handlerToAttach === "function") {
|
353 | var handlerList = this._eventHandler[eventName];
|
354 |
|
355 | if (isUndefined(handlerList)) {
|
356 | this._eventHandler[eventName] = [];
|
357 | handlerList = this._eventHandler[eventName];
|
358 | }
|
359 |
|
360 | handlerList.push(handlerToAttach);
|
361 | }
|
362 |
|
363 | return this;
|
364 | };
|
365 | /**
|
366 | * 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`.
|
367 | * @ko 컴포넌트에 등록된 이벤트를 해제한다.<br/>`eventName`이 주어지지 않았을 경우 모든 이벤트 핸들러를 제거한다.<br/>`handlerToAttach`가 주어지지 않았을 경우 `eventName`에 해당하는 모든 이벤트 핸들러를 제거한다.
|
368 | * @param {string?} eventName The name of the event to be detached <ko>해제할 이벤트의 이름</ko>
|
369 | * @param {function?} handlerToDetach The handler function of the event to be detached <ko>해제할 이벤트의 핸들러 함수</ko>
|
370 | * @return An instance of a component itself <ko>컴포넌트 자신의 인스턴스</ko>
|
371 | * @example
|
372 | * ```ts
|
373 | * import Component, { ComponentEvent } from "@egjs/component";
|
374 | *
|
375 | * class Some extends Component<{
|
376 | * hi: void;
|
377 | * }> {
|
378 | * hi() {
|
379 | * console.log("hi");
|
380 | * }
|
381 | * some() {
|
382 | * this.off("hi",this.hi); //detach event
|
383 | * }
|
384 | * }
|
385 | * ```
|
386 | */
|
387 |
|
388 |
|
389 | __proto.off = function (eventName, handlerToDetach) {
|
390 | var e_1, _a; // Detach all event handlers.
|
391 |
|
392 |
|
393 | if (isUndefined(eventName)) {
|
394 | this._eventHandler = {};
|
395 | return this;
|
396 | } // Detach all handlers for eventname or detach event handlers by object.
|
397 |
|
398 |
|
399 | if (isUndefined(handlerToDetach)) {
|
400 | if (typeof eventName === "string") {
|
401 | delete this._eventHandler[eventName];
|
402 | return this;
|
403 | } else {
|
404 | var eventHash = eventName;
|
405 |
|
406 | for (var name in eventHash) {
|
407 | this.off(name, eventHash[name]);
|
408 | }
|
409 |
|
410 | return this;
|
411 | }
|
412 | } // Detach single event handler
|
413 |
|
414 |
|
415 | var handlerList = this._eventHandler[eventName];
|
416 |
|
417 | if (handlerList) {
|
418 | var idx = 0;
|
419 |
|
420 | try {
|
421 | for (var handlerList_1 = __values(handlerList), handlerList_1_1 = handlerList_1.next(); !handlerList_1_1.done; handlerList_1_1 = handlerList_1.next()) {
|
422 | var handlerFunction = handlerList_1_1.value;
|
423 |
|
424 | if (handlerFunction === handlerToDetach) {
|
425 | handlerList.splice(idx, 1);
|
426 |
|
427 | if (handlerList.length <= 0) {
|
428 | delete this._eventHandler[eventName];
|
429 | }
|
430 |
|
431 | break;
|
432 | }
|
433 |
|
434 | idx++;
|
435 | }
|
436 | } catch (e_1_1) {
|
437 | e_1 = {
|
438 | error: e_1_1
|
439 | };
|
440 | } finally {
|
441 | try {
|
442 | if (handlerList_1_1 && !handlerList_1_1.done && (_a = handlerList_1.return)) _a.call(handlerList_1);
|
443 | } finally {
|
444 | if (e_1) throw e_1.error;
|
445 | }
|
446 | }
|
447 | }
|
448 |
|
449 | return this;
|
450 | };
|
451 | /**
|
452 | * Version info string
|
453 | * @ko 버전정보 문자열
|
454 | * @name VERSION
|
455 | * @static
|
456 | * @example
|
457 | * Component.VERSION; // ex) 3.0.0
|
458 | * @memberof Component
|
459 | */
|
460 |
|
461 |
|
462 | Component.VERSION = "3.0.1";
|
463 | return Component;
|
464 | }();
|
465 |
|
466 | /*
|
467 | * Copyright (c) 2015 NAVER Corp.
|
468 | * egjs projects are licensed under the MIT license
|
469 | */
|
470 |
|
471 | var ComponentEvent$1 = ComponentEvent;
|
472 |
|
473 | /*
|
474 | * Copyright (c) 2015 NAVER Corp.
|
475 | * egjs projects are licensed under the MIT license
|
476 | */
|
477 | Component.ComponentEvent = ComponentEvent$1;
|
478 |
|
479 | return Component;
|
480 |
|
481 | })));
|
482 | //# sourceMappingURL=component.js.map
|