vendor/assets/javascripts/pickadate/picker.time.js in pickadate-rails-1.3.0 vs vendor/assets/javascripts/pickadate/picker.time.js in pickadate-rails-1.3.1
- old
+ new
@@ -1,8 +1,8 @@
/*!
- * Time picker for pickadate.js v3.3.0
+ * Time picker for pickadate.js v3.3.1
* http://amsul.github.io/pickadate.js/time.htm
*/
/*jshint
debug: true,
@@ -100,11 +100,11 @@
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 )
}
}).
on( 'open', function() {
picker.$root.find( 'button' ).attr( 'disable', false )
}).
@@ -168,34 +168,45 @@
*/
TimePicker.prototype.create = function( type, value, options ) {
var clock = this
- // If there's no value, use the type as the value.
+ // If there’s no value, use the type as the value.
value = value === undefined ? type : value
- // If it's an object, use the "pick" value.
- if ( Picker._.isObject( value ) && Picker._.isInteger( value.pick ) ) {
+ // If it’s a date object, convert it into an array.
+ if ( Picker._.isDate( value ) ) {
+ value = [ value.getHours(), value.getMinutes() ]
+ }
+
+ // If it’s an object, use the “pick” value.
+ if ( $.isPlainObject( value ) && Picker._.isInteger( value.pick ) ) {
value = value.pick
}
- // If it's an array, convert it into minutes.
+ // If it’s an array, convert it into minutes.
else if ( $.isArray( value ) ) {
value = +value[ 0 ] * MINUTES_IN_HOUR + (+value[ 1 ])
}
- // If no valid value is passed, set it to "now".
+ // If no valid value is passed, set it to “now”.
else if ( !Picker._.isInteger( value ) ) {
value = clock.now( type, value, options )
}
- // If we're setting the max, make sure it's greater than the min.
+ // If we’re setting the max, make sure it’s greater than the min.
if ( type == 'max' && value < clock.item.min.pick ) {
value += MINUTES_IN_DAY
}
- // Normalize it into a "reachable" interval.
+ // If the value doesn’t fall directly on the interval,
+ // add one interval to indicate it as “passed”.
+ if ( type != 'min' && type != 'max' && (value - clock.item.min.pick) % clock.item.interval !== 0 ) {
+ value += clock.item.interval
+ }
+
+ // Normalize it into a “reachable” interval.
value = clock.normalize( type, value, options )
// Return the compiled object.
return {
@@ -206,11 +217,11 @@
mins: ( MINUTES_IN_HOUR + value % MINUTES_IN_HOUR ) % MINUTES_IN_HOUR,
// The time in total minutes.
time: ( MINUTES_IN_DAY + value ) % MINUTES_IN_DAY,
- // Reference to the "relative" value to pick.
+ // Reference to the “relative” value to pick.
pick: value
}
} //TimePicker.prototype.create
@@ -220,10 +231,13 @@
TimePicker.prototype.now = function( type, value/*, options*/ ) {
var date = new Date(),
dateMinutes = date.getHours() * MINUTES_IN_HOUR + date.getMinutes()
+ // Make sure “now” falls within the interval range.
+ dateMinutes -= dateMinutes % this.item.interval
+
// If the value is a number, adjust by that many intervals because
// the time has passed. In the case of “midnight” and a negative `min`,
// increase the value by 2. Otherwise increase it by 1.
if ( Picker._.isInteger( value ) ) {
value += type == 'min' && value < 0 && dateMinutes === 0 ? 2 : 1
@@ -242,16 +256,16 @@
/**
* Normalize minutes to be “reachable” based on the min and interval.
*/
TimePicker.prototype.normalize = function( type, value/*, options*/ ) {
- var minObject = this.item.min, interval = this.item.interval,
+ var interval = this.item.interval,
- // If setting min and it doesn’t exist, don’t shift anything.
+ // If setting min time, 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
+ difference = type == 'min' ? 0 : ( value - this.item.min.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
@@ -261,22 +275,22 @@
*/
TimePicker.prototype.measure = function( type, value, options ) {
var clock = this
- // If it's anything false-y, set it to the default.
+ // If it’s anything false-y, set it to the default.
if ( !value ) {
value = type == 'min' ? [ 0, 0 ] : [ HOURS_IN_DAY - 1, MINUTES_IN_HOUR - 1 ]
}
- // If it's a literal true, or an integer, make it relative to now.
+ // If it’s a literal true, or an integer, make it relative to now.
else if ( value === true || Picker._.isInteger( value ) ) {
value = clock.now( type, value, options )
}
- // If it's an object already, just normalize it.
- else if ( Picker._.isObject( value ) && Picker._.isInteger( value.pick ) ) {
+ // If it’s an object already, just normalize it.
+ else if ( $.isPlainObject( value ) && Picker._.isInteger( value.pick ) ) {
value = clock.normalize( type, value.pick, options )
}
return value
} ///TimePicker.prototype.measure
@@ -318,25 +332,32 @@
var
clock = this,
// Filter through the disabled times to check if this is one.
- isDisabledTime = clock.item.disable.filter( function( timeToDisable ) {
+ isDisabledMatch = clock.item.disable.filter( function( timeToDisable ) {
// If the time is a number, match the hours.
if ( Picker._.isInteger( timeToDisable ) ) {
return timeObject.hour == timeToDisable
}
- // If it's an array, create the object and match the times.
- if ( $.isArray( timeToDisable ) ) {
+ // If it’s an array, create the object and match the times.
+ if ( $.isArray( timeToDisable ) || Picker._.isDate( timeToDisable ) ) {
return timeObject.pick == clock.create( timeToDisable ).pick
}
- }).length
+ })
+ // If this time matches a disabled time, confirm it’s not inverted.
+ isDisabledMatch = isDisabledMatch.length && !isDisabledMatch.filter(function( timeToDisable ) {
+ return $.isArray( timeToDisable ) && timeToDisable[2] == 'inverted'
+ }).length
+
// If the clock is "enabled" flag is flipped, flip the condition.
- return clock.item.enable === -1 ? !isDisabledTime : isDisabledTime
+ return clock.item.enable === -1 ? !isDisabledMatch : isDisabledMatch ||
+ timeObject.pick < clock.item.min.pick ||
+ timeObject.pick > clock.item.max.pick
} //TimePicker.prototype.disabled
/**
* Shift an object by an interval until we reach an enabled object.
@@ -382,11 +403,11 @@
TimePicker.prototype.parse = function( type, value, options ) {
var clock = this,
parsingObject = {}
- if ( !value || Picker._.isInteger( value ) || $.isArray( value ) || Picker._.isDate( value ) || Picker._.isObject( value ) && Picker._.isInteger( value.pick ) ) {
+ if ( !value || Picker._.isInteger( value ) || $.isArray( value ) || Picker._.isDate( value ) || $.isPlainObject( value ) && Picker._.isInteger( value.pick ) ) {
return value
}
// We need a `.format` to parse the value.
if ( !( options && options.format ) ) {
@@ -489,15 +510,15 @@
*/
TimePicker.prototype.flipItem = function( type, value/*, options*/ ) {
var clock = this,
collection = clock.item.disable,
- isFlipped = clock.item.enable === -1
+ isFlippedBase = clock.item.enable === -1
// Flip the enabled and disabled times.
if ( value == 'flip' ) {
- clock.item.enable = isFlipped ? 1 : -1
+ clock.item.enable = isFlippedBase ? 1 : -1
}
// Reset the collection and enable the base state.
else if ( ( type == 'enable' && value === true ) || ( type == 'disable' && value === false ) ) {
clock.item.enable = 1
@@ -512,44 +533,101 @@
// 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' ) {
+ if ( isFlippedBase && type == 'enable' || !isFlippedBase && type == 'disable' ) {
collection = clock.addDisabled( collection, value )
}
+ else if ( !isFlippedBase && type == 'enable' ) {
+ collection = clock.addEnabled( collection, value )
+ }
+ else if ( isFlippedBase && type == 'disable' ) {
+ collection = clock.removeDisabled( collection, value )
+ }
}
return collection
} //TimePicker.prototype.flipItem
/**
+ * Add an enabled (inverted) item to the disabled collection.
+ */
+TimePicker.prototype.addEnabled = function( collection, item ) {
+
+ var clock = this
+
+ // Go through each item to enable.
+ item.map( function( timeUnit ) {
+
+ // Check if the time unit is already within the collection.
+ if ( clock.filterDisabled( collection, timeUnit, 1 ).length ) {
+
+ // Remove the unit directly from the collection.
+ collection = clock.removeDisabled( collection, [timeUnit] )
+
+ // If the unit is an array and it falls within a
+ // disabled weekday, invert it and then insert it.
+ if (
+ $.isArray( timeUnit ) &&
+ collection.filter( function( disabledHour ) {
+ return Picker._.isInteger( disabledHour ) && clock.create( timeUnit ).hour === disabledHour
+ }).length
+ ) {
+ timeUnit = timeUnit.slice(0)
+ timeUnit.push( 'inverted' )
+ collection.push( timeUnit )
+ }
+ }
+ })
+
+ // Return the final collection.
+ return collection
+} //TimePicker.prototype.addEnabled
+
+
+/**
* Add an item to the disabled collection.
*/
TimePicker.prototype.addDisabled = function( collection, item ) {
+
var clock = this
- if ( item === false ) collection = []
- else item.map( function( timeUnit ) {
+
+ // Go through each item to disable.
+ item.map( function( timeUnit ) {
+
+ // Add the time unit if it isn’t already within the collection.
if ( !clock.filterDisabled( collection, timeUnit ).length ) {
collection.push( timeUnit )
}
+
+ // If the time unit is an array and falls within the range, just remove it.
+ else if ( $.isArray( timeUnit ) && clock.filterDisabled( collection, timeUnit, 1 ).length ) {
+ collection = clock.removeDisabled( collection, [timeUnit] )
+ }
})
+
+ // Return the final collection.
return collection
} //TimePicker.prototype.addDisabled
/**
* Remove an item from the disabled collection.
*/
TimePicker.prototype.removeDisabled = function( collection, item ) {
+
var clock = this
+
+ // Go through each item to enable.
item.map( function( timeUnit ) {
+
+ // Filter each item out of the collection.
collection = clock.filterDisabled( collection, timeUnit, 1 )
})
+
+ // Return the final colleciton.
return collection
} //TimePicker.prototype.removeDisabled
/**