Skip to content

Commit

Permalink
improvement: allow to view uploaded files by {fileupload} inline in v…
Browse files Browse the repository at this point in the history
…iew_selector
  • Loading branch information
interduo committed Dec 15, 2023
1 parent 53ce4bf commit 9bb5678
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
7 changes: 5 additions & 2 deletions js/lms-ui-fileupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* $Id$
*/

function lmsFileUpload(elemid, formid, new_item_custom_content) {
function lmsFileUpload(elemid, formid, new_item_custom_content, view_selector) {
var elem = $("#" + elemid);
var formelem = typeof(formid) != 'undefined' ? $('#' + formid) : $(this).closest("form");
var formdata = new FormData(formelem.get(0));
Expand Down Expand Up @@ -89,11 +89,14 @@ function lmsFileUpload(elemid, formid, new_item_custom_content) {
track: true
});
fileListItem.find('.file-view').click(function() {
lmsFileView(files[key]);
lmsFileView(files[key], view_selector);
});
fileListItem.find('.file-delete').click(function() {
$(this).closest('.fileupload-file').remove();
});
if (typeof(view_selector) !== 'undefined') {
lmsFileView(files[key], view_selector);
}
});
}
},
Expand Down
15 changes: 10 additions & 5 deletions js/lms-ui-fileview.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* $Id$
*/

function lmsFileView(file) {
function lmsFileView(file, view_selector) {
if (typeof(file) == 'undefined') {
alert("404, Error: no file to view");
return;
Expand All @@ -49,14 +49,19 @@ function lmsFileView(file) {
default:
object = $('<object/>').attr({
data: objUrl,
type: file.type
type: file.type,
height: "100%",
width: "100%",
});
content = $('<div/>').append(object);
dialogOptions.height = $(window).height() * 0.8;
break;
}

$(content).dialog(dialogOptions);

url.revokeObjectURL(objUrl);
if (typeof(view_selector) === 'undefined') {
$(content).dialog(dialogOptions);
url.revokeObjectURL(objUrl);
} else {
$( '#' + view_selector ).html(object).removeClass('hidden').addClass('attachment-loaded');
}
}
13 changes: 7 additions & 6 deletions lib/SmartyPlugins/LMSSmartyPlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public static function fileUploadFunction(array $params, $template)
$form = isset($params['form']) ? $params['form'] : null;
$accept = !empty($params['accept']) ? $params['accept'] : null;
$multiple = isset($params['multiple']) ? ConfigHelper::checkValue($params['multiple']) : true;

$view_selector = empty($params['view_selector']) ? null : $params['view_selector'];
$image_resize = !isset($params['image_resize']) || !empty($params['image_resize']);

// special treatment of file upload errors marked in error associative array
Expand Down Expand Up @@ -458,11 +458,12 @@ public static function fileUploadFunction(array $params, $template)
. '" ' . ($form ? ' form="' . $form . '"' : '') . '>
</div>';
$result .= '<script>
$(function() {
new lmsFileUpload(
"' . $id . '", "' . ($form ?: '') . '"'
. ', "' . (strlen($new_item_custom_content) ? base64_encode($new_item_custom_content) : '') . '");
});
$(function() {
new lmsFileUpload("' . $id . '", "' . ($form ?: '') . '"' . ', "'
. (strlen($new_item_custom_content) ? base64_encode($new_item_custom_content) : '')
. '", "' . $view_selector . '"
)
});
</script>';

return $result;
Expand Down

0 comments on commit 9bb5678

Please sign in to comment.