-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemitter.js
More file actions
963 lines (865 loc) · 38.6 KB
/
emitter.js
File metadata and controls
963 lines (865 loc) · 38.6 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
import createEvent from './createEvent'
import off from './off'
import at from './at'
import on from './on'
import only from './only'
import once from './once'
import focusin from './focusin'
import focusout from './focusout'
import isElement from './utils/isElement'
import isString from './utils/isString'
import getListeners from './getListeners'
import getTypes from './getTypes'
import hasEvent from './hasEvent'
import getPageX from './getPageX'
import getPageY from './getPageY'
import getPageXY from './getPageXY'
import getCharCode from './getCharCode'
import getRelatedTarget from './getRelatedTarget'
import getTarget from './getTarget'
import purgeElement from './purgeElement'
import destroy from './destroy'
import preventDefault from './preventDefault'
import stopPropagation from './stopPropagation'
import stopEvent from './stopEvent'
import stopImmediate from './stopImmediate'
import trigger from './trigger'
/**
* Emitter 类 - JavaScript 事件代理对象
* ========================================================================
*/
class Emitter {
/**
* Emitter 构造函数
* ========================================================================
* @constructor
* @param {HTMLElement|String} el - (必须)DOM 元素或其选择器
* @returns {Emitter} - Emitter 对象
*/
constructor(el) {
if (isElement(el)) {
this.$el = el
} else {
if (isString(el)) {
this.$el = document.querySelector(el)
}
}
}
/**
* 获取 DOM 元素(type 事件类型)事件绑定信息
* ========================================================================
* 如果设置了事件类型 type, 则返回指定类型的事件绑定信息,否则返回所有事件绑定信息
* ========================================================================
* @method getListeners
* @param {String} [type] - (可选)事件类型
* @returns {Array} - 已绑定的事件信息
*/
getListeners(type) {
return getListeners(this.$el, type)
}
/**
* 返回已绑定的事件类型的数组(去除名称重复的事件)
* ========================================================================
* @method getTypes
* @since 1.5.0
* @returns {Array}
*/
getTypes() {
return getTypes(this.$el)
}
/**
* 判断是否已经(指定类型的)绑定事件
* ========================================================================
* @method hasEvent
* @since 1.4.0
* @param {String} [type] - (可选)事件名称:
* 指定 type,则判断是否绑定 type 类型事件;
* 未指定 type,则判断是否绑定任意类型的事件;
* @returns {Boolean}
*/
hasEvent(type) {
return hasEvent(this.$el, type)
}
/**
* 获取事件触发时的 pageX 值
* ========================================================================
* @method getPageX
* @see getPageX
* @param {Event} evt - (必须)Event 对象
* @return {Number} - 返回事件触发时的 pageX 值
*/
getPageX(evt) {
return getPageX(evt)
}
/**
* 获取事件触发时的 pageY 值
* ========================================================================
* @method getPageY
* @see getPageY
* @param {Event} evt - (必须)Event 对象
* @return {Number} - 返回事件触发时的 pageY 值
*/
getPageY(evt) {
return getPageY(evt)
}
/**
* 获取事件触发时的 pageX 和 pageY 数组数据
* ========================================================================
* @method getPageXY
* @see getPageXY
* @param {Event} evt - (必须)Event 对象
* @return {Array} - 返回事件触发时的数组数据:[pageX, pageY]
*/
getPageXY(evt) {
return getPageXY(evt)
}
/**
* 返回触发事件的 charCode
* ========================================================================
* @method getCharCode
* @see getCharCode
* @param {Event} evt - (必须)Event 对象
* @return {Number} - 返回事件的 charCode
*/
getCharCode(evt) {
return getCharCode(evt)
}
/**
* 返回触发(鼠标)事件的 relatedTarget DOM 元素。
* ========================================================================
* MouseEvent.relatedTarget 只读属性是鼠标事件的次要目标(如果有)。相关的鼠标事件:
* mouseenter
* mouseleave
* mouseover
* mouseout
* dragenter
* dragleave
* ========================================================================
* @method getRelatedTarget
* @since 1.1.0
* @see https://developer.mozilla.org/en-US/docs/web/api/mouseevent/relatedtarget
* @param {Event} evt - Event 对象
* @return {HTMLElement} - Event 对象的 relatedTarget DOM 元素
*/
getRelatedTarget(evt) {
return getRelatedTarget(evt)
}
/**
* 返回触发事件的 target DOM 元素
* ========================================================================
* @method getTarget
* @since 1.1.0
* @param {Event} evt - Event 对象
* @return {HTMLElement} - Event 对象的 target DOM 元素
*/
getTarget(evt) {
return getTarget(evt)
}
/**
* 销毁(type 类型的)代理事件绑定
* ========================================================================
* 1. 设置了事件类型 type,则销毁指定类型的事件绑定,否则清除所有代理事件绑定
* 2. recurse 设置为 true,递归销毁子节点全部事件绑定
* ========================================================================
* @method purge
* @param {String} type - (必须)事件类型
* @param {Boolean} [recurse] - (可选)是否递归销毁子节点所有事件绑定
* 元素绑定的全部事件处理器
* @returns {Emitter} - Emitter 对象
*/
purge(type, recurse = false) {
purgeElement(this.$el, type, recurse)
return this
}
/**
* 销毁所有已绑定的代理事件
* ========================================================================
* @method destroy
* @returns {Emitter} - Emitter 对象
*/
destroy() {
destroy(this.$el)
return this
}
/**
* 创建自定义事件(CustomerEvent)
* ========================================================================
* @method createEvent
* @since 1.8.0
* @see createEvent
* @param {String} type - (必须)事件类型(名称)
* @param {Object} [detail] - (可选)传递给自定义事件的数据,默认为 null
* @param {Boolean} [bubbles] - (可选)是否支持冒泡,默认为 true
* @param {Boolean} [cancelable] - (可选)是否可以取消,默认为 true
* @returns {CustomEvent} - CustomerEvent 实例
*/
createEvent(type, detail = null, bubbles = true, cancelable = true) {
return createEvent(type, detail, bubbles, cancelable)
}
/**
* 取消 type 类型的代理事件绑定
* ========================================================================
* 如果没有设置 handler,则销毁 this.$options 绑定的所有符合 type 事件类型的事件绑定
* ========================================================================
* @method off
* @param {String} type - (必须)事件类型
* @param {Function} [handler] - (可选)事件处理器回调函数
* @returns {Emitter} - Emitter 对象
*/
off(type, handler) {
off(this.$el, type, handler)
return this
}
/**
* 绑定事件
* ========================================================================
* @method at
* @param {String|Function} type - (必须)事件类型或者事件处理器回调函数
* @param {Function|Object} fn - (必须) 事件处理器回调函数或者传递给事件处理器回调函数的数据对象
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象或者事件处理器回调函数的 this 上下文指向,
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向,或者是否仅触发一次
* 当设置为 true 时,则事件处理器回调函数的 this 上下文指向为 data 对象
* @param {Boolean} once - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
at(type, fn, data, context, once = false) {
at(this.$el, type, fn, data, context, once)
return this
}
/**
* 绑定代理事件
* ========================================================================
* @method on
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {String} type - (必须)事件类型
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向:
* 当设置为 true 时,则事件处理器回调函数的 this 上下文指向为 data 对象;
* 如未指定 context,则事件处理器回调函数的 this 上下文指向为 Emitter 对象;
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
on(selector, type, handler, data, context, once = false) {
on(this.$el, selector, type, handler, data, context || this, once)
return this
}
/**
* 绑定仅触发一次的事件
* ========================================================================
* @method only
* @param {String} type - (必须)事件类型
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @returns {Emitter} - Emitter 对象
*/
only(type, handler, data, context) {
only(this.$el, type, handler, data, context)
return this
}
/**
* 绑定仅触发一次的代理事件
* ========================================================================
* @method once
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {String} type - (必须)事件类型
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @returns {Emitter} - Emitter 对象
*/
once(selector, type, handler, data, context) {
once(this.$el, selector, type, handler, data, context)
return this
}
/**
* 绑定 click 代理事件
* ========================================================================
* @method click
* @since 1.4.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/click_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
click(selector, handler, data, context, once = false) {
on(this.$el, selector, 'click', handler, data, context, once)
return this
}
/**
* 绑定 dbclick 代理事件
* ========================================================================
* @method dbclick
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/dblclick_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
dbclick(selector, handler, data, context, once = false) {
on(this.$el, selector, 'dbclick', handler, data, context, once)
return this
}
/**
* 绑定 mouseenter 代理事件
* ========================================================================
* @method mouseenter
* @since 1.4.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseenter_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mouseenter(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mouseenter', handler, data, context, once)
return this
}
/**
* 绑定 mouseleave 代理事件
* ========================================================================
* @method mouseleave
* @since 1.4.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseleave_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mouseleave(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mouseleave', handler, data, context, once)
return this
}
/**
* 绑定 mousedown 代理事件
* ========================================================================
* @method mousedown
* @since 1.7.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mousedown_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mousedown(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mousedown', handler, data, context, once)
return this
}
/**
* 绑定 mouseup 代理事件
* ========================================================================
* @method mouseup
* @since 1.7.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseup_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mouseup(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mouseup', handler, data, context, once)
return this
}
/**
* 绑定 mouseover 代理事件
* ========================================================================
* @method mouseover
* @since 1.7.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mouseover(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mouseover', handler, data, context, once)
return this
}
/**
* 绑定 mousemove 代理事件
* ========================================================================
* @method mousemove
* @since 1.7.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mousemove(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mousemove', handler, data, context, once)
return this
}
/**
* 绑定 mouseout 代理事件
* ========================================================================
* @method mouseout
* @since 1.7.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseout_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
mouseout(selector, handler, data, context, once = false) {
on(this.$el, selector, 'mouseout', handler, data, context, once)
return this
}
/**
* 绑定 drag 代理事件
* ========================================================================
* @method drag
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/drag_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
drag(selector, handler, data, context, once = false) {
on(this.$el, selector, 'drag', handler, data, context, once)
return this
}
/**
* 绑定 dragend 代理事件
* ========================================================================
* @method dragend
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dragend_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
dragend(selector, handler, data, context, once = false) {
on(this.$el, selector, 'dragend', handler, data, context, once)
return this
}
/**
* 绑定 dragenter 代理事件
* ========================================================================
* @method dragenter
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dragenter_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
dragenter(selector, handler, data, context, once = false) {
on(this.$el, selector, 'dragenter', handler, data, context, once)
return this
}
/**
* 绑定 dragleave 代理事件
* ========================================================================
* @method dragleave
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dragleave_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
dragleave(selector, handler, data, context, once = false) {
on(this.$el, selector, 'dragleave', handler, data, context, once)
return this
}
/**
* 绑定 dragover 代理事件
* ========================================================================
* @method dragover
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dragover_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
dragover(selector, handler, data, context, once = false) {
on(this.$el, selector, 'dragover', handler, data, context, once)
return this
}
/**
* 绑定 dragstart 代理事件
* ========================================================================
* @method dragstart
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/dragstart_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
dragstart(selector, handler, data, context, once = false) {
on(this.$el, selector, 'dragstart', handler, data, context, once)
return this
}
/**
* 绑定 drop 代理事件
* ========================================================================
* @method drop
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/drop_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
drop(selector, handler, data, context, once = false) {
on(this.$el, selector, 'drop', handler, data, context, once)
return this
}
/**
* 绑定 wheel 代理事件
* ========================================================================
* @method wheel
* @since 1.7.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/wheel_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
wheel(selector, handler, data, context, once = false) {
on(this.$el, selector, 'wheel', handler, data, context, once)
return this
}
/**
* 绑定 contextmenu 代理事件
* ========================================================================
* @method contextmenu
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/contextmenu_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
contextmenu(selector, handler, data, context, once = false) {
on(this.$el, selector, 'contextmenu', handler, data, context, once)
return this
}
/**
* 绑定 focusin 或者 focus 代理事件
* ========================================================================
* @method focusin
* @since 1.4.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/focus_event
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/focusin_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
focusin(selector, handler, data, context, once = false) {
focusin(this.$el, selector, handler, data, context, once)
return this
}
/**
* 绑定 focusout 或者 blur 代理事件
* ========================================================================
* @method focusout
* @since 1.4.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/focusout_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
focusout(selector, handler, data, context, once = false) {
focusout(this.$el, selector, handler, data, context, once)
return this
}
/**
* 绑定 change 代理事件
* ========================================================================
* @method change
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/change_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
change(selector, handler, data, context, once = false) {
on(this.$el, selector, 'change', handler, data, context, once)
return this
}
/**
* 绑定 input 代理事件
* ========================================================================
* @method input
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/input_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
input(selector, handler, data, context, once = false) {
on(this.$el, selector, 'input', handler, data, context, once)
return this
}
/**
* 绑定 compositionstart 代理事件
* ========================================================================
* @method compositionstart
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionstart_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
compositionstart(selector, handler, data, context, once = false) {
on(this.$el, selector, 'compositionstart', handler, data, context, once)
return this
}
/**
* 绑定 compositionupdate 代理事件
* ========================================================================
* @method compositionupdate
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionupdate_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
compositionupdate(selector, handler, data, context, once = false) {
on(this.$el, selector, 'compositionupdate', handler, data, context, once)
return this
}
/**
* 绑定 compositionend 代理事件
* ========================================================================
* @method compositionend
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionend_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
compositionend(selector, handler, data, context, once = false) {
on(this.$el, selector, 'compositionend', handler, data, context, once)
return this
}
/**
* 绑定 paste 代理事件
* ========================================================================
* @method paste
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/paste_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
paste(selector, handler, data, context, once = false) {
on(this.$el, selector, 'paste', handler, data, context, once)
return this
}
/**
* 绑定 copy 代理事件
* ========================================================================
* @method copy
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/copy_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
copy(selector, handler, data, context, once = false) {
on(this.$el, selector, 'copy', handler, data, context, once)
return this
}
/**
* 绑定 cut 代理事件
* ========================================================================
* @method cut
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/cut_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
cut(selector, handler, data, context, once = false) {
on(this.$el, selector, 'cut', handler, data, context, once)
return this
}
/**
* 绑定 keydown 代理事件
* ========================================================================
* @method keydown
* @since 1.7.0
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
keydown(selector, handler, data, context, once = false) {
on(this.$el, selector, 'keydown', handler, data, context, once)
return this
}
/**
* 绑定 keyup 代理事件
* ========================================================================
* @method keyup
* @since 1.7.0
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
keyup(selector, handler, data, context, once = false) {
on(this.$el, selector, 'keyup', handler, data, context, once)
return this
}
/**
* 绑定 error 代理事件
* ========================================================================
* @method error
* @since 1.7.0
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/error_event
* @param {String} selector - (必须)事件代理目标 DOM 元素的选择器
* @param {Function} handler - (必须) 事件处理器回调函数
* @param {Object} [data] - (可选)传递给事件处理器回调函数的数据对象
* @param {Object|Boolean} [context] - (可选)事件处理器回调函数的 this 上下文指向
* @param {Boolean} [once] - (可选)是否仅触发一次
* @returns {Emitter} - Emitter 对象
*/
error(selector, handler, data, context, once = false) {
on(this.$el, selector, 'error', handler, data, context, once)
return this
}
/**
* 触发代理自定义事件
* ========================================================================
* @method trigger
* @since 1.6.0
* @param {String} type - (必须)事件类型
* @param {String} selector - (必须)选择器
* @returns {Emitter} - Emitter 对象
*/
trigger(type, selector) {
trigger(this.$el, type, selector)
return this
}
/**
* 阻止事件的默认行为
* ========================================================================
* @method preventDefault
* @see preventDefault
* @param {Event} evt - (必须)Event 对象
* @returns {Emitter} - Emitter 对象
*/
preventDefault(evt) {
preventDefault(evt)
return this
}
/**
* 终止事件在传播过程的捕获或冒泡的事件流
* ========================================================================
* @method stopPropagation
* @see stopPropagation
* @param {Event} evt - (必须)Event 对象
* @returns {Emitter} - Emitter 对象
*/
stopPropagation(evt) {
stopPropagation(evt)
return this
}
/**
* 停止事件(阻止默认行为和阻止事件的捕获或冒泡)
* ========================================================================
* @method stopEvent
* @see stopEvent
* @param {Event} evt - (必须)Event 对象
* @returns {Emitter} - Emitter 对象
*/
stopEvent(evt) {
stopEvent(evt)
return this
}
/**
* 阻止监听同一事件的其他事件监听器被调用,并且阻止默认行为和事件冒泡。
* ========================================================================
* @method stopImmediate
* @since 1.8.0
* @see stopImmediate
* @param {Event} evt - (必须)Event 对象
*/
stopImmediate(evt) {
stopImmediate(evt)
return this
}
}
export default Emitter