2019-03-18 10:58:13 +01:00
|
|
|
/**
|
|
|
|
* Queue requests and handle them at your convenience
|
|
|
|
*
|
|
|
|
* @type {RequestQueue}
|
|
|
|
*/
|
2019-04-01 12:06:00 +02:00
|
|
|
H5P.RequestQueue = (function ($, EventDispatcher) {
|
2019-03-18 10:58:13 +01:00
|
|
|
/**
|
|
|
|
* A queue for requests, will be automatically processed when regaining connection
|
|
|
|
*
|
2019-04-03 09:43:09 +02:00
|
|
|
* @param {boolean} [options.showToast] Show toast when losing or regaining connection
|
2019-03-18 10:58:13 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2019-04-01 12:06:00 +02:00
|
|
|
const RequestQueue = function (options) {
|
|
|
|
EventDispatcher.call(this);
|
2019-03-18 10:58:13 +01:00
|
|
|
this.processingQueue = false;
|
2019-04-03 09:43:09 +02:00
|
|
|
options = options || {};
|
2019-03-18 10:58:13 +01:00
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
this.showToast = options.showToast;
|
2019-04-01 14:46:29 +02:00
|
|
|
this.itemName = 'requestQueue';
|
2019-03-18 10:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-04-03 09:43:09 +02:00
|
|
|
* Add request to queue. Only supports posts currently.
|
2019-03-18 10:58:13 +01:00
|
|
|
*
|
|
|
|
* @param {string} url
|
|
|
|
* @param {Object} data
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
RequestQueue.prototype.add = function (url, data) {
|
|
|
|
if (!window.localStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let storedStatements = this.getStoredRequests();
|
|
|
|
if (!storedStatements) {
|
|
|
|
storedStatements = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
storedStatements.push({
|
|
|
|
url: url,
|
|
|
|
data: data,
|
|
|
|
});
|
|
|
|
|
|
|
|
window.localStorage.setItem(this.itemName, JSON.stringify(storedStatements));
|
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
this.trigger('requestQueued', {
|
|
|
|
storedStatements: storedStatements,
|
|
|
|
processingQueue: this.processingQueue,
|
|
|
|
});
|
2019-03-18 10:58:13 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get stored requests
|
|
|
|
*
|
|
|
|
* @returns {boolean|Array} Stored requests
|
|
|
|
*/
|
|
|
|
RequestQueue.prototype.getStoredRequests = function () {
|
|
|
|
if (!window.localStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = window.localStorage.getItem(this.itemName);
|
|
|
|
if (!item) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.parse(item);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear stored requests
|
|
|
|
*
|
|
|
|
* @returns {boolean} True if the storage was successfully cleared
|
|
|
|
*/
|
2019-04-03 09:43:09 +02:00
|
|
|
RequestQueue.prototype.clearQueue = function () {
|
2019-03-18 10:58:13 +01:00
|
|
|
if (!window.localStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.localStorage.removeItem(this.itemName);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start processing of requests queue
|
2019-04-01 12:06:00 +02:00
|
|
|
*
|
|
|
|
* @return {boolean} Returns false if it was not possible to resume processing queue
|
2019-03-18 10:58:13 +01:00
|
|
|
*/
|
|
|
|
RequestQueue.prototype.resumeQueue = function () {
|
|
|
|
// Not supported
|
|
|
|
if (!H5PIntegration || !window.navigator || !window.localStorage) {
|
2019-04-01 12:06:00 +02:00
|
|
|
return false;
|
2019-03-18 10:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Already processing
|
|
|
|
if (this.processingQueue) {
|
2019-04-01 12:06:00 +02:00
|
|
|
return false;
|
2019-03-18 10:58:13 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
// Attempt to send queued requests
|
2019-03-18 10:58:13 +01:00
|
|
|
const queue = this.getStoredRequests();
|
|
|
|
const queueLength = queue.length;
|
|
|
|
|
|
|
|
// Clear storage, failed requests will be re-added
|
2019-04-03 09:43:09 +02:00
|
|
|
this.clearQueue();
|
2019-03-18 10:58:13 +01:00
|
|
|
|
|
|
|
// No items left in queue
|
|
|
|
if (!queueLength) {
|
2019-04-03 09:43:09 +02:00
|
|
|
this.trigger('emptiedQueue', queue);
|
2019-04-01 12:06:00 +02:00
|
|
|
return true;
|
2019-03-18 10:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure requests are not changed while they're being handled
|
|
|
|
this.processingQueue = true;
|
|
|
|
|
|
|
|
// Process queue in original order
|
|
|
|
this.processQueue(queue);
|
2019-04-01 12:06:00 +02:00
|
|
|
return true
|
2019-03-18 10:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process first item in the request queue
|
|
|
|
*
|
|
|
|
* @param {Array} queue Request queue
|
|
|
|
*/
|
|
|
|
RequestQueue.prototype.processQueue = function (queue) {
|
|
|
|
if (!queue.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:06:00 +02:00
|
|
|
this.trigger('processingQueue');
|
|
|
|
|
2019-03-18 10:58:13 +01:00
|
|
|
// Make sure the requests are processed in a FIFO order
|
|
|
|
const request = queue.shift();
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
$.post(request.url, request.data)
|
|
|
|
.fail(self.onQueuedRequestFail.bind(self, request))
|
|
|
|
.always(self.onQueuedRequestProcessed.bind(self, queue))
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request fail handler
|
|
|
|
*
|
|
|
|
* @param {Object} request
|
|
|
|
*/
|
|
|
|
RequestQueue.prototype.onQueuedRequestFail = function (request) {
|
|
|
|
// Queue the failed request again if we're offline
|
|
|
|
if (!window.navigator.onLine) {
|
|
|
|
this.add(request.url, request.data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-01 12:06:00 +02:00
|
|
|
/**
|
|
|
|
* An item in the queue was processed
|
|
|
|
*
|
|
|
|
* @param {Array} queue Queue that was processed
|
|
|
|
*/
|
2019-03-18 10:58:13 +01:00
|
|
|
RequestQueue.prototype.onQueuedRequestProcessed = function (queue) {
|
|
|
|
if (queue.length) {
|
|
|
|
this.processQueue(queue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished processing this queue
|
|
|
|
this.processingQueue = false;
|
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
// Run empty queue callback with next request queue
|
2019-03-18 10:58:13 +01:00
|
|
|
const requestQueue = this.getStoredRequests();
|
2019-04-03 09:43:09 +02:00
|
|
|
this.trigger('queueEmptied', requestQueue);
|
2019-03-18 10:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display toast message on the first content of current page
|
|
|
|
*
|
|
|
|
* @param {string} msg Message to display
|
2019-04-03 09:43:09 +02:00
|
|
|
* @param {boolean} [forceShow] Force override showing the toast
|
2019-04-05 14:49:55 +02:00
|
|
|
* @param {Object} [configOverride] Override toast message config
|
2019-03-18 10:58:13 +01:00
|
|
|
*/
|
2019-04-05 14:49:55 +02:00
|
|
|
RequestQueue.prototype.displayToastMessage = function (msg, forceShow, configOverride) {
|
2019-04-03 09:43:09 +02:00
|
|
|
if (!this.showToast && !forceShow) {
|
2019-04-01 15:10:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-04-05 14:49:55 +02:00
|
|
|
|
|
|
|
const config = H5P.jQuery.extend(true, {}, {
|
|
|
|
position: {
|
|
|
|
horizontal : 'centered',
|
|
|
|
vertical: 'centered',
|
|
|
|
noOverflowX: true,
|
2019-03-18 10:58:13 +01:00
|
|
|
}
|
2019-04-05 14:49:55 +02:00
|
|
|
}, configOverride);
|
|
|
|
|
|
|
|
H5P.attachToastTo(H5P.jQuery('.h5p-content:first')[0], msg, config);
|
2019-03-18 10:58:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return RequestQueue;
|
2019-04-03 09:43:09 +02:00
|
|
|
})(H5P.jQuery, H5P.EventDispatcher);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request queue for retrying failing requests, will automatically retry them when you come online
|
|
|
|
*
|
|
|
|
* @type {offlineRequestQueue}
|
|
|
|
*/
|
|
|
|
H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) {
|
2019-04-03 16:09:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param {Object} [options] Options for offline request queue
|
|
|
|
* @param {Object} [options.instance] The H5P instance which UI components are placed within
|
|
|
|
*/
|
|
|
|
const offlineRequestQueue = function (options) {
|
2019-04-03 09:43:09 +02:00
|
|
|
const requestQueue = new RequestQueue();
|
|
|
|
|
|
|
|
// We could handle requests from previous pages here, but instead we throw them away
|
|
|
|
requestQueue.clearQueue();
|
|
|
|
|
|
|
|
let startTime = null;
|
|
|
|
const retryIntervals = [10, 20, 40, 60, 120, 300, 600];
|
|
|
|
let intervalIndex = -1;
|
|
|
|
let currentInterval = null;
|
|
|
|
let isAttached = false;
|
|
|
|
let isShowing = false;
|
|
|
|
let isLoading = false;
|
2019-04-03 16:09:53 +02:00
|
|
|
const instance = options.instance;
|
2019-04-03 09:43:09 +02:00
|
|
|
|
|
|
|
const offlineDialog = new Dialog({
|
|
|
|
headerText: H5P.t('offlineDialogHeader'),
|
|
|
|
dialogText: H5P.t('offlineDialogBody'),
|
|
|
|
confirmText: H5P.t('offlineDialogRetryButtonLabel'),
|
|
|
|
hideCancel: true,
|
|
|
|
hideExit: true,
|
|
|
|
classes: ['offline'],
|
2019-04-03 16:09:53 +02:00
|
|
|
instance: instance,
|
2019-04-05 16:11:19 +02:00
|
|
|
skipRestoreFocus: true,
|
2019-04-03 09:43:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const dialog = offlineDialog.getElement();
|
|
|
|
|
|
|
|
// Add retry text to body
|
|
|
|
const countDownText = document.createElement('div');
|
|
|
|
countDownText.classList.add('count-down');
|
|
|
|
countDownText.innerHTML = H5P.t('offlineDialogRetryMessage')
|
|
|
|
.replace(':num', '<span class="count-down-num">0</span>');
|
|
|
|
|
|
|
|
dialog.querySelector('.h5p-confirmation-dialog-text').appendChild(countDownText);
|
|
|
|
const countDownNum = countDownText.querySelector('.count-down-num');
|
|
|
|
|
|
|
|
// Create throbber
|
|
|
|
const throbberWrapper = document.createElement('div');
|
|
|
|
throbberWrapper.classList.add('throbber-wrapper');
|
|
|
|
const throbber = document.createElement('div');
|
|
|
|
throbber.classList.add('sending-requests-throbber');
|
|
|
|
throbberWrapper.appendChild(throbber);
|
|
|
|
|
|
|
|
requestQueue.on('requestQueued', function (e) {
|
|
|
|
// Already processing queue, wait until queue has finished processing before showing dialog
|
|
|
|
if (e.data && e.data.processingQueue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAttached) {
|
|
|
|
const rootContent = document.body.querySelector('.h5p-content');
|
|
|
|
if (!rootContent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
offlineDialog.appendTo(rootContent);
|
|
|
|
rootContent.appendChild(throbberWrapper);
|
|
|
|
isAttached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
startCountDown();
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
requestQueue.on('queueEmptied', function (e) {
|
|
|
|
if (e.data && e.data.length) {
|
|
|
|
// New requests were added while processing queue or requests failed again. Re-queue requests.
|
|
|
|
startCountDown(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Successfully emptied queue
|
|
|
|
clearInterval(currentInterval);
|
|
|
|
toggleThrobber(false);
|
|
|
|
intervalIndex = -1;
|
|
|
|
if (isShowing) {
|
|
|
|
offlineDialog.hide();
|
|
|
|
isShowing = false;
|
|
|
|
}
|
2019-04-05 14:49:55 +02:00
|
|
|
|
2019-04-05 16:11:19 +02:00
|
|
|
requestQueue.displayToastMessage(
|
|
|
|
H5P.t('offlineSuccessfulSubmit'),
|
|
|
|
true,
|
|
|
|
{
|
2019-04-05 14:49:55 +02:00
|
|
|
position: {
|
|
|
|
vertical: 'top',
|
2019-04-05 16:11:19 +02:00
|
|
|
offsetVertical: '100',
|
2019-04-05 14:49:55 +02:00
|
|
|
}
|
2019-04-05 16:11:19 +02:00
|
|
|
}
|
|
|
|
);
|
2019-04-03 09:43:09 +02:00
|
|
|
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
offlineDialog.on('confirmed', function () {
|
|
|
|
// Show dialog on next render in case it is being hidden by the 'confirm' button
|
|
|
|
isShowing = false;
|
|
|
|
setTimeout(function () {
|
|
|
|
retryRequests();
|
|
|
|
}, 100);
|
|
|
|
}.bind(this));
|
|
|
|
|
2019-04-08 10:23:20 +02:00
|
|
|
// Initialize listener for when requests are added to queue
|
|
|
|
window.addEventListener('online', function () {
|
|
|
|
retryRequests();
|
|
|
|
}.bind(this));
|
|
|
|
|
2019-04-03 13:09:18 +02:00
|
|
|
// Listen for queued requests outside the iframe
|
|
|
|
window.addEventListener('message', function (event) {
|
|
|
|
const isValidQueueEvent = window.parent === event.source
|
|
|
|
&& event.data.context === 'h5p'
|
|
|
|
&& event.data.action === 'queueRequest';
|
|
|
|
|
|
|
|
if (!isValidQueueEvent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.add(event.data.url, event.data.data);
|
|
|
|
}.bind(this));
|
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
/**
|
|
|
|
* Toggle throbber visibility
|
|
|
|
*
|
|
|
|
* @param {boolean} [forceShow] Will force throbber visibility if set
|
|
|
|
*/
|
|
|
|
const toggleThrobber = function (forceShow) {
|
|
|
|
isLoading = !isLoading;
|
|
|
|
if (forceShow !== undefined) {
|
|
|
|
isLoading = forceShow;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoading && isShowing) {
|
|
|
|
offlineDialog.hide();
|
|
|
|
isShowing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
throbberWrapper.classList.add('show');
|
2019-04-08 11:19:38 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-04-03 09:43:09 +02:00
|
|
|
throbberWrapper.classList.remove('show');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Retries the failed requests
|
|
|
|
*/
|
2019-04-08 10:23:20 +02:00
|
|
|
const retryRequests = function () {
|
2019-04-03 09:43:09 +02:00
|
|
|
clearInterval(currentInterval);
|
|
|
|
toggleThrobber(true);
|
2019-04-08 10:23:20 +02:00
|
|
|
requestQueue.resumeQueue();
|
2019-04-03 09:43:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increments retry interval
|
|
|
|
*/
|
|
|
|
const incrementRetryInterval = function () {
|
|
|
|
intervalIndex += 1;
|
|
|
|
if (intervalIndex >= retryIntervals.length) {
|
|
|
|
intervalIndex = retryIntervals.length - 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts counting down to retrying queued requests.
|
|
|
|
*
|
|
|
|
* @param forceDelayedShow
|
|
|
|
*/
|
|
|
|
const startCountDown = function (forceDelayedShow) {
|
2019-04-03 13:09:18 +02:00
|
|
|
// Already showing, wait for retry
|
|
|
|
if (isShowing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
toggleThrobber(false);
|
|
|
|
if (!isShowing) {
|
|
|
|
if (forceDelayedShow) {
|
|
|
|
// Must force delayed show since dialog may be hiding, and confirmation dialog does not
|
|
|
|
// support this.
|
|
|
|
setTimeout(function () {
|
2019-04-05 16:11:19 +02:00
|
|
|
offlineDialog.show(0);
|
2019-04-03 09:43:09 +02:00
|
|
|
}, 100);
|
2019-04-08 11:19:38 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-04-05 16:11:19 +02:00
|
|
|
offlineDialog.show(0);
|
2019-04-03 09:43:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
isShowing = true;
|
|
|
|
startTime = new Date().getTime();
|
|
|
|
incrementRetryInterval();
|
2019-04-03 13:09:18 +02:00
|
|
|
clearInterval(currentInterval);
|
2019-04-03 09:43:09 +02:00
|
|
|
currentInterval = setInterval(updateCountDown, 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the count down timer. Retries requests when time expires.
|
|
|
|
*/
|
|
|
|
const updateCountDown = function () {
|
|
|
|
const time = new Date().getTime();
|
|
|
|
const timeElapsed = Math.floor((time - startTime) / 1000);
|
|
|
|
const timeLeft = retryIntervals[intervalIndex] - timeElapsed;
|
|
|
|
countDownNum.textContent = timeLeft.toString();
|
|
|
|
|
|
|
|
// Retry interval reached, retry requests
|
|
|
|
if (timeLeft <= 0) {
|
|
|
|
retryRequests();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add request to offline request queue. Only supports posts for now.
|
|
|
|
*
|
|
|
|
* @param {string} url The request url
|
|
|
|
* @param {Object} data The request data
|
|
|
|
*/
|
|
|
|
this.add = function (url, data) {
|
2019-04-08 10:23:20 +02:00
|
|
|
// Only queue request if it failed because we are offline
|
|
|
|
if (window.navigator.onLine) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-03 09:43:09 +02:00
|
|
|
requestQueue.add(url, data);
|
|
|
|
};
|
|
|
|
};
|
2019-04-03 13:09:18 +02:00
|
|
|
|
|
|
|
return offlineRequestQueue;
|
2019-04-08 11:19:38 +02:00
|
|
|
})(H5P.RequestQueue, H5P.ConfirmationDialog);
|