balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

date.js (2019B)


      1 ( function ( $, _, rwmb ) {
      2 	'use strict';
      3 
      4 	/**
      5 	 * Transform an input into a date picker.
      6 	 */
      7 	function transform() {
      8 		var $this = $( this ),
      9 			options = $this.data( 'options' ),
     10 			$inline = $this.siblings( '.rwmb-datetime-inline' ),
     11 			$timestamp = $this.siblings( '.rwmb-datetime-timestamp' ),
     12 			current = $this.val(),
     13 			$picker = $inline.length ? $inline : $this;
     14 
     15 		$this.siblings( '.ui-datepicker-append' ).remove(); // Remove appended text
     16 
     17 		options.onSelect = function() {
     18 			$this.trigger( 'change' );
     19 		}
     20 		options.beforeShow = function( i ) {
     21 			if ( $( i ).prop( 'readonly' ) ) {
     22 				return false;
     23 			}
     24 		}
     25 
     26 		if ( $timestamp.length ) {
     27 			options.onClose = options.onSelect = function () {
     28 				$timestamp.val( getTimestamp( $picker.datepicker( 'getDate' ) ) );
     29 				$this.trigger( 'change' );
     30 			};
     31 		}
     32 
     33 		if ( ! $inline.length ) {
     34 			$this.removeClass( 'hasDatepicker' ).datepicker( options );
     35 			return;
     36 		}
     37 
     38 		options.altField = '#' + $this.attr( 'id' );
     39 		$this.on( 'keydown', _.debounce( function () {
     40 			// if val is empty, return to allow empty datepicker input.
     41 			if ( ! $this.val() ) {
     42 				return;
     43 			}
     44 			$picker
     45 				.datepicker( 'setDate', $this.val() )
     46 				.find( '.ui-datepicker-current-day' )
     47 				.trigger( 'click' );
     48 		}, 600 ) );
     49 
     50 		$inline
     51 			.removeClass( 'hasDatepicker' )
     52 			.empty()
     53 			.prop( 'id', '' )
     54 			.datepicker( options )
     55 			.datepicker( 'setDate', current );
     56 	}
     57 
     58 	/**
     59 	 * Convert date to Unix timestamp in milliseconds
     60 	 * @link http://stackoverflow.com/a/14006555/556258
     61 	 * @param date
     62 	 * @return number
     63 	 */
     64 	function getTimestamp( date ) {
     65 		if ( date === null ) {
     66 			return '';
     67 		}
     68 		var milliseconds = Date.UTC( date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
     69 		return Math.floor( milliseconds / 1000 );
     70 	}
     71 
     72 	function init( e ) {
     73 		$( e.target ).find( '.rwmb-date' ).each( transform );
     74 	}
     75 
     76 	rwmb.$document
     77 		.on( 'mb_ready', init )
     78 		.on( 'clone', '.rwmb-date', transform );
     79 } )( jQuery, _, rwmb );