1 /*global Globalize:false, range:false, regexp:false*/
3 * jQuery Mobile Widget @VERSION
5 * This software is licensed under the MIT licence (as defined by the OSI at
6 * http://www.opensource.org/licenses/mit-license.php)
8 * ***************************************************************************
9 * Copyright (C) 2011 by Intel Corporation Ltd.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 * ***************************************************************************
30 * Authors: Salvatore Iovene <salvatore.iovene@intel.com>
31 * Daehyon Jung <darrenh.jung@samsung.com>
35 * datetimepicker is a widget that lets the user select a date and/or a
36 * time. If you'd prefer use as auto-initialization of form elements,
37 * use input elements with type=date/time/datetime within form tag
38 * as same as other form elements.
42 * data-role: 'datetimepicker'
43 * data-format: date format string. e.g) "MMM dd yyyy, HH:mm"
44 * type: 'date', 'datetime', 'time'
45 * value: pre-set value. only accepts ISO date string. e.g) "2012-05-04", "2012-05-04T01:02:03+09:00"
46 * data-date: any date/time string "new Date()" accepts.
49 * type: 'date', 'datetime', 'time'
50 * format: see data-format in HTML Attributes.
51 * value: see value in HTML Attributes.
52 * date: preset value as JavaScript Date Object representation.
56 * : Set date/time to 'datestring'.
58 * : Get current selected date/time as W3C DTF style string.
59 * getValue() - replaced with 'value()'
61 * setValue( datestring ) - replaced with 'value(datestring)'
62 * : same as value( datestring )
63 * changeTypeFormat( type, format ) - deprecated
64 * : Change Type and Format options. use datetimepicker( "option", "format" ) instead
67 * date-changed: Raised when date/time was changed.
70 * <ul data-role="listview">
71 * <li class="ui-li-3-2-2">
72 * <span class="ui-li-text-main">
73 * <input type="datetime" name="demo-date" id="demo-date"
74 * data-format="MMM dd yyyy hh:mm tt"/>
76 * <span class="ui-li-text-sub">
77 * Date/Time Picker - <span id="selected-date1"><em>(select a date first)</em></span>
80 * <li class="ui-li-3-2-2">
81 * <span class="ui-li-text-main">
82 * <input type="date" name="demo-date2" id="demo-date2"/>
84 * <span class="ui-li-text-sub">
85 * Date Picker - <span id="selected-date2"><em>(select a date first)</em></span>
88 * <li class="ui-li-3-2-2">
89 * <span class="ui-li-text-main">
90 * <input type="time" name="demo-date3" id="demo-date3"/>
92 * <span class="ui-li-text-sub">
93 * Time Picker - <span id="selected-date3"><em>(select a date first)</em></span>
97 * How to get a return value:
98 * ==========================
99 * Bind to the 'date-changed' event, e.g.:
100 * $("#myDatetimepicker").bind("date-changed", function(e, date) {
101 * alert("New date: " + date.toString());
106 ( function ( $, window, undefined ) {
107 $.widget( "tizen.datetimepicker", $.tizen.widgetex, {
110 type: null, // date, time, datetime applicable
113 initSelector: "input[type='date'], input[type='datetime'], input[type='time'], :jqmData(role='datetimepicker')"
116 _calendar: function () {
117 return window.Globalize.culture().calendars.standard;
121 attr: "data-" + ( $.mobile.ns || "" ) + "date",
122 signal: "date-changed"
125 _daysInMonth: [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
127 _isLeapYear: function ( year ) {
128 return year % 4 ? 0 : ( year % 100 ? 1 : ( year % 400 ? 0 : 1 ) );
131 _makeTwoDigits: function ( val ) {
132 var ret = val.toString(10);
140 _setType: function ( type ) {
141 //datetime, date, time
146 this.options.type = type;
149 this.options.type = 'datetime';
153 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "type", this.options.type );
154 return this.options.type;
157 _setFormat: function ( format ) {
158 if ( this.options.format != format ) {
159 this.options.format = format;
164 this.ui.children().remove();
166 var token = this._parsePattern( format ),
167 div = document.createElement('div'),
174 while ( token.length > 0 ) {
176 tpl = '<span class="ui-datefield-%1" data-pat="' + pat + '">%2</span>';
178 case 'H': //0 1 2 3 ... 21 22 23
179 case 'HH': //00 01 02 ... 21 22 23
180 case 'h': //0 1 2 3 ... 11 12
181 case 'hh': //00 01 02 ... 11 12
182 $(div).append( tpl.replace('%1', 'hour') );
184 case 'mm': //00 01 ... 59
185 case 'm': //0 1 2 ... 59
186 $(div).append( tpl.replace('%1', 'min') );
190 $(div).append( tpl.replace('%1', 'sec') );
192 case 'd': // day of month 5
193 case 'dd': // day of month(leading zero) 05
194 $(div).append( tpl.replace('%1', 'day') );
196 case 'M': // Month of year 9
197 case 'MM': // Month of year(leading zero) 09
200 $(div).append( tpl.replace('%1', 'month') );
202 case 'yy': // year two digit
203 case 'yyyy': // year four digit
204 $(div).append( tpl.replace('%1', 'year') );
206 case 't': //AM / PM indicator(first letter) A, P
208 case 'tt': //AM / PM indicator AM/PM
210 btn = '<a href="#" class="ui-datefield-period"' +
211 ' data-role="button" data-inline="true">period</a>';
212 $(div).append( btn );
216 $(div).append( tpl.replace('%1', 'era').replace('%2', this._calendar().eras.name) );
219 $(div).append( tpl.replace('%1', 'tab').replace('%2', pat) );
221 default : // string or any non-clickable object
222 $(div).append( tpl.replace('%1', 'seperator').replace('%2', pat) );
227 this.ui.append( div );
228 if ( this.options.date ) {
229 this._setDate( this.options.date );
232 this.ui.find('.ui-datefield-period').buttonMarkup().bind( 'vclick', function ( e ) {
233 obj._switchAmPm( obj );
236 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "format", this.options.format );
237 return this.options.format;
240 _setDate: function ( newdate ) {
241 if ( typeof ( newdate ) == "string" ) {
242 newdate = new Date( newdate );
245 var fields = $('span,a', this.ui),
252 function getMonth() {
253 return newdate.getMonth() + 1;
256 for ( i = 0; i < fields.length; i++ ) {
257 $field = $(fields[i]);
258 type = $field.attr("class").match(/ui-datefield-([\w]*)/);
264 fn = newdate.getHours;
267 fn = newdate.getMinutes;
270 fn = newdate.getSeconds;
273 fn = newdate.getFullYear;
279 fn = newdate.getDate;
282 fn = newdate.getHours() < 12 ? this._calendar().AM[0] : this._calendar().PM[0];
283 btn = $field.find( '.ui-btn-text' );
284 if ( btn.length == 0 ) {
286 } else if ( btn.text() != fn ) {
296 this._updateField( $field, fn.call( newdate ) );
300 this.options.date = newdate;
302 this._setValue( this.value() );
304 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "date", this.options.date );
305 return this.options.date;
308 destroy: function () {
313 if ( this.element ) {
318 value: function ( val ) {
319 function timeStr( t, obj ) {
320 return obj._makeTwoDigits( t.getHours() ) + ':' +
321 obj._makeTwoDigits( t.getMinutes() ) + ':' +
322 obj._makeTwoDigits( t.getSeconds() );
325 function dateStr( d, obj ) {
326 return ( ( d.getFullYear() % 10000 ) + 10000 ).toString().substr(1) + '-' +
327 obj._makeTwoDigits( d.getMonth() + 1 ) + '-' +
328 obj._makeTwoDigits( d.getDate() );
333 rvalue = this._setDate( val );
335 switch ( this.options.type ) {
337 rvalue = timeStr( this.options.date, this );
340 rvalue = dateStr( this.options.date, this );
343 rvalue = dateStr( this.options.date, this ) + 'T' + timeStr( this.options.date, this );
350 setValue: function ( newdate ) {
351 console.warn( "setValue was deprecated. use datetimepicker('option', 'date', value) instead." );
352 return this.value( newdate );
356 * return W3C DTF string
358 getValue: function () {
359 console.warn("getValue() was deprecated. use datetimepicker('value') instead.");
363 _updateField: function ( target, value ) {
364 if ( !target || target.length == 0 ) {
372 var pat = target.jqmData( 'pat' ),
381 if ( pat.charAt(0) == 'h' ) {
384 } else if ( hour == 0 ) {
388 if ( pat.length == 2 ) {
389 hour = this._makeTwoDigits( hour );
403 text = this._makeTwoDigits( value );
406 text = this._calendar().months.namesAbbr[ value - 1];
409 text = this._calendar().months.names[ value - 1 ];
412 text = this._makeTwoDigits( value % 100 );
416 value = '000' + value;
417 } else if ( value < 100 ) {
418 value = '00' + value;
419 } else if ( value < 1000 ) {
425 // to avoid reflow where its value isn't out-dated
426 if ( target.text() != text ) {
431 _switchAmPm: function ( obj ) {
432 if ( this._calendar().AM != null ) {
433 var date = new Date( this.options.date ),
435 change = 1000 * 60 * 60 * 12;
436 if ( date.getHours() > 11 ) {
439 date.setTime( date.getTime() + change );
440 this._setDate( date );
444 _parsePattern: function ( pattern ) {
445 var regex = /\/|\s|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|s|tt|t|f|gg|g|\'[\w\W]*\'$|[\w\W]/g,
449 matches = pattern.match( regex );
451 for ( i = 0; i < matches.length; i++ ) {
452 if ( matches[i].charAt(0) == "'" ) {
453 matches[i] = matches[i].substr( 1, matches[i].length - 2 );
460 changeTypeFormat: function ( type, format ) {
461 console.warn('changeTypeFormat() was deprecated. use datetimepicker("option", "type"|"format", value) instead');
463 this._setType( type );
467 this._setFormat( format );
471 _create: function () {
474 if ( this.element.is( "input" ) ) {
476 var type, value, format;
478 type = obj.element.attr( "type" );
479 obj.options.type = type;
481 value = obj.element.attr( "value" );
483 obj.options.date = new Date( value );
488 if ( !this.options.format ) {
489 switch ( this.options.type ) {
491 this.options.format = this._calendar().patterns.d + "\t" + this._calendar().patterns.t;
494 this.options.format = this._calendar().patterns.d;
497 this.options.format = this._calendar().patterns.t;
502 if ( !this.options.date ) {
503 this.options.date = new Date();
507 this.ui = $('<div class="ui-datefield"></div>');
508 $(this.element).after( this.ui );
510 this.ui.bind('vclick', function ( e ) {
511 obj._showDataSelector( obj, this, e.target );
515 _populateDataSelector: function ( field, pat ) {
520 range = window.range,
530 values = range( 0, 23 );
531 data = range( 0, 23 );
532 current = this.options.date.getHours();
534 values = range( 1, 12 );
535 current = this.options.date.getHours() - 1;//11
536 if ( current >= 11 ) {
537 current = current - 12;
538 data = range( 13, 23 );
539 data.push( 12 ); // consider 12:00 am as 00:00
541 data = range( 1, 11 );
545 current = 11; // 12:00 or 00:00
548 if ( pat.length == 2 ) {
550 values = values.map( this._makeTwoDigits );
552 numItems = values.length;
556 values = range( 0, 59 );
557 if ( pat.length == 2 ) {
558 values = values.map( this._makeTwoDigits );
560 data = range( 0, 59 );
561 current = ( field == 'min' ? this.options.date.getMinutes() : this.options.date.getSeconds() );
562 numItems = values.length;
567 data = range( yearlb, yearhb );
568 current = this.options.date.getFullYear() - yearlb;
569 values = range( yearlb, yearhb );
570 numItems = values.length;
573 switch ( pat.length ) {
575 values = range( 1, 12 );
578 values = range( 1, 12 ).map( this._makeTwoDigits );
581 values = this._calendar().months.namesAbbr.slice();
584 values = this._calendar().months.names.slice();
587 if ( values.length == 13 ) { // @TODO Lunar calendar support
588 if ( values[12] == "" ) { // to remove lunar calendar reserved space
592 data = range( 1, values.length );
593 current = this.options.date.getMonth();
594 numItems = values.length;
597 day = this._daysInMonth[ this.options.date.getMonth() ];
599 day += this._isLeapYear( this.options.date.getFullYear() );
601 values = range( 1, day );
602 if ( pat.length == 2 ) {
603 values = values.map( this._makeTwoDigits );
605 data = range( 1, day );
606 current = this.options.date.getDate() - 1;
620 _showDataSelector: function ( obj, ui, target ) {
623 var attr = target.attr("class"),
624 field = attr ? attr.match(/ui-datefield-([\w]*)/) : undefined,
646 target.not('.ui-datefield-seperator').addClass('ui-datefield-selected');
648 pat = target.jqmData('pat');
649 data = obj._populateDataSelector.call( obj, field[1], pat );
651 values = data.values;
652 numItems = data.numItems;
653 current = data.current;
654 valuesData = data.data;
657 datans = "data-" + ($.mobile.ns ? ($.mobile.ns + '-') : "") + 'val="';
658 for ( i = 0; i < values.length; i++ ) {
659 html += '<li><a class="ui-link" ' + datans + valuesData[i] + '">' + values[i] + '</a></li>';
662 $ul = $("<ul></ul>");
663 $div = $('<div class="ui-datetimepicker-selector" data-transition="none" data-fade="false"></div>');
664 $div.append( $ul ).appendTo( ui );
665 $ctx = $div.ctxpopup();
666 $ctx.parents('.ui-popupwindow').addClass('ui-datetimepicker');
668 $( $li[current] ).addClass("current");
669 $div.jqmData( "list", $li );
671 if ( !obj._reflow ) {
672 obj._reflow = function () {
673 $div.circularview("reflow");
675 $(window).bind("resize", obj._reflow);
677 // cause ctxpopup forced to subtract 10
678 $ctx.popupwindow( 'open',
679 target.offset().left + ( target.width() / 2 ) + 10 - window.pageXOffset ,
680 target.offset().top + target.height() - window.pageYOffset );
681 $div.bind('popupafterclose', function ( e ) {
683 $(window).unbind("resize", obj._reflow);
686 $div.unbind( 'popupafterclose' );
687 $ul.unbind( 'vclick' );
688 $(obj).unbind( 'update' );
689 $(ui).find('.ui-datefield-selected').removeClass('ui-datefield-selected');
690 $ctx.popupwindow( 'destroy' );
694 $(obj).bind( 'update', function ( e, val ) {
695 $ctx.popupwindow( 'close' );
696 var date = new Date( this.options.date );
697 switch ( field[1] ) {
699 date.setMinutes( val );
702 date.setHours( val );
705 date.setSeconds( val );
708 date.setFullYear( val );
711 date.setMonth( val - 1 );
717 obj._setDate( date );
720 $ul.bind( 'click', function ( e ) {
721 if ( $(e.target).is('a') ) {
722 $ul.find(".current").removeClass("current");
723 $(e.target).parent().addClass('current');
724 var val = $(e.target).jqmData("val");
725 $(obj).trigger( 'update', val ); // close popup, unselect field
729 $div.circularview( 'centerTo', '.current', 500 );
736 $(document).bind("pagecreate create", function ( e ) {
737 $($.tizen.datetimepicker.prototype.options.initSelector, e.target)
738 .not(":jqmData(role='none'), :jqmData(role='nojs')")
742 } ( jQuery, this ) );