Controller action for card inline preview

This commit is contained in:
Urban Rotnik 2020-08-28 11:05:52 +02:00
parent 12b903dbe7
commit 4eebfa5461
6 changed files with 94 additions and 1 deletions

View file

@ -380,6 +380,27 @@
});
}
function initPreviewToggle(){
$('.attachment-placeholder').on('click', '.dataPreviewAction', function (e) {
var view_mode = $(this).data('preview-type');
var asset_id = $(this).data('asset-id');
e.preventDefault();
$.ajax({
url: `/files/${asset_id}/toggle_view_mode`,
type: "PATCH",
dataType: "json",
data: { asset: { view_mode: view_mode }},
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
});
}
function initCallBacks() {
if (typeof(applyCreateWopiFileCallback) === 'function') applyCreateWopiFileCallback();
applyCheckboxCallBack();
@ -389,6 +410,7 @@
applyCollapseLinkCallBack();
initDeleteStep();
TinyMCE.highlight();
initPreviewToggle();
}
/*

View file

@ -97,6 +97,24 @@ class AssetsController < ApplicationController
return edit_supported, title
end
def toggle_view_mode
# view_mode: card / inline
# @asset.update!(view_mode: toggle_view_mode_params[:view_mode])
html = if @asset.inline_card && wopi_enabled? && wopi_file?(@asset)
url = @asset.get_action_url(current_user, 'embedview')
"<iframe src=\"#{url}\" title=\"DocumentPreview\"></iframe>"
else
render_to_string(partial: 'shared/asset_placeholder.html.erb', locals: { asset: @asset, edit_page: false })
end
respond_to do |format|
format.json do
render json: { html: html }, status: :ok
end
end
end
def file_url
return render_404 unless @asset.file.attached?
@ -300,6 +318,10 @@ class AssetsController < ApplicationController
params.permit(:file)
end
def toggle_view_mode_params
params.require(:asset).permit(:view_mode)
end
def asset_data_type(asset)
return 'wopi' if wopi_file?(asset)
return 'image' if asset.image?

View file

@ -403,6 +403,14 @@ class Asset < ApplicationRecord
return convert_variant_to_base64(medium_preview) if style == :medium
end
def small_card
false
end
def inline_card
!small_card
end
private
def tempdir

View file

@ -1,5 +1,5 @@
<div class="attachment-placeholder pull-left">
<a href="#" class="dataPreviewAction" data-asset-id="<%= asset.id %>" data-preview-type="inline">InLine</a>
<div class="attachment-thumbnail <%= asset.previewable? ? '' : 'no-shadow' %> <%= asset.file.attached? ? asset.file.metadata['asset_type'] : '' %>">
<% if asset.previewable? %>
<%= image_tag asset.medium_preview,

View file

@ -640,6 +640,7 @@ Rails.application.routes.draw do
get 'files/:id/file_url', to: 'assets#file_url', as: 'asset_file_url'
get 'files/:id/download', to: 'assets#download', as: 'asset_download'
get 'files/:id/edit', to: 'assets#edit', as: 'edit_asset'
patch 'files/:id/toggle_view_mode', to: 'assets#toggle_view_mode', as: 'toggle_view_mode'
post 'files/:id/update_image', to: 'assets#update_image',
as: 'update_asset_image'
post 'files/create_wopi_file',

View file

@ -67,4 +67,44 @@ describe AssetsController, type: :controller do
.to(change { Activity.count })
end
end
describe 'GET asset_card' do
let(:action) { get :toggle_view_mode, params: params, format: :json }
let!(:params) do
{ id: asset.id }
end
it do
action
expect(response).to have_http_status 200
end
context 'when small card' do
it do
action
expect(response).to have_http_status 200
# wtf, not working here?, render_to_string is returning nil
# expect(JSON.parse(response.body)['html']).to be_eql 'hellow world'
end
end
context 'when iframe' do
before do
allow_any_instance_of(Asset).to receive(:small_card).and_return(false)
allow_any_instance_of(Asset).to receive(:get_action_url).and_return('fakeurl.com')
allow(controller).to receive(:wopi_enabled?).and_return(true)
allow(controller).to receive(:wopi_file?).and_return(true)
end
it do
action
expect(response).to have_http_status 200
expect(JSON.parse(response.body)['html'])
.to be_eql '<iframe src="fakeurl.com" title="DocumentPreview"></iframe>'
end
end
end
end