Skip to content

Commit

Permalink
927745f3bd0ed891a69e8c006d0de368bf115a85 New: Buttons with a split ca…
Browse files Browse the repository at this point in the history
…n now be enabled and disabled like normal buttons

New: Split button will disable automatically if there are no enabled buttons inside it, and then enable itself if a button is enabled (e.g. from an action such as selecting a row in the table).

7db9c8c69204a0391179fa7bcac67116df10a470 Fix: Clicking on a button from a split list will now cause the collection to hide.

70c236b07d2fb7c41887b178e59c10c3258fe9ab Merge branch 'master' of github.com:DataTables/Buttons

Sync to source repo @70c236b07d2fb7c41887b178e59c10c3258fe9ab
  • Loading branch information
dtbuild committed Oct 9, 2024
1 parent dc05fc0 commit 4f4e658
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 15 deletions.
2 changes: 1 addition & 1 deletion datatables.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
],
"src-repo": "http://github.com/DataTables/Buttons",
"last-tag": "3.1.2",
"last-sync": "822bb21ef766a967c1d99819d00aa273d2001b30"
"last-sync": "70c236b07d2fb7c41887b178e59c10c3258fe9ab"
}
89 changes: 83 additions & 6 deletions js/dataTables.buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,20 @@ $.extend(Buttons.prototype, {
disable: function (node) {
var button = this._nodeToButton(node);

$(button.node)
.addClass(this.c.dom.button.disabled)
.prop('disabled', true);
if (button.isSplit) {
$(button.node.childNodes[0])
.addClass(this.c.dom.button.disabled)
.prop('disabled', true);
}
else {
$(button.node)
.addClass(this.c.dom.button.disabled)
.prop('disabled', true);
}

button.disabled = true;

this._checkSplitEnable();

return this;
},
Expand Down Expand Up @@ -355,9 +366,21 @@ $.extend(Buttons.prototype, {
}

var button = this._nodeToButton(node);
$(button.node)
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);

if (button.isSplit) {
$(button.node.childNodes[0])
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);
}
else {
$(button.node)
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);
}

button.disabled = false;

this._checkSplitEnable();

return this;
},
Expand Down Expand Up @@ -946,6 +969,7 @@ $.extend(Buttons.prototype, {
.append(button);

var dropButtonConfig = $.extend(config, {
autoClose: true,
align: dropdownConf.dropdown.align,
attr: {
'aria-haspopup': 'dialog',
Expand Down Expand Up @@ -1024,6 +1048,57 @@ $.extend(Buttons.prototype, {
};
},

/**
* Spin over buttons checking if splits should be enabled or not.
* @param {*} buttons Array of buttons to check
*/
_checkSplitEnable: function (buttons) {
if (! buttons) {
buttons = this.s.buttons;
}

for (var i=0 ; i<buttons.length ; i++) {
var button = buttons[i];

// Check if the button is a split one and if so, determine
// its state
if (button.isSplit) {
var splitBtn = button.node.childNodes[1];

if (this._checkAnyEnabled(button.buttons)) {
// Enable the split
$(splitBtn)
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);
}
else {
$(splitBtn)
.addClass(this.c.dom.button.disabled)
.prop('disabled', false);
}
}
else if (button.isCollection) {
// Nest down into collections
this._checkSplitEnable(button.buttons);
}
}
},

/**
* Check an array of buttons and see if any are enabled in it
* @param {*} buttons Button array
* @returns true if a button is enabled, false otherwise
*/
_checkAnyEnabled: function (buttons) {
for (var i=0 ; i<buttons.length ; i++) {
if (! buttons[i].disabled) {
return true;
}
}

return false;
},

/**
* Get the button object from a node (recursive)
* @param {node} node Button node
Expand Down Expand Up @@ -1304,6 +1379,8 @@ $.extend(Buttons.prototype, {
inOpts
);

console.log(inOpts, options);

var containerSelector =
options.tag + '.' + options.containerClassName.replace(/ /g, '.');
var hostNode = hostButton.node();
Expand Down
2 changes: 1 addition & 1 deletion js/dataTables.buttons.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dataTables.buttons.min.mjs

Large diffs are not rendered by default.

89 changes: 83 additions & 6 deletions js/dataTables.buttons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,20 @@ $.extend(Buttons.prototype, {
disable: function (node) {
var button = this._nodeToButton(node);

$(button.node)
.addClass(this.c.dom.button.disabled)
.prop('disabled', true);
if (button.isSplit) {
$(button.node.childNodes[0])
.addClass(this.c.dom.button.disabled)
.prop('disabled', true);
}
else {
$(button.node)
.addClass(this.c.dom.button.disabled)
.prop('disabled', true);
}

button.disabled = true;

this._checkSplitEnable();

return this;
},
Expand Down Expand Up @@ -315,9 +326,21 @@ $.extend(Buttons.prototype, {
}

var button = this._nodeToButton(node);
$(button.node)
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);

if (button.isSplit) {
$(button.node.childNodes[0])
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);
}
else {
$(button.node)
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);
}

button.disabled = false;

this._checkSplitEnable();

return this;
},
Expand Down Expand Up @@ -906,6 +929,7 @@ $.extend(Buttons.prototype, {
.append(button);

var dropButtonConfig = $.extend(config, {
autoClose: true,
align: dropdownConf.dropdown.align,
attr: {
'aria-haspopup': 'dialog',
Expand Down Expand Up @@ -984,6 +1008,57 @@ $.extend(Buttons.prototype, {
};
},

/**
* Spin over buttons checking if splits should be enabled or not.
* @param {*} buttons Array of buttons to check
*/
_checkSplitEnable: function (buttons) {
if (! buttons) {
buttons = this.s.buttons;
}

for (var i=0 ; i<buttons.length ; i++) {
var button = buttons[i];

// Check if the button is a split one and if so, determine
// its state
if (button.isSplit) {
var splitBtn = button.node.childNodes[1];

if (this._checkAnyEnabled(button.buttons)) {
// Enable the split
$(splitBtn)
.removeClass(this.c.dom.button.disabled)
.prop('disabled', false);
}
else {
$(splitBtn)
.addClass(this.c.dom.button.disabled)
.prop('disabled', false);
}
}
else if (button.isCollection) {
// Nest down into collections
this._checkSplitEnable(button.buttons);
}
}
},

/**
* Check an array of buttons and see if any are enabled in it
* @param {*} buttons Button array
* @returns true if a button is enabled, false otherwise
*/
_checkAnyEnabled: function (buttons) {
for (var i=0 ; i<buttons.length ; i++) {
if (! buttons[i].disabled) {
return true;
}
}

return false;
},

/**
* Get the button object from a node (recursive)
* @param {node} node Button node
Expand Down Expand Up @@ -1264,6 +1339,8 @@ $.extend(Buttons.prototype, {
inOpts
);

console.log(inOpts, options);

var containerSelector =
options.tag + '.' + options.containerClassName.replace(/ /g, '.');
var hostNode = hostButton.node();
Expand Down

0 comments on commit 4f4e658

Please sign in to comment.