From b12916b828acbe16b30aa68380e97ec702dff62f Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Sun, 17 Mar 2019 19:23:17 +0100 Subject: [PATCH] Add main functionality --- .../assets/wopi/create_wopi_file.js | 58 ++++++++-------- .../javascripts/my_modules/results.js.erb | 2 + app/controllers/assets_controller.rb | 66 +++++++++++++++---- app/helpers/file_icons_helper.rb | 12 ++++ .../wopi/_create_wopi_file_modal.html.erb | 28 ++++---- app/views/my_modules/results.html.erb | 4 ++ config/locales/en.yml | 3 + 7 files changed, 119 insertions(+), 54 deletions(-) diff --git a/app/assets/javascripts/assets/wopi/create_wopi_file.js b/app/assets/javascripts/assets/wopi/create_wopi_file.js index df32ee4a2..9ce511490 100644 --- a/app/assets/javascripts/assets/wopi/create_wopi_file.js +++ b/app/assets/javascripts/assets/wopi/create_wopi_file.js @@ -1,6 +1,8 @@ +// Opens create wopi modal on the click of the button function applyCreateWopiFileCallback() { $(".create-wopi-file-btn").off().on('click', function(e){ var $modal = $('#new-office-file-modal'); + $($modal).find('form').clearFormErrors(); // Append element info to which the new file will be attached $modal.find('#element_id').val($(this).data('id')); @@ -11,34 +13,32 @@ function applyCreateWopiFileCallback() { }); } - -(function(global) { - function initCreateWopiFileModal() { - console.log('jeje') - //Click on cancel button - $("#new-office-file-modal form") - .submit(function(e) { - //var $form = $(this).closest("form"); - $.ajax({ - url: $btn.data('href'), - method: 'GET', - success: function(data) { - var $form = $(data.html); - animateSpinner(null, false); - $('#results').prepend($form); - formAjaxResultText($form); - Results.initCancelFormButton($form, initNewReslutText); - Results.toggleResultEditButtons(false); - TinyMCE.refresh(); - TinyMCE.highlight(); - $('#result_name').focus(); - }, - error: function() { - animateSpinner(null, false); - initNewReslutText(); - } - }) +// Show errors on create wopi modal +function initCreateWopiFileModal() { + // Ajax actions + $("#new-office-file-modal form") + .on('ajax:success', function(ev, data) { + window.open(data.edit_url, '_blank'); + //location.reload(); }) - } + .on('ajax:error', function(ev, response) { + $(this).clearFormErrors(); + var element, msg; + + if (response.status == 400) { + element = $(this).find('#new-wopi-file-name'); + msg = response.responseJSON.message.file.toString(); + } else if (response.status == 403) { + element = $(this).find('#other-wopi-errors'), + msg = I18n.t('assets.create_wopi_file.errors.forbidden'); + } else if (response.status == 404) { + element = $(this).find('#other-wopi-errors'), + msg = I18n.t('assets.create_wopi_file.errors.not_found'); + } + renderFormError(undefined, element, msg); + }); +} + +$(document).ready(function() { initCreateWopiFileModal(); -})(window); +}); diff --git a/app/assets/javascripts/my_modules/results.js.erb b/app/assets/javascripts/my_modules/results.js.erb index 9478b5fdc..2c0be5951 100644 --- a/app/assets/javascripts/my_modules/results.js.erb +++ b/app/assets/javascripts/my_modules/results.js.erb @@ -16,6 +16,7 @@ initHandsOnTables($(document)); _expandAllResults(); applyCollapseLinkCallBack(); + applyCreateWopiFileCallback(); Comments.initialize(); @@ -185,6 +186,7 @@ initFormSubmitLinks($(this)); ResutlAssets.applyEditResultAssetCallback(); applyCollapseLinkCallBack(); + applyCreateWopiFileCallback(); toggleResultEditButtons(true); FilePreviewModal.init(); Comments.initialize(); diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 19e3db21c..4c104ae05 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -169,24 +169,60 @@ class AssetsController < ApplicationController formats: :html ) } + end + end + end + # POST: create_wopi_file_path def create_wopi_file - # ASSOC: (stepasset, resultasset), - # ASSOC_ID: bla, - # TYPE OF DOC: PDF/DOC/XML - # name of file - # retrieve - #file = Paperclip.io_adapters.for(StringIO.new()) - #file.original_filename = 'test.docx' - #file.content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' - #asset = Asset.new(file: file, created_by: User.first, file_present: true) - #step_asset = StepAsset.create!(step: Step.last, asset: asset) + # Presence validation + create_wopi_params + + # File type validation + render_403 && return unless ['docx', 'xlsx', 'pptx'].include?(params[:file_type]) + + # Asset validation + file = Paperclip.io_adapters.for(StringIO.new()) + file.original_filename = "#{params[:file_name]}.#{params[:file_type]}" + file.content_type = wopi_content_type(params[:file_type]) + asset = Asset.new(file: file, created_by: current_user, file_present: true) - #redirect_to edit_asset_url(step_asset.asset_id) - # respond_to do |format| format.json do - render json: { 'bla': 'he' } + render json: { + error: true, + message: asset.errors, + }, status: 400 + end + end and return unless asset.valid? + + if params[:element_type] == 'Step' + step = Step.find(params[:element_id].to_i) + render_403 && return unless can_manage_protocol_in_module?(step.protocol) || + can_manage_protocol_in_repository?(step.protocol) + step_asset = StepAsset.create!(step: step, asset: asset) + + edit_url = edit_asset_url(step_asset.asset_id) + elsif params[:element_type] == 'Result' + my_module = MyModule.find(params[:element_id].to_i) + render_403 and return unless can_manage_module?(my_module) + + result = Result.create(name: file.original_filename, + my_module: my_module, + user: current_user) + result_asset = ResultAsset.create!(result: result, asset: asset) + + edit_url = edit_asset_url(result_asset.asset_id) + else + render_404 + end + + respond_to do |format| + format.json do + render json: { + success: true, + edit_url: edit_url + } end end end @@ -244,6 +280,10 @@ class AssetsController < ApplicationController url + wd_params end + def create_wopi_params + params.require([:element_type, :element_id, :file_type]) + end + def asset_params params.permit( :file diff --git a/app/helpers/file_icons_helper.rb b/app/helpers/file_icons_helper.rb index f531363f9..4e6ee3eef 100644 --- a/app/helpers/file_icons_helper.rb +++ b/app/helpers/file_icons_helper.rb @@ -86,4 +86,16 @@ module FileIconsHelper t('result_assets.wopi_edit_file', app: app) end end + + # Returns correct content type for given extension + def wopi_content_type(extension) + case extension + when 'docx' + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' + when 'xlsx' + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + when 'pptx' + 'application/vnd.openxmlformats-officedocument.presentationml.presentation' + end + end end diff --git a/app/views/assets/wopi/_create_wopi_file_modal.html.erb b/app/views/assets/wopi/_create_wopi_file_modal.html.erb index 310f9b104..4ca26cfe2 100644 --- a/app/views/assets/wopi/_create_wopi_file_modal.html.erb +++ b/app/views/assets/wopi/_create_wopi_file_modal.html.erb @@ -16,24 +16,28 @@ <%= hidden_field_tag :element_id, '' %> <%= hidden_field_tag :element_type, '' %> - <%= f.text_field :name, + <%= f.text_field :file_name, label: t('assets.create_wopi_file.text_field_label'), autofocus: true, placeholder: t('assets.create_wopi_file.text_field_placeholder'), id: 'new-wopi-file-name'%> -
-
- -
-
- -
-
- -
-
+
+ + + +
+
+