-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.js
113 lines (92 loc) · 3.45 KB
/
client.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
//////////////////////////////////
//= SETUP HOOKS OBJECT
//////////////////////////////////
Hooks = {
//////////////////////////////////
//= OPTIONS
//////////////////////////////////
updateFocus: 0, // Number of milliseconds to wait before checking whether the window is focused
treatCloseAsLogout: false,
//////////////////////////////////
//= INTERNAL STATES
//////////////////////////////////
focused: true,
loggedIn: false,
lastUserId: null,
//////////////////////////////////
//= METHODS
//////////////////////////////////
checkFocus: function () {
// Check if the window is currently focused
if (document.hasFocus() && Hooks.focused === false) {
// We've gained focus
Hooks.focused = true;
if (typeof Hooks.onGainFocus !== undefined) Hooks.onGainFocus(); // Fire the event on the client
Meteor.call('eventsOnGainFocus'); // Fire the event on the server
} else if (! document.hasFocus() && Hooks.focused === true) {
// We've lost focus
Hooks.focused = false;
if (typeof Hooks.onLoseFocus !== undefined) Hooks.onLoseFocus(); // Fire the event on the client
Meteor.call('eventsOnLoseFocus'); // Fire the event on the server
}
},
init: function (options) {
//////////////////////////////////
//= BASIC INITIALIZATION
//////////////////////////////////
// Fire up the server
Meteor.call('eventsOnHooksInit');
// Initialize options
if (typeof options !== 'undefined') {
if (options.updateFocus) Hooks.updateFocus = options.updateFocus;
if (options.treatCloseAsLogout) Hooks.treatCloseAsLogout = options.treatCloseAsLogout;
}
//////////////////////////////////
//= INITIALIZE CLIENT EVENTS
//////////////////////////////////
// Start checking for focus if a truthy integer is given
if (Hooks.updateFocus != false) {
Meteor.setInterval(Hooks.checkFocus, Hooks.updateFocus);
}
// Close window/tab
window.onbeforeunload = function() {
Meteor.call('eventsOnCloseSession'); // Fire the event on the server
// If we're treating close as logout, fire the logout event as well
if (Hooks.treatCloseAsLogout === true) {
Meteor.call('eventsOnLoggedOut', Hooks.lastUserId); // Fire the event on the server
}
}
//////////////////////////////////
//= SETUP LOGIN MONITORING
//////////////////////////////////
Deps.autorun(function () {
if (Meteor.userId()) {
// User is logged in
if (Hooks.loggedIn === false) {
// Update the latest user id
Hooks.lastUserId = Meteor.userId();
// User wasn't logged in before this updated, so fire the loggedIn event
if (typeof Hooks.onLoggedIn !== undefined) Hooks.onLoggedIn(); // Fire the event on the client
Meteor.call('eventsOnLoggedIn'); // Fire the event on the server
}
Hooks.loggedIn = true; // Now set the proper state
} else {
// There is no user logged in right now
if (Hooks.loggedIn === true) {
// User was logged in before this updated, so fire the loggedOut event
if (typeof Hooks.onLoggedOut !== undefined) Hooks.onLoggedOut(Hooks.lastUserId); // Fire the event on the client
Meteor.call('eventsOnLoggedOut', Hooks.lastUserId); // Fire the event on the server
}
Hooks.loggedIn = false; // Now set the proper state
}
});
},
//////////////////////////////////
//= DEFINE API METHODS
//////////////////////////////////
onLoseFocus: function(){},
onGainFocus: function(){},
onCloseSession: function(){},
onLoggedIn: function(){},
onLoggedOut: function(){}
};