Skip to content

Commit e5e0033

Browse files
committed
feat(media): enhance media browser with configurable endpoints and error handling
Improves the media browser functionality with better flexibility and error handling: - Configurable endpoints: Added support for custom media fetch URLs via `data-media-fetch` attribute - Enhanced error handling: Added error callback to properly close modal on request failures - Better loading states: Improved visual feedback during media loading operations - Validation: Added endpoint URL validation to prevent broken requests - Modal management: Better control over modal show/hide timing - Version Bump: Incremented from `1.6.8` to `1.6.9` The changes allow for more flexible media integration across different contexts while providing a more robust user experience.
1 parent dd2e891 commit e5e0033

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

js/admin.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -747,18 +747,25 @@
747747
const isEditor = target instanceof HTMLElement;
748748
const isField = !isEditor && target && target.mode === 'field';
749749

750+
const url = isEditor ? (target.dataset.fetch || null) : target.url;
751+
if (!url?.length) {
752+
console.error("Missing browser endpoint.");
753+
return;
754+
}
755+
750756
const $modal = $("#media-modal");
751757
const $browser = $("#media-browser");
752758
const $insert = $("#media-insert-btn");
753759

754-
$browser.empty().html('<div class="text-center py-5 text-muted">'+csk.i18n.default.loading+'...</div>');
755-
756-
$insert.prop('disabled', true);
757-
$("#media-modal .media-item").removeClass("selected");
758-
759760
// Load existing media
760-
csk.ajax.request(csk.config.adminURL + "/ajax/media", {
761+
csk.ajax.request(url, {
761762
type: "GET",
763+
beforeSend: function () {
764+
$browser.empty().html('<div class="text-center py-5 text-muted">'+csk.i18n.default.loading+'</div>');
765+
766+
$insert.prop('disabled', true);
767+
$("#media-modal .media-item").removeClass("selected");
768+
},
762769
onSuccess: function(data, textStatus, jqXHR) {
763770
if (!data?.results?.length) {
764771
$browser.html('<div class="text-center py-5 text-muted">'+csk.i18n.default.no_data+'</div>');
@@ -784,6 +791,13 @@
784791
$(this).addClass("selected");
785792
$insert.trigger("click");
786793
});
794+
795+
// Render browser
796+
$modal.modal("show");
797+
798+
},
799+
onError: function() {
800+
$modal.modal("hide");
787801
}
788802
});
789803

@@ -826,8 +840,6 @@
826840

827841
$modal.modal("hide");
828842
});
829-
830-
$modal.modal("show");
831843
},
832844

833845
/**
@@ -911,7 +923,8 @@
911923
mode: el.dataset.mediaMode || 'field',
912924
target: el.dataset.mediaTarget || null,
913925
preview: el.dataset.mediaPreview || null,
914-
size: el.dataset.mediaSize || null
926+
size: el.dataset.mediaSize || null,
927+
url: el.dataset.mediaFetch || null,
915928
});
916929
});
917930

0 commit comments

Comments
 (0)