2018-07-07 00:48:14 +00:00
|
|
|
/* global Whisper, extension, Backbone, moment, i18n */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.NetworkStatusView = Whisper.View.extend({
|
|
|
|
className: 'network-status',
|
|
|
|
templateName: 'networkStatus',
|
2018-07-07 00:48:14 +00:00
|
|
|
initialize() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.$el.hide();
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
this.renderIntervalHandle = setInterval(this.update.bind(this), 5000);
|
2018-07-07 00:48:14 +00:00
|
|
|
extension.windows.onClosed(() => {
|
|
|
|
clearInterval(this.renderIntervalHandle);
|
|
|
|
});
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
setTimeout(this.finishConnectingGracePeriod.bind(this), 5000);
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
this.withinConnectingGracePeriod = true;
|
|
|
|
this.setSocketReconnectInterval(null);
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.addEventListener('online', this.update.bind(this));
|
|
|
|
window.addEventListener('offline', this.update.bind(this));
|
2017-04-12 23:41:06 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
this.model = new Backbone.Model();
|
|
|
|
this.listenTo(this.model, 'change', this.onChange);
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
onReconnectTimer() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.setSocketReconnectInterval(60000);
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
finishConnectingGracePeriod() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.withinConnectingGracePeriod = false;
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
setSocketReconnectInterval(millis) {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.socketReconnectWaitDuration = moment.duration(millis);
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
navigatorOnLine() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return navigator.onLine;
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
getSocketStatus() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return window.getSocketStatus();
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
getNetworkStatus() {
|
|
|
|
let message = '';
|
|
|
|
let instructions = '';
|
|
|
|
let hasInterruption = false;
|
|
|
|
let action = null;
|
|
|
|
let buttonClass = null;
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
const socketStatus = this.getSocketStatus();
|
2018-04-27 21:25:04 +00:00
|
|
|
switch (socketStatus) {
|
|
|
|
case WebSocket.CONNECTING:
|
|
|
|
message = i18n('connecting');
|
|
|
|
this.setSocketReconnectInterval(null);
|
|
|
|
break;
|
|
|
|
case WebSocket.OPEN:
|
|
|
|
this.setSocketReconnectInterval(null);
|
|
|
|
break;
|
2018-07-07 00:48:14 +00:00
|
|
|
case WebSocket.CLOSED:
|
2018-04-27 21:25:04 +00:00
|
|
|
message = i18n('disconnected');
|
|
|
|
instructions = i18n('checkNetworkConnection');
|
|
|
|
hasInterruption = true;
|
|
|
|
break;
|
2018-07-07 00:48:14 +00:00
|
|
|
case WebSocket.CLOSING:
|
|
|
|
default:
|
2018-04-27 21:25:04 +00:00
|
|
|
message = i18n('disconnected');
|
|
|
|
instructions = i18n('checkNetworkConnection');
|
|
|
|
hasInterruption = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
if (
|
2018-07-07 00:48:14 +00:00
|
|
|
socketStatus === WebSocket.CONNECTING &&
|
2018-04-27 21:25:04 +00:00
|
|
|
!this.withinConnectingGracePeriod
|
|
|
|
) {
|
|
|
|
hasInterruption = true;
|
|
|
|
}
|
|
|
|
if (this.socketReconnectWaitDuration.asSeconds() > 0) {
|
|
|
|
instructions = i18n('attemptingReconnection', [
|
|
|
|
this.socketReconnectWaitDuration.asSeconds(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (!this.navigatorOnLine()) {
|
|
|
|
hasInterruption = true;
|
|
|
|
message = i18n('offline');
|
|
|
|
instructions = i18n('checkNetworkConnection');
|
|
|
|
} else if (!Whisper.Registration.isDone()) {
|
|
|
|
hasInterruption = true;
|
|
|
|
message = i18n('Unlinked');
|
|
|
|
instructions = i18n('unlinkedWarning');
|
|
|
|
action = i18n('relink');
|
|
|
|
buttonClass = 'openInstaller';
|
|
|
|
}
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return {
|
2018-07-07 00:48:14 +00:00
|
|
|
message,
|
|
|
|
instructions,
|
|
|
|
hasInterruption,
|
|
|
|
action,
|
|
|
|
buttonClass,
|
2018-04-27 21:25:04 +00:00
|
|
|
};
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
update() {
|
|
|
|
const status = this.getNetworkStatus();
|
2018-04-27 21:25:04 +00:00
|
|
|
this.model.set(status);
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
render_attributes() {
|
2018-04-27 21:25:04 +00:00
|
|
|
return this.model.attributes;
|
|
|
|
},
|
2018-07-07 00:48:14 +00:00
|
|
|
onChange() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.render();
|
|
|
|
if (this.model.attributes.hasInterruption) {
|
|
|
|
this.$el.slideDown();
|
|
|
|
} else {
|
|
|
|
this.$el.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-01-04 03:37:56 +00:00
|
|
|
})();
|