-
Notifications
You must be signed in to change notification settings - Fork 396
Mantis #45000: ILIAS 10 - Show confirmation modal on contact unlink #10933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release_10
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,7 +18,11 @@ | |||||||||||
|
|
||||||||||||
| declare(strict_types=1); | ||||||||||||
|
|
||||||||||||
| use ILIAS\Filesystem\Stream\Streams; | ||||||||||||
| use ILIAS\HTTP\Response\ResponseHeader; | ||||||||||||
| use ILIAS\HTTP\Services; | ||||||||||||
| use ILIAS\UI\Factory as UIFactory; | ||||||||||||
| use ILIAS\UI\Renderer as UIRenderer; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Class ilBuddySystemGUI | ||||||||||||
|
|
@@ -39,6 +43,8 @@ class ilBuddySystemGUI | |||||||||||
| protected ilLanguage $lng; | ||||||||||||
| protected Services $http; | ||||||||||||
| private readonly ilGlobalTemplateInterface $main_tpl; | ||||||||||||
| private UIFactory $ui_factory; | ||||||||||||
| private UIRenderer $ui_renderer; | ||||||||||||
|
|
||||||||||||
| public function __construct() | ||||||||||||
| { | ||||||||||||
|
|
@@ -49,6 +55,8 @@ public function __construct() | |||||||||||
| $this->ctrl = $DIC['ilCtrl']; | ||||||||||||
| $this->user = $DIC['ilUser']; | ||||||||||||
| $this->lng = $DIC['lng']; | ||||||||||||
| $this->ui_factory = $DIC->ui()->factory(); | ||||||||||||
| $this->ui_renderer = $DIC->ui()->renderer(); | ||||||||||||
|
|
||||||||||||
| $this->buddyList = ilBuddyList::getInstanceByGlobalUser(); | ||||||||||||
| $this->stateFactory = ilBuddySystemRelationStateFactory::getInstance(); | ||||||||||||
|
|
@@ -68,8 +76,14 @@ public static function initializeFrontend(ilGlobalTemplateInterface $page): void | |||||||||||
| $DIC->language()->loadLanguageModule('buddysystem'); | ||||||||||||
|
|
||||||||||||
| $page->addJavaScript('./assets/js/buddy_system.js'); | ||||||||||||
| $page->addJavaScript('./assets/js/modal.min.js'); | ||||||||||||
|
|
||||||||||||
| $config = new stdClass(); | ||||||||||||
| $config->async_get_unlink_modal_confirmation_html = $DIC->ctrl()->getLinkTargetByClass([ | ||||||||||||
| ilUIPluginRouterGUI::class, | ||||||||||||
| self::class | ||||||||||||
| ], 'asyncGetUnlinkModalConfirmationHtml', '', true, false); | ||||||||||||
|
|
||||||||||||
| $config->http_post_url = $DIC->ctrl()->getFormActionByClass([ | ||||||||||||
| ilUIPluginRouterGUI::class, | ||||||||||||
| self::class | ||||||||||||
|
|
@@ -87,6 +101,32 @@ public static function initializeFrontend(ilGlobalTemplateInterface $page): void | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| public function asyncGetUnlinkModalConfirmationHtmlCommand(): never | ||||||||||||
| { | ||||||||||||
| $confirmation_modal = $this->ui_factory->modal()->interruptive( | ||||||||||||
| $this->lng->txt('confirmation'), | ||||||||||||
| $this->lng->txt('buddy_confirm_unlink'), | ||||||||||||
| '' | ||||||||||||
| ) | ||||||||||||
| ->withActionButtonLabel($this->lng->txt('confirm')); | ||||||||||||
|
|
||||||||||||
| $this->http->saveResponse( | ||||||||||||
| $this->http->response()->withBody( | ||||||||||||
| Streams::ofString( | ||||||||||||
| json_encode([ | ||||||||||||
| "html" => $this->ui_renderer->renderAsync($confirmation_modal), | ||||||||||||
| "signals" => [ | ||||||||||||
| "show" => $confirmation_modal->getShowSignal()->getId(), | ||||||||||||
| "close" => $confirmation_modal->getCloseSignal()->getId() | ||||||||||||
| ] | ||||||||||||
|
Comment on lines
+120
to
+121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try:
Suggested change
|
||||||||||||
| ], JSON_THROW_ON_ERROR) | ||||||||||||
| ) | ||||||||||||
| )->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json') | ||||||||||||
| ); | ||||||||||||
| $this->http->sendResponse(); | ||||||||||||
| $this->http->close(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * @throws RuntimeException | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,15 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| const BuddySystemButton = { | ||||||||||||||||||||
| config: {}, | ||||||||||||||||||||
| unlinkConfirmationModal: { | ||||||||||||||||||||
| signals: { | ||||||||||||||||||||
| show: '', | ||||||||||||||||||||
| close: '', | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| unlinkModalAbortController: null, | ||||||||||||||||||||
| unlinkModal: null, | ||||||||||||||||||||
| unlinkModalReady: false, | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try (maybe it should be name
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| setConfig(config) { | ||||||||||||||||||||
| this.config = config; | ||||||||||||||||||||
|
|
@@ -40,32 +49,84 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| const triggerButton = e.target.closest(triggerSelector); | ||||||||||||||||||||
| const container = triggerButton.closest(`.${this.config.bnt_class}`); | ||||||||||||||||||||
| if (triggerButton.dataset.submitted === 'true') return Promise.resolve(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const values = new FormData(); | ||||||||||||||||||||
| values.append('usr_id', container.dataset.buddyId); | ||||||||||||||||||||
| values.append('action', triggerButton.dataset.action); | ||||||||||||||||||||
| values.append(`cmd[${BuddySystem.config.transition_state_cmd}]`, 1); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return disableButtons(container) | ||||||||||||||||||||
| .then(() => fetch(BuddySystem.config.http_post_url, { | ||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||
| headers: { Accept: 'application/json' }, | ||||||||||||||||||||
| body: values, | ||||||||||||||||||||
| })) | ||||||||||||||||||||
| .then((response) => { | ||||||||||||||||||||
| if (!response.ok) throw new Error('Request failed'); | ||||||||||||||||||||
| return response.json(); | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| .then((data) => processResponse(container, data)) | ||||||||||||||||||||
| .then(() => { | ||||||||||||||||||||
| container.querySelector(toggleSelector)?.focus(); | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| .catch((error) => { | ||||||||||||||||||||
| console.error(error); | ||||||||||||||||||||
| enableButtons(container); | ||||||||||||||||||||
| container.querySelector(toggleSelector)?.focus(); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const widgetClickAction = () => { | ||||||||||||||||||||
| if (triggerButton.dataset.submitted === 'true') return Promise.resolve(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const values = new FormData(); | ||||||||||||||||||||
| values.append('usr_id', container.dataset.buddyId); | ||||||||||||||||||||
| values.append('action', triggerButton.dataset.action); | ||||||||||||||||||||
| values.append(`cmd[${BuddySystem.config.transition_state_cmd}]`, 1); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return disableButtons(container) | ||||||||||||||||||||
| .then(() => fetch(BuddySystem.config.http_post_url, { | ||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||
| headers: { Accept: 'application/json' }, | ||||||||||||||||||||
| body: values, | ||||||||||||||||||||
| })) | ||||||||||||||||||||
| .then((response) => { | ||||||||||||||||||||
| if (!response.ok) throw new Error('Request failed'); | ||||||||||||||||||||
| return response.json(); | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| .then((data) => processResponse(container, data)) | ||||||||||||||||||||
| .then(() => { | ||||||||||||||||||||
| container.querySelector(toggleSelector)?.focus(); | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| .catch((error) => { | ||||||||||||||||||||
| console.error(error); | ||||||||||||||||||||
| enableButtons(container); | ||||||||||||||||||||
| container.querySelector(toggleSelector)?.focus(); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (triggerButton.dataset.action === 'unlink') { | ||||||||||||||||||||
| return showUnlinkConfirmationModal().then(() => widgetClickAction()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return widgetClickAction(); | ||||||||||||||||||||
mBeym marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const showUnlinkConfirmationModal = () => { | ||||||||||||||||||||
| const ensureModal = () => { | ||||||||||||||||||||
| if (this.unlinkModalReady) { | ||||||||||||||||||||
| return Promise.resolve(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return fetch(BuddySystem.config.async_get_unlink_modal_confirmation_html) | ||||||||||||||||||||
| .then((response) => { | ||||||||||||||||||||
| if (!response.ok) throw new Error('Request failed'); | ||||||||||||||||||||
| return response.json(); | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| .then((data) => { | ||||||||||||||||||||
| const wrapper = document.createElement('div'); | ||||||||||||||||||||
| const modalFragment = document.createRange().createContextualFragment(data.html); | ||||||||||||||||||||
| wrapper.appendChild(modalFragment); | ||||||||||||||||||||
| document.body.append(wrapper); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| this.unlinkModal = wrapper.querySelector('dialog'); | ||||||||||||||||||||
| this.unlinkConfirmationModal.signals.show = data.signals.show; | ||||||||||||||||||||
| this.unlinkConfirmationModal.signals.close = data.signals.close; | ||||||||||||||||||||
| this.unlinkModalReady = true; | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try:
Suggested change
|
||||||||||||||||||||
| }); | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ensureModal().then(() => new Promise((resolve) => { | ||||||||||||||||||||
| if (this.unlinkModalAbortController) { | ||||||||||||||||||||
| this.unlinkModalAbortController.abort(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| this.unlinkModalAbortController = new AbortController(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const submitButton = this.unlinkModal.querySelector('input[type="submit"]'); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| submitButton.addEventListener('click', (event) => { | ||||||||||||||||||||
| event.preventDefault(); | ||||||||||||||||||||
| global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.close, {}); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try:
Suggested change
|
||||||||||||||||||||
| resolve(); | ||||||||||||||||||||
| }, { signal: this.unlinkModalAbortController.signal }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| global.jQuery(document).trigger(this.unlinkConfirmationModal.signals.show, {}); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try:
Suggested change
|
||||||||||||||||||||
| })); | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const disableButtons = (container) => new Promise((resolve) => { | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try: