Add fileUtils for sizeCheck, add url to textarea data attribute

This commit is contained in:
Urban Rotnik 2020-09-30 12:10:49 +02:00
parent dc0072e707
commit fc8704b520
3 changed files with 29 additions and 16 deletions

View file

@ -0,0 +1,14 @@
/* eslint no-unused-vars: "off" */
/* global I18n GLOBAL_CONSTANTS HelperModule*/
function validateFileSize(file, showErrorMessage = false) {
var validSize = true;
if (file.size > GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB * 1024 * 1024) {
validSize = false;
}
if (!validSize && showErrorMessage) {
HelperModule.flashAlertMsg(I18n.t('general.file.size_exceeded', { file_size: GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB }), 'danger');
}
return validSize;
}

View file

@ -1,7 +1,7 @@
/* eslint no-underscore-dangle: "off" */
/* eslint no-use-before-define: "off" */
/* eslint no-restricted-syntax: ["off", "BinaryExpression[operator='in']"] */
/* global tinymce I18n GLOBAL_CONSTANTS HelperModule*/
/* global tinymce I18n HelperModule validateFileSize */
(function() {
'use strict';
@ -25,23 +25,18 @@
let files = $('#tinymce_current_upload')[0].files;
Array.from(files).forEach(file => formData.append('files[]', file, file.name));
$.post({
url: '/tiny_mce_assets',
url: textAreaElement.data('tinymce-asset-path'),
data: formData,
processData: false,
contentType: false,
beforeSend: function(xhr) {
let sizeLimit = false;
Array.from(files).forEach(file => {
if (file.size > GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB * 1024 * 1024) {
sizeLimit = true;
Array.from(files).every(file => {
if (!validateFileSize(file, true)) {
xhr.abort();
return false;
}
});
if (sizeLimit) {
HelperModule.flashAlertMsg(I18n.t('general.file.size_exceeded', { file_size: GLOBAL_CONSTANTS.FILE_MAX_SIZE_MB }), 'danger');
xhr.abort();
}
},
success: function(data) {
handleResponse(data);
@ -69,10 +64,9 @@
}
function buildHTML(image) {
var imgstr = "<img src='" + image.url + "'";
imgstr += " data-mce-token='" + image.token + "'";
imgstr += " alt='description-" + image.token + "' />";
return imgstr;
return `<img src="${image.url}"
data-mce-token="${image.token}"
alt="description-${image.token}" />`;
}
// Create hidden field for images

View file

@ -254,7 +254,12 @@ module BootstrapFormHelper
# Returns <textarea> helper tag for tinyMCE editor
def tiny_mce_editor(name, options = {})
options.merge!(cols: 120, rows: 10)
options.deep_merge!(cols: 120,
rows: 10,
data: {
tinymce_asset_path:
Rails.application.routes.url_helpers.tiny_mce_assets_path
})
text_area(name, options)
end
end