time.js (1379B)
1 ( function ( $, rwmb, i18n ) { 2 'use strict'; 3 4 /** 5 * Transform an input into a time picker. 6 */ 7 function transform() { 8 var $this = $( this ), 9 options = $this.data( 'options' ), 10 $inline = $this.siblings( '.rwmb-datetime-inline' ), 11 current = $this.val(); 12 13 $this.siblings( '.ui-datepicker-append' ).remove(); // Remove appended text 14 15 options.onSelect = function() { 16 $this.trigger( 'change' ); 17 } 18 options.beforeShow = function( i ) { 19 if ( $( i ).prop( 'readonly' ) ) { 20 return false; 21 } 22 } 23 24 if ( ! $inline.length ) { 25 $this.removeClass( 'hasDatepicker' ).timepicker( options ); 26 return; 27 } 28 29 options.altField = '#' + $this.attr( 'id' ); 30 $inline 31 .removeClass( 'hasDatepicker' ) 32 .empty() 33 .prop( 'id', '' ) 34 .timepicker( options ) 35 .timepicker( 'setTime', current ); 36 } 37 38 // Set language if available 39 function setTimeI18n() { 40 if ( $.timepicker.regional.hasOwnProperty( i18n.locale ) ) { 41 $.timepicker.setDefaults( $.timepicker.regional[i18n.locale] ); 42 } else if ( $.timepicker.regional.hasOwnProperty( i18n.localeShort ) ) { 43 $.timepicker.setDefaults( $.timepicker.regional[i18n.localeShort] ); 44 } 45 } 46 47 function init( e ) { 48 $( e.target ).find( '.rwmb-time' ).each( transform ); 49 } 50 51 setTimeI18n(); 52 rwmb.$document 53 .on( 'mb_ready', init ) 54 .on( 'clone', '.rwmb-time', transform ); 55 } )( jQuery, rwmb, RWMB_Time );