Skip to content

Commit effa323

Browse files
committed
feat(admin,js): Added AJAX HTTP shortcuts
- Refactored "select2" by adding optional placeholder. - Added forced uppercase value for "input" with ".uppercase" class. - Fixed "error" alert when using toastr. - Bumped version to `1.5.3`.
1 parent 75abc69 commit effa323

5 files changed

Lines changed: 74 additions & 35 deletions

File tree

css/admin.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,13 @@ table td>input[type="checkbox"] {
25872587
inline-size: 2rem;
25882588
}
25892589

2590+
.select2-container--default .select2-selection--single .select2-selection__clear {
2591+
block-size: 100%;
2592+
text-align: center;
2593+
font-size: var(--cs-font-size-h3);
2594+
color: var(--bs-danger);
2595+
}
2596+
25902597
.select2-container--default .select2-selection--single .select2-selection__arrow b {
25912598
margin-block-start: 0;
25922599
margin-inline-start: -.25rem;

css/admin.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/admin.js

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
// Attempt to use sprintf
4848
if (typeof elem === "object") {
4949
var name = elem.data("name") || elem.attr("aria-label");
50-
if (typeof name !== "undefined" && name.length) {
50+
if (name?.length) {
5151
message = sprintf(message, name);
5252
}
5353
}
@@ -91,7 +91,7 @@
9191

9292
// Normalize and validate alert type
9393
type = type?.toLowerCase() || 'info';
94-
type = (type === "error" || type === "critical") ? "danger" : (csk.ui.alertClasses.includes(type) ? type : "info");
94+
type = (type === "critical") ? "error" : type;
9595

9696
// Case 1: Toastr is available?
9797
if (typeof toastr !== "undefined") {
@@ -100,6 +100,7 @@
100100
}
101101

102102
// Case 2: Use provided alter template.
103+
type = csk.ui.alertClasses.includes(type) ? type : "info";
103104
const clone = document.getElementById("csk-alert-template")?.content?.querySelector('div')?.cloneNode(true);
104105
if (clone) {
105106
// Add alert Bootstrap alert classes then add message.
@@ -329,7 +330,7 @@
329330
*/
330331
clipboard: function(el) {
331332
var copyText = el.getAttribute("data-text");
332-
if (typeof copyText === "undefined" || !copyText.length) {
333+
if (!copyText?.length) {
333334
return false;
334335
}
335336
navigator.clipboard.writeText(copyText);
@@ -512,8 +513,8 @@
512513
},
513514
error: function(jqXHR, textStatus, errorThrown) {
514515
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");
517518
}
518519
}
519520
}, params);
@@ -561,12 +562,12 @@
561562
csk.ajax.context = context;
562563

563564
/** Did we receive a message? */
564-
if (typeof data.message !== "undefined" && data.message.length) {
565+
if (data?.message?.length) {
565566
csk.ui.alert(data.message, "success");
566567
}
567568

568569
/** No scripts passed? Nothing to do. */
569-
if (typeof data.scripts === "undefined") {
570+
if (!data.scripts?.length) {
570571
return;
571572
}
572573

@@ -581,15 +582,28 @@
581582
}
582583
}
583584
};
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+
});
584598

585599
/**
586600
* Upload file handler.
587601
* @since 2.16
588602
*/
589603
csk.sendFile = function(file, that) {
590604
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) {
593607
return false;
594608
}
595609
data.append("file", file);
@@ -817,14 +831,24 @@
817831
* @since 2.16
818832
*/
819833
if (typeof $.fn.select2 !== "undefined") {
820-
$("select.select2").each(function() {
834+
$("select.select2").each(function () {
821835
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+
825848
if ($that.hasClass("select2-modal")) {
826849
config.dropdownParent = $(".modal");
827850
}
851+
828852
$that.select2(config);
829853
});
830854
}
@@ -863,13 +887,21 @@
863887
var data = $(this).sortable("toArray", {
864888
attribute: "data-id"
865889
});
866-
target.attr("data-fields", "id:" + data.join(","));
890+
target.data("fields", "id:" + data.join(","));
867891
}
868892
}
869893
});
870894
});
871895
}
872896

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+
873905
/**
874906
* Holds an array of checkbox.
875907
* @since 2.16
@@ -932,8 +964,8 @@
932964
multiSelect.delete(row_id);
933965
}
934966

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);
937969

938970
csk.ui.toggleDisabled(document.querySelectorAll(".bulk-action"), multiSelect.size === 0);
939971
});
@@ -947,10 +979,10 @@
947979
var $that = $(this),
948980
href = $that.attr("ajaxify") || $that.attr("href"),
949981
message = $that.data("message");
950-
if ($that.attr("data-form")) {
982+
if ($that.data("form")) {
951983
return false;
952984
}
953-
if (typeof href === "undefined" || !href.length) {
985+
if (!href?.length) {
954986
return false;
955987
}
956988
if (!multiSelect.size) {
@@ -1040,7 +1072,7 @@
10401072
var $that = $(this),
10411073
rel = $that.attr("rel"),
10421074
href = $that.attr("ajaxify") || $that.attr("href");
1043-
if (typeof href === "undefined" || !href.length) {
1075+
if (!href?.length) {
10441076
e.preventDefault();
10451077
return false;
10461078
}
@@ -1082,7 +1114,7 @@
10821114
href = $form.attr("ajaxify") || $form.attr("action");
10831115

10841116
/** No action provided? Nothing to do... */
1085-
if (typeof href === "undefined" || !href.length) {
1117+
if (!href?.length) {
10861118
e.preventDefault();
10871119
return false;
10881120
}
@@ -1142,24 +1174,24 @@
11421174
message = $that.data("confirm");
11431175

11441176
/** No URL provided? Nothing to do... */
1145-
if (typeof href === "undefined" || !href.length) {
1177+
if (!href?.length) {
11461178
return false;
11471179
}
11481180

11491181
/** See if we have any optional fields */
1150-
if (typeof data !== "undefined" && data.length) {
1182+
if (data?.length) {
11511183
data = csk.ui.prepFields(data);
11521184
}
11531185

11541186
/** No URL provided? Nothing to do...No message provided? Just proceed. */
1155-
if (typeof message === "undefined" || !message.length) {
1187+
if (!message?.length) {
11561188
message = "Are you sure?";
11571189
}
11581190

11591191
/** Display the confirmation box before proceeding. */
11601192
return csk.ui.confirm(message, function() {
11611193
/** ajax request. */
1162-
if (typeof rel !== "undefined" && rel.length) {
1194+
if (rel?.length) {
11631195
csk.ajax.request(href, {
11641196
el: this,
11651197
type: rel === "async" ? "GET" : "POST",
@@ -1214,13 +1246,13 @@
12141246
}
12151247

12161248
/** No target? Nothing to do... */
1217-
if (typeof target === "undefined" || !target.length || typeof csk[target] === "undefined") {
1249+
if (!target?.length || typeof csk[target] === "undefined") {
12181250
console.error("error target: " + target);
12191251
return false;
12201252
}
12211253

12221254
/** No href? Nothing to do... */
1223-
if (typeof href === "undefined" || !href.length) {
1255+
if (!href?.length) {
12241256
console.error("error href: " + href);
12251257
return false;
12261258
}
@@ -1243,7 +1275,7 @@
12431275
}
12441276

12451277
/** We display a confirmation message if defined. */
1246-
if (typeof message !== "undefined" && message.length) {
1278+
if (message?.length) {
12471279
csk.ui.confirm(sprintf(message, name), function() {
12481280
window.location.href = href;
12491281
}, function() {
@@ -1272,7 +1304,7 @@
12721304
if (typeof target === "undefined") {
12731305
return false;
12741306
}
1275-
if (typeof message === "undefined" || !message.length) {
1307+
if (!message?.length) {
12761308
target.submit();
12771309
return;
12781310
}
@@ -1293,17 +1325,17 @@
12931325
message = $that.data("confirm");
12941326

12951327
// href url is required!
1296-
if (typeof href === "undefined" || !href.length) {
1328+
if (!href?.length) {
12971329
return false;
12981330
}
12991331

13001332
// data are required.
1301-
if (typeof data !== "undefined" && data.length) {
1333+
if (data?.length) {
13021334
data = csk.ui.prepFields(data);
13031335
}
13041336

13051337
// message is undefined.
1306-
if (typeof message === "undefined" || !message.length) {
1338+
if (!message?.length) {
13071339
csk.submit(href, data);
13081340
return;
13091341
}
@@ -1336,7 +1368,7 @@
13361368
$(".dropZone").each(function() {
13371369
var $that = $(this),
13381370
href = $that.data("upload");
1339-
if (typeof href === "undefined" || !href.length) {
1371+
if (!href?.length) {
13401372
return;
13411373
}
13421374
$that.dropzone({

0 commit comments

Comments
 (0)