|
47 | 47 | // Attempt to use sprintf |
48 | 48 | if (typeof elem === "object") { |
49 | 49 | var name = elem.data("name") || elem.attr("aria-label"); |
50 | | - if (typeof name !== "undefined" && name.length) { |
| 50 | + if (name?.length) { |
51 | 51 | message = sprintf(message, name); |
52 | 52 | } |
53 | 53 | } |
|
91 | 91 |
|
92 | 92 | // Normalize and validate alert type |
93 | 93 | type = type?.toLowerCase() || 'info'; |
94 | | - type = (type === "error" || type === "critical") ? "danger" : (csk.ui.alertClasses.includes(type) ? type : "info"); |
| 94 | + type = (type === "critical") ? "error" : type; |
95 | 95 |
|
96 | 96 | // Case 1: Toastr is available? |
97 | 97 | if (typeof toastr !== "undefined") { |
|
100 | 100 | } |
101 | 101 |
|
102 | 102 | // Case 2: Use provided alter template. |
| 103 | + type = csk.ui.alertClasses.includes(type) ? type : "info"; |
103 | 104 | const clone = document.getElementById("csk-alert-template")?.content?.querySelector('div')?.cloneNode(true); |
104 | 105 | if (clone) { |
105 | 106 | // Add alert Bootstrap alert classes then add message. |
|
329 | 330 | */ |
330 | 331 | clipboard: function(el) { |
331 | 332 | var copyText = el.getAttribute("data-text"); |
332 | | - if (typeof copyText === "undefined" || !copyText.length) { |
| 333 | + if (!copyText?.length) { |
333 | 334 | return false; |
334 | 335 | } |
335 | 336 | navigator.clipboard.writeText(copyText); |
|
512 | 513 | }, |
513 | 514 | error: function(jqXHR, textStatus, errorThrown) { |
514 | 515 | var response = jqXHR.responseJSON || undefined; |
515 | | - if (typeof response !== "undefined" && response.message !== "undefined" && response.message.length) { |
516 | | - csk.ui.alert(response.message, "error"); |
| 516 | + if (response?.message?.length) { |
| 517 | + csk.ui.alert(response.message, response.status || "error"); |
517 | 518 | } |
518 | 519 | } |
519 | 520 | }, params); |
|
561 | 562 | csk.ajax.context = context; |
562 | 563 |
|
563 | 564 | /** Did we receive a message? */ |
564 | | - if (typeof data.message !== "undefined" && data.message.length) { |
| 565 | + if (data?.message?.length) { |
565 | 566 | csk.ui.alert(data.message, "success"); |
566 | 567 | } |
567 | 568 |
|
568 | 569 | /** No scripts passed? Nothing to do. */ |
569 | | - if (typeof data.scripts === "undefined") { |
| 570 | + if (!data.scripts?.length) { |
570 | 571 | return; |
571 | 572 | } |
572 | 573 |
|
|
581 | 582 | } |
582 | 583 | } |
583 | 584 | }; |
| 585 | + /** |
| 586 | + * HTTP method shortcuts |
| 587 | + * @since 3.11.1 |
| 588 | + */ |
| 589 | + ['get', 'post', 'put', 'patch', 'delete'].forEach(function (method) { |
| 590 | + if (csk.ajax[method]) return; |
| 591 | + csk.ajax[method] = function (url, params) { |
| 592 | + console.log(url); |
| 593 | + params = params || {}; |
| 594 | + params.type = params.method = method.toUpperCase(); |
| 595 | + return this.request(url, params); |
| 596 | + }; |
| 597 | + }); |
584 | 598 |
|
585 | 599 | /** |
586 | 600 | * Upload file handler. |
587 | 601 | * @since 2.16 |
588 | 602 | */ |
589 | 603 | csk.sendFile = function(file, that) { |
590 | 604 | var data = new FormData(), |
591 | | - href = $(that).attr("data-upload"); |
592 | | - if (typeof href === "undefined" || !href.length) { |
| 605 | + href = $(that).data("upload"); |
| 606 | + if (!href?.length) { |
593 | 607 | return false; |
594 | 608 | } |
595 | 609 | data.append("file", file); |
|
817 | 831 | * @since 2.16 |
818 | 832 | */ |
819 | 833 | if (typeof $.fn.select2 !== "undefined") { |
820 | | - $("select.select2").each(function() { |
| 834 | + $("select.select2").each(function () { |
821 | 835 | var $that = $(this), |
822 | | - config = { |
823 | | - width: "100%" |
824 | | - }; |
| 836 | + config = { width: "100%" }, |
| 837 | + ph = $that.data("placeholder") || $that.attr("placeholder"); |
| 838 | + |
| 839 | + if (ph?.length && !$that.prop('multiple')) { |
| 840 | + config.placeholder = ph; |
| 841 | + config.allowClear = true; |
| 842 | + |
| 843 | + if (!$that.find('option[value=""]').length) { |
| 844 | + $that.prepend('<option></option>'); |
| 845 | + } |
| 846 | + } |
| 847 | + |
825 | 848 | if ($that.hasClass("select2-modal")) { |
826 | 849 | config.dropdownParent = $(".modal"); |
827 | 850 | } |
| 851 | + |
828 | 852 | $that.select2(config); |
829 | 853 | }); |
830 | 854 | } |
|
863 | 887 | var data = $(this).sortable("toArray", { |
864 | 888 | attribute: "data-id" |
865 | 889 | }); |
866 | | - target.attr("data-fields", "id:" + data.join(",")); |
| 890 | + target.data("fields", "id:" + data.join(",")); |
867 | 891 | } |
868 | 892 | } |
869 | 893 | }); |
870 | 894 | }); |
871 | 895 | } |
872 | 896 |
|
| 897 | + /** |
| 898 | + * Input with forced Uppercase. |
| 899 | + * @since 3.11.1 |
| 900 | + */ |
| 901 | + $(document).on("input", "input.uppercase", function (e) { |
| 902 | + e.target.value = e.target.value.toUpperCase(); |
| 903 | + }); |
| 904 | + |
873 | 905 | /** |
874 | 906 | * Holds an array of checkbox. |
875 | 907 | * @since 2.16 |
|
932 | 964 | multiSelect.delete(row_id); |
933 | 965 | } |
934 | 966 |
|
935 | | - target.attr("data-fields", "id:" + Array.from(multiSelect).join(",")); |
936 | | - table.attr("data-selected", multiSelect); |
| 967 | + target.data("fields", "id:" + Array.from(multiSelect).join(",")); |
| 968 | + table.data("selected", multiSelect); |
937 | 969 |
|
938 | 970 | csk.ui.toggleDisabled(document.querySelectorAll(".bulk-action"), multiSelect.size === 0); |
939 | 971 | }); |
|
947 | 979 | var $that = $(this), |
948 | 980 | href = $that.attr("ajaxify") || $that.attr("href"), |
949 | 981 | message = $that.data("message"); |
950 | | - if ($that.attr("data-form")) { |
| 982 | + if ($that.data("form")) { |
951 | 983 | return false; |
952 | 984 | } |
953 | | - if (typeof href === "undefined" || !href.length) { |
| 985 | + if (!href?.length) { |
954 | 986 | return false; |
955 | 987 | } |
956 | 988 | if (!multiSelect.size) { |
|
1040 | 1072 | var $that = $(this), |
1041 | 1073 | rel = $that.attr("rel"), |
1042 | 1074 | href = $that.attr("ajaxify") || $that.attr("href"); |
1043 | | - if (typeof href === "undefined" || !href.length) { |
| 1075 | + if (!href?.length) { |
1044 | 1076 | e.preventDefault(); |
1045 | 1077 | return false; |
1046 | 1078 | } |
|
1082 | 1114 | href = $form.attr("ajaxify") || $form.attr("action"); |
1083 | 1115 |
|
1084 | 1116 | /** No action provided? Nothing to do... */ |
1085 | | - if (typeof href === "undefined" || !href.length) { |
| 1117 | + if (!href?.length) { |
1086 | 1118 | e.preventDefault(); |
1087 | 1119 | return false; |
1088 | 1120 | } |
|
1142 | 1174 | message = $that.data("confirm"); |
1143 | 1175 |
|
1144 | 1176 | /** No URL provided? Nothing to do... */ |
1145 | | - if (typeof href === "undefined" || !href.length) { |
| 1177 | + if (!href?.length) { |
1146 | 1178 | return false; |
1147 | 1179 | } |
1148 | 1180 |
|
1149 | 1181 | /** See if we have any optional fields */ |
1150 | | - if (typeof data !== "undefined" && data.length) { |
| 1182 | + if (data?.length) { |
1151 | 1183 | data = csk.ui.prepFields(data); |
1152 | 1184 | } |
1153 | 1185 |
|
1154 | 1186 | /** No URL provided? Nothing to do...No message provided? Just proceed. */ |
1155 | | - if (typeof message === "undefined" || !message.length) { |
| 1187 | + if (!message?.length) { |
1156 | 1188 | message = "Are you sure?"; |
1157 | 1189 | } |
1158 | 1190 |
|
1159 | 1191 | /** Display the confirmation box before proceeding. */ |
1160 | 1192 | return csk.ui.confirm(message, function() { |
1161 | 1193 | /** ajax request. */ |
1162 | | - if (typeof rel !== "undefined" && rel.length) { |
| 1194 | + if (rel?.length) { |
1163 | 1195 | csk.ajax.request(href, { |
1164 | 1196 | el: this, |
1165 | 1197 | type: rel === "async" ? "GET" : "POST", |
|
1214 | 1246 | } |
1215 | 1247 |
|
1216 | 1248 | /** No target? Nothing to do... */ |
1217 | | - if (typeof target === "undefined" || !target.length || typeof csk[target] === "undefined") { |
| 1249 | + if (!target?.length || typeof csk[target] === "undefined") { |
1218 | 1250 | console.error("error target: " + target); |
1219 | 1251 | return false; |
1220 | 1252 | } |
1221 | 1253 |
|
1222 | 1254 | /** No href? Nothing to do... */ |
1223 | | - if (typeof href === "undefined" || !href.length) { |
| 1255 | + if (!href?.length) { |
1224 | 1256 | console.error("error href: " + href); |
1225 | 1257 | return false; |
1226 | 1258 | } |
|
1243 | 1275 | } |
1244 | 1276 |
|
1245 | 1277 | /** We display a confirmation message if defined. */ |
1246 | | - if (typeof message !== "undefined" && message.length) { |
| 1278 | + if (message?.length) { |
1247 | 1279 | csk.ui.confirm(sprintf(message, name), function() { |
1248 | 1280 | window.location.href = href; |
1249 | 1281 | }, function() { |
|
1272 | 1304 | if (typeof target === "undefined") { |
1273 | 1305 | return false; |
1274 | 1306 | } |
1275 | | - if (typeof message === "undefined" || !message.length) { |
| 1307 | + if (!message?.length) { |
1276 | 1308 | target.submit(); |
1277 | 1309 | return; |
1278 | 1310 | } |
|
1293 | 1325 | message = $that.data("confirm"); |
1294 | 1326 |
|
1295 | 1327 | // href url is required! |
1296 | | - if (typeof href === "undefined" || !href.length) { |
| 1328 | + if (!href?.length) { |
1297 | 1329 | return false; |
1298 | 1330 | } |
1299 | 1331 |
|
1300 | 1332 | // data are required. |
1301 | | - if (typeof data !== "undefined" && data.length) { |
| 1333 | + if (data?.length) { |
1302 | 1334 | data = csk.ui.prepFields(data); |
1303 | 1335 | } |
1304 | 1336 |
|
1305 | 1337 | // message is undefined. |
1306 | | - if (typeof message === "undefined" || !message.length) { |
| 1338 | + if (!message?.length) { |
1307 | 1339 | csk.submit(href, data); |
1308 | 1340 | return; |
1309 | 1341 | } |
|
1336 | 1368 | $(".dropZone").each(function() { |
1337 | 1369 | var $that = $(this), |
1338 | 1370 | href = $that.data("upload"); |
1339 | | - if (typeof href === "undefined" || !href.length) { |
| 1371 | + if (!href?.length) { |
1340 | 1372 | return; |
1341 | 1373 | } |
1342 | 1374 | $that.dropzone({ |
|
0 commit comments