diff --git a/app/assets/javascripts/protocols/steps.js.erb b/app/assets/javascripts/protocols/steps.js.erb index 8f96b177b..c9dbcd9e2 100644 --- a/app/assets/javascripts/protocols/steps.js.erb +++ b/app/assets/javascripts/protocols/steps.js.erb @@ -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(); } /* diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index ba7a45ffe..e8dab1b70 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -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') + "" + 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? diff --git a/app/models/asset.rb b/app/models/asset.rb index f6ec993ec..887268fa7 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -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 diff --git a/app/views/shared/_asset_placeholder.html.erb b/app/views/shared/_asset_placeholder.html.erb index a9b2ace1d..34bf8544a 100644 --- a/app/views/shared/_asset_placeholder.html.erb +++ b/app/views/shared/_asset_placeholder.html.erb @@ -1,5 +1,5 @@
- + InLine
<% if asset.previewable? %> <%= image_tag asset.medium_preview, diff --git a/config/routes.rb b/config/routes.rb index 298b2ead2..8801b7687 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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', diff --git a/spec/controllers/assets_controller_spec.rb b/spec/controllers/assets_controller_spec.rb index a4a853bb8..5a08b756c 100644 --- a/spec/controllers/assets_controller_spec.rb +++ b/spec/controllers/assets_controller_spec.rb @@ -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 '' + end + end + end end