vendor/assets/javascripts/pickadate/picker.date.js in pickadate-rails-1.0.0 vs vendor/assets/javascripts/pickadate/picker.date.js in pickadate-rails-1.0.1

- old
+ new

@@ -1,8 +1,8 @@ /*! - * Date picker for pickadate.js v3.0.3 + * Date picker for pickadate.js v3.0.4 * http://amsul.github.io/pickadate.js/date.htm */ /*jshint debug: true, @@ -154,35 +154,35 @@ DatePicker.prototype.create = function( type, value, options ) { var isInfiniteValue, calendar = 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 infinity, update the value. + // If it’s infinity, update the value. if ( value == -Infinity || value == Infinity ) { isInfiniteValue = value } - // If it's an object, use the “time” value. + // If it’s an object, use the native date object. else if ( Picker._.isObject( value ) && Picker._.isInteger( value.pick ) ) { value = value.obj } - // If it's an array, convert it into a date. + // If it’s an array, convert it into a date. else if ( Array.isArray( value ) ) { value = new Date( value[ 0 ], value[ 1 ], value[ 2 ] ) } - // If it's a number or date object, make a normalized date. + // If it’s a number or date object, make a normalized date. else if ( Picker._.isInteger( value ) || Picker._.isDate( value ) ) { value = calendar.normalize( new Date( value ), options ) } - // If it's a literal true or any other case, set it to now. + // If it’s a literal true or any other case, set it to now. else /*if ( value === true )*/ { value = calendar.now( type, value, options ) } // Return the compiled object. @@ -283,22 +283,25 @@ originalDateObject = dateObject, // Make sure we have an interval. interval = options && options.interval ? options.interval : 1, + // Check if the calendar enabled dates are inverted. + isInverted = calendar.item.enable === -1, + // Check if we have any enabled dates after/before now. hasEnabledBeforeTarget, hasEnabledAfterTarget, // The min & max limits. minLimitObject = calendar.item.min, maxLimitObject = calendar.item.max, // Check if we’ve reached the limit during shifting. reachedMin, reachedMax, - // Check if the calendar is flipped and at least one weekday is enabled. - hasEnabledWeekdays = calendar.item.enable === -1 && calendar.item.disable.filter( function( value ) { + // Check if the calendar is inverted and at least one weekday is enabled. + hasEnabledWeekdays = isInverted && calendar.item.disable.filter( function( value ) { // If there’s a date, check where it is relative to the target. if ( Array.isArray( value ) ) { var dateTime = calendar.create( value ).pick if ( dateTime < dateObject.pick ) hasEnabledBeforeTarget = true @@ -309,20 +312,30 @@ return Picker._.isInteger( value ) }).length - // No need to validate if we’re navigating months or there are no enabled days. + // Cases to validate for: + // [1] Not inverted and date disabled. + // [2] Inverted and some dates enabled. + // [3] Out of range. + // + // Cases to **not** validate for: + // • Not inverted and date enabled. + // • Inverted and all dates disabled. + // • Navigating months. + // • ..and anything else. if ( - ( !( options && options.nav ) && !( !hasEnabledWeekdays && !hasEnabledBeforeTarget && !hasEnabledAfterTarget ) ) || - ( dateObject.pick <= minLimitObject.pick || dateObject.pick >= maxLimitObject.pick ) + /* 1 */ ( !isInverted && calendar.disabled( dateObject ) ) || + /* 2 */ ( isInverted && calendar.disabled( dateObject ) && ( hasEnabledWeekdays || hasEnabledBeforeTarget || hasEnabledAfterTarget ) ) || + /* 3 */ ( dateObject.pick <= minLimitObject.pick || dateObject.pick >= maxLimitObject.pick ) ) { - // Flip the direction if there aren’t any enabled weekdays + // When inverted, flip the direction if there aren’t any enabled weekdays // and there are no enabled dates in the direction of the interval. - if ( !hasEnabledWeekdays && ( ( !hasEnabledAfterTarget && interval > 0 ) || ( !hasEnabledBeforeTarget && interval < 0 ) ) ) { + if ( isInverted && !hasEnabledWeekdays && ( ( !hasEnabledAfterTarget && interval > 0 ) || ( !hasEnabledBeforeTarget && interval < 0 ) ) ) { interval *= -1 } // Keep looping until we reach an enabled date. @@ -545,22 +558,22 @@ */ DatePicker.prototype.flipItem = function( type, value/*, options*/ ) { var calendar = this, collection = calendar.item.disable, - isFlipped = calendar.item.enable === -1 + isInverted = calendar.item.enable === -1 // Flip the enabled and disabled dates. if ( value == 'flip' ) { - calendar.item.enable = isFlipped ? 1 : -1 + calendar.item.enable = isInverted ? 1 : -1 } // Check if we have to add/remove from collection. - else if ( !isFlipped && type == 'enable' || isFlipped && type == 'disable' ) { + else if ( !isInverted && type == 'enable' || isInverted && type == 'disable' ) { collection = calendar.removeDisabled( collection, value ) } - else if ( !isFlipped && type == 'disable' || isFlipped && type == 'enable' ) { + else if ( !isInverted && type == 'disable' || isInverted && type == 'enable' ) { collection = calendar.addDisabled( collection, value ) } return collection } //DatePicker.prototype.flipItem @@ -667,11 +680,11 @@ 'data-nav=' + ( next || -1 ) ) //endreturn }, //createMonthNav - // Create the month label + // Create the month label. createMonthLabel = function( monthsCollection ) { // If there are months to select, add a dropdown menu. if ( settings.selectMonths ) { @@ -705,11 +718,11 @@ // If there's a need for a month selector return Picker._.node( 'div', monthsCollection[ viewsetObject.month ], settings.klass.month ) }, //createMonthLabel - // Create the year label + // Create the year label. createYearLabel = function() { var focusedYear = viewsetObject.year, // If years selector is set to a literal "true", set it to 5. Otherwise @@ -784,54 +797,57 @@ max: WEEKS_IN_CALENDAR - 1, i: 1, node: 'tr', item: function( rowCounter ) { + // If Monday is the first day and the month starts on Sunday, shift the date back a week. + var shiftDateBy = settings.firstDay && calendar.create([ viewsetObject.year, viewsetObject.month, 1 ]).day === 0 ? -7 : 0 + return [ Picker._.group({ - min: DAYS_IN_WEEK * rowCounter - viewsetObject.day + 1, // Add 1 for weekday 0index + min: DAYS_IN_WEEK * rowCounter - viewsetObject.day + shiftDateBy + 1, // Add 1 for weekday 0index max: function() { return this.min + DAYS_IN_WEEK - 1 }, i: 1, node: 'td', - item: function( timeDate ) { + item: function( targetDate ) { - // Convert the time date from a relative date to a date object - timeDate = calendar.create([ viewsetObject.year, viewsetObject.month, timeDate + ( settings.firstDay ? 1 : 0 ) ]) + // Convert the time date from a relative date to a target date. + targetDate = calendar.create([ viewsetObject.year, viewsetObject.month, targetDate + ( settings.firstDay ? 1 : 0 ) ]) return [ Picker._.node( 'div', - timeDate.date, + targetDate.date, (function( klasses ) { // Add the `infocus` or `outfocus` classes based on month in view. - klasses.push( viewsetObject.month == timeDate.month ? settings.klass.infocus : settings.klass.outfocus ) + klasses.push( viewsetObject.month == targetDate.month ? settings.klass.infocus : settings.klass.outfocus ) // Add the `today` class if needed. - if ( nowObject.pick == timeDate.pick ) { + if ( nowObject.pick == targetDate.pick ) { klasses.push( settings.klass.now ) } // Add the `selected` class if something's selected and the time matches. - if ( selectedObject && selectedObject.pick == timeDate.pick ) { + if ( selectedObject && selectedObject.pick == targetDate.pick ) { klasses.push( settings.klass.selected ) } // Add the `highlighted` class if something's highlighted and the time matches. - if ( highlightedObject && highlightedObject.pick == timeDate.pick ) { + if ( highlightedObject && highlightedObject.pick == targetDate.pick ) { klasses.push( settings.klass.highlighted ) } // Add the `disabled` class if something's disabled and the object matches. - if ( disabledCollection && calendar.disabled( timeDate ) || timeDate.pick < minLimitObject.pick || timeDate.pick > maxLimitObject.pick ) { + if ( disabledCollection && calendar.disabled( targetDate ) || targetDate.pick < minLimitObject.pick || targetDate.pick > maxLimitObject.pick ) { klasses.push( settings.klass.disabled ) } return klasses.join( ' ' ) })([ settings.klass.day ]), - 'data-pick=' + timeDate.pick + 'data-pick=' + targetDate.pick ) ] //endreturn } }) ] //endreturn