Skip to content

Commit

Permalink
Refactor JS code related to listings
Browse files Browse the repository at this point in the history
  • Loading branch information
Usbac committed Feb 24, 2024
1 parent 328a001 commit 7c087ee
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
9 changes: 0 additions & 9 deletions app/views/admin/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,5 @@
Listing.setNextPageUrl(<?= js("$current_path/page") ?>);
Listing.loadNextPage();
});

function listHandleResponse(res) {
if (res.success) {
document.querySelector('dialog[open]')?.close();
Dropdown.close();
Listing.setNextPage(1);
Listing.loadNextPage();
}
}
</script>
<?php $this->sectionEnd() ?>
2 changes: 1 addition & 1 deletion app/views/admin/partials/lists/links.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class="danger"
if (confirm(<?= e(js(t('delete_confirm', false))) ?>.sprintf(<?= e(js($link['title'])) ?>))) {
Form.send('/admin/links/remove/' + <?= e(js($link['id'])) ?>, null, null, {
csrf: <?= e(js($this->csrfToken())) ?>,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}
"
><?= $this->include('icons/trash.svg') ?> <?= t('delete') ?></div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/partials/lists/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class="danger"
if (confirm(<?= e(js(t('delete_confirm', false))) ?>.sprintf(<?= e(js($page['title'])) ?>))) {
Form.send('/admin/pages/remove/' + <?= e(js($page['id'])) ?>, null, null, {
csrf: <?= e(js($this->csrfToken())) ?>,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}
"
><?= $this->include('icons/trash.svg') ?> <?= t('delete') ?></div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/partials/lists/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class="danger"
if (confirm(<?= e(js(t('delete_confirm', false))) ?>.sprintf(<?= e(js($post['title'])) ?>))) {
Form.send('/admin/posts/remove/' + <?= e(js($post['id'])) ?>, null, null, {
csrf: <?= e(js($this->csrfToken())) ?>,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}
"
><?= $this->include('icons/trash.svg') ?> <?= t('delete') ?></div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/partials/lists/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class="danger"
if (confirm(<?= e(js(t('delete_confirm', false))) ?>.sprintf(<?= e(js($tag['name'])) ?>))) {
Form.send('/admin/tags/remove/' + <?= e(js($tag['id'])) ?>, null, null, {
csrf: <?= e(js($this->csrfToken())) ?>,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}
"
><?= $this->include('icons/trash.svg') ?> <?= t('delete') ?></div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/partials/lists/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class="danger"
if (confirm(<?= e(js(t('delete_confirm', false))) ?>.sprintf(<?= e(js($user['name'])) ?>))) {
Form.send('/admin/users/remove/' + <?= e(js($user['id'])) ?>, null, null, {
csrf: <?= e(js($this->csrfToken())) ?>,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}
"
><?= $this->include('icons/trash.svg') ?> <?= t('delete') ?></div>
Expand Down
14 changes: 7 additions & 7 deletions app/views/admin/partials/media_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function uploadFile() {
Form.send('/admin/media/upload' + window.location.search, 'file-form', get('#file-form button'), {
csrf: csrf_token,
}).then(res => {
listHandleResponse(res);
Listing.handleResponse(res);
get('#input-file').value = '';
});
}
Expand All @@ -124,7 +124,7 @@ function deleteFile(i) {
if (confirm(<?= js(t('delete_confirm', false)) ?>.sprintf(file_name))) {
Form.send('/admin/media/remove?path=' + path + '/' + file_name, null, null, {
csrf: csrf_token,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}
}

Expand All @@ -136,7 +136,7 @@ function openEditFileDialog(i) {
function editFile() {
Form.send('/admin/media/save?path=' + path + '/' + file_name, 'edit-dialog', null, {
csrf: csrf_token,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}

function openFolderDialog() {
Expand All @@ -147,7 +147,7 @@ function openFolderDialog() {
function createFolder() {
Form.send('/admin/media/createFolder' + window.location.search, 'folder-dialog', null, {
csrf: csrf_token,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}

function openMoveDialog(i) {
Expand All @@ -158,7 +158,7 @@ function openMoveDialog(i) {
function moveFile() {
Form.send('/admin/media/move?path=' + path + '/' + file_name, 'move-dialog', null, {
csrf: csrf_token,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}

function openDuplicateDialog(i) {
Expand All @@ -169,7 +169,7 @@ function openDuplicateDialog(i) {
function duplicateFile() {
Form.send('/admin/media/duplicate?path=' + path + '/' + file_name, 'duplicate-dialog', null, {
csrf: csrf_token,
}).then(res => listHandleResponse(res));
}).then(res => Listing.handleResponse(res));
}

function copyPath(path) {
Expand Down Expand Up @@ -217,7 +217,7 @@ function downloadAll() {
Snackbar.show(res.errors[0], false);
}

listHandleResponse(res);
Listing.handleResponse(res);
})
.catch(() => Snackbar.show(LANG.unexpected_error, false))
.finally(() => document.body.style.cursor = 'default');
Expand Down
9 changes: 9 additions & 0 deletions public/assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ class Listing {
get('#main-listing > svg')?.remove();
});
}

static handleResponse(res) {
if (res.success) {
document.querySelector('dialog[open]')?.close();
Dropdown.close();
this.setNextPage(1);
this.loadNextPage();
}
}
}

class Dropdown {
Expand Down

0 comments on commit 7c087ee

Please sign in to comment.