cptui.js
4 years ago
cptui.min.js
4 years ago
dashicons-picker.js
4 years ago
dashicons-picker.min.js
4 years ago
cptui.js
293 lines
| 1 | /** |
| 2 | * Add collapseable boxes to our editor screens. |
| 3 | */ |
| 4 | postboxes.add_postbox_toggles(pagenow); |
| 5 | |
| 6 | /** |
| 7 | * The rest of our customizations. |
| 8 | */ |
| 9 | (function($) { |
| 10 | |
| 11 | $('#cptui_select_post_type_submit').hide(); |
| 12 | $('#cptui_select_taxonomy_submit').hide(); |
| 13 | |
| 14 | if ('edit' === getParameterByName('action')) { |
| 15 | // Store our original slug on page load for edit checking. |
| 16 | var original_slug = $('#name').val(); |
| 17 | } |
| 18 | |
| 19 | // Automatically toggle the "page attributes" checkbox if |
| 20 | // setting a hierarchical post type. |
| 21 | $('#hierarchical').on('change', function() { |
| 22 | var hierarchical = $(this).val(); |
| 23 | if ('1' === hierarchical) { |
| 24 | $('#page-attributes').prop('checked', true); |
| 25 | } else { |
| 26 | $('#page-attributes').prop('checked', false); |
| 27 | } |
| 28 | }); |
| 29 | |
| 30 | // Switch to newly selected post type or taxonomy automatically. |
| 31 | $('#post_type').on('change',function(){ |
| 32 | $('#cptui_select_post_type').submit(); |
| 33 | }); |
| 34 | |
| 35 | $('#taxonomy').on('change',function(){ |
| 36 | $( '#cptui_select_taxonomy' ).submit(); |
| 37 | }); |
| 38 | |
| 39 | // Confirm our deletions |
| 40 | $('.cptui-delete-top, .cptui-delete-bottom').on('click',function(e) { |
| 41 | e.preventDefault(); |
| 42 | var msg = ''; |
| 43 | if (typeof cptui_type_data !== 'undefined') { |
| 44 | msg = cptui_type_data.confirm; |
| 45 | } else if (typeof cptui_tax_data !== 'undefined') { |
| 46 | msg = cptui_tax_data.confirm; |
| 47 | } |
| 48 | var submit_delete_warning = $('<div class="cptui-submit-delete-dialog">' + msg + '</div>').appendTo('#poststuff').dialog({ |
| 49 | 'dialogClass' : 'wp-dialog', |
| 50 | 'modal' : true, |
| 51 | 'autoOpen' : true, |
| 52 | 'buttons' : { |
| 53 | "OK": function() { |
| 54 | var form = $(e.target).closest('form'); |
| 55 | $(e.target).off('click').click(); |
| 56 | }, |
| 57 | "Cancel": function() { |
| 58 | $(this).dialog('close'); |
| 59 | } |
| 60 | } |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | // Toggles help/support accordions. |
| 65 | $('#support .question').each(function() { |
| 66 | var tis = $(this), state = false, answer = tis.next('div').slideUp(); |
| 67 | tis.on('click keydown',function(e) { |
| 68 | // Helps with accessibility and keyboard navigation. |
| 69 | if(e.type==='keydown' && e.keyCode!==32 && e.keyCode!==13) { |
| 70 | return; |
| 71 | } |
| 72 | e.preventDefault(); |
| 73 | state = !state; |
| 74 | answer.slideToggle(state); |
| 75 | tis.toggleClass('active',state); |
| 76 | tis.attr('aria-expanded', state.toString() ); |
| 77 | tis.focus(); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | // Switch spaces for underscores on our slug fields. |
| 82 | $('#name').on('keyup',function(e){ |
| 83 | var value, original_value; |
| 84 | value = original_value = $(this).val(); |
| 85 | if ( e.keyCode !== 9 && e.keyCode !== 37 && e.keyCode !== 38 && e.keyCode !== 39 && e.keyCode !== 40 ) { |
| 86 | value = value.replace(/ /g, "_"); |
| 87 | value = value.toLowerCase(); |
| 88 | value = replaceDiacritics(value); |
| 89 | value = transliterate(value); |
| 90 | value = replaceSpecialCharacters(value); |
| 91 | if ( value !== original_value ) { |
| 92 | $(this).prop('value', value); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | //Displays a message if slug changes. |
| 97 | if(typeof original_slug !== 'undefined') { |
| 98 | var $slugchanged = $('#slugchanged'); |
| 99 | if(value != original_slug) { |
| 100 | $slugchanged.removeClass('hidemessage'); |
| 101 | } else { |
| 102 | $slugchanged.addClass('hidemessage'); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | var $slugexists = $('#slugexists'); |
| 107 | if ( typeof cptui_type_data != 'undefined' ) { |
| 108 | if (cptui_type_data.existing_post_types.hasOwnProperty(value) && value !== original_slug) { |
| 109 | $slugexists.removeClass('hidemessage'); |
| 110 | } else { |
| 111 | $slugexists.addClass('hidemessage'); |
| 112 | } |
| 113 | } |
| 114 | if ( typeof cptui_tax_data != 'undefined' ) { |
| 115 | if (cptui_tax_data.existing_taxonomies.hasOwnProperty(value) && value !== original_slug) { |
| 116 | $slugexists.removeClass('hidemessage'); |
| 117 | } else { |
| 118 | $slugexists.addClass('hidemessage'); |
| 119 | } |
| 120 | } |
| 121 | }); |
| 122 | |
| 123 | // Replace diacritic characters with latin characters. |
| 124 | function replaceDiacritics(s) { |
| 125 | var diacritics = [ |
| 126 | /[\300-\306]/g, /[\340-\346]/g, // A, a |
| 127 | /[\310-\313]/g, /[\350-\353]/g, // E, e |
| 128 | /[\314-\317]/g, /[\354-\357]/g, // I, i |
| 129 | /[\322-\330]/g, /[\362-\370]/g, // O, o |
| 130 | /[\331-\334]/g, /[\371-\374]/g, // U, u |
| 131 | /[\321]/g, /[\361]/g, // N, n |
| 132 | /[\307]/g, /[\347]/g // C, c |
| 133 | ]; |
| 134 | |
| 135 | var chars = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u', 'N', 'n', 'C', 'c']; |
| 136 | |
| 137 | for (var i = 0; i < diacritics.length; i++) { |
| 138 | s = s.replace(diacritics[i], chars[i]); |
| 139 | } |
| 140 | |
| 141 | return s; |
| 142 | } |
| 143 | |
| 144 | function replaceSpecialCharacters(s) { |
| 145 | if ( 'cpt-ui_page_cptui_manage_post_types' === window.pagenow ) { |
| 146 | s = s.replace(/[^a-z0-9\s-]/gi, '_'); |
| 147 | } else { |
| 148 | s = s.replace(/[^a-z0-9\s]/gi, '_'); |
| 149 | } |
| 150 | |
| 151 | return s; |
| 152 | } |
| 153 | |
| 154 | function composePreviewContent(value) { |
| 155 | |
| 156 | var re = /(http|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/; |
| 157 | var is_url = re.test(value); |
| 158 | |
| 159 | if (!value) { |
| 160 | return ''; |
| 161 | } else if (0 === value.indexOf('dashicons-')) { |
| 162 | return $('<div class="dashicons-before"><br></div>').addClass(htmlEncode(value)); |
| 163 | } else if ( is_url ) { |
| 164 | var imgsrc = encodeURI(value); |
| 165 | var theimg = document.createElement('IMG'); |
| 166 | theimg.src = imgsrc; |
| 167 | return theimg; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function htmlEncode(str) { |
| 172 | return String(str).replace(/[^-\w. ]/gi, function (c) { |
| 173 | return '&#' + c.charCodeAt(0) + ';'; |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | var cyrillic = { |
| 178 | "Ё": "YO", "Й": "I", "Ц": "TS", "У": "U", "К": "K", "Е": "E", "Н": "N", "Г": "G", "Ш": "SH", "Щ": "SCH", "З": "Z", "Х": "H", "Ъ": "'", "ё": "yo", "й": "i", "ц": "ts", "у": "u", "к": "k", "е": "e", "н": "n", "г": "g", "ш": "sh", "щ": "sch", "з": "z", "� |
| 179 | ": "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" |
| 180 | }; |
| 181 | |
| 182 | function transliterate(word) { |
| 183 | return word.split('').map(function (char) { |
| 184 | return cyrillic[char] || char; |
| 185 | }).join(""); |
| 186 | } |
| 187 | |
| 188 | if ( undefined != wp.media ) { |
| 189 | var _custom_media = true, |
| 190 | _orig_send_attachment = wp.media.editor.send.attachment; |
| 191 | } |
| 192 | |
| 193 | function getParameterByName(name, url) { |
| 194 | if (!url) url = window.location.href; |
| 195 | name = name.replace(/[\[\]]/g, "\\$&"); |
| 196 | var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), |
| 197 | results = regex.exec(url); |
| 198 | if (!results) return null; |
| 199 | if (!results[2]) return ''; |
| 200 | return decodeURIComponent(results[2].replace(/\+/g, " ")); |
| 201 | } |
| 202 | |
| 203 | $('#cptui_choose_icon').on('click',function(e){ |
| 204 | e.preventDefault(); |
| 205 | |
| 206 | var button = $(this); |
| 207 | var id = jQuery('#menu_icon').attr('id'); |
| 208 | _custom_media = true; |
| 209 | wp.media.editor.send.attachment = function (props, attachment) { |
| 210 | if (_custom_media) { |
| 211 | $("#" + id).val(attachment.url).change(); |
| 212 | } else { |
| 213 | return _orig_send_attachment.apply(this, [props, attachment]); |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | wp.media.editor.open(button); |
| 218 | return false; |
| 219 | }); |
| 220 | |
| 221 | $('#menu_icon').on('change', function () { |
| 222 | var value = $(this).val(); |
| 223 | value = value.trim(); |
| 224 | $('#menu_icon_preview').html(composePreviewContent(value)); |
| 225 | }); |
| 226 | |
| 227 | $('.cptui-help').on('click',function(e){ |
| 228 | e.preventDefault(); |
| 229 | }); |
| 230 | |
| 231 | $('.cptui-taxonomy-submit').on('click',function(e){ |
| 232 | if ( $('.cptui-table :checkbox:checked').length == 0 ) { |
| 233 | e.preventDefault(); |
| 234 | var no_associated_type_warning = $('<div class="cptui-taxonomy-empty-types-dialog">' + cptui_tax_data.no_associated_type + '</div>').appendTo('#poststuff').dialog({ |
| 235 | 'dialogClass' : 'wp-dialog', |
| 236 | 'modal' : true, |
| 237 | 'autoOpen' : true, |
| 238 | 'buttons' : { |
| 239 | "OK": function() { |
| 240 | $(this).dialog('close'); |
| 241 | } |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | }); |
| 246 | |
| 247 | $('#auto-populate').on( 'click tap', function(e){ |
| 248 | e.preventDefault(); |
| 249 | |
| 250 | var slug = $('#name').val(); |
| 251 | var plural = $('#label').val(); |
| 252 | var singular = $('#singular_label').val(); |
| 253 | var fields = $('.cptui-labels input[type="text"]'); |
| 254 | |
| 255 | if ( '' === slug ) { |
| 256 | return; |
| 257 | } |
| 258 | if ( '' === plural ) { |
| 259 | plural = slug; |
| 260 | } |
| 261 | if ( '' === singular ) { |
| 262 | singular = slug; |
| 263 | } |
| 264 | |
| 265 | $(fields).each( function( i, el ) { |
| 266 | var newval = $( el ).data( 'label' ); |
| 267 | var plurality = $( el ).data( 'plurality' ); |
| 268 | if ( 'undefined' !== newval ) { |
| 269 | // "slug" is our placeholder from the labels. |
| 270 | if ( 'plural' === plurality ) { |
| 271 | newval = newval.replace(/item/gi, plural); |
| 272 | } else { |
| 273 | newval = newval.replace(/item/gi, singular); |
| 274 | } |
| 275 | if ( $( el ).val() === '' ) { |
| 276 | $(el).val(newval); |
| 277 | } |
| 278 | } |
| 279 | } ); |
| 280 | }); |
| 281 | |
| 282 | $('#auto-clear').on( 'click tap', function(e) { |
| 283 | e.preventDefault(); |
| 284 | |
| 285 | var fields = $('.cptui-labels input[type="text"]'); |
| 286 | |
| 287 | $(fields).each( function( i, el ) { |
| 288 | $(el).val(''); |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | })(jQuery); |
| 293 |