|
360 | 360 |
|
361 | 361 | // Remove the class after blink sequence |
362 | 362 | setTimeout(() => target.classList.remove('highlight-focus'), 4000); |
| 363 | + }, |
| 364 | + /** |
| 365 | + * Enables or disable one or more elements. |
| 366 | + * @since 3.10.1 |
| 367 | + * |
| 368 | + * @param {HTMLElement|NodeList|Array} els - Element(s) to toggle. |
| 369 | + * @param {boolean} disabled - Whether to disable (true) or enable (false). |
| 370 | + */ |
| 371 | + toggleDisabled: function(els, disabled) { |
| 372 | + if (!els) return; |
| 373 | + |
| 374 | + // Handle single element |
| 375 | + if (els instanceof HTMLElement) { |
| 376 | + els = [els]; |
| 377 | + } |
| 378 | + // Handle NodeList |
| 379 | + else if (els instanceof NodeList) { |
| 380 | + els = Array.from(els); |
| 381 | + } |
| 382 | + // Handle jQuery object |
| 383 | + else if (window.jQuery && els instanceof jQuery) { |
| 384 | + els = els.toArray(); |
| 385 | + } |
| 386 | + // Handle array-like objects (e.g. HTMLCollection) |
| 387 | + else if (!Array.isArray(els) && typeof els.length === "number") { |
| 388 | + els = Array.from(els); |
| 389 | + } |
| 390 | + |
| 391 | + // Bail if still not iterable |
| 392 | + if (!Array.isArray(els)) return; |
| 393 | + |
| 394 | + els.forEach(function(el) { |
| 395 | + if (!(el instanceof HTMLElement)) return; |
| 396 | + |
| 397 | + el.disabled = disabled; |
| 398 | + |
| 399 | + if (disabled) { |
| 400 | + el.setAttribute("disabled", "disabled"); |
| 401 | + el.classList.add("disabled"); |
| 402 | + } else { |
| 403 | + el.removeAttribute("disabled"); |
| 404 | + el.classList.remove("disabled"); |
| 405 | + } |
| 406 | + }); |
363 | 407 | } |
364 | 408 | }; |
365 | 409 |
|
|
526 | 570 | }).appendTo(that); |
527 | 571 |
|
528 | 572 | // Enable submit button abck |
529 | | - $(that).closest('form').find("[type=submit]").prop("disabled", false).removeClass("disabled"); |
| 573 | + csk.ui.toggleDisabled($(that).closest('form').find("[type=submit]"), false); |
530 | 574 | } |
531 | 575 | } |
532 | 576 | }); |
|
779 | 823 | target.attr("data-fields", "id:" + Array.from(multiSelect).join(",")); |
780 | 824 | table.attr("data-selected", multiSelect); |
781 | 825 |
|
782 | | - if (multiSelect.size === 0) { |
783 | | - $(".bulk-action").prop("disabled", true).addClass("disabled"); |
784 | | - } else { |
785 | | - $(".bulk-action").prop("disabled", false).removeClass("disabled"); |
786 | | - } |
| 826 | + csk.ui.toggleDisabled(document.querySelectorAll(".bulk-action"), multiSelect.size === 0); |
787 | 827 | }); |
788 | 828 |
|
789 | 829 | /** |
|
816 | 856 | }, |
817 | 857 | beforeSend: function() { |
818 | 858 | if (!$that.prop("disabled")) { |
819 | | - $that.prop("disabled", true).addClass("disabled"); |
| 859 | + csk.ui.toggleDisabled($that[0], true); |
820 | 860 | } |
821 | 861 | }, |
822 | 862 | success: function(data, textStatus, jqXHR) { |
823 | 863 | csk.ajax._response(data); |
824 | | - $that.removeProp("disabled").removeClass("disabled"); |
| 864 | + csk.ui.toggleDisabled($that[0], false); |
825 | 865 | setTimeout(location.reload.bind(location), 2000); |
826 | 866 | } |
827 | 867 | }); |
|
836 | 876 | }, |
837 | 877 | beforeSend: function() { |
838 | 878 | if (!$that.prop("disabled")) { |
839 | | - $that.prop("disabled", true).addClass("disabled"); |
| 879 | + csk.ui.toggleDisabled($that[0], true); |
840 | 880 | } |
841 | 881 | }, |
842 | 882 | complete: function() { |
843 | | - $that.removeProp("disabled").removeClass("disabled"); |
| 883 | + csk.ui.toggleDisabled($that[0], false); |
844 | 884 | window.location.href = location.href; |
845 | 885 | } |
846 | 886 | }); |
|
864 | 904 | * @since 1.20 |
865 | 905 | */ |
866 | 906 | $(document).on("submit", "form", function(e) { |
867 | | - var $form = $(this); |
868 | | - $("[type=submit]").prop("disabled", true).addClass("disabled"); |
| 907 | + const $form = $(this); |
| 908 | + |
| 909 | + // Disable only submit buttons in this form |
| 910 | + csk.ui.toggleDisabled($form.find("[type=submit]"), true); |
| 911 | + |
| 912 | + // Prevent re-submission |
| 913 | + $form.on("submit", () => false); |
869 | 914 |
|
870 | | - /** Disable the submit event. */ |
871 | | - $form.submit(function() { |
872 | | - return false; |
873 | | - }); |
874 | 915 | return true; |
875 | 916 | }); |
876 | 917 |
|
|
906 | 947 | } |
907 | 948 |
|
908 | 949 | /** We disable the element before proceeding. */ |
909 | | - $that.prop("disabled", true).addClass("disabled"); |
| 950 | + csk.ui.toggleDisabled($that[0], true); |
910 | 951 | }, |
911 | 952 | success: function(data, textStatus, jqXHR) { |
912 | 953 | /** let _response handle response/ */ |
913 | 954 | csk.ajax._response(data); |
914 | 955 | /** remove disabled property and reload page. */ |
915 | | - $that.removeProp("disabled").removeClass("disabled"); |
| 956 | + csk.ui.toggleDisabled($that[0], false); |
916 | 957 | setTimeout(location.reload.bind(location), 1500); |
917 | 958 | } |
918 | 959 | }); |
|
924 | 965 | * @since 1.30 |
925 | 966 | */ |
926 | 967 | $(document).on("submit", "form[rel]", function(e) { |
927 | | - var $that = $(this), |
928 | | - rel = $that.attr("rel"), |
929 | | - href = $that.attr("ajaxify") || $that.attr("action"); |
| 968 | + var $form = $(this), |
| 969 | + rel = $form.attr("rel"), |
| 970 | + href = $form.attr("ajaxify") || $form.attr("action"); |
930 | 971 |
|
931 | 972 | /** No action provided? Nothing to do... */ |
932 | 973 | if (typeof href === "undefined" || !href.length) { |
|
940 | 981 | csk.ajax.request(href, { |
941 | 982 | el: this, |
942 | 983 | type: "POST", |
943 | | - data: $that.serializeArray(), |
| 984 | + data: $form.serializeArray(), |
944 | 985 | beforeSend: function() { |
945 | | - if ($that.prop("disabled")) { |
| 986 | + if ($form.prop("disabled")) { |
946 | 987 | return; |
947 | 988 | } |
948 | | - $that.find("[type=submit]").prop("disabled", true).addClass("disabled"); |
| 989 | + csk.ui.toggleDisabled($form.find("[type=submit]"), true); |
949 | 990 | }, |
950 | 991 | complete: function() { |
951 | | - $that.trigger("reset"); |
952 | | - $that.find("[type=submit]").prop("disabled", false).removeClass("disabled"); |
| 992 | + $form.trigger("reset"); |
| 993 | + csk.ui.toggleDisabled($form.find("[type=submit]"), false); |
953 | 994 | setTimeout(location.reload.bind(location), 1500); |
954 | 995 | } |
955 | 996 | }); |
|
1017 | 1058 | } |
1018 | 1059 |
|
1019 | 1060 | /** We disable the element before proceeding. */ |
1020 | | - $that.prop("disabled", true).addClass("disabled"); |
| 1061 | + csk.ui.toggleDisabled($that[0], true); |
1021 | 1062 | }, |
1022 | 1063 | success: function(data, textStatus, jqXHR) { |
1023 | 1064 | /** let _response handle response/ */ |
1024 | 1065 | csk.ajax._response(data); |
1025 | 1066 | /** remove disabled property and reload page. */ |
1026 | | - $that.removeProp("disabled").removeClass("disabled"); |
| 1067 | + csk.ui.toggleDisabled($that[0], false); |
1027 | 1068 | setTimeout(location.reload.bind(location), 1500); |
1028 | 1069 | } |
1029 | 1070 | }); |
|
0 commit comments