2019-05-13 22:53:58 +08:00
|
|
|
/* global animateSpinner FilePreviewModal */
|
|
|
|
|
2019-05-21 23:29:33 +08:00
|
|
|
var Assets = (function() {
|
2019-06-11 19:14:07 +08:00
|
|
|
var DELAY = 5000;
|
|
|
|
var REPETITIONS_LIMIT = 30;
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
function finalizeAsset(element) {
|
|
|
|
var $el = $(element);
|
|
|
|
|
|
|
|
$el.attr('data-status', 'asset-failed');
|
|
|
|
if ($el.data('filename')) {
|
|
|
|
$el.html($el.data('filename'));
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2019-06-11 19:14:07 +08:00
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
function refreshAsset(element) {
|
|
|
|
var $el = $(element);
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
if ($el.data('refreshCounter') > REPETITIONS_LIMIT) {
|
|
|
|
finalizeAsset($el);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform an AJAX call to present URL
|
|
|
|
// to check if file already exists
|
|
|
|
$.ajax({
|
|
|
|
url: $el.data('present-url'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
if (data.processing === true) {
|
|
|
|
setTimeout(function() {
|
|
|
|
refreshAsset($el);
|
|
|
|
}, DELAY);
|
|
|
|
}
|
2019-05-13 22:53:58 +08:00
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
if (data.processing === false) {
|
|
|
|
$el.html(data.placeholder_html);
|
|
|
|
$el.attr('data-status', 'asset-loaded');
|
|
|
|
animateSpinner($el, false);
|
|
|
|
FilePreviewModal.init();
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2019-06-11 19:14:07 +08:00
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
}
|
2016-07-21 19:11:15 +08:00
|
|
|
});
|
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
$el.data().refreshCounter += 1;
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
function setupAssetsLoading() {
|
2016-07-21 19:11:15 +08:00
|
|
|
var elements = $("[data-status='asset-loading']");
|
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
if (!elements.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
$.each(elements, function(_, el) {
|
|
|
|
var $el = $(el);
|
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
if ($el.data('refreshIsRunning') === true) return;
|
2019-05-21 23:29:33 +08:00
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
$el.data('refreshCounter', 1);
|
|
|
|
$el.data('refreshIsRunning', true);
|
2019-05-21 23:29:33 +08:00
|
|
|
|
2019-06-11 19:14:07 +08:00
|
|
|
setTimeout(function() {
|
|
|
|
refreshAsset($el);
|
|
|
|
}, DELAY);
|
|
|
|
});
|
2019-05-21 23:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Object.freeze({
|
|
|
|
setupAssetsLoading: setupAssetsLoading
|
|
|
|
});
|
|
|
|
}());
|