mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-22 07:16:13 +01:00
136 lines
4 KiB
JavaScript
136 lines
4 KiB
JavaScript
|
|
||
|
exports.showCountDownTimerToReconnectOnModal = function($modal) {
|
||
|
if (clientVars.automaticReconnectionTimeout && $modal.is('.with_reconnect_timer')) {
|
||
|
createCountDownElementsIfNecessary($modal);
|
||
|
|
||
|
var timer = createTimerForModal($modal);
|
||
|
|
||
|
$modal.find('#cancelreconnect').one('click', function() {
|
||
|
timer.cancel();
|
||
|
disableAutomaticReconnection($modal);
|
||
|
});
|
||
|
|
||
|
enableAutomaticReconnection($modal);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var createCountDownElementsIfNecessary = function($modal) {
|
||
|
var elementsDoNotExist = $modal.find('#cancelreconnect').length === 0;
|
||
|
if (elementsDoNotExist) {
|
||
|
var $defaultMessage = $modal.find('#defaulttext');
|
||
|
var $reconnectButton = $modal.find('#forcereconnect');
|
||
|
|
||
|
// create extra DOM elements, if they don't exist
|
||
|
var $reconnectTimerMessage = $('<p class="reconnecttimer"> \
|
||
|
<span data-l10n-id="pad.modals.reconnecttimer">This window will automatically reconnect in </span> \
|
||
|
<span class="timetoexpire"></span> \
|
||
|
</p>');
|
||
|
var $cancelReconnect = $('<button id="cancelreconnect" data-l10n-id="pad.modals.cancel">Cancel</button>');
|
||
|
|
||
|
$reconnectTimerMessage.insertAfter($defaultMessage);
|
||
|
$cancelReconnect.insertAfter($reconnectButton);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var createTimerForModal = function($modal) {
|
||
|
var timer = new CountDownTimer(clientVars.automaticReconnectionTimeout);
|
||
|
|
||
|
timer.onTick(function(minutes, seconds) {
|
||
|
updateCountDownTimerMessage($modal, minutes, seconds);
|
||
|
}).onExpire(function() {
|
||
|
reconnect($modal);
|
||
|
}).start();
|
||
|
|
||
|
return timer;
|
||
|
}
|
||
|
|
||
|
var disableAutomaticReconnection = function($modal) {
|
||
|
toggleAutomaticReconnectionOption($modal, true);
|
||
|
}
|
||
|
var enableAutomaticReconnection = function($modal) {
|
||
|
toggleAutomaticReconnectionOption($modal, false);
|
||
|
}
|
||
|
var toggleAutomaticReconnectionOption = function($modal, disableAutomaticReconnect) {
|
||
|
$modal.find('#cancelreconnect, .reconnecttimer').toggleClass('hidden', disableAutomaticReconnect);
|
||
|
$modal.find('#defaulttext').toggleClass('hidden', !disableAutomaticReconnect);
|
||
|
}
|
||
|
|
||
|
var reconnect = function($modal) {
|
||
|
$modal.find('#forcereconnect').click();
|
||
|
}
|
||
|
|
||
|
var updateCountDownTimerMessage = function($modal, minutes, seconds) {
|
||
|
minutes = minutes < 10 ? '0' + minutes : minutes;
|
||
|
seconds = seconds < 10 ? '0' + seconds : seconds;
|
||
|
|
||
|
$modal.find('.timetoexpire').text(minutes + ':' + seconds);
|
||
|
}
|
||
|
|
||
|
// Timer based on http://stackoverflow.com/a/20618517.
|
||
|
// duration: how many **seconds** until the timer ends
|
||
|
// granularity (optional): how many **milliseconds** between each 'tick' of timer. Default: 1000ms (1s)
|
||
|
var CountDownTimer = function(duration, granularity) {
|
||
|
this.duration = duration;
|
||
|
this.granularity = granularity || 1000;
|
||
|
this.running = false;
|
||
|
|
||
|
this.onTickCallbacks = [];
|
||
|
this.onExpireCallbacks = [];
|
||
|
}
|
||
|
|
||
|
CountDownTimer.prototype.start = function() {
|
||
|
if (this.running) {
|
||
|
return;
|
||
|
}
|
||
|
this.running = true;
|
||
|
var start = Date.now(),
|
||
|
that = this,
|
||
|
diff, obj;
|
||
|
|
||
|
(function timer() {
|
||
|
diff = that.duration - Math.floor((Date.now() - start) / 1000);
|
||
|
|
||
|
if (diff > 0) {
|
||
|
that.timeoutId = setTimeout(timer, that.granularity);
|
||
|
|
||
|
obj = CountDownTimer.parse(diff);
|
||
|
that.onTickCallbacks.forEach(function(callback) {
|
||
|
callback.call(this, obj.minutes, obj.seconds);
|
||
|
}, that);
|
||
|
} else {
|
||
|
that.running = false;
|
||
|
|
||
|
that.onExpireCallbacks.forEach(function(callback) {
|
||
|
callback.call(this);
|
||
|
}, that);
|
||
|
}
|
||
|
}());
|
||
|
};
|
||
|
|
||
|
CountDownTimer.prototype.onTick = function(callback) {
|
||
|
if (typeof callback === 'function') {
|
||
|
this.onTickCallbacks.push(callback);
|
||
|
}
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
CountDownTimer.prototype.onExpire = function(callback) {
|
||
|
if (typeof callback === 'function') {
|
||
|
this.onExpireCallbacks.push(callback);
|
||
|
}
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
CountDownTimer.prototype.cancel = function() {
|
||
|
this.running = false;
|
||
|
clearTimeout(this.timeoutId);
|
||
|
return this;
|
||
|
};
|
||
|
|
||
|
CountDownTimer.parse = function(seconds) {
|
||
|
return {
|
||
|
'minutes': (seconds / 60) | 0,
|
||
|
'seconds': (seconds % 60) | 0
|
||
|
};
|
||
|
};
|