scinote-web/app/assets/javascripts/sitewide/file_preview.js.erb

107 lines
3.4 KiB
Text
Raw Normal View History

2018-03-30 17:50:28 +08:00
(function(global) {
'use strict';
global.initPreviewModal = function initPreviewModal() {
var name;
var url;
var downloadUrl;
$('.file-preview-link').off('click');
2018-03-30 17:50:28 +08:00
$('.file-preview-link').click(function(e) {
e.preventDefault();
name = $(this).find('p').text();
url = $(this).data('preview-url');
downloadUrl = $(this).attr('href');
openPreviewModal(name, url, downloadUrl);
});
}
function openPreviewModal(name, url, downloadUrl) {
var modal = $('#filePreviewModal');
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function(data) {
modal.find('.file-preview-container').empty();
modal.find('.file-wopi-controls').empty();
if (data.hasOwnProperty('wopi-controls')) {
modal.find('.file-wopi-controls').html(data['wopi-controls']);
}
var link = modal.find('.file-download-link');
link.attr('href', downloadUrl);
link.attr('data-no-turbolink', true);
link.attr('data-status', 'asset-present');
if (data['type'] === 'image') {
if(data['processing']) {
animateSpinner('.file-preview-container', true);
} else {
animateSpinner('.file-preview-container', false);
modal.find('.file-preview-container')
.append($('<img>')
.attr('src', data['large-preview-url'])
.attr('alt', name)
.click(function(ev) {
ev.stopPropagation();
})
);
}
2018-03-30 17:50:28 +08:00
} else {
modal.find('.file-preview-container').html(data['preview-icon']);
}
2018-04-26 23:00:51 +08:00
if(data['processing']) {
checkFileReady(url, modal);
}
2018-03-30 17:50:28 +08:00
modal.find('.file-name').text(name);
modal.find('.preview-close').click(function() {
2018-03-30 17:50:28 +08:00
modal.modal('hide');
});
modal.modal();
$('.modal-backdrop').last().css('z-index', modal.css('z-index') - 1);
},
error: function(ev) {
// TODO
}
});
}
function checkFileReady(url, modal) {
$.get(url, function(data) {
if(data['processing']) {
$('.file-download-link')
.addClass('disabled-with-click-events')
.attr('title',
'<%= I18n.t('general.file.processing')%>')
.click(function(ev) {
ev.preventDefault();
ev.stopPropagation();
});
setTimeout(function() {
checkFileReady(url, modal);
}, 10000);
} else {
2018-04-26 23:00:51 +08:00
if(data['type'] === 'image') {
modal.find('.file-preview-container').empty();
modal.find('.file-preview-container')
.append($('<img>')
.attr('src', data['large-preview-url'])
.attr('alt', data['filename'])
.click(function(ev) {
ev.stopPropagation();
})
);
modal.find('.file-name').text(data['filename']);
modal.find('.modal-body').click(function() {
modal.modal('hide');
});
modal.modal();
$('.modal-backdrop').last().css('z-index', modal.css('z-index') - 1);
}
$('.file-download-link')
.removeClass('disabled-with-click-events')
.removeAttr('title')
.off();
}
})
}
2018-03-30 17:50:28 +08:00
})(window);