diff --git a/src/media/kunena/core/js/upload.main.js b/src/media/kunena/core/js/upload.main.js index cff9d7a316..fb3882b35c 100644 --- a/src/media/kunena/core/js/upload.main.js +++ b/src/media/kunena/core/js/upload.main.js @@ -1,158 +1,25 @@ /** + * Kunena Component + * @package Kunena.Media + * + * @copyright Copyright (C) 2008 - @currentyear@ Kunena Team. All rights reserved. + * @license https://www.gnu.org/copyleft/gpl.html GNU/GPL + * @link https://www.kunena.org - **/ + **/ jQuery(function ($) { 'use strict'; - - $.widget('blueimp.fileupload', $.blueimp.fileupload, { - options: { - // The maximum width of resized images: - imageMaxWidth: Joomla.getOptions('com_kunena.imageWidth'), - // The maximum height of resized images: - imageMaxHeight: Joomla.getOptions('com_kunena.imageHeight') - } - }); - -// Modified insertInMessage function to handle both regular and protected attachments - function insertInMessage(attachid, filename, button) { - // Ensure we have a valid attachment ID - if (!attachid && button) { - const data = button.data(); - attachid = data.file_id || data.result?.data?.id || data.id; - } - - // Ensure we have a valid filename - if (!filename && button) { - const data = button.data(); - filename = data.name || data.result?.data?.filename; - } - - // Only proceed if we have both id and filename - if (attachid && filename) { - const content = ' [attachment=' + attachid + ']' + filename + '[/attachment]'; - - if (Joomla.getOptions('com_kunena.ckeditor_config') !== undefined) { - CKEDITOR.instances.message.insertText(content); - } else { - sceditor.instance(document.getElementById('message')).insert(content); - } - - if (button !== undefined) { - button.removeClass('btn-primary') - .addClass('btn-success') - .html(Joomla.getOptions('com_kunena.icons.upload') + ' ' + - Joomla.Text._('COM_KUNENA_EDITOR_IN_MESSAGE')); - } - } - } - jQuery.fn.extend({ - insertAtCaret: function (myValue) { - return this.each(function (i) { - if (document.selection) { - //For browsers like Internet Explorer - this.focus(); - //noinspection JSUnresolvedconstiable - let sel; - sel = document.selection.createRange(); - sel.text = myValue; - this.focus(); - } else if (this.selectionStart || this.selectionStart === '0') { - //For browsers like Firefox and Webkit based - const startPos = this.selectionStart; - const endPos = this.selectionEnd; - const scrollTop = this.scrollTop; - this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length); - this.focus(); - this.selectionStart = startPos + myValue.length; - this.selectionEnd = startPos + myValue.length; - this.scrollTop = scrollTop; - } else { - this.value += myValue; - this.focus(); - } - }) - } - }); - - var fileCount = null; - var filesedit = null; - var fileeditinline = 0; - - $('#set-secure-all').on('click', function (e) { - e.preventDefault(); - - const child = $('#kattach-list').find('input'); - const filesidtosetprivate = []; - const $this = $(this); - - child.each(function (i, el) { - const elem = $(el); - - if (!elem.attr('id').match("[a-z]{8}")) { - const fileid = elem.attr('id').match("[0-9]{1,8}"); - filesidtosetprivate.push(fileid); - } - }); - - if (filesidtosetprivate.length !== 0) { - $.ajax({ - url: Joomla.getOptions('com_kunena.kunena_upload_files_set_private') + '&files_id=' + JSON.stringify(filesidtosetprivate), - type: 'POST' - }) - .done(function (data) { - // Update all individual private buttons - $('#files button').each(function() { - const $btn = $(this); - if ($btn.html().includes(Joomla.Text._('COM_KUNENA_EDITOR_INSERT_PRIVATE_ATTACHMENT'))) { - $btn.removeClass('btn-primary') - .addClass('btn-success') - .prop('disabled', true) - .html(Joomla.getOptions('com_kunena.icons.secure') + ' ' + - Joomla.Text._('COM_KUNENA_EDITOR_ATTACHMENT_IS_SECURED')); - - // Hide the corresponding insert button in the same container - $btn.siblings('button').each(function() { - const $siblingBtn = $(this); - if ($siblingBtn.html().includes(Joomla.Text._('COM_KUNENA_EDITOR_INSERT')) || - $siblingBtn.html().includes(Joomla.Text._('COM_KUNENA_EDITOR_IN_MESSAGE'))) { - $siblingBtn.hide(); - } - }); - } - }); - - // Update the set-secure-all button - $this.removeClass('btn-primary') - .addClass('btn-success') - .prop('disabled', true) - .html(Joomla.getOptions('com_kunena.icons.secure') + ' ' + - Joomla.Text._('COM_KUNENA_EDITOR_ATTACHMENTS_ARE_SECURED')); - - // Hide both insert and insert-all buttons - $('button').each(function() { - const $btn = $(this); - if ($btn.html().includes(Joomla.Text._('COM_KUNENA_EDITOR_INSERT')) || - $btn.html().includes(Joomla.Text._('COM_KUNENA_EDITOR_IN_MESSAGE')) || - $btn.attr('id') === 'insert-all') { - $btn.hide(); - } - }); - - // Explicitly hide the insert-all button - $('#insert-all').hide(); - }) - .fail(function () { - //TODO: handle the error of ajax request - }); - } -}); - + // Single declaration of global variables at the top + let fileCount = 0; + let filesedit = null; + let fileeditinline = 0; + // Add click handler for remove-all button $('#remove-all').on('click', function (e) { e.preventDefault(); @@ -251,9 +118,7 @@ jQuery(function ($) { // Remove any alert messages $('#alert_max_file').remove(); }); - - - $('#insert-all').on('click', function (e) { + $('#insert-all').on('click', function (e) { e.preventDefault(); const child = $('#kattach-list').find('input'); @@ -340,481 +205,468 @@ jQuery(function ($) { filesedit = null; }); - - // Modified setPrivateButton to properly handle private attachments - const setPrivateButton = $('') - .insertBefore($('#files')); - - $('#form_submit_button').prop('disabled', false); - return false; - } - - fileCount = fileCountTotal; -}) -.bind('fileuploadchange', function (e, data) { - $('#form_submit_button').prop('disabled', true); - $('#remove-all').show(); - $('#insert-all').show(); - - if (Joomla.getOptions('com_kunena.privateMessage') == 1) { - $('#set-secure-all').show(); - } - - const fileCountTotal = Object.keys(data['files']).length + fileCount; - - if (fileCountTotal > Joomla.getOptions('com_kunena.kunena_upload_files_maxfiles')) { - $('') - .insertBefore($('#files')); - - $('#form_submit_button').prop('disabled', false); - return false; - } - - fileCount = fileCountTotal; -}) -.on('fileuploadadd', function (e, data) { - $('#progress-bar').css('width', '0%'); - $('#progress').show(); - - data.context = $('
').appendTo('#files'); - - $.each(data.files, function (index, file) { - const node = $('

').append($('').text(file.name)); - if (!index) { - node.append('
'); } - node.appendTo(data.context); }); -}) -.on('fileuploadprocessalways', function (e, data) { - const index = data.index, - file = data.files[index], - node = $(data.context.children()[index]); - - if (file.preview) { - node.prepend('
').prepend(file.preview); - } - - if (file.error) { - node.append('
') - .append($('').text(file.error)); - } - - if (index + 1 === data.files.length) { - data.context.find('button.btn-primary') - .text(Joomla.Text._('COM_KUNENA_UPLOADED_LABEL_UPLOAD_BUTTON')) - .prop('disabled', !!data.files.error); - } -}) -.on('fileuploaddone', function (e, data) { - const progress = parseInt(data.loaded / data.total * 100, 10); - $('.progress-bar').css('width', progress + '%') - .prop('aria-valuenow', progress); - - const link = $('').attr('target', '_blank') - .prop('href', data.result.location); - - data.context.find('span').wrap(link); - - if (data.result.success === true) { + const removeButton = $('

').insertBefore($('#files')); + $('#form_submit_button').prop('disabled', false); + return false; + } + fileCount = fileCountTotal; + }).bind('fileuploadchange', function (e, data) { + $('#form_submit_button').prop('disabled', true); + $('#remove-all').show(); + $('#insert-all').show(); if (Joomla.getOptions('com_kunena.privateMessage') == 1) { - data.context.append(setPrivateButton.clone(true).data(data)); + $('#set-secure-all').show(); } - - data.context.append(removeButton.clone(true).data(data)); - } else if (data.result.message) { - $('#form_submit_button').prop('disabled', false); - data.uploaded = false; - - // Add remove button and show error message - data.context.append(removeButton.clone(true).data(data)); - - if (data.result.message.length > 0) { - const error = $('