Add main functionality

This commit is contained in:
Jure Grabnar 2019-03-17 19:23:17 +01:00
parent 613843d095
commit b12916b828
7 changed files with 119 additions and 54 deletions

View file

@ -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);
});

View file

@ -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();

View file

@ -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

View file

@ -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

View file

@ -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'%>
<div class="btn-group btn-group-justified" role="group" aria-label="...">
<div class="btn-group" role="group">
<button type="button" class="btn btn-default">Left</button>
</div>
<div class="btn-group" role="group">
<button type="button" class="btn btn-default">Middle</button>
</div>
<div class="btn-group" role="group">
<button type="button" class="btn btn-default">Right</button>
</div>
</div>
<div class="btn-group btn-group-toggle btn-group-justified"
data-toggle="buttons">
<label class="btn btn-default active">
<%= image_tag 'office/Excel-xlsx_20x20x32.png' %>
<input type="radio" name="file_type" value='docx' checked> Active
</label>
<label class="btn btn-default">
<input type="radio" name="file_type" value='xlsx'> Radio
</label>
<label class="btn btn-default">
<input type="radio" name="file_type" value='pptx'> Radio
</label>
</div>
<div class='form-group' id='other-wopi-errors'>
</div>
</div>
<div class="modal-footer">
<%= f.submit t('assets.create_wopi_file.create_document_button'),

View file

@ -2,6 +2,7 @@
<%= render partial: "shared/sidebar", locals: { current_task: @my_module, page: 'task' } %>
<%= render partial: "shared/secondary_navigation" %>
<%= render partial: 'shared/drag_n_drop_overlay' %>
<%= render partial: 'assets/wopi/create_wopi_file_modal' %>
<div class="content-pane">
<div id="results-toolbar">
@ -40,6 +41,8 @@
<span class="fas fa-paperclip"></span>
<span class="hidden-xs"><%= t("my_modules.results.new_asset_result") %></span>
</a>
<%= render partial: "assets/wopi/create_wopi_file_button",
locals: { element_id: @my_module.id, element_type: 'Result' } %>
</div>
<% end %>
</div>
@ -69,6 +72,7 @@
<%= stylesheet_link_tag "handsontable.formula" %>
<%= javascript_include_tag("canvas-to-blob.min") %>
<%= javascript_include_tag "assets/wopi/create_wopi_file" %>
<%= javascript_include_tag "my_modules/results" %>
<%= javascript_include_tag "results/result_texts" %>
<%= javascript_include_tag "results/result_tables" %>

View file

@ -1892,6 +1892,9 @@ en:
text_field_label: 'Document name & type'
text_field_placeholder: 'New Document'
create_document_button: 'Create Document'
errors:
forbidden: 'You do not have permission to add files.'
not_found: 'Element not found.'
atwho:
no_results: "No results found"
users: