Skip to content

Commit

Permalink
fixes #393 and #388
Browse files Browse the repository at this point in the history
also cleaned erroneous whitespace
  • Loading branch information
amsul committed Apr 12, 2014
1 parent 9e22bcf commit ae85e5a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 46 deletions.
26 changes: 17 additions & 9 deletions lib/picker.date.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ DatePicker.prototype.overlapRanges = function( one, two ) {
one = calendar.createRange( one.from, one.to )
two = calendar.createRange( two.from, two.to )

return calendar.withinRange( one, two.from ) || calendar.withinRange( one, two.to ) ||
return calendar.withinRange( one, two.from ) || calendar.withinRange( one, two.to ) ||
calendar.withinRange( two, one.from ) || calendar.withinRange( two, one.to )
}

Expand Down Expand Up @@ -440,9 +440,9 @@ DatePicker.prototype.validate = function( type, dateObject, options ) {

// Return only integers for enabled weekdays.
return _.isInteger( value )
}).length/*,
}).length,

safety = 100*/
safety = 100



Expand Down Expand Up @@ -471,12 +471,12 @@ DatePicker.prototype.validate = function( type, dateObject, options ) {


// Keep looping until we reach an enabled date.
while ( /*safety &&*/ calendar.disabled( dateObject ) ) {
while ( safety && calendar.disabled( dateObject ) ) {

/*safety -= 1
safety -= 1
if ( !safety ) {
throw 'Fell into an infinite loop while validating ' + dateObject.obj + '.'
}*/
}


// If we’ve looped into the next/prev month with a large interval, return to the original date and flatten the interval.
Expand All @@ -490,12 +490,20 @@ DatePicker.prototype.validate = function( type, dateObject, options ) {
if ( dateObject.pick <= minLimitObject.pick ) {
reachedMin = true
interval = 1
dateObject = calendar.create([ minLimitObject.year, minLimitObject.month, minLimitObject.date - 1 ])
dateObject = calendar.create([
minLimitObject.year,
minLimitObject.month,
minLimitObject.date + (dateObject.pick === minLimitObject.pick ? 0 : -1)
])
}
else if ( dateObject.pick >= maxLimitObject.pick ) {
reachedMax = true
interval = -1
dateObject = calendar.create([ maxLimitObject.year, maxLimitObject.month, maxLimitObject.date + 1 ])
dateObject = calendar.create([
maxLimitObject.year,
maxLimitObject.month,
maxLimitObject.date + (dateObject.pick === maxLimitObject.pick ? 0 : 1)
])
}


Expand Down Expand Up @@ -579,7 +587,7 @@ DatePicker.prototype.parse = function( type, value, options ) {
}

// Calculate the month index to adjust with.
monthIndex = typeof value == 'string' && !options.fromValue ? 1 : 0
monthIndex = typeof value == 'string' && !options.fromValue ? 1 : 0

// Convert the format into an array and then map through it.
calendar.formats.toArray( options.format ).map( function( label ) {
Expand Down
8 changes: 4 additions & 4 deletions lib/picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
thingObject = thingIsObject ? thing : {}

// Make sure we have usable options.
options = thingIsObject && $.isPlainObject( value ) ? value : options || {}
options = thingIsObject && $.isPlainObject( value ) ? value : options || {}

if ( thing ) {

Expand Down Expand Up @@ -922,17 +922,17 @@ function aria(element, attribute, value) {
}
function ariaSet(element, attribute, value) {
element.setAttribute(
(attribute == 'role' ? '' : 'aria-') + attribute,
(attribute == 'role' ? '' : 'aria-') + attribute,
value
)
}
function ariaAttr(attribute, data) {
if ( !$.isPlainObject(attribute) ) {
attribute = { attribute: data }
attribute = { attribute: data }
}
data = ''
for ( var key in attribute ) {
var attr = (key == 'role' ? '' : 'aria-') + key,
var attr = (key == 'role' ? '' : 'aria-') + key,
attrVal = attribute[key]
data += attrVal == null ? '' : attr + '="' + attribute[key] + '"'
}
Expand Down
2 changes: 1 addition & 1 deletion lib/picker.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ TimePicker.prototype.overlapRanges = function( one, two ) {
one = clock.createRange( one.from, one.to )
two = clock.createRange( two.from, two.to )

return clock.withinRange( one, two.from ) || clock.withinRange( one, two.to ) ||
return clock.withinRange( one, two.from ) || clock.withinRange( one, two.to ) ||
clock.withinRange( two, one.from ) || clock.withinRange( two, one.to )
}

Expand Down
30 changes: 2 additions & 28 deletions tests/dev/date.htm
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,12 @@ <h3><label for="input_01">Pick a date. Go ahead...</label></h3>
<script type="text/javascript">

var $input = $( '.datepicker' ).pickadate({
selectYears: true,
selectMonths: true,
formatSubmit: 'yyyy-mm-dd',
// min: new Date(2014,3,20),
// max: new Date(2013,7,14),
disable: [
// 3,
// [2014,1,11,'inverted'],
// [2014,1,4],
// [2014,1,18],
// [2014,1,25],
// { from: [2014,1,4], to: [2014,1,14] },
// { from: true, to: 4, inverted: true },
// { from: new Date(2014,1,4), to: new Date(2014,1,14) },
// { from: true, to: [2014,1,10] },
// { from: [2014,1,2], to: 10 },
// { from: true, to: 10 },
// { from: -10, to: true }
]
min: [2014,3,12], //Saturday
disable: [1,7],
})

var picker = $input.pickadate('picker')

picker.set('disable', [
{ from: [2014,1,4], to: [2014,1,10] },
{ from: [2014,1,8], to: [2014,1,12] }
])
picker.set('enable', [
{ from: [2014,1,8], to: [2014,1,12] }
])
console.log( JSON.stringify( picker.get( 'disable' ), null, '\t' ) );

// $('button').on('click', function() {
// picker.set('disable', true);
// });
Expand Down
46 changes: 42 additions & 4 deletions tests/units/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module( 'Date picker setup', {

test( 'Editable', function() {

$DOM.append( $INPUT.clone()).append( $INPUT.clone() )
$DOM.append( $INPUT.clone() ).append( $INPUT.clone() )

var $input1 = $DOM.find( 'input' ).eq(0).pickadate()
var $input2 = $DOM.find( 'input' ).eq(1).pickadate({
Expand All @@ -132,6 +132,44 @@ test( 'Editable', function() {
strictEqual( $input2[0].readOnly, false, 'Editable: true' )
})

test( 'Disable today with `min` as `true`', function() {

$DOM.append( $INPUT.clone() )

var today = new Date()
var $input = $DOM.find( 'input' ).pickadate({
min: true,
disable: [ today ]
})
var picker = $input.pickadate('picker')
var highlighted = picker.get('highlight')

deepEqual(
[today.getFullYear(), today.getMonth(), today.getDate() + 1],
[highlighted.year, highlighted.month, highlighted.date],
'Able to disable today'
)
})

test( 'Disable today with `max` as `true`', function() {

$DOM.append( $INPUT.clone() )

var today = new Date()
var $input = $DOM.find( 'input' ).pickadate({
max: true,
disable: [ today ]
})
var picker = $input.pickadate('picker')
var highlighted = picker.get('highlight')

deepEqual(
[today.getFullYear(), today.getMonth(), today.getDate() - 1],
[highlighted.year, highlighted.month, highlighted.date],
'Able to disable today'
)
})




Expand Down Expand Up @@ -867,8 +905,8 @@ test( '`disable` and `enable` using overlapping ranges', function() {
$dates = $root.find('.' + $.fn.pickadate.defaults.klass.disabled)
for ( index = 0, datesCount = $dates.length; index < datesCount; index += 1 ) {
disabledDate = +$dates[index].innerHTML
ok( disabledDate >= 4 && disabledDate < 14 ||
disabledDate > 18 && disabledDate <= 28,
ok( disabledDate >= 4 && disabledDate < 14 ||
disabledDate > 18 && disabledDate <= 28,
'Date is disabled: ' + disabledDate
);
}
Expand Down Expand Up @@ -912,7 +950,7 @@ test( '`disable` and `enable` using overlapping ranges', function() {
$dates = $root.find('.' + $.fn.pickadate.defaults.klass.disabled)
for ( index = 0, datesCount = $dates.length; index < datesCount; index += 1 ) {
disabledDate = +$dates[index].innerHTML
ok( disabledDate >= 4 && disabledDate < 16,
ok( disabledDate >= 4 && disabledDate < 16,
'Date is disabled: ' + disabledDate
);
}
Expand Down

0 comments on commit ae85e5a

Please sign in to comment.