forked from marko-js/marko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.js
54 lines (47 loc) · 1.75 KB
/
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
var ready = require('./ready');
var idRegExp = /^\#(\S+)( .*)?/;
exports.patchWidget = function(jQuery) {
/* globals window */
if (!jQuery) {
jQuery = window.$;
if (!jQuery) {
throw new Error('jQuery not found');
}
}
require('./widgets/Widget').prototype.$ = function jqueryProxy(arg) {
var args = arguments;
var self = this;
if (args.length === 1) {
//Handle an "ondomready" callback function
if (typeof arg === 'function') {
return ready(function() {
arg.call(self);
});
} else if (typeof arg === 'string') {
var match = idRegExp.exec(arg);
//Reset the search to 0 so the next call to exec will start from the beginning for the new string
if (match != null) {
var widgetElId = match[1];
if (match[2] == null) {
return jQuery(self.getEl(widgetElId));
} else {
return jQuery('#' + self.getElId(widgetElId) + match[2]);
}
} else {
var rootEl = self.getEl();
if (!rootEl) {
throw new Error('Root element is not defined for widget');
}
if (rootEl) {
return jQuery(arg, rootEl);
}
}
}
} else if (args.length === 2 && typeof args[1] === 'string') {
return jQuery(arg, self.getEl(args[1]));
} else if (args.length === 0) {
return jQuery(self.el);
}
return jQuery.apply(window, arguments);
};
};