forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreality-check.js
More file actions
240 lines (187 loc) · 8.83 KB
/
Copy pathreality-check.js
File metadata and controls
240 lines (187 loc) · 8.83 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
RealityCheck = (function ($) {
"use strict";
var reality_check_url = page.url.url_for('user/reality_check');
var reality_freq_url = page.url.url_for('user/reality_check_frequency');
var logout_url = page.url.url_for('logout');
RealityCheck.prototype.setInterval = function (intv) {
this.interval = intv * 60 * 1000; // convert minutes to millisec
this.storage.set('reality_check.interval', this.interval);
};
RealityCheck.prototype.getInterval = function () {
return this.getIntervalMs() / (60 * 1000); // convert to minutes
};
RealityCheck.prototype.getIntervalMs = function () {
if (this.interval > 0) return this.interval;
this.interval = parseFloat(this.storage.get('reality_check.interval'));
// use default if garbage
if (isNaN(this.interval) || this.interval<=0)
this.interval = this.default_interval;
return this.interval;
};
function RealityCheck(cookieName, persistentStore) {
var val, that = this;
val = ($.cookie(cookieName)||'').split(',');
val[0] = parseFloat(val[0]);
if (isNaN(val[0]) || val[0]<=0) return; // no or invalid cookie
this.default_interval = val[0] * 60 * 1000;
this.storage = persistentStore;
// A storage event handler is used to notify about interval changes.
// That way all windows see the same interval.
$(window).on('storage', function (jq_event) {
if (jq_event.originalEvent.key === 'reality_check.interval') {
that.interval = parseFloat(jq_event.originalEvent.newValue);
// garbage here can only happen if the user tries to tamper
if (isNaN(that.interval) || that.interval<=0)
that.interval = that.default_interval;
// console.log('interval storage handler new value = '+that.interval);
that.setAlarm();
}
if (jq_event.originalEvent.key === 'reality_check.basetime') {
var val = parseInt(jq_event.originalEvent.newValue);
// garbage here can only happen if the user tries to tamper
if (isNaN(val) || val<=0) return;
that.basetime = val;
// console.log('basetime storage handler new value = '+that.basetime);
that.setAlarm();
}
});
// The cookie is formatted as DEFAULT_INTERVAL , SERVER_TIME_WHEN_IT_WAS_ISSUED
// We save the server time in local storage. If the stored time differs from
// the cookie time we are in a new session. Hence, we have to reset all stored
// data and to ask the user to check the reality-check frequency.
if (val[1] && val[1] != persistentStore.get('reality_check.srvtime')) {
persistentStore.set('reality_check.srvtime', val[1]);
persistentStore.set('reality_check.basetime', this.basetime = new Date().getTime());
persistentStore.set('reality_check.ack', 1);
this.askForFrequency();
} else if (persistentStore.get('reality_check.askingForInterval')) {
this.basetime = parseInt(persistentStore.get('reality_check.basetime'));
this.askForFrequency();
} else {
this.basetime = parseInt(persistentStore.get('reality_check.basetime'));
this.setAlarm();
}
}
RealityCheck.prototype.setAlarm = function () {
var that = this;
var intv = this.getIntervalMs();
var alrm = intv - (new Date().getTime() - this.basetime) % intv;
// console.log('interval = '+this.interval+', next alarm in '+alrm+' ms');
// console.log('alrm at '+(new Date((new Date()).getTime()+alrm)).toUTCString());
if (this.tmout) window.clearTimeout(this.tmout);
this.tmout = window.setTimeout(function () {
// console.log('fire at '+(new Date()).toUTCString());
that.fire();
}, alrm);
};
RealityCheck.prototype._fire = function (url, next) {
var that = this;
$.ajax({
url: url,
dataType: 'text',
success: function (data) {
next.call(that, data);
},
error: function (xhr) {
if (xhr.status === 404) return; // no MF loginid
window.setTimeout(function () {
that.fire();
}, 5000);
},
});
};
RealityCheck.prototype.fire = function () {
this._fire(reality_check_url, this.display);
};
RealityCheck.prototype.display = function (data) {
var that = this, outer, middle, storage_handler;
outer = $('#reality-check');
if (outer) outer.remove();
outer = $("<div id='reality-check' class='lightbox'></div>").appendTo('body');
middle = $('<div />').appendTo(outer);
$('<div>' + data + '</div>').appendTo(middle);
$('#reality-check [interval=1]').val(this.getInterval());
storage_handler = function (jq_event) {
var ack;
if (jq_event.originalEvent.key !== 'reality_check.ack') return;
ack = parseInt(jq_event.originalEvent.newValue || 1);
if (ack > that.lastAck) {
// console.log('Display storage handler');
$(window).off('storage', storage_handler);
that.setAlarm();
$('#reality-check').remove();
}
};
// in case the client works with multiple windows, check if he has acknowleged
// it in another window.
$(window).on('storage', storage_handler);
this.lastAck = parseInt(this.storage.get('reality_check.ack') || 1);
$('#reality-check [bcont=1]').on('click', function () {
var intv = parseFloat($('#reality-check [interval=1]').val());
if (isNaN(intv) || intv <= 0) {
$('#reality-check p.msg').show('fast');
return;
}
that.setInterval(intv);
that.storage.set('reality_check.ack', that.lastAck+1);
$(window).off('storage', storage_handler);
that.setAlarm();
$('#reality-check').remove();
});
$('#reality-check .blogout').on('click', function () {
window.location.href = logout_url;
});
};
// On session start we need to ask for the reality-check interval.
// This is an ajax call because it depends on the user's language.
RealityCheck.prototype.askForFrequency = function () {
this._fire(reality_freq_url, this.displayFrequencyChoice);
};
RealityCheck.prototype.displayFrequencyChoice = function (data) {
var that = this, outer, middle, storage_handler, click_handler;
outer = $('#reality-check');
if (outer) outer.remove();
outer = $("<div id='reality-check' class='lightbox'></div>").appendTo('body');
middle = $('<div />').appendTo(outer);
$('<div>' + data + '</div>').appendTo(middle);
$('#reality-check [interval=1]').val(this.getInterval());
this.storage.set('reality_check.askingForInterval', 1);
storage_handler = function (jq_event) {
var ack;
if (jq_event.originalEvent.key !== 'reality_check.ack') return;
ack = parseInt(jq_event.originalEvent.newValue || 1);
if (ack > that.lastAck) {
// console.log('FreqSet storage handler');
$(window).off('storage', storage_handler);
$('#reality-check').remove();
that.setAlarm();
}
};
// in case the client works with multiple windows, check if he has acknowleged
// it in another window.
$(window).on('storage', storage_handler);
this.lastAck = parseInt(this.storage.get('reality_check.ack') || 1);
click_handler = function () {
var intv = parseFloat($('#reality-check [interval=1]').val());
if (isNaN(intv) || intv <= 0) {
$('#reality-check p.msg').show('fast');
return;
}
// console.log('set interval handler: intv = '+intv);
that.storage.remove('reality_check.askingForInterval');
that.setInterval(intv);
that.storage.set('reality_check.ack', that.lastAck+1);
$(window).off('storage', storage_handler);
$('#reality-check').remove();
that.setAlarm();
};
$('#reality-check [bcont=1]').on('click', click_handler);
$('#reality-check [interval=1]').on('change', click_handler);
};
return RealityCheck;
}(jQuery));
$(document).ready(function () {
// console.log('About to create reality-check object');
if (window.reality_check_object) return;
window.reality_check_object = new RealityCheck('reality_check', LocalStore);
});