This repository was archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjquery.selector-set.next.js
More file actions
132 lines (116 loc) · 3.53 KB
/
jquery.selector-set.next.js
File metadata and controls
132 lines (116 loc) · 3.53 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
// jQuery SelectorSet
import $ from 'jquery';
import SelectorSet from 'selector-set';
var document = window.document; // jshint ignore:line
var originalEventAdd = $.event.add;
var originalEventRemove = $.event.remove;
var handleObjs = {};
// Internal: Compute event propagation path using SelectorSet's fast match.
//
// event - jQuery Event
//
// Returns an Array of {elem: Element, handlers: Array}.
function selectorSetHandlers(event) {
var handlerQueue = [],
cur = event.target,
set = event.handleObj.selectorSet;
do {
// Stop if cur is no longer an Element. Normally breaks at document.
if (cur.nodeType !== 1) {
break;
}
// Use SelectorSet#matches.
var matches = set.matches(cur);
if (matches.length) {
handlerQueue.push({elem: cur, handlers: matches});
}
} while (cur = cur.parentNode);
return handlerQueue;
}
// Internal: Handle delegated event handler.
//
// Mostly duplicated from $.event.dispatch.
//
// event - jQuery Event
//
// Returns nothing.
function selectorSetHandler(event) {
var handlerQueue = selectorSetHandlers(event);
var matched, i = 0;
while ((matched = handlerQueue[i++]) && !event.isPropagationStopped()) {
event.currentTarget = matched.elem;
var handleObj, j = 0;
while ((handleObj = matched.handlers[j++]) && !event.isImmediatePropagationStopped()) {
var ret = handleObj.data.apply(matched.elem, arguments);
if (ret !== undefined) {
event.result = ret;
if (ret === false) {
event.preventDefault();
event.stopPropagation();
}
}
}
}
}
// Public: Monkey patch $.event.add.
//
// elem - Target Element
// types - String event types (including namespaces)
// handler - Function
// data - Optional data object
// selector - String delegated selector
//
// Returns nothing.
$.event.add = function(elem, types, handler, data, selector) {
if (elem === document && !types.match(/\./) && !data && selector) {
var ts = types.match(/\S+/g);
var t = ts.length;
while (t--) {
var type = ts[t];
var special = $.event.special[type] || {};
type = special.delegateType || type;
var handleObj = handleObjs[type];
if (!handleObj) {
handleObj = handleObjs[type] = {
handler: selectorSetHandler,
selectorSet: new SelectorSet()
};
handleObj.selectorSet.matchesSelector = $.find.matchesSelector;
originalEventAdd.call(this, elem, type, handleObj);
}
handleObj.selectorSet.add(selector, handler);
$.expr.cacheLength++;
// if sizzle is available
if ($.find.compile) {
$.find.compile(selector);
}
}
} else {
originalEventAdd.call(this, elem, types, handler, data, selector);
}
};
// Public: Monkey patch $.event.remove.
//
// elem - Target Element
// types - String event types (including namespaces)
// handler - Function
// selector - String delegated selector
// mappedTypes - Boolean
//
// Returns nothing.
$.event.remove = function(elem, types, handler, selector, mappedTypes) {
if (elem === document && types && !types.match(/\./) && selector) {
var ts = types.match(/\S+/g);
var t = ts.length;
while (t--) {
var type = ts[t];
var special = $.event.special[type] || {};
type = special.delegateType || type;
var handleObj = handleObjs[type];
if (handleObj) {
handleObj.selectorSet.remove(selector, handler);
}
}
}
originalEventRemove.call(this, elem, types, handler, selector, mappedTypes);
};