Skip to content

Commit

Permalink
Merge pull request #601 from oat-sa/feature/AUT-3790/resource-manager…
Browse files Browse the repository at this point in the history
…-deletion-hook

Feature/AUT-3790/resource manager deletion hook
  • Loading branch information
jsconan authored Sep 2, 2024
2 parents 912df6c + e01562b commit 9a7a54c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
41 changes: 39 additions & 2 deletions src/resourcemgr/fileSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2015 (original work) Open Assessment Technologies SA;
* Copyright (c) 2015-2024 (original work) Open Assessment Technologies SA;
*
*/

Expand All @@ -31,8 +31,10 @@ import feedback from 'ui/feedback';
import context from 'context';
import 'ui/uploader';
import updatePermissions from './util/updatePermissions';
import loggerFactory from 'core/logger';

let ns = 'resourcemgr';
const ns = 'resourcemgr';
const logger = loggerFactory(`ui/${ns}`);

function shortenPath(path) {
let tokens = path.replace(/\/$/, '').split('/');
Expand Down Expand Up @@ -161,6 +163,41 @@ export default function (options) {
});

//delete a file
$(parentSelector)
.off('click', '.files li a.delete')
.on('click', '.files li a.delete', function (e) {
// This function replaces ui/deleter and must follow the same logic.
// The main difference is that it insert a confirmation dialog before deleting the file.
e.preventDefault();
const $elt = $(e.target);
if ($elt.hasClass(options.disableClass)) {
return;
}

const $target = $elt.closest('li');
const path = $target.data('file');
const hooks = [];

if (options.hooks && 'function' === typeof options.hooks.deleteFile) {
hooks.push(options.hooks.deleteFile(path));
}

Promise.all(hooks)
.then(() => {
$(this).trigger('delete.deleter', [$target]);
$target.trigger('delete', [false]);

$target.detach();
$target.remove();

$fileContainer.trigger('deleted.deleter', [$target]);
})
.catch(err => {
if (err instanceof Error) {
logger.error(err);
}
});
});
$fileContainer.on('delete.deleter', function (e, $target) {
let path,
params = {};
Expand Down
14 changes: 7 additions & 7 deletions src/resourcemgr/tpl/fileSelect.tpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{#each files}}
<li data-type="{{type}}"
data-file="{{uri}}"
data-display="{{display}}"
data-mime="{{mime}}"
data-size="{{size}}"
data-url="{{viewUrl}}"
<li data-type="{{type}}"
data-file="{{uri}}"
data-display="{{display}}"
data-mime="{{mime}}"
data-size="{{size}}"
data-url="{{viewUrl}}"
{{#if permissions.download}} data-download="true" {{/if}}
{{#if permissions.preview}} data-preview="true" {{/if}}
{{#if permissions.read}} data-select="true" {{/if}}
Expand All @@ -25,7 +25,7 @@
<a href="{{downloadUrl}}" download="{{name}}" target="_blank" class="tlb-button-off download" title="{{__ 'Download this file'}}"><span class="icon-download"></span></a>
{{/if}}
{{#if permissions.delete }}
<a href="#" class="tlb-button-off" title="{{__ 'Remove this file'}}" data-delete=":parent li"><span class="icon-bin"></span></a>
<a href="#" class="tlb-button-off delete" title="{{__ 'Remove this file'}}"><span class="icon-bin"></span></a>
{{/if}}
</span>
{{/if}}
Expand Down

0 comments on commit 9a7a54c

Please sign in to comment.