2017-01-04 03:37:56 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
Whisper.NetworkStatusView = Whisper.View.extend({
|
|
|
|
className: 'network-status',
|
2017-04-12 23:41:06 +00:00
|
|
|
templateName: 'networkStatus',
|
2017-01-04 03:37:56 +00:00
|
|
|
initialize: function() {
|
|
|
|
this.$el.hide();
|
|
|
|
|
2017-05-18 17:42:20 +00:00
|
|
|
this.renderIntervalHandle = setInterval(this.update.bind(this), 5000);
|
|
|
|
extension.windows.onClosed(function () {
|
|
|
|
clearInterval(this.renderIntervalHandle);
|
|
|
|
}.bind(this));
|
2017-01-04 03:37:56 +00:00
|
|
|
|
|
|
|
setTimeout(this.finishConnectingGracePeriod.bind(this), 5000);
|
|
|
|
|
|
|
|
this.withinConnectingGracePeriod = true;
|
|
|
|
this.setSocketReconnectInterval(null);
|
|
|
|
|
2017-04-12 23:41:06 +00:00
|
|
|
window.addEventListener('online', this.update.bind(this));
|
|
|
|
window.addEventListener('offline', this.update.bind(this));
|
|
|
|
|
|
|
|
this.model = new Backbone.Model();
|
|
|
|
this.listenTo(this.model, 'change', this.onChange);
|
2017-01-04 03:37:56 +00:00
|
|
|
},
|
2017-04-12 21:11:51 +00:00
|
|
|
onReconnectTimer: function() {
|
|
|
|
this.setSocketReconnectInterval(60000);
|
|
|
|
},
|
2017-01-04 03:37:56 +00:00
|
|
|
finishConnectingGracePeriod: function() {
|
|
|
|
this.withinConnectingGracePeriod = false;
|
|
|
|
},
|
|
|
|
setSocketReconnectInterval: function(millis) {
|
|
|
|
this.socketReconnectWaitDuration = moment.duration(millis);
|
|
|
|
},
|
|
|
|
navigatorOnLine: function() { return navigator.onLine; },
|
|
|
|
getSocketStatus: function() { return window.getSocketStatus(); },
|
|
|
|
getNetworkStatus: function() {
|
|
|
|
|
|
|
|
var message = '';
|
|
|
|
var instructions = '';
|
|
|
|
var hasInterruption = false;
|
2017-04-12 23:46:51 +00:00
|
|
|
var action = null;
|
|
|
|
var buttonClass = null;
|
2017-01-04 03:37:56 +00:00
|
|
|
|
|
|
|
var socketStatus = this.getSocketStatus();
|
|
|
|
switch(socketStatus) {
|
|
|
|
case WebSocket.CONNECTING:
|
|
|
|
message = i18n('connecting');
|
|
|
|
this.setSocketReconnectInterval(null);
|
|
|
|
break;
|
|
|
|
case WebSocket.OPEN:
|
|
|
|
this.setSocketReconnectInterval(null);
|
|
|
|
break;
|
|
|
|
case WebSocket.CLOSING:
|
|
|
|
message = i18n('disconnected');
|
|
|
|
instructions = i18n('checkNetworkConnection');
|
|
|
|
hasInterruption = true;
|
|
|
|
break;
|
|
|
|
case WebSocket.CLOSED:
|
|
|
|
message = i18n('disconnected');
|
|
|
|
instructions = i18n('checkNetworkConnection');
|
|
|
|
hasInterruption = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socketStatus == WebSocket.CONNECTING && !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');
|
2017-04-12 23:46:51 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
message: message,
|
|
|
|
instructions: instructions,
|
2017-04-12 23:46:51 +00:00
|
|
|
hasInterruption: hasInterruption,
|
|
|
|
action: action,
|
|
|
|
buttonClass: buttonClass
|
2017-01-04 03:37:56 +00:00
|
|
|
};
|
|
|
|
},
|
2017-04-12 23:41:06 +00:00
|
|
|
update: function() {
|
2017-01-04 03:37:56 +00:00
|
|
|
var status = this.getNetworkStatus();
|
2017-04-12 23:41:06 +00:00
|
|
|
this.model.set(status);
|
|
|
|
},
|
|
|
|
render_attributes: function() {
|
|
|
|
return this.model.attributes;
|
|
|
|
},
|
|
|
|
onChange: function() {
|
|
|
|
this.render();
|
|
|
|
if (this.model.attributes.hasInterruption) {
|
2017-01-04 03:37:56 +00:00
|
|
|
this.$el.slideDown();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.$el.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})();
|