Skip to content
Snippets Groups Projects
Commit 5a30fa24 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

transfer changes to dialog buttons from original button, fixes #4745

Closes #4745

Merge request studip/studip!3540
parent efb19ff8
No related branches found
No related tags found
No related merge requests found
...@@ -21,31 +21,40 @@ var dialog_margin = 0; ...@@ -21,31 +21,40 @@ var dialog_margin = 0;
/** /**
* Extract buttons from given element. * Extract buttons from given element.
*
* @param {Element} element
* @param {function|null} callback
*/ */
function extractButtons(element) { function extractButtons(element, callback = null) {
var buttons = {}; const buttons = [];
$('[data-dialog-button]', element) $('[data-dialog-button]', element)
.hide() .hide()
.find('a,button') .find('a,button')
.addBack() .addBack()
.filter('a,button') .filter('a,button')
.each(function() { .each(function() {
var label = $(this).text(); const id = $(this).uniqueId().attr('id');
var cancel = $(this).is('.cancel'); const label = $(this).text();
var index = cancel ? 'cancel' : label; const cancel = $(this).is('.cancel');
var classes = $(this).attr('class') || ''; const index = cancel ? 'cancel' : label;
var name = $(this).attr('name') || ''; let classes = $(this).attr('class') || '';
var disabled = $(this).is(':disabled'); const name = $(this).attr('name') || '';
const disabled = $(this).is(':disabled');
classes = classes.replace(/\bbutton\b/, '').trim(); classes = classes.replace(/\bbutton\b/, '').trim();
buttons[index] = { buttons.push({
id: id,
text: label, text: label,
class: classes, class: classes,
name: name, name: name,
disabled: disabled, disabled: disabled,
click: () => this.click() click: () => this.click()
}; });
if (callback !== null) {
callback(this, index);
}
}); });
return buttons; return buttons;
...@@ -403,13 +412,6 @@ Dialog.show = function(content, options = {}) { ...@@ -403,13 +412,6 @@ Dialog.show = function(content, options = {}) {
$('head').append(scripts); $('head').append(scripts);
$(options.origin || document).trigger('dialog-open', { dialog: this, options: options }); $(options.origin || document).trigger('dialog-open', { dialog: this, options: options });
// Transfer defined classes from options to actual displayed buttons
// This should work natively, but it kinda does not
Object.keys(dialog_options.buttons).forEach(function(label, index) {
var classes = dialog_options.buttons[label]['class'];
$(buttons.get(index)).addClass(classes);
});
}, },
close: function(event) { close: function(event) {
$(options.origin || document).trigger('dialog-close', { dialog: this, options: options }); $(options.origin || document).trigger('dialog-close', { dialog: this, options: options });
...@@ -425,17 +427,41 @@ Dialog.show = function(content, options = {}) { ...@@ -425,17 +427,41 @@ Dialog.show = function(content, options = {}) {
options.buttons === undefined options.buttons === undefined
|| (options.buttons && !$.isPlainObject(options.buttons)) || (options.buttons && !$.isPlainObject(options.buttons))
) { ) {
dialog_options.buttons = extractButtons.call(this, instance.element); // Create observer to detect changes on disabled attribute
const observer = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
const id = mutation.target.id;
const buttonIndex = dialog_options.buttons.findIndex(button => button.id === id);
if (mutation.attributeName === 'disabled') {
dialog_options.buttons[buttonIndex].disabled = mutation.target.disabled;
} else if (mutation.attributeName === 'class') {
const classes = mutation.target.classList.toString();
dialog_options.buttons[buttonIndex].class = classes.replace(/\bbutton\b/, '');
}
instance.element.dialog('option', 'buttons', dialog_options.buttons);
});
});
dialog_options.buttons = extractButtons(instance.element, (button) => {
observer.observe(button, {
attributes: true,
attributeFilter: ['class', 'disabled'],
});
});
// Create 'close' button // Create 'close' button
if (dialog_options.buttons.cancel === undefined) { const cancelButton = dialog_options.buttons.find(button => button.class.split(' ').includes('cancel'));
dialog_options.buttons.cancel = { if (!cancelButton) {
dialog_options.buttons.push({
text: $gettext('Schließen'), text: $gettext('Schließen'),
'class': 'cancel' class: 'cancel',
}; click: () => Dialog.close(options),
});
} else {
cancelButton.click = () => Dialog.close(options);
} }
dialog_options.buttons.cancel.click = function() {
Dialog.close(options);
};
} }
// Create/update dialog // Create/update dialog
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment