-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathjquery-ext.js
187 lines (160 loc) · 5.93 KB
/
jquery-ext.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
/**
* @license
* Patterns @VERSION@ jquery-ext - various jQuery extensions
*
* Copyright 2011 Humberto Sermeño
*/
import $ from "jquery";
var methods = {
init: function (options) {
var settings = {
time: 3 /* time it will wait before moving to "timeout" after a move event */,
initialTime: 8 /* time it will wait before first adding the "timeout" class */,
exceptionAreas:
[] /* IDs of elements that, if the mouse is over them, will reset the timer */,
};
return this.each(function () {
var $this = $(this),
data = $this.data("timeout");
if (!data) {
if (options) {
$.extend(settings, options);
}
$this.data("timeout", {
lastEvent: new Date(),
trueTime: settings.time,
time: settings.initialTime,
untouched: true,
inExceptionArea: false,
});
$this.on("mouseover.timeout", methods.mouseMoved);
$this.on("mouseenter.timeout", methods.mouseMoved);
$(settings.exceptionAreas).each(function () {
$this
.find(this)
.live(
"mouseover.timeout",
{ parent: $this },
methods.enteredException
)
.live(
"mouseleave.timeout",
{ parent: $this },
methods.leftException
);
});
if (settings.initialTime > 0) $this.timeout("startTimer");
else $this.addClass("timeout");
}
});
},
enteredException: function (event) {
var data = event.data.parent.data("timeout");
data.inExceptionArea = true;
event.data.parent.data("timeout", data);
event.data.parent.trigger("mouseover");
},
leftException: function (event) {
var data = event.data.parent.data("timeout");
data.inExceptionArea = false;
event.data.parent.data("timeout", data);
},
destroy: function () {
return this.each(function () {
var $this = $(this),
data = $this.data("timeout");
$(window).off(".timeout");
data.timeout.remove();
$this.removeData("timeout");
});
},
mouseMoved: function () {
var $this = $(this),
data = $this.data("timeout");
if ($this.hasClass("timeout")) {
$this.removeClass("timeout");
$this.timeout("startTimer");
} else if (data.untouched) {
data.untouched = false;
data.time = data.trueTime;
}
data.lastEvent = new Date();
$this.data("timeout", data);
},
startTimer: function () {
var $this = $(this),
data = $this.data("timeout");
var fn = function () {
var data = $this.data("timeout");
if (data && data.lastEvent) {
if (data.inExceptionArea) {
setTimeout(fn, Math.floor(data.time * 1000));
} else {
var now = new Date();
var diff = Math.floor(data.time * 1000) - (now - data.lastEvent);
if (diff > 0) {
// the timeout has not ocurred, so set the timeout again
setTimeout(fn, diff + 100);
} else {
// timeout ocurred, so set the class
$this.addClass("timeout");
}
}
}
};
setTimeout(fn, Math.floor(data.time * 1000));
},
};
$.fn.timeout = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist on jQuery.timeout");
}
};
// Custom jQuery selector to find elements with scrollbars
$.extend($.expr[":"], {
scrollable: function (element) {
var vertically_scrollable, horizontally_scrollable;
if (
$(element).css("overflow") === "scroll" ||
$(element).css("overflowX") === "scroll" ||
$(element).css("overflowY") === "scroll"
)
return true;
vertically_scrollable =
element.clientHeight < element.scrollHeight &&
($.inArray($(element).css("overflowY"), ["scroll", "auto"]) !== -1 ||
$.inArray($(element).css("overflow"), ["scroll", "auto"]) !== -1);
if (vertically_scrollable) return true;
horizontally_scrollable =
element.clientWidth < element.scrollWidth &&
($.inArray($(element).css("overflowX"), ["scroll", "auto"]) !== -1 ||
$.inArray($(element).css("overflow"), ["scroll", "auto"]) !== -1);
return horizontally_scrollable;
},
});
//Work around warning for jQuery 3.x:
//JQMIGRATE: jQuery.fn.offset() requires an element connected to a document
$.fn.safeOffset = function () {
var docElem,
elem = this[0],
origin = { top: 0, left: 0 };
if (!elem || !elem.nodeType) {
return origin;
}
docElem = (elem.ownerDocument || document).documentElement;
if (!$.contains(docElem, elem)) {
return origin;
}
return $.fn.offset.apply(this, arguments);
};
$.fn.slideIn = function (speed, easing, callback) {
return this.animate({ width: "show" }, speed, easing, callback);
};
$.fn.slideOut = function (speed, easing, callback) {
return this.animate({ width: "hide" }, speed, easing, callback);
};
export default undefined;