bindings
1 year ago
commands
1 year ago
pro
1 year ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
1 year ago
_acf-condition.js
1 year ago
_acf-conditions.js
1 year ago
_acf-field-accordion.js
1 year ago
_acf-field-button-group.js
1 year ago
_acf-field-checkbox.js
1 year ago
_acf-field-color-picker.js
1 year ago
_acf-field-date-picker.js
1 year ago
_acf-field-date-time-picker.js
1 year ago
_acf-field-file.js
1 year ago
_acf-field-google-map.js
1 year ago
_acf-field-icon-picker.js
1 year ago
_acf-field-image.js
1 year ago
_acf-field-link.js
1 year ago
_acf-field-oembed.js
1 year ago
_acf-field-page-link.js
1 year ago
_acf-field-post-object.js
1 year ago
_acf-field-radio.js
1 year ago
_acf-field-range.js
1 year ago
_acf-field-relationship.js
1 year ago
_acf-field-select.js
1 year ago
_acf-field-tab.js
1 year ago
_acf-field-taxonomy.js
1 year ago
_acf-field-time-picker.js
1 year ago
_acf-field-true-false.js
1 year ago
_acf-field-url.js
1 year ago
_acf-field-user.js
1 year ago
_acf-field-wysiwyg.js
1 year 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
1 year ago
_acf-media.js
1 year ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
1 year ago
_acf-panel.js
1 year ago
_acf-popup.js
1 year ago
_acf-postbox.js
1 year ago
_acf-screen.js
1 year ago
_acf-select2.js
1 year ago
_acf-tinymce.js
1 year ago
_acf-tooltip.js
1 year ago
_acf-unload.js
1 year ago
_acf-validation.js
1 year ago
_acf.js
1 year ago
_browse-fields-modal.js
1 year ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
1 year ago
_field-group-fields.js
1 year ago
_field-group-locations.js
1 year ago
_field-group-settings.js
1 year ago
_field-group.js
1 year 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
116 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 | }, |
| 11 | |
| 12 | $control: function () { |
| 13 | return this.$( '.acf-checkbox-list' ); |
| 14 | }, |
| 15 | |
| 16 | $toggle: function () { |
| 17 | return this.$( '.acf-checkbox-toggle' ); |
| 18 | }, |
| 19 | |
| 20 | $input: function () { |
| 21 | return this.$( 'input[type="hidden"]' ); |
| 22 | }, |
| 23 | |
| 24 | $inputs: function () { |
| 25 | return this.$( 'input[type="checkbox"]' ).not( |
| 26 | '.acf-checkbox-toggle' |
| 27 | ); |
| 28 | }, |
| 29 | |
| 30 | getValue: function () { |
| 31 | var val = []; |
| 32 | this.$( ':checked' ).each( function () { |
| 33 | val.push( $( this ).val() ); |
| 34 | } ); |
| 35 | return val.length ? val : false; |
| 36 | }, |
| 37 | |
| 38 | onChange: function ( e, $el ) { |
| 39 | // Vars. |
| 40 | var checked = $el.prop( 'checked' ); |
| 41 | var $label = $el.parent( 'label' ); |
| 42 | var $toggle = this.$toggle(); |
| 43 | |
| 44 | // Add or remove "selected" class. |
| 45 | if ( checked ) { |
| 46 | $label.addClass( 'selected' ); |
| 47 | } else { |
| 48 | $label.removeClass( 'selected' ); |
| 49 | } |
| 50 | |
| 51 | // Update toggle state if all inputs are checked. |
| 52 | if ( $toggle.length ) { |
| 53 | var $inputs = this.$inputs(); |
| 54 | |
| 55 | // all checked |
| 56 | if ( $inputs.not( ':checked' ).length == 0 ) { |
| 57 | $toggle.prop( 'checked', true ); |
| 58 | } else { |
| 59 | $toggle.prop( 'checked', false ); |
| 60 | } |
| 61 | } |
| 62 | }, |
| 63 | |
| 64 | onClickAdd: function ( e, $el ) { |
| 65 | var html = |
| 66 | '<li><input class="acf-checkbox-custom" type="checkbox" checked="checked" /><input type="text" name="' + |
| 67 | this.getInputName() + |
| 68 | '[]" /></li>'; |
| 69 | $el.parent( 'li' ).before( html ); |
| 70 | $el.parent( 'li' ) |
| 71 | .parent() |
| 72 | .find( 'input[type="text"]' ) |
| 73 | .last() |
| 74 | .focus(); |
| 75 | }, |
| 76 | |
| 77 | onClickToggle: function ( e, $el ) { |
| 78 | // Vars. |
| 79 | var checked = $el.prop( 'checked' ); |
| 80 | var $inputs = this.$( 'input[type="checkbox"]' ); |
| 81 | var $labels = this.$( 'label' ); |
| 82 | |
| 83 | // Update "checked" state. |
| 84 | $inputs.prop( 'checked', checked ); |
| 85 | |
| 86 | // Add or remove "selected" class. |
| 87 | if ( checked ) { |
| 88 | $labels.addClass( 'selected' ); |
| 89 | } else { |
| 90 | $labels.removeClass( 'selected' ); |
| 91 | } |
| 92 | }, |
| 93 | |
| 94 | onClickCustom: function ( e, $el ) { |
| 95 | var checked = $el.prop( 'checked' ); |
| 96 | var $text = $el.next( 'input[type="text"]' ); |
| 97 | |
| 98 | // checked |
| 99 | if ( checked ) { |
| 100 | $text.prop( 'disabled', false ); |
| 101 | |
| 102 | // not checked |
| 103 | } else { |
| 104 | $text.prop( 'disabled', true ); |
| 105 | |
| 106 | // remove |
| 107 | if ( $text.val() == '' ) { |
| 108 | $el.parent( 'li' ).remove(); |
| 109 | } |
| 110 | } |
| 111 | }, |
| 112 | } ); |
| 113 | |
| 114 | acf.registerFieldType( Field ); |
| 115 | } )( jQuery ); |
| 116 |