Skip to content

Commit

Permalink
fixed amsul#128 & fixes amsul#316: parsing date’s month using correct…
Browse files Browse the repository at this point in the history
… index
  • Loading branch information
amsul committed Jan 31, 2014
1 parent 58402ef commit ce813fd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 80 deletions.
38 changes: 18 additions & 20 deletions _raw/lib/picker.date.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,11 @@ function DatePicker( picker, settings ) {
set( 'now' ).

// Setting the `select` also sets the `highlight` and `view`.
set( 'select',

// Use the value provided or default to selecting “today”.
valueString || calendar.item.now,
{
// Use the appropriate format.
format: formatString,

// Set user-provided month data as true when there is a
// “mm” or “m” used in the relative format string.
data: (function( formatArray ) {
return valueString && ( formatArray.indexOf( 'mm' ) > -1 || formatArray.indexOf( 'm' ) > -1 )
})( calendar.formats.toArray( formatString ) )
}
)
// Use the value provided or default to selecting “today”.
set( 'select', valueString || calendar.item.now, {
format: formatString,
fromValue: !!elementValue
})


// The keycode to movement mapping.
Expand Down Expand Up @@ -441,18 +431,22 @@ DatePicker.prototype.disabled = function( dateObject ) {
DatePicker.prototype.parse = function( type, value, options ) {

var calendar = this,
parsingObject = {}
parsingObject = {},
monthIndex

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.
// We need a `.format` to parse the value with.
if ( !( options && options.format ) ) {
// should probably default to the default format.
throw "Need a formatting option to parse this.."
options = options || {}
options.format = calendar.settings.format
}

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

// Convert the format into an array and then map through it.
calendar.formats.toArray( options.format ).map( function( label ) {

Expand All @@ -475,7 +469,11 @@ DatePicker.prototype.parse = function( type, value, options ) {
})

// If it’s parsing a user provided month value, compensate for month 0index.
return [ parsingObject.yyyy || parsingObject.yy, +( parsingObject.mm || parsingObject.m ) - ( options.data ? 1 : 0 ), parsingObject.dd || parsingObject.d ]
return [
parsingObject.yyyy || parsingObject.yy,
+( parsingObject.mm || parsingObject.m ) - monthIndex,
parsingObject.dd || parsingObject.d
]
} //DatePicker.prototype.parse


Expand Down
28 changes: 10 additions & 18 deletions _raw/lib/picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {

// Then, check to update the element value and broadcast a change.
if ( thingItem == 'select' || thingItem == 'clear' ) {
$ELEMENT.val( thingItem == 'clear' ? '' :
PickerConstructor._.trigger( P.component.formats.toString, P.component, [ SETTINGS.format, P.component.get( thingItem ) ] )
$ELEMENT.val( thingItem == 'clear' ?
'' : P.get( thingItem, SETTINGS.format )
).trigger( 'change' )
}
}
Expand Down Expand Up @@ -411,7 +411,11 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
// Check if a component item exists, return that.
if ( P.component.item[ thing ] ) {
if ( typeof format == 'string' ) {
return PickerConstructor._.trigger( P.component.formats.toString, P.component, [ format, P.component.get( thing ) ] )
return PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[ format, P.component.get( thing ) ]
)
}
return P.component.get( thing )
}
Expand Down Expand Up @@ -522,11 +526,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {

// If there’s a `data-value`, update the value of the element.
val( $ELEMENT.data('value') ?
PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[SETTINGS.format, P.component.item.select]
) :
P.get('select', SETTINGS.format) :
ELEMENT.value
).

Expand Down Expand Up @@ -680,11 +680,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
// If the element has a value, set the hidden value as well.
(
$ELEMENT.data('value') || ELEMENT.value ?
' value="' + PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[SETTINGS.formatSubmit, P.component.item.select]
) + '"' :
' value="' + P.get('select', SETTINGS.formatSubmit) + '"' :
''
) +
'>'
Expand All @@ -695,11 +691,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
// If the value changes, update the hidden input with the correct format.
on('change.' + STATE.id, function() {
P._hidden.value = ELEMENT.value ?
PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[SETTINGS.formatSubmit, P.component.item.select]
) :
P.get('select', SETTINGS.formatSubmit) :
''
}).

Expand Down
5 changes: 3 additions & 2 deletions _raw/lib/picker.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,10 @@ TimePicker.prototype.parse = function( type, value, options ) {
return value
}

// We need a `.format` to parse the value.
// We need a `.format` to parse the value with.
if ( !( options && options.format ) ) {
throw "Need a formatting option to parse this.."
options = options || {}
options.format = clock.settings.format
}

// Convert the format into an array and then map through it.
Expand Down
38 changes: 18 additions & 20 deletions lib/picker.date.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,11 @@ function DatePicker( picker, settings ) {
set( 'now' ).

// Setting the `select` also sets the `highlight` and `view`.
set( 'select',

// Use the value provided or default to selecting “today”.
valueString || calendar.item.now,
{
// Use the appropriate format.
format: formatString,

// Set user-provided month data as true when there is a
// “mm” or “m” used in the relative format string.
data: (function( formatArray ) {
return valueString && ( formatArray.indexOf( 'mm' ) > -1 || formatArray.indexOf( 'm' ) > -1 )
})( calendar.formats.toArray( formatString ) )
}
)
// Use the value provided or default to selecting “today”.
set( 'select', valueString || calendar.item.now, {
format: formatString,
fromValue: !!elementValue
})


// The keycode to movement mapping.
Expand Down Expand Up @@ -441,18 +431,22 @@ DatePicker.prototype.disabled = function( dateObject ) {
DatePicker.prototype.parse = function( type, value, options ) {

var calendar = this,
parsingObject = {}
parsingObject = {},
monthIndex

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.
// We need a `.format` to parse the value with.
if ( !( options && options.format ) ) {
// should probably default to the default format.
throw "Need a formatting option to parse this.."
options = options || {}
options.format = calendar.settings.format
}

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

// Convert the format into an array and then map through it.
calendar.formats.toArray( options.format ).map( function( label ) {

Expand All @@ -475,7 +469,11 @@ DatePicker.prototype.parse = function( type, value, options ) {
})

// If it’s parsing a user provided month value, compensate for month 0index.
return [ parsingObject.yyyy || parsingObject.yy, +( parsingObject.mm || parsingObject.m ) - ( options.data ? 1 : 0 ), parsingObject.dd || parsingObject.d ]
return [
parsingObject.yyyy || parsingObject.yy,
+( parsingObject.mm || parsingObject.m ) - monthIndex,
parsingObject.dd || parsingObject.d
]
} //DatePicker.prototype.parse


Expand Down
28 changes: 10 additions & 18 deletions lib/picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {

// Then, check to update the element value and broadcast a change.
if ( thingItem == 'select' || thingItem == 'clear' ) {
$ELEMENT.val( thingItem == 'clear' ? '' :
PickerConstructor._.trigger( P.component.formats.toString, P.component, [ SETTINGS.format, P.component.get( thingItem ) ] )
$ELEMENT.val( thingItem == 'clear' ?
'' : P.get( thingItem, SETTINGS.format )
).trigger( 'change' )
}
}
Expand Down Expand Up @@ -411,7 +411,11 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
// Check if a component item exists, return that.
if ( P.component.item[ thing ] ) {
if ( typeof format == 'string' ) {
return PickerConstructor._.trigger( P.component.formats.toString, P.component, [ format, P.component.get( thing ) ] )
return PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[ format, P.component.get( thing ) ]
)
}
return P.component.get( thing )
}
Expand Down Expand Up @@ -522,11 +526,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {

// If there’s a `data-value`, update the value of the element.
val( $ELEMENT.data('value') ?
PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[SETTINGS.format, P.component.item.select]
) :
P.get('select', SETTINGS.format) :
ELEMENT.value
).

Expand Down Expand Up @@ -680,11 +680,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
// If the element has a value, set the hidden value as well.
(
$ELEMENT.data('value') || ELEMENT.value ?
' value="' + PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[SETTINGS.formatSubmit, P.component.item.select]
) + '"' :
' value="' + P.get('select', SETTINGS.formatSubmit) + '"' :
''
) +
'>'
Expand All @@ -695,11 +691,7 @@ function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
// If the value changes, update the hidden input with the correct format.
on('change.' + STATE.id, function() {
P._hidden.value = ELEMENT.value ?
PickerConstructor._.trigger(
P.component.formats.toString,
P.component,
[SETTINGS.formatSubmit, P.component.item.select]
) :
P.get('select', SETTINGS.formatSubmit) :
''
}).

Expand Down
5 changes: 3 additions & 2 deletions lib/picker.time.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,10 @@ TimePicker.prototype.parse = function( type, value, options ) {
return value
}

// We need a `.format` to parse the value.
// We need a `.format` to parse the value with.
if ( !( options && options.format ) ) {
throw "Need a formatting option to parse this.."
options = options || {}
options.format = clock.settings.format
}

// Convert the format into an array and then map through it.
Expand Down

0 comments on commit ce813fd

Please sign in to comment.