-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathcptui-scripts.js
312 lines (311 loc) · 10.4 KB
/
cptui-scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
postboxes.add_postbox_toggles(pagenow);
(function($) {
$("#cptui_select_post_type_submit").hide();
$("#cptui_select_taxonomy_submit").hide();
if ("edit" === getParameterByName("action")) {
var original_slug = $("#name").val();
}
$("#hierarchical").on("change", function() {
var hierarchical = $(this).val();
if ("1" === hierarchical) {
$("#page-attributes").prop("checked", true);
} else {
$("#page-attributes").prop("checked", false);
}
});
$("#post_type").on("change", function() {
$("#cptui_select_post_type").submit();
});
$("#taxonomy").on("change", function() {
$("#cptui_select_taxonomy").submit();
});
$(".cptui-delete-top, .cptui-delete-bottom").on("click", function(e) {
e.preventDefault();
var msg = "";
if (typeof cptui_type_data !== "undefined") {
msg = cptui_type_data.confirm;
} else if (typeof cptui_tax_data !== "undefined") {
msg = cptui_tax_data.confirm;
}
var submit_delete_warning = $('<div class="cptui-submit-delete-dialog">' + msg + "</div>").appendTo("#poststuff").dialog({
dialogClass: "wp-dialog",
modal: true,
autoOpen: true,
buttons: {
OK: function() {
var form = $(e.target).closest("form");
$(e.target).off("click").click();
},
Cancel: function() {
$(this).dialog("close");
}
}
});
});
$("#support .question").each(function() {
var tis = $(this), state = false, answer = tis.next("div").slideUp();
tis.on("click keydown", function(e) {
if (e.type === "keydown" && e.keyCode !== 32 && e.keyCode !== 13) {
return;
}
e.preventDefault();
state = !state;
answer.slideToggle(state);
tis.toggleClass("active", state);
tis.attr("aria-expanded", state.toString());
tis.focus();
});
});
$("#name").on("keyup", function(e) {
var value, original_value;
value = original_value = $(this).val();
if (e.keyCode !== 9 && e.keyCode !== 37 && e.keyCode !== 38 && e.keyCode !== 39 && e.keyCode !== 40) {
value = value.replace(/ /g, "_");
value = value.toLowerCase();
value = replaceDiacritics(value);
value = transliterate(value);
value = replaceSpecialCharacters(value);
if (value !== original_value) {
$(this).prop("value", value);
}
}
if (typeof original_slug !== "undefined") {
var $slugchanged = $("#slugchanged");
if (value != original_slug) {
$slugchanged.removeClass("hidemessage");
} else {
$slugchanged.addClass("hidemessage");
}
}
var $slugexists = $("#slugexists");
if (typeof cptui_type_data != "undefined") {
if (cptui_type_data.existing_post_types.hasOwnProperty(value) && value !== original_slug) {
$slugexists.removeClass("hidemessage");
} else {
$slugexists.addClass("hidemessage");
}
}
if (typeof cptui_tax_data != "undefined") {
if (cptui_tax_data.existing_taxonomies.hasOwnProperty(value) && value !== original_slug) {
$slugexists.removeClass("hidemessage");
} else {
$slugexists.addClass("hidemessage");
}
}
});
function replaceDiacritics(s) {
var diacritics = [ /[\300-\306]/g, /[\340-\346]/g, /[\310-\313]/g, /[\350-\353]/g, /[\314-\317]/g, /[\354-\357]/g, /[\322-\330]/g, /[\362-\370]/g, /[\331-\334]/g, /[\371-\374]/g, /[\321]/g, /[\361]/g, /[\307]/g, /[\347]/g ];
var chars = [ "A", "a", "E", "e", "I", "i", "O", "o", "U", "u", "N", "n", "C", "c" ];
for (var i = 0; i < diacritics.length; i++) {
s = s.replace(diacritics[i], chars[i]);
}
return s;
}
function replaceSpecialCharacters(s) {
if ("cpt-ui_page_cptui_manage_post_types" === window.pagenow) {
s = s.replace(/[^a-z0-9\s-]/gi, "_");
} else {
s = s.replace(/[^a-z0-9\s]/gi, "_");
}
return s;
}
function composePreviewContent(value) {
var re = /(http|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/;
var is_url = re.test(value);
if (!value) {
return "";
} else if (0 === value.indexOf("dashicons-")) {
return $('<div class="dashicons-before"><br></div>').addClass(htmlEncode(value));
} else if (is_url) {
var imgsrc = encodeURI(value);
var theimg = document.createElement("IMG");
theimg.src = imgsrc;
return theimg;
}
}
function htmlEncode(str) {
return String(str).replace(/[^-\w. ]/gi, function(c) {
return "&#" + c.charCodeAt(0) + ";";
});
}
var cyrillic = {
"Ё": "YO",
"Й": "I",
"Ц": "TS",
"У": "U",
"К": "K",
"Е": "E",
"Н": "N",
"Г": "G",
"Ш": "SH",
"Щ": "SCH",
"З": "Z",
"Х": "H",
"Ъ": "'",
"ё": "yo",
"й": "i",
"ц": "ts",
"у": "u",
"к": "k",
"е": "e",
"н": "n",
"г": "g",
"ш": "sh",
"щ": "sch",
"з": "z",
"х": "h",
"ъ": "'",
"Ф": "F",
"Ы": "I",
"В": "V",
"А": "a",
"П": "P",
"Р": "R",
"О": "O",
"Л": "L",
"Д": "D",
"Ж": "ZH",
"Э": "E",
"ф": "f",
"ы": "i",
"в": "v",
"а": "a",
"п": "p",
"р": "r",
"о": "o",
"л": "l",
"д": "d",
"ж": "zh",
"э": "e",
"Я": "Ya",
"Ч": "CH",
"С": "S",
"М": "M",
"И": "I",
"Т": "T",
"Ь": "'",
"Б": "B",
"Ю": "YU",
"я": "ya",
"ч": "ch",
"с": "s",
"м": "m",
"и": "i",
"т": "t",
"ь": "'",
"б": "b",
"ю": "yu"
};
function transliterate(word) {
return word.split("").map(function(char) {
return cyrillic[char] || char;
}).join("");
}
if (undefined != wp.media) {
var _custom_media = true, _orig_send_attachment = wp.media.editor.send.attachment;
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
$("#cptui_choose_icon").on("click", function(e) {
e.preventDefault();
var button = $(this);
var id = jQuery("#menu_icon").attr("id");
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment) {
if (_custom_media) {
$("#" + id).val(attachment.url).change();
} else {
return _orig_send_attachment.apply(this, [ props, attachment ]);
}
};
wp.media.editor.open(button);
return false;
});
$("#menu_icon").on("change", function() {
var value = $(this).val();
value = value.trim();
$("#menu_icon_preview").html(composePreviewContent(value));
});
$(".cptui-help").on("click", function(e) {
e.preventDefault();
});
$(".cptui-taxonomy-submit").on("click", function(e) {
if ($(".cptui-table :checkbox:checked").length == 0) {
e.preventDefault();
var no_associated_type_warning = $('<div class="cptui-taxonomy-empty-types-dialog">' + cptui_tax_data.no_associated_type + "</div>").appendTo("#poststuff").dialog({
dialogClass: "wp-dialog",
modal: true,
autoOpen: true,
buttons: {
OK: function() {
$(this).dialog("close");
}
}
});
}
});
$("#auto-populate").on("click tap", function(e) {
e.preventDefault();
var slug = $("#name").val();
var plural = $("#label").val();
var singular = $("#singular_label").val();
var fields = $('.cptui-labels input[type="text"]');
if ("" === slug) {
return;
}
if ("" === plural) {
plural = slug;
}
if ("" === singular) {
singular = slug;
}
$(fields).each(function(i, el) {
var newval = $(el).data("label");
var plurality = $(el).data("plurality");
if ("undefined" !== newval) {
if ("plural" === plurality) {
newval = newval.replace(/item/gi, plural);
} else {
newval = newval.replace(/item/gi, singular);
}
if ($(el).val() === "") {
$(el).val(newval);
}
}
});
});
$("#auto-clear").on("click tap", function(e) {
e.preventDefault();
var fields = $('.cptui-labels input[type="text"]');
$(fields).each(function(i, el) {
$(el).val("");
});
});
$(".cptui-delete-post-type, .cptui-delete-taxonomy").on("click tap", function(e) {
e.preventDefault();
let msg = "";
let href = $(this).attr("href");
if (typeof cptui_listings_data !== "undefined") {
msg = cptui_listings_data.confirm;
}
let submit_delete_warning = $('<div class="cptui-submit-delete-dialog">' + msg + "</div>").appendTo(".cptui-listings").dialog({
dialogClass: "wp-dialog",
modal: true,
autoOpen: true,
buttons: {
OK: function() {
window.location.href = href;
},
Cancel: function() {
$(this).dialog("close");
}
}
});
});
})(jQuery);