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-time-picker.js
176 lines
| 1 | ( function ( $, undefined ) { |
| 2 | var Field = acf.models.DatePickerField.extend( { |
| 3 | type: 'date_time_picker', |
| 4 | |
| 5 | $control: function () { |
| 6 | return this.$( '.acf-date-time-picker' ); |
| 7 | }, |
| 8 | |
| 9 | setValue: function ( val ) { |
| 10 | acf.val( this.$input(), val ); |
| 11 | |
| 12 | const $inputText = this.$inputText(); |
| 13 | if ( val && $inputText.length ) { |
| 14 | try { |
| 15 | const parts = val.split( ' ' ); |
| 16 | const dateValue = parts[ 0 ] || ''; |
| 17 | const timeValue = parts[ 1 ] || ''; |
| 18 | const date = $.datepicker.parseDate( |
| 19 | 'yy-mm-dd', |
| 20 | dateValue |
| 21 | ); |
| 22 | const dateFormat = |
| 23 | this.get( 'date_format' ) || |
| 24 | $inputText.datetimepicker( 'option', 'dateFormat' ); |
| 25 | const timeFormat = |
| 26 | this.get( 'time_format' ) || |
| 27 | $inputText.datetimepicker( 'option', 'timeFormat' ); |
| 28 | const dateText = $.datepicker.formatDate( |
| 29 | dateFormat, |
| 30 | date |
| 31 | ); |
| 32 | let timeText = timeValue; |
| 33 | const matches = timeValue.match( |
| 34 | /^(\d{2}):(\d{2}):(\d{2})$/ |
| 35 | ); |
| 36 | |
| 37 | if ( matches && $.datepicker.formatTime ) { |
| 38 | timeText = $.datepicker.formatTime( timeFormat, { |
| 39 | hour: parseInt( matches[ 1 ], 10 ), |
| 40 | minute: parseInt( matches[ 2 ], 10 ), |
| 41 | second: parseInt( matches[ 3 ], 10 ), |
| 42 | } ); |
| 43 | } |
| 44 | |
| 45 | $inputText.val( dateText + ' ' + timeText ); |
| 46 | } catch ( e ) { |
| 47 | $inputText.val( val ); |
| 48 | } |
| 49 | } else { |
| 50 | $inputText.val( '' ); |
| 51 | } |
| 52 | }, |
| 53 | |
| 54 | initialize: function () { |
| 55 | // vars |
| 56 | var $input = this.$input(); |
| 57 | var $inputText = this.$inputText(); |
| 58 | |
| 59 | // args |
| 60 | var args = { |
| 61 | dateFormat: this.get( 'date_format' ), |
| 62 | timeFormat: this.get( 'time_format' ), |
| 63 | altField: $input, |
| 64 | altFieldTimeOnly: false, |
| 65 | altFormat: 'yy-mm-dd', |
| 66 | altTimeFormat: 'HH:mm:ss', |
| 67 | changeYear: true, |
| 68 | yearRange: '-100:+100', |
| 69 | changeMonth: true, |
| 70 | showButtonPanel: true, |
| 71 | firstDay: this.get( 'first_day' ), |
| 72 | controlType: 'select', |
| 73 | oneLine: true, |
| 74 | }; |
| 75 | |
| 76 | // filter |
| 77 | args = acf.applyFilters( 'date_time_picker_args', args, this ); |
| 78 | |
| 79 | // add date time picker |
| 80 | acf.newDateTimePicker( $inputText, args ); |
| 81 | |
| 82 | // Check if default to today is enabled and field is empty |
| 83 | if ( |
| 84 | $inputText.data( 'default-to-today' ) === 1 && |
| 85 | ! $input.val() |
| 86 | ) { |
| 87 | // Get current date |
| 88 | const currentDate = new Date(); |
| 89 | |
| 90 | // Format display date and time |
| 91 | const displayDate = $.datepicker.formatDate( |
| 92 | args.dateFormat, |
| 93 | currentDate |
| 94 | ); |
| 95 | const displayTime = $.datepicker.formatTime( args.timeFormat, { |
| 96 | hour: currentDate.getHours(), |
| 97 | minute: currentDate.getMinutes(), |
| 98 | second: currentDate.getSeconds(), |
| 99 | } ); |
| 100 | |
| 101 | // Set the display input value (what user sees) |
| 102 | $inputText.val( `${ displayDate } ${ displayTime }` ); |
| 103 | |
| 104 | // Format hidden field date and time (for database storage) |
| 105 | const hiddenDate = $.datepicker.formatDate( |
| 106 | 'yy-mm-dd', |
| 107 | currentDate |
| 108 | ); |
| 109 | const hiddenTime = $.datepicker.formatTime( 'hh:mm:ss', { |
| 110 | hour: currentDate.getHours(), |
| 111 | minute: currentDate.getMinutes(), |
| 112 | second: currentDate.getSeconds(), |
| 113 | } ); |
| 114 | |
| 115 | // Set the hidden input value (what gets saved) |
| 116 | $input.val( `${ hiddenDate } ${ hiddenTime }` ); |
| 117 | } |
| 118 | |
| 119 | // action |
| 120 | acf.doAction( 'date_time_picker_init', $inputText, args, this ); |
| 121 | }, |
| 122 | } ); |
| 123 | |
| 124 | acf.registerFieldType( Field ); |
| 125 | |
| 126 | // manager |
| 127 | var dateTimePickerManager = new acf.Model( { |
| 128 | priority: 5, |
| 129 | wait: 'ready', |
| 130 | initialize: function () { |
| 131 | // vars |
| 132 | var locale = acf.get( 'locale' ); |
| 133 | var rtl = acf.get( 'rtl' ); |
| 134 | var l10n = acf.get( 'dateTimePickerL10n' ); |
| 135 | |
| 136 | // bail early if no l10n |
| 137 | if ( ! l10n ) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | // bail early if no datepicker library |
| 142 | if ( typeof $.timepicker === 'undefined' ) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // rtl |
| 147 | l10n.isRTL = rtl; |
| 148 | |
| 149 | // append |
| 150 | $.timepicker.regional[ locale ] = l10n; |
| 151 | $.timepicker.setDefaults( l10n ); |
| 152 | }, |
| 153 | } ); |
| 154 | |
| 155 | // add |
| 156 | acf.newDateTimePicker = function ( $input, args ) { |
| 157 | // bail early if no datepicker library |
| 158 | if ( typeof $.timepicker === 'undefined' ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // defaults |
| 163 | args = args || {}; |
| 164 | |
| 165 | // initialize |
| 166 | $input.datetimepicker( args ); |
| 167 | |
| 168 | // wrap the datepicker (only if it hasn't already been wrapped) |
| 169 | if ( $( 'body > #ui-datepicker-div' ).exists() ) { |
| 170 | $( 'body > #ui-datepicker-div' ).wrap( |
| 171 | '<div class="acf-ui-datepicker" />' |
| 172 | ); |
| 173 | } |
| 174 | }; |
| 175 | } )( jQuery ); |
| 176 |