bindings
6 months ago
commands
3 weeks ago
pro
3 weeks ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
8 months ago
_acf-condition.js
1 year ago
_acf-conditions.js
1 year ago
_acf-field-accordion.js
6 months ago
_acf-field-button-group.js
8 months ago
_acf-field-checkbox.js
1 month ago
_acf-field-color-picker.js
8 months ago
_acf-field-date-picker.js
1 month ago
_acf-field-date-time-picker.js
1 month ago
_acf-field-file.js
1 month ago
_acf-field-google-map.js
6 months ago
_acf-field-icon-picker.js
1 month ago
_acf-field-image.js
1 month ago
_acf-field-link.js
1 year ago
_acf-field-oembed.js
1 month ago
_acf-field-page-link.js
1 year ago
_acf-field-post-object.js
1 year ago
_acf-field-radio.js
1 month ago
_acf-field-range.js
1 year ago
_acf-field-relationship.js
1 month ago
_acf-field-select.js
1 month ago
_acf-field-tab.js
1 month ago
_acf-field-taxonomy.js
8 months ago
_acf-field-time-picker.js
1 month ago
_acf-field-true-false.js
1 month ago
_acf-field-url.js
1 year ago
_acf-field-user.js
1 year ago
_acf-field-wysiwyg.js
1 month ago
_acf-field.js
1 year ago
_acf-fields.js
1 year ago
_acf-helpers.js
1 year ago
_acf-hooks.js
1 year ago
_acf-internal-post-type.js
8 months ago
_acf-media.js
3 weeks ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
8 months ago
_acf-panel.js
1 year ago
_acf-popup.js
8 months ago
_acf-postbox.js
1 year ago
_acf-screen.js
10 months ago
_acf-select2.js
1 year ago
_acf-tinymce.js
6 months ago
_acf-tooltip.js
10 months ago
_acf-unload.js
1 year ago
_acf-validation.js
1 month ago
_acf.js
7 months ago
_browse-fields-modal.js
8 months ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
4 months ago
_field-group-fields.js
10 months ago
_field-group-locations.js
1 year ago
_field-group-settings.js
1 year ago
_field-group.js
10 months ago
acf-escaped-html-notice.js
1 year ago
acf-field-group.js
1 year ago
acf-input.js
1 year ago
acf-internal-post-type.js
1 year ago
acf.js
1 year ago
_acf-field-checkbox.js
147 lines
| 1 | ( function ( $, undefined ) { |
| 2 | var Field = acf.Field.extend( { |
| 3 | type: 'checkbox', |
| 4 | |
| 5 | events: { |
| 6 | 'change input': 'onChange', |
| 7 | 'click .acf-add-checkbox': 'onClickAdd', |
| 8 | 'click .acf-checkbox-toggle': 'onClickToggle', |
| 9 | 'click .acf-checkbox-custom': 'onClickCustom', |
| 10 | 'keydown input[type="checkbox"]': 'onKeyDownInput', |
| 11 | }, |
| 12 | |
| 13 | $control: function () { |
| 14 | return this.$( '.acf-checkbox-list' ); |
| 15 | }, |
| 16 | |
| 17 | $toggle: function () { |
| 18 | return this.$( '.acf-checkbox-toggle' ); |
| 19 | }, |
| 20 | |
| 21 | $input: function () { |
| 22 | return this.$( 'input[type="hidden"]' ); |
| 23 | }, |
| 24 | |
| 25 | $inputs: function () { |
| 26 | return this.$( 'input[type="checkbox"]' ).not( |
| 27 | '.acf-checkbox-toggle' |
| 28 | ); |
| 29 | }, |
| 30 | |
| 31 | setValue: function ( val ) { |
| 32 | if ( ! Array.isArray( val ) ) { |
| 33 | val = val ? [ val ] : []; |
| 34 | } |
| 35 | |
| 36 | this.$inputs().each( function () { |
| 37 | const $input = $( this ); |
| 38 | const checked = val.includes( $input.val() ); |
| 39 | $input.prop( 'checked', checked ); |
| 40 | |
| 41 | if ( checked ) { |
| 42 | $input.parent( 'label' ).addClass( 'selected' ); |
| 43 | } else { |
| 44 | $input.parent( 'label' ).removeClass( 'selected' ); |
| 45 | } |
| 46 | } ); |
| 47 | |
| 48 | const $toggle = this.$toggle(); |
| 49 | if ( $toggle.length ) { |
| 50 | const checked = this.$inputs().not( ':checked' ).length === 0; |
| 51 | $toggle.prop( 'checked', checked ); |
| 52 | } |
| 53 | }, |
| 54 | |
| 55 | getValue: function () { |
| 56 | var val = []; |
| 57 | this.$( ':checked' ).each( function () { |
| 58 | val.push( $( this ).val() ); |
| 59 | } ); |
| 60 | return val.length ? val : false; |
| 61 | }, |
| 62 | |
| 63 | onChange: function ( e, $el ) { |
| 64 | // Vars. |
| 65 | var checked = $el.prop( 'checked' ); |
| 66 | var $label = $el.parent( 'label' ); |
| 67 | var $toggle = this.$toggle(); |
| 68 | |
| 69 | // Add or remove "selected" class. |
| 70 | if ( checked ) { |
| 71 | $label.addClass( 'selected' ); |
| 72 | } else { |
| 73 | $label.removeClass( 'selected' ); |
| 74 | } |
| 75 | |
| 76 | // Update toggle state if all inputs are checked. |
| 77 | if ( $toggle.length ) { |
| 78 | var $inputs = this.$inputs(); |
| 79 | |
| 80 | // all checked |
| 81 | if ( $inputs.not( ':checked' ).length == 0 ) { |
| 82 | $toggle.prop( 'checked', true ); |
| 83 | } else { |
| 84 | $toggle.prop( 'checked', false ); |
| 85 | } |
| 86 | } |
| 87 | }, |
| 88 | |
| 89 | onClickAdd: function ( e, $el ) { |
| 90 | var html = |
| 91 | '<li><input class="acf-checkbox-custom" type="checkbox" checked="checked" /><input type="text" name="' + |
| 92 | this.getInputName() + |
| 93 | '[]" /></li>'; |
| 94 | $el.parent( 'li' ).before( html ); |
| 95 | $el.parent( 'li' ) |
| 96 | .parent() |
| 97 | .find( 'input[type="text"]' ) |
| 98 | .last() |
| 99 | .trigger( 'focus' ); |
| 100 | }, |
| 101 | |
| 102 | onClickToggle: function ( e, $el ) { |
| 103 | var $inputs = this.$inputs(); |
| 104 | var checked = $el.prop( 'checked' ); |
| 105 | $inputs.prop( 'checked', checked ).trigger( 'change' ); |
| 106 | }, |
| 107 | |
| 108 | onClickCustom: function ( e, $el ) { |
| 109 | var checked = $el.prop( 'checked' ); |
| 110 | var $text = $el.next( 'input[type="text"]' ); |
| 111 | |
| 112 | // checked |
| 113 | if ( checked ) { |
| 114 | $text.prop( 'disabled', false ); |
| 115 | |
| 116 | // not checked |
| 117 | } else { |
| 118 | $text.prop( 'disabled', true ); |
| 119 | |
| 120 | // remove |
| 121 | if ( $text.val() == '' ) { |
| 122 | $el.parent( 'li' ).remove(); |
| 123 | } |
| 124 | } |
| 125 | }, |
| 126 | onKeyDownInput: function ( e, $el ) { |
| 127 | // Check if Enter key (keyCode 13) was pressed |
| 128 | if ( e.which === 13 ) { |
| 129 | // Prevent default form submission |
| 130 | e.preventDefault(); |
| 131 | |
| 132 | // Toggle the checkbox state and trigger change event |
| 133 | $el.prop( 'checked', ! $el.prop( 'checked' ) ).trigger( |
| 134 | 'change' |
| 135 | ); |
| 136 | |
| 137 | // If this is the "Select All" toggle checkbox, run the toggle logic |
| 138 | if ( $el.is( '.acf-checkbox-toggle' ) ) { |
| 139 | this.onClickToggle( e, $el ); |
| 140 | } |
| 141 | } |
| 142 | }, |
| 143 | } ); |
| 144 | |
| 145 | acf.registerFieldType( Field ); |
| 146 | } )( jQuery ); |
| 147 |