vendor/assets/javascripts/pickadate/picker.time.js in pickadate-rails-1.2.2 vs vendor/assets/javascripts/pickadate/picker.time.js in pickadate-rails-1.3.0

- old
+ new

@@ -1,8 +1,8 @@ /*! - * Time picker for pickadate.js v3.2.2 + * Time picker for pickadate.js v3.3.0 * http://amsul.github.io/pickadate.js/time.htm */ /*jshint debug: true, @@ -15,16 +15,16 @@ (function ( factory ) { // Register as an anonymous module. if ( typeof define === 'function' && define.amd ) - define( ['picker'], factory ) + define( ['picker','jquery'], factory ) // Or using browser globals. - else factory( Picker ) + else factory( Picker, jQuery ) -}(function( Picker ) { +}(function( Picker, $ ) { /** * Globals and constants */ @@ -100,15 +100,12 @@ picker. on( 'render', function() { var $pickerHolder = picker.$root.children(), $viewset = $pickerHolder.find( '.' + settings.klass.viewset ) if ( $viewset.length ) { - $pickerHolder[ 0 ].scrollTop = ~~( $viewset.position().top - ( $viewset[ 0 ].clientHeight * 2 ) ) + $pickerHolder[ 0 ].scrollTop += $viewset.position().top - ( $viewset[ 0 ].clientHeight * 2 ) } - else { - console.warn( 'Nothing to viewset with', clock.item.view ) - } }). on( 'open', function() { picker.$root.find( 'button' ).attr( 'disable', false ) }). on( 'close', function() { @@ -195,11 +192,11 @@ if ( type == 'max' && value < clock.item.min.pick ) { value += MINUTES_IN_DAY } // Normalize it into a "reachable" interval. - value = clock.normalize( value, options ) + value = clock.normalize( type, value, options ) // Return the compiled object. return { // Divide to get hours from minutes. @@ -241,15 +238,23 @@ return value * this.item.interval + dateMinutes } //TimePicker.prototype.now /** - * Normalize minutes or an object to be "reachable" based on the interval. + * Normalize minutes to be “reachable” based on the min and interval. */ -TimePicker.prototype.normalize = function( value/*, options*/ ) { - // If it's a negative value, add one interval to keep it as "passed". - return value - ( ( value < 0 ? this.item.interval : 0 ) + value % this.item.interval ) +TimePicker.prototype.normalize = function( type, value/*, options*/ ) { + + var minObject = this.item.min, interval = this.item.interval, + + // If setting min and it doesn’t exist, don’t shift anything. + // Otherwise get the value and min difference and then + // normalize the difference with the interval. + difference = type == 'min' && !minObject ? 0 : ( value - minObject.pick ) % interval + + // If it’s a negative value, add one interval to keep it as “passed”. + return value - ( difference + ( value < 0 ? interval : 0 ) ) } //TimePicker.prototype.normalize /** * Measure the range of minutes. @@ -268,11 +273,11 @@ value = clock.now( type, value, options ) } // If it's an object already, just normalize it. else if ( Picker._.isObject( value ) && Picker._.isInteger( value.pick ) ) { - value = clock.normalize( value.pick, options ) + value = clock.normalize( type, value.pick, options ) } return value } ///TimePicker.prototype.measure @@ -336,21 +341,24 @@ /** * Shift an object by an interval until we reach an enabled object. */ TimePicker.prototype.shift = function( timeObject, interval ) { - var - clock = this + var clock = this, + minLimit = clock.item.min.pick, + maxLimit = clock.item.max.pick + interval = interval || clock.item.interval + // Keep looping as long as the time is disabled. while ( clock.disabled( timeObject ) ) { // Increase/decrease the time by the interval and keep looping. - timeObject = clock.create( timeObject.pick += interval || clock.item.interval ) + timeObject = clock.create( timeObject.pick += interval ) // If we've looped beyond the limits, break out of the loop. - if ( timeObject.pick <= clock.item.min.pick || timeObject.pick >= clock.item.max.pick ) { + if ( timeObject.pick <= minLimit || timeObject.pick >= maxLimit ) { break } } // Return the final object. @@ -371,12 +379,11 @@ /** * Parse a string into a usable type. */ TimePicker.prototype.parse = function( type, value, options ) { - var - clock = this, + var clock = this, parsingObject = {} if ( !value || Picker._.isInteger( value ) || $.isArray( value ) || Picker._.isDate( value ) || Picker._.isObject( value ) && Picker._.isInteger( value.pick ) ) { return value } @@ -489,28 +496,45 @@ // Flip the enabled and disabled times. if ( value == 'flip' ) { clock.item.enable = isFlipped ? 1 : -1 } - // Check if we have to add/remove from collection. - else if ( !isFlipped && type == 'enable' || isFlipped && type == 'disable' ) { - collection = clock.removeDisabled( collection, value ) + // Reset the collection and enable the base state. + else if ( ( type == 'enable' && value === true ) || ( type == 'disable' && value === false ) ) { + clock.item.enable = 1 + collection = [] } - else if ( !isFlipped && type == 'disable' || isFlipped && type == 'enable' ) { - collection = clock.addDisabled( collection, value ) + + // Reset the collection and disable the base state. + else if ( ( type == 'enable' && value === false ) || ( type == 'disable' && value === true ) ) { + clock.item.enable = -1 + collection = [] } + // Make sure a collection of things was passed to add/remove. + else if ( $.isArray( value ) ) { + + // Check if we have to add/remove from collection. + if ( !isFlipped && type == 'enable' || isFlipped && type == 'disable' ) { + collection = clock.removeDisabled( collection, value ) + } + else if ( !isFlipped && type == 'disable' || isFlipped && type == 'enable' ) { + collection = clock.addDisabled( collection, value ) + } + } + return collection } //TimePicker.prototype.flipItem /** * Add an item to the disabled collection. */ TimePicker.prototype.addDisabled = function( collection, item ) { var clock = this - item.map( function( timeUnit ) { + if ( item === false ) collection = [] + else item.map( function( timeUnit ) { if ( !clock.filterDisabled( collection, timeUnit ).length ) { collection.push( timeUnit ) } }) return collection @@ -593,10 +617,13 @@ return klasses.join( ' ' ) })( [ settings.klass.listItem ], loopedTime.pick ), 'data-pick=' + loopedTime.pick ] } - }) + Picker._.node( 'li', Picker._.node( 'button', settings.clear, settings.klass.buttonClear, 'data-clear=1' + ( isOpen ? '' : ' disable' ) ) ), settings.klass.list ) + }) + + + // * For Firefox forms to submit, make sure to set the button’s `type` attribute as “button”. + Picker._.node( 'li', Picker._.node( 'button', settings.clear, settings.klass.buttonClear, 'type=button data-clear=1' + ( isOpen ? '' : ' disable' ) ) ), settings.klass.list ) } //TimePicker.prototype.nodes