From 4f4e6585785d662a8671cc4e910d83d4e97cc260 Mon Sep 17 00:00:00 2001 From: dtbuild Date: Wed, 9 Oct 2024 15:15:17 +0000 Subject: [PATCH] 927745f3bd0ed891a69e8c006d0de368bf115a85 New: Buttons with a split can 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 --- datatables.json | 2 +- js/dataTables.buttons.js | 89 ++++++++++++++++++++++++++++++++--- js/dataTables.buttons.min.js | 2 +- js/dataTables.buttons.min.mjs | 2 +- js/dataTables.buttons.mjs | 89 ++++++++++++++++++++++++++++++++--- 5 files changed, 169 insertions(+), 15 deletions(-) diff --git a/datatables.json b/datatables.json index 8bfdee5..f305275 100644 --- a/datatables.json +++ b/datatables.json @@ -25,5 +25,5 @@ ], "src-repo": "http://github.com/DataTables/Buttons", "last-tag": "3.1.2", - "last-sync": "822bb21ef766a967c1d99819d00aa273d2001b30" + "last-sync": "70c236b07d2fb7c41887b178e59c10c3258fe9ab" } \ No newline at end of file diff --git a/js/dataTables.buttons.js b/js/dataTables.buttons.js index fa5dea2..a023fcf 100644 --- a/js/dataTables.buttons.js +++ b/js/dataTables.buttons.js @@ -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; }, @@ -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; }, @@ -946,6 +969,7 @@ $.extend(Buttons.prototype, { .append(button); var dropButtonConfig = $.extend(config, { + autoClose: true, align: dropdownConf.dropdown.align, attr: { 'aria-haspopup': 'dialog', @@ -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").addClass(this.c.dom.container.className)},this._constructor()}x.extend(A.prototype,{action:function(t,n){t=this._nodeToButton(t);return void 0===n?t.conf.action:(t.conf.action=n,this)},active:function(t,n){var t=this._nodeToButton(t),e=this.c.dom.button.active,o=x(t.node);return t.inCollection&&this.c.dom.collection.button&&void 0!==this.c.dom.collection.button.active&&(e=this.c.dom.collection.button.active),void 0===n?o.hasClass(e):(o.toggleClass(e,void 0===n||n),this)},add:function(t,n,e){var o=this.s.buttons;if("string"==typeof n){for(var i=n.split("-"),s=this.s,r=0,a=i.length-1;r"),h.conf._collection=h.collection,x(h.node).append(c.action.dropHtml),this._expandButton(h.buttons,h.conf.buttons,h.conf.split,!a,a,s,h.conf)),h.conf.split){h.collection=x("<"+c.container.tag+"/>"),h.conf._collection=h.collection;for(var b=0;b").addClass("dt-button-spacer "+n.style+" "+p.spacer.className).html(i(n.text)),{conf:n,node:d,inserter:d,buttons:[],inCollection:t,isSplit:e,collection:null,textNode:d};if(n.available&&!n.available(f,n)&&!n.html)return!1;n.html?c=x(n.html):(r=function(t,n,e,o,i){o.action.call(n.button(e),t,n,e,o,i),x(n.table().node()).triggerHandler("buttons-action.dt",[n.button(e),n,e,o])},a=function(t,n,e,o){o.async?(u.processing(e[0],!0),setTimeout(function(){r(t,n,e,o,function(){u.processing(e[0],!1)})},o.async)):r(t,n,e,o,function(){})},d=n.tag||p.tag,l=void 0===n.clickBlurs||n.clickBlurs,c=x("<"+d+"/>").addClass(p.className).attr("tabindex",this.s.dt.settings()[0].iTabIndex).attr("aria-controls",this.s.dt.table().node().id).on("click.dtb",function(t){t.preventDefault(),!c.hasClass(p.disabled)&&n.action&&a(t,f,c,n),l&&c.trigger("blur")}).on("keypress.dtb",function(t){13===t.keyCode&&(t.preventDefault(),!c.hasClass(p.disabled))&&n.action&&a(t,f,c,n)}),"a"===d.toLowerCase()&&c.attr("href","#"),"button"===d.toLowerCase()&&c.attr("type","button"),s=p.liner.tag?(d=x("<"+p.liner.tag+"/>").html(i(n.text)).addClass(p.liner.className),"a"===p.liner.tag.toLowerCase()&&d.attr("href","#"),c.append(d),d):(c.html(i(n.text)),c),!1===n.enabled&&c.addClass(p.disabled),n.className&&c.addClass(n.className),n.titleAttr&&c.attr("title",i(n.titleAttr)),n.attr&&c.attr(n.attr),n.namespace||(n.namespace=".dt-button-"+C++),void 0!==n.config&&n.config.split&&(n.split=n.config.split));var h,b,g,m,v,y,d=this.c.dom.buttonContainer,d=d&&d.tag?x("<"+d.tag+"/>").addClass(d.className).append(c):c;return this._addKey(n),this.c.buttonCreated&&(d=this.c.buttonCreated(n,d)),e&&(b=(h=t?x.extend(!0,this.c.dom.split,this.c.dom.collection.split):this.c.dom.split).wrapper,g=x("<"+b.tag+"/>").addClass(b.className).append(c),m=x.extend(n,{align:h.dropdown.align,attr:{"aria-haspopup":"dialog","aria-expanded":!1},className:h.dropdown.className,closeButton:!1,splitAlignClass:h.dropdown.splitAlignClass,text:h.dropdown.text}),this._addKey(m),v=function(t,n,e,o){w.split.action.call(n.button(g),t,n,e,o),x(n.table().node()).triggerHandler("buttons-action.dt",[n.button(e),n,e,o]),e.attr("aria-expanded",!0)},y=x('').html(h.dropdown.dropHtml).on("click.dtb",function(t){t.preventDefault(),t.stopPropagation(),y.hasClass(p.disabled)||v(t,f,y,m),l&&y.trigger("blur")}).on("keypress.dtb",function(t){13===t.keyCode&&(t.preventDefault(),y.hasClass(p.disabled)||v(t,f,y,m))}),0===n.split.length&&y.addClass("dtb-hide-drop"),g.append(y).attr(m.attr)),{conf:n,node:(e?g:c).get(0),inserter:e?g:d,buttons:[],inCollection:t,isSplit:e,inSplit:o,collection:null,textNode:s}},_nodeToButton:function(t,n){for(var e=0,o=(n=n||this.s.buttons).length;e").addClass(h.containerClassName).addClass(h.collectionLayout).addClass(h.splitAlignClass).addClass(n).css("display","none").attr({"aria-modal":!0,role:"dialog"}),o=x(o).addClass(h.contentClassName).attr("role","menu").appendTo(e),g.attr("aria-expanded","true"),g.parents("body")[0]!==v.body&&(g=v.body.lastChild),h.popoverTitle?e.prepend('
'+h.popoverTitle+"
"):h.collectionTitle&&e.prepend('
'+h.collectionTitle+"
"),h.closeButton&&e.prepend('
×
').addClass("dtb-collection-closeable"),y(e.insertAfter(g),h.fade),f=x(t.table().container()),n=e.css("position"),"container"!==h.span&&"dt-container"!==h.align||(g=g.parent(),e.css("width",f.width())),"absolute"===n?(t=x(g[0].offsetParent),f=g.position(),n=g.offset(),a=t.offset(),s=t.position(),r=m.getComputedStyle(t[0]),a.height=t.outerHeight(),a.width=t.width()+parseFloat(r.paddingLeft),a.right=a.left+a.width,a.bottom=a.top+a.height,a=f.top+g.outerHeight(),l=f.left,e.css({top:a,left:l}),r=m.getComputedStyle(e[0]),(c=e.offset()).height=e.outerHeight(),c.width=e.outerWidth(),c.right=c.left+c.width,c.bottom=c.top+c.height,c.marginTop=parseFloat(r.marginTop),c.marginBottom=parseFloat(r.marginBottom),h.dropup&&(a=f.top-c.height-c.marginTop-c.marginBottom),"button-right"!==h.align&&!e.hasClass(h.rightAlignClassName)||(l=f.left-c.width+g.outerWidth()),"dt-container"!==h.align&&"container"!==h.align||lx(m).width()&&(l=x(m).width()-c.width-s.left),n.left+l<0&&(l=-n.left),s.top+a+c.height>x(m).height()+x(m).scrollTop()&&(a=f.top-c.height-c.marginTop-c.marginBottom),t.offset().top+a").addClass(n).css("display","none").insertAfter(o),e):_(x("div."+n),e,function(){x(this).removeClass(n).remove()})},A.instanceSelector=function(t,s){var r,a,l;return null==t?x.map(s,function(t){return t.inst}):(r=[],a=x.map(s,function(t){return t.name}),(l=function(t){var n;if(Array.isArray(t))for(var e=0,o=t.length;e)|(