-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.stuff.js
41 lines (38 loc) · 1.38 KB
/
jquery.stuff.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
//Example: if($('#message').exists()) {...}
$.fn.exists = function() {
return this.length > 0;
};
//Example: if($('#message').isEmpty()) {...}
$.fn.isEmpty = function() {
return this.length == 0;
};
//Cross-browser event that supports key up, mouse paste and a small throttle
$.fn.textEvent = function(func, ms) {
var currentId = $.textEventId ? ++$.textEventId : $.textEventId = 1
this.live("keyup", throttle(currentId, ms));
this.live($.support.noCloneEvent ? "input" : "paste", throttle(currentId, ms));
function throttle(currentId, ms) {
return function() {
var _this = this;
var id = 'textEvent' + currentId;
if(typeof this[id] == 'undefined') this[id] = {};
if(this[id].timeoutId) clearTimeout(this[id].timeoutId);
this[id].timeoutId = setTimeout(function() {
if(typeof _this[id].oldValue == 'undefined' || _this[id].oldValue != _this.value) {
func.apply(_this, arguments);
_this[id].oldValue = _this.value;
}
} , ms || 100);
}
}
};
//Usefull for simulating events in tests
//example: $('#searchField').callEvent('keyup',{ctrlKey:true, shiftKey:false, keyCode:40});
jQuery.fn.callEvent = function(eventType, eventObj) {
return this.each(function() {
var eventFunctions = $(this).data('events')[eventType];
for (var i in eventFunctions) {
eventFunctions[i].call($(this), eventObj);
}
});
};