Merge pull request #1758 from okriuchykhin/ok_SCI_3381

Fix handling of processing step attachments [SCI-3381]
This commit is contained in:
Alex Kriuchykhin 2019-05-14 09:26:30 +02:00 committed by GitHub
commit e544edeaf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 80 additions and 44 deletions

View file

@ -1,6 +1,10 @@
/* global animateSpinner FilePreviewModal */
function setupAssetsLoading() { function setupAssetsLoading() {
var DELAY = 2500; var DELAY = 2500;
var REPETITIONS = 60; var REPETITIONS = 60;
var cntr = 0;
var intervalId;
function refreshAssets() { function refreshAssets() {
var elements = $("[data-status='asset-loading']"); var elements = $("[data-status='asset-loading']");
@ -15,50 +19,55 @@ function setupAssetsLoading() {
// Perform an AJAX call to present URL // Perform an AJAX call to present URL
// to check if file already exists // to check if file already exists
$.ajax({ $.ajax({
url: $el.data("present-url"), url: $el.data('present-url'),
type: "GET", type: 'GET',
dataType: "json", dataType: 'json',
success: function (data) { success: function(data) {
var wopiBtns; if (data.processing === true) {
$el.attr("data-status", "asset-loaded"); return;
}
if (data.processing === false) {
$el.html(data.placeholder_html);
$el.attr('data-status', 'asset-loaded');
return;
}
$el.attr('data-status', 'asset-loaded');
$el.find('img').hide(); $el.find('img').hide();
$el.next().hide(); $el.next().hide();
$el.html(""); $el.html('');
if (data.type === 'image') { if (data.type === 'image') {
$el.html( $el.html(
"<a class='file-preview-link' id='modal_link" + "<a class='file-preview-link' id='modal_link"
data['asset-id'] + "' data-status='asset-present' " + + data['asset-id'] + "' data-status='asset-present' "
"href='" + data['download-url'] + "' data-preview-url='" + + "href='" + data['download-url'] + "' data-preview-url='" + data['preview-url'] + "'>"
data['preview-url'] + "'>" + + "<img src='" + data['image-tag-url'] + "'><p>" + data.filename + '</p></a>'
"<img src='" + data['image-tag-url'] + "'><p>" +
data.filename + '</p></a>'
); );
} else { } else {
$el.html( $el.html(
"<a class='file-preview-link' id='modal_link" + "<a class='file-preview-link' id='modal_link"
data['asset-id'] + "' data-status='asset-present' " + + data['asset-id'] + "' data-status='asset-present' "
"href='" + data['download-url'] + "' data-preview-url='" + + "href='" + data['download-url'] + "' data-preview-url='"
data['preview-url'] + "'><p>" + + data['preview-url'] + "'><p>" + data.filename + '</p></a>'
data.filename + '</p></a>'
); );
} }
animateSpinner(null, false); animateSpinner(null, false);
FilePreviewModal.init(); FilePreviewModal.init();
}, },
error: function(data) { error: function(data) {
if (data.status == 403) { if (data.status === 403) {
$el.find('img').hide(); $el.find('img').hide();
$el.next().hide(); $el.next().hide();
// Image/file exists, but user doesn't have // Image/file exists, but user doesn't have
// rights to download it // rights to download it
if (type === "image") { if (data.type === 'image') {
$el.html( $el.html(
"<img src='" + data['image-tag-url'] + "'><p>" + "<img src='" + data['image-tag-url'] + "'><p>" + data.filename + '</p>'
data.filename + "</p>"
); );
} else { } else {
$el.html("<p>" + data.filename + "</p>"); $el.html('<p>' + data.filename + '</p>');
} }
} else { } else {
// Do nothing, file is not yet present // Do nothing, file is not yet present
@ -76,14 +85,15 @@ function setupAssetsLoading() {
$.each(elements, function(_, el) { $.each(elements, function(_, el) {
var $el = $(el); var $el = $(el);
$el.attr("data-status", "asset-failed"); $el.attr('data-status', 'asset-failed');
$el.html($el.data("filename")); if ($el.data('filename')) {
$el.html($el.data('filename'));
}
}); });
} }
var cntr = 0; intervalId = window.setInterval(function() {
var intervalId = window.setInterval(function() { cntr += 1;
cntr++;
if (cntr >= REPETITIONS || !refreshAssets()) { if (cntr >= REPETITIONS || !refreshAssets()) {
finalizeAssets(); finalizeAssets();
window.clearInterval(intervalId); window.clearInterval(intervalId);

View file

@ -17,7 +17,6 @@ function init() {
initLoadFromRepository(); initLoadFromRepository();
initRefreshStatusBar(); initRefreshStatusBar();
initImport(); initImport();
setupAssetsLoading();
} }
function initEditMyModuleDescription() { function initEditMyModuleDescription() {

View file

@ -190,7 +190,7 @@
$('.attachment-placeholder.new').remove(); $('.attachment-placeholder.new').remove();
_dragNdropAssetsOff(); _dragNdropAssetsOff();
for(var i = 0; i < droppedFiles.length; i++) { for(var i = 0; i < droppedFiles.length; i++) {
$('.attacments.edit') $('.attachments.edit')
.append(_uploadedAssetPreview(droppedFiles[i], i)) .append(_uploadedAssetPreview(droppedFiles[i], i))
.promise() .promise()
.done(function() { .done(function() {

View file

@ -431,7 +431,7 @@ var FilePreviewModal = (function() {
link.attr('data-status', 'asset-present'); link.attr('data-status', 'asset-present');
if (data.type === 'image') { if (data.type === 'image') {
if (data.processing) { if (data.processing) {
animateSpinner('.file-preview-container', true); modal.find('.file-preview-container').append(data['processing-img']);
} else { } else {
animateSpinner('.file-preview-container', false); animateSpinner('.file-preview-container', false);
modal.find('.file-preview-container') modal.find('.file-preview-container')

View file

@ -113,7 +113,7 @@
} }
} }
.attacments { .attachments {
display: grid; display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));

View file

@ -42,6 +42,24 @@ class AssetsController < ApplicationController
end end
end end
def step_file_present
respond_to do |format|
format.json do
if @asset.file.processing?
render json: { processing: true }
else
render json: {
placeholder_html: render_to_string(
partial: 'steps/attachments/placeholder.html.erb',
locals: { asset: @asset, edit_page: false }
),
processing: false
}
end
end
end
end
def file_preview def file_preview
response_json = { response_json = {
'id' => @asset.id, 'id' => @asset.id,
@ -69,7 +87,7 @@ class AssetsController < ApplicationController
'mime-type' => @asset.file.content_type, 'mime-type' => @asset.file.content_type,
'processing' => @asset.file.processing?, 'processing' => @asset.file.processing?,
'large-preview-url' => @asset.url(:large), 'large-preview-url' => @asset.url(:large),
'processing-url' => image_tag('medium/processing.gif') 'processing-img' => image_tag('medium/processing.gif')
) )
else else
response_json.merge!( response_json.merge!(
@ -251,12 +269,9 @@ class AssetsController < ApplicationController
@asset = Asset.find_by_id(params[:id]) @asset = Asset.find_by_id(params[:id])
return render_404 unless @asset return render_404 unless @asset
step_assoc = @asset.step @assoc ||= @asset.step
result_assoc = @asset.result @assoc ||= @asset.result
repository_cell_assoc = @asset.repository_cell @assoc ||= @asset.repository_cell
@assoc = step_assoc unless step_assoc.nil?
@assoc = result_assoc unless result_assoc.nil?
@assoc = repository_cell_assoc unless repository_cell_assoc.nil?
if @assoc.class == Step if @assoc.class == Step
@protocol = @asset.step.protocol @protocol = @asset.step.protocol

View file

@ -67,7 +67,7 @@
</span> </span>
</div> </div>
<div id="new-step-assets-group" class="form-group"> <div id="new-step-assets-group" class="form-group">
<div class="col-xs-12 attacments edit"> <div class="col-xs-12 attachments edit">
<%= f.nested_fields_for :assets do |ff| %> <%= f.nested_fields_for :assets do |ff| %>
<%= render partial: 'steps/attachments/placeholder.html.erb', <%= render partial: 'steps/attachments/placeholder.html.erb',
locals: { edit_page: true, asset: ff.object, ff: ff } %> locals: { edit_page: true, asset: ff.object, ff: ff } %>

View file

@ -1,8 +1,19 @@
<% if asset.file.processing? && asset.is_image? %>
<% asset_status = 'asset-loading' %>
<% present_url = step_file_present_asset_path(asset.id) %>
<% else %>
<% asset_status = 'asset-present' %>
<% present_url = '' %>
<% end %>
<div class="pseudo-attachment-container" style="order: <%= assets_count - i %>"> <div class="pseudo-attachment-container" style="order: <%= assets_count - i %>">
<%= link_to download_asset_path(asset), <%= link_to download_asset_path(asset),
class: 'file-preview-link', class: 'file-preview-link',
id: "modal_link#{asset.id}", id: "modal_link#{asset.id}",
data: { no_turbolink: true, id: true, status: 'asset-present', data: { no_turbolink: true,
id: true,
status: asset_status,
'present-url': present_url,
'preview-url': asset_file_preview_path(asset), 'preview-url': asset_file_preview_path(asset),
'order-atoz': az_ordered_assets_index(step, asset.id), 'order-atoz': az_ordered_assets_index(step, asset.id),
'order-ztoa': assets_count - az_ordered_assets_index(step, asset.id), 'order-ztoa': assets_count - az_ordered_assets_index(step, asset.id),

View file

@ -33,7 +33,7 @@
</div> </div>
</div> </div>
<div class="col-xs-12 attacments" id="att-<%= step.id %>"> <div class="col-xs-12 attachments" id="att-<%= step.id %>">
<% assets.each_with_index do |asset, i| %> <% assets.each_with_index do |asset, i| %>
<%= render partial: 'steps/attachments/item.html.erb', <%= render partial: 'steps/attachments/item.html.erb',
locals: { asset: asset, i: i, assets_count: assets.count, step: step } %> locals: { asset: asset, i: i, assets_count: assets.count, step: step } %>

View file

@ -580,6 +580,7 @@ Rails.application.routes.draw do
# We cannot use 'resources :assets' because assets is a reserved route # We cannot use 'resources :assets' because assets is a reserved route
# in Rails (assets pipeline) and causes funky behavior # in Rails (assets pipeline) and causes funky behavior
get 'files/:id/present', to: 'assets#file_present', as: 'file_present_asset' get 'files/:id/present', to: 'assets#file_present', as: 'file_present_asset'
get 'files/:id/present_in_step', to: 'assets#step_file_present', as: 'step_file_present_asset'
get 'files/:id/preview', get 'files/:id/preview',
to: 'assets#file_preview', to: 'assets#file_preview',
as: 'asset_file_preview' as: 'asset_file_preview'