| 1 |
jQuery( function( $ ) { |
| 2 |
$( '.smart-cf-fields-wrapper' ).each( function( i, e ) { |
| 3 |
var wrapper = $( e ); |
| 4 |
var btn_add_group = wrapper.find( '.btn-add-group' ); |
| 5 |
var btn_remove_group = wrapper.find( '.btn-remove-group' ); |
| 6 |
var btn_add_field = wrapper.find( '.btn-add-field' ); |
| 7 |
var btn_remove_field = wrapper.find( '.btn-remove-field' ); |
| 8 |
var group_class = '.smart-cf-group'; |
| 9 |
var field_class = '.smart-cf-field'; |
| 10 |
var duplicate_alert_class = '.smart-cf-duplicate-alert'; |
| 11 |
var options = wrapper.find( '.smart-cf-field-options' ); |
| 12 |
var cnt = wrapper.find( field_class ).length; |
| 13 |
|
| 14 |
/** |
| 15 |
* 重複エラーメッセージ表示 & 重複時の更新不可 |
| 16 |
*/ |
| 17 |
wrapper.find( 'input[class="smart-cf-group-name"], input[class="smart-cf-field-name"]' ).keyup( function() { |
| 18 |
var val = $( this ).val(); |
| 19 |
var cnt = 0; |
| 20 |
wrapper.find( 'input[class="smart-cf-group-name"], input[class="smart-cf-field-name"]' ).each( function( i, e ) { |
| 21 |
if ( val === $( this ).val() && val !== '' ) { |
| 22 |
cnt ++; |
| 23 |
} |
| 24 |
} ); |
| 25 |
if ( cnt > 1 ) { |
| 26 |
if ( $( this ).siblings( duplicate_alert_class ).length < 1 ) { |
| 27 |
$( this ).after( |
| 28 |
$( '<div class="smart-cf-alert" />' ) |
| 29 |
.addClass( duplicate_alert_class.replace( '.', '' ) ) |
| 30 |
.text( smart_cf_settings.duplicate_alert ) |
| 31 |
); |
| 32 |
} |
| 33 |
cnt = 0; |
| 34 |
} else { |
| 35 |
$( this ).siblings( duplicate_alert_class ).remove(); |
| 36 |
} |
| 37 |
|
| 38 |
if ( $( duplicate_alert_class ).length ) { |
| 39 |
$( '#publish' ).attr( 'disabled', 'disabled' ); |
| 40 |
} else { |
| 41 |
$( '#publish' ).removeAttr( 'disabled' ); |
| 42 |
} |
| 43 |
} ); |
| 44 |
|
| 45 |
/** |
| 46 |
* sortable |
| 47 |
*/ |
| 48 |
$( '.smart-cf-groups' ).sortable( { |
| 49 |
cursor: 'move', |
| 50 |
handle: '.smart-cf-icon-handle' |
| 51 |
} ); |
| 52 |
$( '.smart-cf-fields' ).sortable( { |
| 53 |
cursor: 'move', |
| 54 |
handle: '.smart-cf-icon-handle' |
| 55 |
} ); |
| 56 |
|
| 57 |
/** |
| 58 |
* フィールドの開閉 |
| 59 |
*/ |
| 60 |
$( '.field-label' ).click( function() { |
| 61 |
var field_label = $( this ); |
| 62 |
var table = $( this ).parents( field_class ).find( 'table' ); |
| 63 |
if ( table.hasClass( 'hide' ) ) { |
| 64 |
field_label.html( " " ); |
| 65 |
table.fadeIn( 'fast', function() { |
| 66 |
$( this ).removeClass( 'hide' ); |
| 67 |
table.find( '.smart-cf-field-options' ).each( function( i, e ) { |
| 68 |
$( this ).trigger( 'smart-cf-setting-field-open', e ); |
| 69 |
} ); |
| 70 |
} ); |
| 71 |
} else { |
| 72 |
var field_options = table.find( '.smart-cf-field-options:visible' ); |
| 73 |
var label = field_options.find( '.smart-cf-field-label' ).val(); |
| 74 |
var name = $( '<small>' ).text( '[ ' + field_options.find('.smart-cf-field-name').val() + ' ]' ); |
| 75 |
|
| 76 |
if ( !label ) { |
| 77 |
label = field_options.find( '.smart-cf-field-name' ).val(); |
| 78 |
} |
| 79 |
table.fadeOut( 'fast', function() { |
| 80 |
$( this ).addClass( 'hide' ); |
| 81 |
field_label.text( label + " " ).append( name ); |
| 82 |
} ); |
| 83 |
} |
| 84 |
} ); |
| 85 |
|
| 86 |
/** |
| 87 |
* グループ追加ボタン |
| 88 |
*/ |
| 89 |
btn_add_group.click( function() { |
| 90 |
cnt ++; |
| 91 |
var group = wrapper.find( group_class ); |
| 92 |
var group_clone = group.first().clone( true, true ); |
| 93 |
group.last().after( group_clone.fadeIn( 'fast', function() { |
| 94 |
$( this ).removeClass( 'hide' ); |
| 95 |
} ) ); |
| 96 |
|
| 97 |
var field = group_clone.find( field_class ); |
| 98 |
var field_clone = field.first().clone( true, true ); |
| 99 |
field.last().after( field_clone.removeClass( 'hide' ) ); |
| 100 |
|
| 101 |
group_clone.find( 'input, select, textarea' ).each( function( i, e ) { |
| 102 |
$( this ).attr( 'name', |
| 103 |
$( this ).attr( 'name' ).replace( |
| 104 |
/^(smart-custom-fields)\[\d+\]/, |
| 105 |
'$1[' + cnt + ']' |
| 106 |
) |
| 107 |
); |
| 108 |
} ); |
| 109 |
|
| 110 |
field_clone.find( 'input, select, textarea' ).each( function( i, e ) { |
| 111 |
$( this ).attr( 'name', |
| 112 |
$( this ).attr( 'name' ).replace( |
| 113 |
/^(smart-custom-fields)\[.+?\](\[fields\])\[\d+?\]/, |
| 114 |
'$1[' + cnt + ']$2[' + cnt + ']' |
| 115 |
) |
| 116 |
); |
| 117 |
} ); |
| 118 |
} ); |
| 119 |
|
| 120 |
/** |
| 121 |
* グループ削除ボタン |
| 122 |
*/ |
| 123 |
btn_remove_group.click( function() { |
| 124 |
$( this ).parents( group_class ).fadeOut( 'fast', function() { |
| 125 |
$( this ).remove(); |
| 126 |
} ); |
| 127 |
} ); |
| 128 |
|
| 129 |
/** |
| 130 |
* フィールド追加ボタン |
| 131 |
*/ |
| 132 |
btn_add_field.click( function() { |
| 133 |
cnt ++; |
| 134 |
var group = $( this ).parents( group_class ); |
| 135 |
var field = group.find( field_class ); |
| 136 |
var clone = field.first().clone( true, true ); |
| 137 |
field.last().after( clone.fadeIn( 'fast', function() { |
| 138 |
$( this ).removeClass( 'hide' ); |
| 139 |
} ) ); |
| 140 |
|
| 141 |
clone.find( 'input, select, textarea' ).each( function( i, e ) { |
| 142 |
$( this ).attr( 'name', |
| 143 |
$( this ).attr( 'name' ).replace( |
| 144 |
/^(smart-custom-fields\[.+?\]\[fields\])\[\d+?\]/, |
| 145 |
'$1[' + cnt + ']' |
| 146 |
) |
| 147 |
); |
| 148 |
} ); |
| 149 |
} ); |
| 150 |
|
| 151 |
/** |
| 152 |
* フィールド削除ボタン |
| 153 |
*/ |
| 154 |
btn_remove_field.click( function() { |
| 155 |
$( this ).parents( field_class ).fadeOut( 'fast', function() { |
| 156 |
$( this ).remove(); |
| 157 |
} ); |
| 158 |
} ); |
| 159 |
|
| 160 |
/** |
| 161 |
* 選択� |
| 162 |
目オプション |
| 163 |
*/ |
| 164 |
options.find( 'input, textarea, select' ).attr( 'disabled', 'disabled' ); |
| 165 |
wrapper.find( '.smart-cf-field-select' ).each( function( i, e ) { |
| 166 |
var selected_type = $( this ).val(); |
| 167 |
$( this ).parents( field_class ).find( '.smart-cf-field-options-' + selected_type ) |
| 168 |
.removeClass( 'hide' ) |
| 169 |
.find( 'input, textarea, select' ).removeAttr( 'disabled' ); |
| 170 |
} ); |
| 171 |
|
| 172 |
wrapper.find( '.smart-cf-field-select' ).change( function() { |
| 173 |
var field = $( this ).parents( field_class ); |
| 174 |
var val = $( this ).val(); |
| 175 |
|
| 176 |
var hide_options = field.find( '.smart-cf-field-options' ); |
| 177 |
hide_options.addClass( 'hide' ); |
| 178 |
hide_options.find( 'input, textarea, select' ).attr( 'disabled', 'disabled' ); |
| 179 |
|
| 180 |
var show_options = field.find( '.smart-cf-field-options-' + val ); |
| 181 |
show_options.find( 'input, textarea, select' ).removeAttr( 'disabled' ); |
| 182 |
show_options.removeClass( 'hide' ); |
| 183 |
show_options.trigger( 'smart-cf-setting-show-options', show_options ); |
| 184 |
} ); |
| 185 |
|
| 186 |
/** |
| 187 |
* リピートボタンクリック時 |
| 188 |
*/ |
| 189 |
wrapper.find( '.smart-cf-group-repeat input' ).click( function() { |
| 190 |
var group = $( this ).parents( group_class ); |
| 191 |
var names = group.find( '.smart-cf-group-names' ); |
| 192 |
var btn_add_field = group.find( '.btn-add-field' ); |
| 193 |
if ( $( this ).prop( 'checked' ) ) { |
| 194 |
names.removeClass( 'hide' ); |
| 195 |
btn_add_field.removeClass( 'hide' ); |
| 196 |
} else { |
| 197 |
names.addClass( 'hide' ); |
| 198 |
btn_add_field.addClass( 'hide' ); |
| 199 |
} |
| 200 |
} ); |
| 201 |
|
| 202 |
/** |
| 203 |
* Convert string to slug |
| 204 |
* https://gist.github.com/codeguy/6684588 |
| 205 |
*/ |
| 206 |
function string_to_slug(str) { |
| 207 |
str = str.replace(/^\s+|\s+$/g, ""); // trim |
| 208 |
str = str.toLowerCase(); |
| 209 |
|
| 210 |
// remove accents, swap ñ for n, etc |
| 211 |
var from = "åàáãäâèéëêìíïîòóöôùúüûñç·/-,:;"; |
| 212 |
var to = "aaaaaaeeeeiiiioooouuuunc______"; |
| 213 |
|
| 214 |
for (var i = 0, l = from.length; i < l; i++) { |
| 215 |
str = str.replace(new RegExp(from.charAt(i), "g"), to.charAt(i)); |
| 216 |
} |
| 217 |
|
| 218 |
str = str |
| 219 |
.replace(/[^a-z0-9 -]/g, "") // remove invalid chars |
| 220 |
.replace(/\s+/g, "_") // collapse whitespace and replace by - |
| 221 |
.replace(/-+/g, "_"); // collapse dashes |
| 222 |
|
| 223 |
return str; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* フィールド名� |
| 228 |
�力ボックス |
| 229 |
*/ |
| 230 |
wrapper.find( '.smart-cf-field-name' ).focus( function() { |
| 231 |
var field = $( this ).parents( '.smart-cf-field-options' ); |
| 232 |
var label_val = field.find( '.smart-cf-field-label' ).val(); |
| 233 |
var name_val = $( this ).val(); |
| 234 |
if ( label_val && !name_val) { |
| 235 |
$( this ).val( string_to_slug(label_val) ); |
| 236 |
} |
| 237 |
} ); |
| 238 |
} ); |
| 239 |
|
| 240 |
/** |
| 241 |
* Add autocomplete (selectivity plguin) in posts condition field |
| 242 |
* https://github.com/arendjr/selectivity |
| 243 |
*/ |
| 244 |
$('#smart-cf-autocomplete-condition-post').selectivity({ |
| 245 |
data: smart_cf_saved_posts, |
| 246 |
multiple: true, |
| 247 |
placeholder: smart_cf_settings.autocomplete_placeholder, |
| 248 |
ajax: { |
| 249 |
url: smart_cf_settings.rest_api_url, |
| 250 |
quietMillis: 200, |
| 251 |
params: function(term, offset) { |
| 252 |
return { |
| 253 |
_wpnonce: smart_cf_settings.nonce, |
| 254 |
}; |
| 255 |
}, |
| 256 |
}, |
| 257 |
templates: { |
| 258 |
multipleSelectInput: function(options) { |
| 259 |
return ( |
| 260 |
'<div class="selectivity-multiple-input-container">' + |
| 261 |
(options.enabled ? |
| 262 |
'<input type="text" autocomplete="off" autocorrect="off" ' + |
| 263 |
'autocapitalize="off" class="selectivity-multiple-input">' : |
| 264 |
'<div class="selectivity-multiple-input ' + 'selectivity-placeholder"></div>') + |
| 265 |
'<div class="selectivity-clearfix"></div>' + |
| 266 |
'</div>' |
| 267 |
); |
| 268 |
}, |
| 269 |
multipleSelectedItem: function(options) { |
| 270 |
var extraClass = options.highlighted ? ' highlighted' : ''; |
| 271 |
return ( |
| 272 |
'<span class="selectivity-multiple-selected-item button button-primary' + |
| 273 |
extraClass + |
| 274 |
'" ' + |
| 275 |
'data-item-id="' + |
| 276 |
options.id + |
| 277 |
'">' + |
| 278 |
(options.removable ? |
| 279 |
'<a class="selectivity-multiple-selected-item-remove">' + |
| 280 |
'x' + |
| 281 |
'</a>' : |
| 282 |
'') + |
| 283 |
options.id + |
| 284 |
'</span>' |
| 285 |
); //options.text |
| 286 |
}, |
| 287 |
dropdown: function(options) { |
| 288 |
var extraClass = options.dropdownCssClass ? ' ' + options.dropdownCssClass : '', |
| 289 |
searchInput = ''; |
| 290 |
if (options.showSearchInput) { |
| 291 |
extraClass += ' has-search-input'; |
| 292 |
var placeholder = options.searchInputPlaceholder; |
| 293 |
searchInput = |
| 294 |
'<div class="selectivity-search-input-container">' + |
| 295 |
'<input type="text" class="selectivity-search-input"' + |
| 296 |
(placeholder ? ' placeholder="' + escape(placeholder) + '"' : '') + |
| 297 |
'>' + |
| 298 |
'</div>'; |
| 299 |
} |
| 300 |
|
| 301 |
return ( |
| 302 |
'<div class="selectivity-dropdown' + |
| 303 |
extraClass + |
| 304 |
'">' + |
| 305 |
searchInput + |
| 306 |
'<div class="selectivity-results-container"></div>' + |
| 307 |
'</div>' |
| 308 |
); |
| 309 |
}, |
| 310 |
loading: function() { |
| 311 |
return '<div class="selectivity-loading">' + smart_cf_settings.loading + '</div>'; |
| 312 |
}, |
| 313 |
loadMore: function() { |
| 314 |
return '<div class="selectivity-load-more">' + smart_cf_settings.load_more + '</div>'; |
| 315 |
}, |
| 316 |
} |
| 317 |
}); |
| 318 |
$('#smart-cf-autocomplete-condition-post').on('change', function() { |
| 319 |
var data = $(this).selectivity('value'); |
| 320 |
$('[name="smart-cf-condition-post-ids"]').val(data); |
| 321 |
}); |
| 322 |
|
| 323 |
/** |
| 324 |
* Add IOS style for checkboxes |
| 325 |
*/ |
| 326 |
$('.smart-cf-group .smart-cf-group-repeat label, #smart-cf-meta-box-condition-post, #smart-cf-meta-box-condition-profile, #smart-cf-meta-box-condition-taxonomy, #smart-cf-meta-box-condition-options-page') |
| 327 |
.find('input[type=checkbox]') |
| 328 |
.iosCheckbox(); |
| 329 |
|
| 330 |
} ); |
| 331 |
|