-
Notifications
You must be signed in to change notification settings - Fork 758
/
Copy pathdetect.js
53 lines (50 loc) · 1.85 KB
/
detect.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
var nativeEvents = require("./native")
var pjaxEvents = require("./pjax")
var turbolinksEvents = require("./turbolinks")
var turbolinksClassicDeprecatedEvents = require("./turbolinksClassicDeprecated")
var turbolinksClassicEvents = require("./turbolinksClassic")
// see what things are globally available
// and setup event handlers to those things
module.exports = function(ujs) {
if (ujs.handleEvent) {
// We're calling this a second time -- remove previous handlers
if (typeof Turbolinks !== "undefined" && typeof Turbolinks.EVENTS !== "undefined") {
turbolinksClassicEvents.teardown(ujs);
}
turbolinksEvents.teardown(ujs);
turbolinksClassicDeprecatedEvents.teardown(ujs);
pjaxEvents.teardown(ujs);
nativeEvents.teardown(ujs);
}
if ('addEventListener' in window) {
ujs.handleEvent = function(eventName, callback) {
document.addEventListener(eventName, callback);
};
ujs.removeEvent = function(eventName, callback) {
document.removeEventListener(eventName, callback);
};
} else {
ujs.handleEvent = function(eventName, callback) {
window.attachEvent(eventName, callback);
};
ujs.removeEvent = function(eventName, callback) {
window.detachEvent(eventName, callback);
};
}
// Detect which kind of events to set up:
if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) {
if (typeof Turbolinks.EVENTS !== 'undefined') {
// Turbolinks.EVENTS is in classic version 2.4.0+
turbolinksClassicEvents.setup(ujs)
} else if (typeof Turbolinks.controller !== "undefined") {
// Turbolinks.controller is in version 5+
turbolinksEvents.setup(ujs);
} else {
turbolinksClassicDeprecatedEvents.setup(ujs);
}
} else if (typeof $ !== "undefined" && typeof $.pjax === 'function') {
pjaxEvents.setup(ujs);
} else {
nativeEvents.setup(ujs);
}
}