bindings
6 months ago
commands
1 week ago
pro
1 week ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
7 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
7 months ago
_acf-field-checkbox.js
1 month ago
_acf-field-color-picker.js
7 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
3 weeks ago
_acf-field-taxonomy.js
7 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
7 months ago
_acf-media.js
1 week ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
7 months ago
_acf-panel.js
1 year ago
_acf-popup.js
7 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
7 months ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
3 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-date-picker.js
205 lines
| 1 | ( function ( $, undefined ) { |
| 2 | var Field = acf.Field.extend( { |
| 3 | type: 'date_picker', |
| 4 | |
| 5 | events: { |
| 6 | 'blur input[type="text"]': 'onBlur', |
| 7 | duplicateField: 'onDuplicate', |
| 8 | }, |
| 9 | |
| 10 | $control: function () { |
| 11 | return this.$( '.acf-date-picker' ); |
| 12 | }, |
| 13 | |
| 14 | $input: function () { |
| 15 | return this.$( 'input[type="hidden"]' ); |
| 16 | }, |
| 17 | |
| 18 | $inputText: function () { |
| 19 | return this.$( 'input[type="text"]' ); |
| 20 | }, |
| 21 | |
| 22 | initialize: function () { |
| 23 | // save_format: compatibility with ACF < 5.0.0 |
| 24 | if ( this.has( 'save_format' ) ) { |
| 25 | return this.initializeCompatibility(); |
| 26 | } |
| 27 | |
| 28 | // vars |
| 29 | var $input = this.$input(); |
| 30 | var $inputText = this.$inputText(); |
| 31 | |
| 32 | // args |
| 33 | var args = { |
| 34 | dateFormat: this.get( 'date_format' ), |
| 35 | altField: $input, |
| 36 | altFormat: 'yymmdd', |
| 37 | changeYear: true, |
| 38 | yearRange: '-100:+100', |
| 39 | changeMonth: true, |
| 40 | showButtonPanel: true, |
| 41 | firstDay: this.get( 'first_day' ), |
| 42 | }; |
| 43 | |
| 44 | // filter |
| 45 | args = acf.applyFilters( 'date_picker_args', args, this ); |
| 46 | |
| 47 | // add date picker |
| 48 | acf.newDatePicker( $inputText, args ); |
| 49 | |
| 50 | // Check if default to today is enabled and field is empty |
| 51 | if ( |
| 52 | $inputText.data( 'default-to-today' ) === 1 && |
| 53 | ! $input.val() |
| 54 | ) { |
| 55 | // Get current date |
| 56 | const currentDate = new Date(); |
| 57 | |
| 58 | // Format display date |
| 59 | const displayDate = $.datepicker.formatDate( |
| 60 | args.dateFormat, |
| 61 | currentDate |
| 62 | ); |
| 63 | |
| 64 | // Set the display input value (what user sees) |
| 65 | $inputText.val( `${ displayDate }` ); |
| 66 | |
| 67 | // Format hidden field date (for database storage) |
| 68 | const hiddenDate = $.datepicker.formatDate( |
| 69 | 'yymmdd', |
| 70 | currentDate |
| 71 | ); |
| 72 | |
| 73 | // Set the hidden input value (what gets saved) |
| 74 | $input.val( `${ hiddenDate }` ); |
| 75 | } |
| 76 | acf.doAction( 'date_picker_init', $inputText, args, this ); |
| 77 | }, |
| 78 | |
| 79 | initializeCompatibility: function () { |
| 80 | // vars |
| 81 | var $input = this.$input(); |
| 82 | var $inputText = this.$inputText(); |
| 83 | |
| 84 | // get and set value from alt field |
| 85 | $inputText.val( $input.val() ); |
| 86 | |
| 87 | // args |
| 88 | var args = { |
| 89 | dateFormat: this.get( 'date_format' ), |
| 90 | altField: $input, |
| 91 | altFormat: this.get( 'save_format' ), |
| 92 | changeYear: true, |
| 93 | yearRange: '-100:+100', |
| 94 | changeMonth: true, |
| 95 | showButtonPanel: true, |
| 96 | firstDay: this.get( 'first_day' ), |
| 97 | }; |
| 98 | |
| 99 | // filter for 3rd party customization |
| 100 | args = acf.applyFilters( 'date_picker_args', args, this ); |
| 101 | |
| 102 | // backup |
| 103 | var dateFormat = args.dateFormat; |
| 104 | |
| 105 | // change args.dateFormat |
| 106 | args.dateFormat = this.get( 'save_format' ); |
| 107 | |
| 108 | // add date picker |
| 109 | acf.newDatePicker( $inputText, args ); |
| 110 | |
| 111 | // now change the format back to how it should be. |
| 112 | $inputText.datepicker( 'option', 'dateFormat', dateFormat ); |
| 113 | |
| 114 | // action for 3rd party customization |
| 115 | acf.doAction( 'date_picker_init', $inputText, args, this ); |
| 116 | }, |
| 117 | |
| 118 | setValue: function ( val ) { |
| 119 | acf.val( this.$input(), val ); |
| 120 | |
| 121 | const $inputText = this.$inputText(); |
| 122 | if ( val && $inputText.length ) { |
| 123 | try { |
| 124 | const date = $.datepicker.parseDate( 'yymmdd', val ); |
| 125 | const dateFormat = |
| 126 | this.get( 'date_format' ) || |
| 127 | $inputText.datepicker( 'option', 'dateFormat' ); |
| 128 | $inputText.val( |
| 129 | $.datepicker.formatDate( dateFormat, date ) |
| 130 | ); |
| 131 | } catch ( e ) { |
| 132 | $inputText.val( val ); |
| 133 | } |
| 134 | } else { |
| 135 | $inputText.val( '' ); |
| 136 | } |
| 137 | }, |
| 138 | |
| 139 | onBlur: function () { |
| 140 | if ( ! this.$inputText().val() ) { |
| 141 | acf.val( this.$input(), '' ); |
| 142 | } |
| 143 | }, |
| 144 | |
| 145 | onDuplicate: function ( e, $el, $duplicate ) { |
| 146 | $duplicate |
| 147 | .find( 'input[type="text"]' ) |
| 148 | .removeClass( 'hasDatepicker' ) |
| 149 | .removeAttr( 'id' ); |
| 150 | }, |
| 151 | } ); |
| 152 | |
| 153 | acf.registerFieldType( Field ); |
| 154 | |
| 155 | // manager |
| 156 | var datePickerManager = new acf.Model( { |
| 157 | priority: 5, |
| 158 | wait: 'ready', |
| 159 | initialize: function () { |
| 160 | // vars |
| 161 | var locale = acf.get( 'locale' ); |
| 162 | var rtl = acf.get( 'rtl' ); |
| 163 | var l10n = acf.get( 'datePickerL10n' ); |
| 164 | |
| 165 | // bail early if no l10n |
| 166 | if ( ! l10n ) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | // bail early if no datepicker library |
| 171 | if ( typeof $.datepicker === 'undefined' ) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // rtl |
| 176 | l10n.isRTL = rtl; |
| 177 | |
| 178 | // append |
| 179 | $.datepicker.regional[ locale ] = l10n; |
| 180 | $.datepicker.setDefaults( l10n ); |
| 181 | }, |
| 182 | } ); |
| 183 | |
| 184 | // add |
| 185 | acf.newDatePicker = function ( $input, args ) { |
| 186 | // bail early if no datepicker library |
| 187 | if ( typeof $.datepicker === 'undefined' ) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // defaults |
| 192 | args = args || {}; |
| 193 | |
| 194 | // initialize |
| 195 | $input.datepicker( args ); |
| 196 | |
| 197 | // wrap the datepicker (only if it hasn't already been wrapped) |
| 198 | if ( $( 'body > #ui-datepicker-div' ).exists() ) { |
| 199 | $( 'body > #ui-datepicker-div' ).wrap( |
| 200 | '<div class="acf-ui-datepicker" />' |
| 201 | ); |
| 202 | } |
| 203 | }; |
| 204 | } )( jQuery ); |
| 205 |