Permalink
Browse files

Timepicker: Refactoring culture to be an option

  • Loading branch information...
gnarf committed Oct 27, 2011
1 parent 8d0e3c8 commit 9e3d21c9fac5ae376f9e6e7b20944cc2a7b316af
Showing with 66 additions and 91 deletions.
  1. +1 −3 tests/visual/timepicker/timepicker.html
  2. +65 −88 ui/jquery.ui.timepicker.js
@@ -18,9 +18,7 @@
<script>
$(function() {
$("#culture").change(function() {
- Globalize.culture( $( this ).val() );
- $( "input" ).timepicker( "destroy" );
- setup();
+ $( "input" ).timepicker( "option", "culture", $( this ).val() );
});
function setup() {
$("#timepicker").timepicker();
View
@@ -26,50 +26,15 @@ function makeBetweenMaskFunction( min, max, def, pad ) {
};
}
-// a wrapper function for Globalize integration
-function getAmPmArrays() {
- if ( window.Globalize ) {
- var calendar = Globalize.culture().calendars.standard;
-
- return {
- am: calendar.AM,
- pm: calendar.PM
- };
- } else {
- return {
- am: [ "AM", "am" ],
- pm: [ "PM", "pm" ]
- };
- }
-}
-
-function validAmPm( value ) {
- var i, j, l,
- valid = getAmPmArrays();
-
- if ( value === "" ) {
- return valid.pm && valid.pm[0];
- }
-
- for ( i in valid ) {
- for ( j = 0, l = valid[ i ].length; j < l; j++ ) {
- if ( valid[ i ][ j ].substr( 0, value.length ) === value ) {
- return valid[ i ][ 0 ];
- }
- }
- }
-}
-
-var rsingleh = /\b(h)(?=:)/i,
- rlowerhg = /h/g,
+var formatNonPaddedHours = /\b(h)(?=:)/i,
+ format12Hour = /h/g,
maskDefinitions = {
_h: makeBetweenMaskFunction( 1, 12, "12", " " ),
hh: makeBetweenMaskFunction( 1, 12, "12", "0" ),
_H: makeBetweenMaskFunction( 0, 23, "12", " " ),
HH: makeBetweenMaskFunction( 0, 23, "12", "0" ),
mm: makeBetweenMaskFunction( 0, 59, "00", "0" ),
- ss: makeBetweenMaskFunction( 0, 59, "00", "0" ),
- tt: validAmPm
+ ss: makeBetweenMaskFunction( 0, 59, "00", "0" )
};
$.widget( "ui.timepicker", {
@@ -85,7 +50,9 @@ $.widget( "ui.timepicker", {
this.element.mask({
mask: this._generateMask(),
clearEmpty: false,
- definitions: maskDefinitions
+ definitions: $.extend({
+ tt: $.proxy( this, "_validAmPm" )
+ }, maskDefinitions )
});
this.mask = this.element.data( "mask" );
this.element.spinner();
@@ -123,7 +90,8 @@ $.widget( "ui.timepicker", {
var bufferIndex, bufferObject,
buffer = this.mask.buffer,
bufferLength = buffer.length,
- ampm = getAmPmArrays();
+ maskDefinitions = this.mask.options.definitions,
+ ampm = this._getCulture();
if ( value == null ) {
@@ -132,17 +100,17 @@ $.widget( "ui.timepicker", {
for ( bufferIndex = 0; bufferIndex < bufferLength; bufferIndex += 3 ) {
bufferObject = buffer[ bufferIndex ];
if (
- bufferObject.valid == maskDefinitions._h || bufferObject.valid == maskDefinitions.hh ||
- bufferObject.valid == maskDefinitions._H || bufferObject.valid == maskDefinitions.HH
+ bufferObject.valid === maskDefinitions._h || bufferObject.valid === maskDefinitions.hh ||
+ bufferObject.valid === maskDefinitions._H || bufferObject.valid === maskDefinitions.HH
) {
value[ 0 ] = parseInt( bufferObject.value, 10 );
- } else if ( bufferObject.valid == maskDefinitions.mm ) {
+ } else if ( bufferObject.valid === maskDefinitions.mm ) {
value[ 1 ] = bufferObject.value;
- } else if ( bufferObject.valid == maskDefinitions.ss ) {
+ } else if ( bufferObject.valid === maskDefinitions.ss ) {
value[ 2 ] = bufferObject.value;
- } else if ( bufferObject.valid == maskDefinitions.tt ) {
+ } else if ( bufferObject.valid === maskDefinitions.tt ) {
value[ 0 ] %= 12;
- if ( jQuery.inArray( bufferObject.value, ampm.pm ) > -1 ) {
+ if ( jQuery.inArray( bufferObject.value, ampm.PM ) > -1 ) {
value[ 0 ] += 12;
}
}
@@ -157,14 +125,14 @@ $.widget( "ui.timepicker", {
value = value.split( ":" );
for ( bufferIndex = 0; bufferIndex < bufferLength; bufferIndex += 3 ) {
bufferObject = buffer[ bufferIndex ];
- if ( bufferObject.valid == maskDefinitions._h || bufferObject.valid == maskDefinitions.hh ) {
+ if ( bufferObject.valid === maskDefinitions._h || bufferObject.valid === maskDefinitions.hh ) {
// 12 hr mode
bufferObject.value = bufferObject.valid( parseInt( value[0], 10 ) % 12 || 12 );
- } else if ( bufferObject.valid == maskDefinitions.tt ) {
+ } else if ( bufferObject.valid === maskDefinitions.tt ) {
// am/pm
- bufferObject.value = ampm[ parseInt( value[0], 10 ) < 12 ? "am" : "pm" ][ 0 ];
+ bufferObject.value = ampm[ parseInt( value[0], 10 ) < 12 ? "AM" : "PM" ][ 0 ];
} else {
// minutes/seconds
@@ -198,28 +166,31 @@ $.widget( "ui.timepicker", {
this._highlightField();
}
},
- _generateMask: function() {
- var mask = "";
-
+ _getCulture: function() {
if ( window.Globalize ) {
- mask = Globalize.culture().calendars.standard.patterns[ this.options.seconds ? "T" : "t" ];
- mask = mask.replace( rsingleh, "_$1" );
-
- if ( !this.options.ampm ) {
- mask = mask.replace( rlowerhg, "H" ).replace( " tt", "" );
- }
+ return Globalize.culture( this.options.culture ).calendars.standard;
+ } else {
- return mask;
+ // minimal calendar object for timepicker
+ return {
+ patterns: {
+ t: "h:mm tt",
+ T: "h:mm:ss tt"
+ },
+ AM: [ "AM", "am" ],
+ PM: [ "PM", "pm" ]
+ };
}
+ },
+ _generateMask: function() {
+ var mask = this._getCulture().patterns[ this.options.seconds ? "T" : "t" ];
- mask += this.options.ampm ? "hh" : "HH";
- mask += ":mm";
- if ( this.options.seconds ) {
- mask += ":ss";
- }
- if ( this.options.ampm ) {
- mask += " tt";
+ mask = mask.replace( formatNonPaddedHours, "_$1" );
+
+ if ( !this.options.ampm ) {
+ mask = mask.replace( format12Hour, "H" ).replace( " tt", "" );
}
+
return mask;
},
_highlightField: function( field ) {
@@ -248,45 +219,51 @@ $.widget( "ui.timepicker", {
break;
}
},
- _setOption: function( key, value ) {
- this._super( "_setOption", key, value );
+ _setOptions: function( options ) {
- if ( key === "ampm" ) {
- var currentValue = this.value(),
- newMask = this._generateMask();
+ var currentValue = this.value();
- this.element.mask( "option", "mask", this._generateMask() );
- this.value( currentValue );
- }
- if ( key === "seconds" ) {
- if ( value ) {
- this.element.val( function( index, value ) {
- return value.substr( 0, 6 ) + ":00" + value.substr( 6 );
- });
- } else {
- this.element.val( function( index, value ) {
- return value.substr( 0, 6 ) + value.substr( 9 );
- });
- }
- this.element.mask( "option", "mask", this._generateMask() );
- }
+ // change the option
+ this._super( "_setOptions", options );
+
+ // update the mask, all of the option changes have a chance of changing it
+ this.element.mask( "option", "mask", this._generateMask() );
+
+ // restore the value from before the option changed
+ this.value( currentValue );
},
_spinnerParse: function( val ) {
val = this.mask.buffer[ this.currentField * 3 ].value;
if ( this.currentField === ( this.options.seconds ? 3 : 2 ) ) {
- return jQuery.inArray( val, getAmPmArrays().am ) > -1 ? 0 : 1;
+ return jQuery.inArray( val, this._getCulture().AM ) > -1 ? 0 : 1;
}
return parseInt( val, 10 ) || 0;
},
_spinnerValue: function( val ) {
var bufferObject = this.mask.buffer[ this.currentField * 3 ];
if ( this.currentField === ( this.options.seconds ? 3 : 2 ) ) {
- val = getAmPmArrays()[ parseInt( val, 10 ) ? "pm" : "am" ][ 0 ];
+ val = this._getCulture()[ parseInt( val, 10 ) ? "PM" : "AM" ][ 0 ];
}
bufferObject.value = bufferObject.valid( val + "" );
this.mask._paint();
this.spinner._refresh();
this.mask._caretSelect( this.currentField * 3 );
+ },
+ _validAmPm: function( val ) {
+ var ampm, j, l,
+ valid = this._getCulture();
+
+ if ( val === "" ) {
+ return valid.PM && valid.PM[0];
+ }
+
+ for ( ampm in { AM: 1, PM: 1 } ) {
+ for ( j = 0, l = valid[ ampm ].length; j < l; j++ ) {
+ if ( valid[ ampm ][ j ].substr( 0, val.length ) === val ) {
+ return valid[ ampm ][ 0 ];
+ }
+ }
+ }
}
});

0 comments on commit 9e3d21c

Please sign in to comment.