-
Notifications
You must be signed in to change notification settings - Fork 4
/
can-jquery.js
285 lines (244 loc) · 7.97 KB
/
can-jquery.js
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
var $ = require("jquery");
var ns = require("can-util/namespace");
var buildFragment = require("can-util/dom/fragment/fragment");
var domEvents = require("can-util/dom/events/events");
var domDispatch = require("can-util/dom/dispatch/dispatch");
var each = require("can-util/js/each/each");
var getChildNodes = require("can-util/dom/child-nodes/child-nodes");
var isArrayLike = require("can-util/js/is-array-like/is-array-like");
var makeArray = require("can-util/js/make-array/make-array");
var mutate = require("can-util/dom/mutate/mutate");
var setImmediate = require("can-util/js/set-immediate/set-immediate");
var canViewModel = require("can-view-model");
var getMutationObserver = require("can-globals/mutation-observer/mutation-observer");
var CIDMap = require("can-util/js/cid-map/cid-map");
var assign = require("can-util/js/assign/assign");
var addEnterEvent = require('can-event-dom-enter/compat');
addEnterEvent(domEvents);
module.exports = ns.$ = $;
var specialEvents = {};
var nativeDispatchEvents = { focus: true };
var inSpecial = false;
var slice = Array.prototype.slice;
var removedEventHandlerMap = new CIDMap();
if ($) {
// Override dispatch to use $.trigger.
// This is needed so that extra arguments can be used
// when using domEvents.dispatch/domEvents.trigger.
var domDispatch = domEvents.dispatch;
domEvents.dispatch = function(event, args) {
var eventObj;
if (!specialEvents[event] && !nativeDispatchEvents[event]) {
// fix KeyboardEvent and other constructed events that do not have an own `type` property
// jquery.trigger will throw when on event.indexOf(...) if event.hasOwnProperty('type') fails
if (typeof event !== 'string' && !event.hasOwnProperty('type')) {
// convert event to an object
eventObj = assign({}, event);
}
$(this).trigger(eventObj || event, args);
} else {
domDispatch.apply(this, arguments);
}
};
var isFragment = function(node) {
// Node.DOCUMENT_FRAGMENT_NODE === 11
return node && node.nodeType === 11;
};
// Override addEventListener to listen to jQuery events.
// This is needed to add the arguments provided to $.trigger
// onto the event.
var addEventListener = domEvents.addEventListener;
domEvents.addEventListener = function(event, callback){
var handler;
// don't set up event listeners for document fragments
// since events will not be triggered and the handlers
// could lead to memory leaks
if (isFragment(this)) {
return;
}
if(!inSpecial && !domEvents._compatRegistry[event]) {
if(event === "removed") {
var element = this;
// overwrite `removed` event handlers
// to ensure they are dispatched async,
// pass arguments through correctly from $.trigger,
// and automatically unbind
handler = function(ev){
ev.eventArguments = slice.call(arguments, 1);
// Remove the event handler to prevent the event from being called twice
domEvents.removeEventListener.call(element, event, handler);
var self = this, args = arguments;
if (getMutationObserver()) {
return callback.apply(self, args);
} else {
// if not using mutation observers, ensure event is dispatch async
return setImmediate(function(){
return callback.apply(self, args);
});
}
};
// add mapping of original handler to overwritten handler
// so that the correct handler can be unbound in removeEventListener
removedEventHandlerMap.set(callback, handler);
}
// if handler was created, set it up
// otherwise, just set up original callback
$(this).on(event, handler || callback);
return;
}
return addEventListener.call(this, event, handler || callback);
};
var removeEventListener = domEvents.removeEventListener;
domEvents.removeEventListener = function(event, callback){
// event handlers are not set up on document fragments
// so they do not need to be removed
if (isFragment(this)) {
return;
}
if(!inSpecial) {
var handler;
if(event === "removed") {
// map callback back to overwritten handler
handler = removedEventHandlerMap.get(callback);
// remove mapping since handler is being removed
removedEventHandlerMap.delete(callback);
}
// if handler was found (set up above in addEventListener),
// remove it. otherwise, just remove original callback
$(this).off(event, handler || callback);
return;
}
return removeEventListener.apply(this, arguments);
};
// jQuery defines special event types for focus and blur
// for use with event delegation. They do this because
// focus and blur do not bubble.
var delegateEventType = function delegateEventType(type) {
var typeMap = {
focus: 'focusin',
enter: 'keyup',
blur: 'focusout'
};
return typeMap[type] || type;
};
domEvents.addDelegateListener = function(type, selector, callback){
$(this).on(delegateEventType(type), selector, callback);
};
domEvents.removeDelegateListener = function(type, selector, callback){
$(this).off(delegateEventType(type), selector, callback);
};
var withSpecial = function withSpecial(callback){
return function(){
inSpecial = true;
callback.apply(this, arguments);
inSpecial = false;
};
};
var setupSpecialEvent = function setupSpecialEvent(eventName){
specialEvents[eventName] = true;
var handler = function(){
$(this).trigger(eventName);
};
$.event.special[eventName] = {
noBubble: true,
setup: withSpecial(function(){
// setup is called the first time a handler for `eventName`
// is set up for each element
domEvents.addEventListener.call(this, eventName, handler);
}),
teardown: withSpecial(function(){
// teardown is called after the last handler is removed for
// each element
domEvents.removeEventListener.call(this, eventName, handler);
})
};
};
[
"inserted",
"removed",
"attributes",
"beforeremove"
].forEach(setupSpecialEvent);
// Dom Mapip stuff
var oldDomManip = $.fn.domManip,
cbIndex;
// feature detect which domManip we are using
$.fn.domManip = function () {
for (var i = 1; i < arguments.length; i++) {
if (typeof arguments[i] === 'function') {
cbIndex = i;
break;
}
}
return oldDomManip.apply(this, arguments);
};
$(document.createElement("div"))
.append(document.createElement("div"));
if(cbIndex === undefined) {
$.fn.domManip = oldDomManip;
// we must manually overwrite
each(['after', 'prepend', 'before', 'append','replaceWith'], function (name) {
var original = $.fn[name];
$.fn[name] = function () {
var elems = [],
args = makeArray(arguments);
if (args[0] != null) {
// documentFragment
if (typeof args[0] === "string") {
args[0] = buildFragment(args[0]);
}
if (isFragment(args[0])) {
elems = getChildNodes(args[0]);
} else if( isArrayLike( args[0] ) ) {
elems = makeArray(args[0]);
} else {
elems = [args[0]];
}
}
var ret = original.apply(this, args);
mutate.inserted(elems);
return ret;
};
});
} else {
// Older jQuery that supports domManip
$.fn.domManip = (cbIndex === 2 ?
function (args, table, callback) {
return oldDomManip.call(this, args, table, function (elem) {
var elems;
if (isFragment(elem)) {
elems = makeArray( getChildNodes(elem) );
}
var ret = callback.apply(this, arguments);
mutate.inserted(elems ? elems : [elem]);
return ret;
});
} :
function (args, callback) {
return oldDomManip.call(this, args, function (elem) {
var elems;
if (isFragment(elem)) {
elems = makeArray( getChildNodes(elem) );
}
var ret = callback.apply(this, arguments);
mutate.inserted(elems ? elems : [elem]);
return ret;
});
});
}
// Memory safe destruction.
var oldClean = $.cleanData;
$.cleanData = function (elems){
$.each(elems, function(i, elem){
if(elem) {
domDispatch.call(elem, "beforeremove", [], false);
domDispatch.call(elem, "removed", [], false);
}
});
oldClean(elems);
};
// $.fn.viewModel
$.fn.viewModel = function(){
return canViewModel(this[0]);
};
}