scinote-web/app/assets/javascripts/assets.js

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-07-21 19:11:15 +08:00
function setupAssetsLoading() {
var DELAY = 2500;
var REPETITIONS = 60;
2016-07-21 19:11:15 +08:00
function refreshAssets() {
var elements = $("[data-status='asset-loading']");
if (!elements.length) {
return false;
}
$.each(elements, function(_, el) {
var $el = $(el);
// 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) {
2017-03-16 22:38:11 +08:00
var wopiBtns;
2016-07-21 19:11:15 +08:00
$el.attr("data-status", "asset-loaded");
$el.find('img').hide();
$el.next().hide();
2016-07-21 19:11:15 +08:00
$el.html("");
if (data.type === "image") {
2016-07-21 19:11:15 +08:00
$el.html(
"<a class='image-preview-link' id='modal_link" +
data['asset-id'] + "' data-status='asset-present' " +
"href='" + data['download-url'] + "'>" +
"<img src='" + data['image-tag-url'] + "' data-preview-url='" +
data['preview-url'] + "'><p>" +
data.filename + '</p></a>'
2016-07-21 19:11:15 +08:00
);
2017-03-17 23:40:57 +08:00
} else if (data.type === 'wopi') {
if (data['wopi-edit']) {
2017-03-16 22:38:11 +08:00
wopiBtns = data['wopi-file-name'] +
data['wopi-view'] +
data['wopi-edit'];
} else {
wopiBtns = data['wopi-file-name'] +
data['wopi-view'];
}
$el.html(wopiBtns);
2016-07-21 19:11:15 +08:00
} else {
$el.html(
"<a href='" + data['download-url'] + "'><p>" +
data.filename + "</p></a>"
2016-07-21 19:11:15 +08:00
);
}
animateSpinner(null, false);
2016-12-20 00:36:18 +08:00
initPreviewModal();
2016-07-21 19:11:15 +08:00
},
error: function(data) {
if (data.status == 403) {
$el.find('img').hide();
$el.next().hide();
2016-07-21 19:11:15 +08:00
// Image/file exists, but user doesn't have
// rights to download it
if (type === "image") {
$el.html(
"<img src='" + data['image-tag-url'] + "'><p>" +
data.filename + "</p>"
2016-07-21 19:11:15 +08:00
);
} else {
$el.html("<p>" + data.filename + "</p>");
2016-07-21 19:11:15 +08:00
}
} else {
// Do nothing, file is not yet present
animateSpinner(null, false);
2016-07-21 19:11:15 +08:00
}
}
});
});
return true;
}
function finalizeAssets() {
var elements = $("[data-status='asset-loading']");
$.each(elements, function(_, el) {
var $el = $(el);
$el.attr("data-status", "asset-failed");
$el.html($el.data("filename"));
});
}
var cntr = 0;
var intervalId = window.setInterval(function() {
cntr++;
if (cntr >= REPETITIONS || !refreshAssets()) {
finalizeAssets();
window.clearInterval(intervalId);
}
}, DELAY);
2016-08-19 21:36:12 +08:00
}