2 /*global Globalize:false, range:false, regexp:false*/
4 * jQuery Mobile Widget @VERSION
6 * This software is licensed under the MIT licence (as defined by the OSI at
7 * http://www.opensource.org/licenses/mit-license.php)
9 * ***************************************************************************
10 * Copyright (C) 2011 by Intel Corporation Ltd.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 * ***************************************************************************
31 * Authors: Salvatore Iovene <salvatore.iovene@intel.com>
32 * Daehyon Jung <darrenh.jung@samsung.com>
36 * datetimepicker is a widget that lets the user select a date and/or a
37 * time. If you'd prefer use as auto-initialization of form elements,
38 * use input elements with type=date/time/datetime within form tag
39 * as same as other form elements.
43 * data-role: 'datetimepicker'
44 * data-format: date format string. e.g) "MMM dd yyyy, HH:mm"
45 * type: 'date', 'datetime', 'time'
46 * value: pre-set value. only accepts ISO date string. e.g) "2012-05-04", "2012-05-04T01:02:03+09:00"
47 * data-date: any date/time string "new Date()" accepts.
50 * type: 'date', 'datetime', 'time'
51 * format: see data-format in HTML Attributes.
52 * value: see value in HTML Attributes.
53 * date: preset value as JavaScript Date Object representation.
57 * : Set date/time to 'datestring'.
59 * : Get current selected date/time as W3C DTF style string.
60 * getValue() - replaced with 'value()'
62 * setValue( datestring ) - replaced with 'value(datestring)'
63 * : same as value( datestring )
64 * changeTypeFormat( type, format ) - deprecated
65 * : Change Type and Format options. use datetimepicker( "option", "format" ) instead
68 * date-changed: Raised when date/time was changed. Date-changed event will be deprecated
71 * <ul data-role="listview">
72 * <li class="ui-li-3-2-2">
73 * <span class="ui-li-text-main">
74 * <input type="datetime" name="demo-date" id="demo-date"
75 * data-format="MMM dd yyyy hh:mm tt"/>
77 * <span class="ui-li-text-sub">
78 * Date/Time Picker - <span id="selected-date1"><em>(select a date first)</em></span>
81 * <li class="ui-li-3-2-2">
82 * <span class="ui-li-text-main">
83 * <input type="date" name="demo-date2" id="demo-date2"/>
85 * <span class="ui-li-text-sub">
86 * Date Picker - <span id="selected-date2"><em>(select a date first)</em></span>
89 * <li class="ui-li-3-2-2">
90 * <span class="ui-li-text-main">
91 * <input type="time" name="demo-date3" id="demo-date3"/>
93 * <span class="ui-li-text-sub">
94 * Time Picker - <span id="selected-date3"><em>(select a date first)</em></span>
98 * How to get a return value:
99 * ==========================
100 * Bind to the 'date-changed' event, e.g.:
101 * $("#myDatetimepicker").bind("change", function() {
107 @class DateTimePicker
108 The picker widgets show a control that you can use to enter date and time values. <br/> To add a date time picker widget to the application, use the following code:
110 <li class="ui-li-dialogue ui-datetime">
111 <div class="ui-datetime-text-main">
112 <input type="datetime" data-format="MMM dd yyyy hh:mm:ss" name="demo-date" id="demo-date" />
114 <div class="ui-li-text-sub">Date/Time Picker
115 <span id="selected-date1"><em>(select a date first)</em></span>
121 ( function ( $, window, undefined ) {
122 $.widget( "tizen.datetimepicker", $.tizen.widgetex, {
125 type: null, // date, time, datetime applicable
128 initSelector: "input[type='date'], input[type='datetime'], input[type='time'], :jqmData(role='datetimepicker')"
133 _calendar: function () {
134 return window.Globalize.culture().calendars.standard;
138 attr: "data-" + ( $.mobile.ns || "" ) + "date",
139 signal: "date-changed"
142 _daysInMonth: [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
144 _isLeapYear: function ( year ) {
145 return year % 4 ? 0 : ( year % 100 ? 1 : ( year % 400 ? 0 : 1 ) );
148 _makeTwoDigits: function ( val ) {
149 var ret = val.toString(10);
156 _setType: function ( type ) {
157 //datetime, date, time
162 this.options.type = type;
165 this.options.type = 'datetime';
169 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "type", this.options.type );
170 return this.options.type;
173 _setFormat: function ( format ) {
174 if ( this.options.format != format ) {
175 this.options.format = format;
180 this.ui.children().remove();
182 var token = this._parsePattern( format ),
183 div = document.createElement('div'),
190 while ( token.length > 0 ) {
192 tpl = '<span class="ui-datefield-%1" data-pat="' + pat + '">%2</span>';
194 case 'H': //0 1 2 3 ... 21 22 23
195 case 'HH': //00 01 02 ... 21 22 23
196 case 'h': //0 1 2 3 ... 11 12
197 case 'hh': //00 01 02 ... 11 12
198 $(div).append( tpl.replace('%1', 'hour') );
200 case 'mm': //00 01 ... 59
201 case 'm': //0 1 2 ... 59
202 if ( this.options.type == 'date' ) {
203 $(div).append( tpl.replace('%1', 'month') );
205 $(div).append( tpl.replace('%1', 'min') );
210 $(div).append( tpl.replace('%1', 'sec') );
212 case 'd': // day of month 5
213 case 'dd': // day of month(leading zero) 05
214 $(div).append( tpl.replace('%1', 'day') );
216 case 'M': // Month of year 9
217 case 'MM': // Month of year(leading zero) 09
220 $(div).append( tpl.replace('%1', 'month') );
222 case 'yy': // year two digit
223 case 'yyyy': // year four digit
224 $(div).append( tpl.replace('%1', 'year') );
226 case 't': //AM / PM indicator(first letter) A, P
228 case 'tt': //AM / PM indicator AM/PM
230 btn = '<a href="#" class="ui-datefield-period"' +
231 ' data-role="button" data-inline="true">period</a>';
232 $(div).append( btn );
236 $(div).append( tpl.replace('%1', 'era').replace('%2', this._calendar().eras.name) );
239 $(div).append( tpl.replace('%1', 'tab').replace('%2', pat) );
241 default : // string or any non-clickable object
242 $(div).append( tpl.replace('%1', 'seperator').replace('%2', pat) );
247 this.ui.append( div );
248 if ( this.options.date ) {
249 this._setDate( this.options.date );
252 this.ui.find('.ui-datefield-period').buttonMarkup().bind( 'vclick', function ( e ) {
253 obj._switchAmPm( obj );
257 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "format", this.options.format );
258 return this.options.format;
261 _setDate: function ( newdate ) {
262 if ( typeof ( newdate ) == "string" ) {
263 newdate = new Date( newdate );
266 var fields = $('span,a', this.ui),
273 function getMonth() {
274 return newdate.getMonth() + 1;
277 for ( i = 0; i < fields.length; i++ ) {
278 $field = $(fields[i]);
279 type = $field.attr("class").match(/ui-datefield-([\w]*)/);
285 fn = newdate.getHours;
288 fn = newdate.getMinutes;
291 fn = newdate.getSeconds;
294 fn = newdate.getFullYear;
300 fn = newdate.getDate;
303 fn = newdate.getHours() < 12 ? this._calendar().AM[0] : this._calendar().PM[0];
304 btn = $field.find( '.ui-btn-text' );
305 if ( btn.length == 0 ) {
307 } else if ( btn.text() != fn ) {
317 this._updateField( $field, fn.call( newdate ) );
321 this.options.date = newdate;
323 this._setValue( newdate );
325 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "date", this.options.date );
326 return this.options.date;
329 destroy: function () {
334 if ( this.element ) {
339 value: function ( val ) {
340 function timeStr( t, obj ) {
341 return obj._makeTwoDigits( t.getHours() ) + ':' +
342 obj._makeTwoDigits( t.getMinutes() ) + ':' +
343 obj._makeTwoDigits( t.getSeconds() );
346 function dateStr( d, obj ) {
347 return ( ( d.getFullYear() % 10000 ) + 10000 ).toString().substr(1) + '-' +
348 obj._makeTwoDigits( d.getMonth() + 1 ) + '-' +
349 obj._makeTwoDigits( d.getDate() );
354 rvalue = this._setDate( val );
356 switch ( this.options.type ) {
358 rvalue = timeStr( this.options.date, this );
361 rvalue = dateStr( this.options.date, this );
364 rvalue = dateStr( this.options.date, this ) + 'T' + timeStr( this.options.date, this );
371 setValue: function ( newdate ) {
372 console.warn( "setValue was deprecated. use datetimepicker('option', 'date', value) instead." );
373 return this.value( newdate );
377 * return W3C DTF string
379 getValue: function () {
380 console.warn("getValue() was deprecated. use datetimepicker('value') instead.");
384 _updateField: function ( target, value ) {
385 if ( !target || target.length == 0 ) {
393 var pat = target.jqmData( 'pat' ),
404 if ( pat.charAt(0) == 'h' ) {
407 } else if ( hour == 0 ) {
411 hour = this._makeTwoDigits( hour );
424 text = this._makeTwoDigits( value );
427 text = this._calendar().months.namesAbbr[ value - 1];
430 text = this._calendar().months.names[ value - 1 ];
433 text = this._makeTwoDigits( value % 100 );
437 value = '000' + value;
438 } else if ( value < 100 ) {
439 value = '00' + value;
440 } else if ( value < 1000 ) {
447 // to avoid reflow where its value isn't out-dated
448 if ( target.text() != text ) {
449 if ( target.hasClass("ui-datefield-selected") ) {
450 target.addClass("out");
451 this._new_value = text;
453 target.animationComplete( function () {
454 target.text( self._new_value);
455 target.addClass("in")
458 target.animationComplete( function () {
459 target.removeClass("in").
460 removeClass("ui-datefield-selected");
469 _switchAmPm: function ( obj ) {
470 if ( this._calendar().AM != null ) {
471 var date = new Date( this.options.date ),
473 change = 1000 * 60 * 60 * 12;
474 if ( date.getHours() > 11 ) {
477 date.setTime( date.getTime() + change );
478 this._setDate( date );
482 _parsePattern: function ( pattern ) {
483 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,
487 matches = pattern.match( regex );
489 for ( i = 0; i < matches.length; i++ ) {
490 if ( matches[i].charAt(0) == "'" ) {
491 matches[i] = matches[i].substr( 1, matches[i].length - 2 );
498 changeTypeFormat: function ( type, format ) {
499 console.warn('changeTypeFormat() was deprecated. use datetimepicker("option", "type"|"format", value) instead');
501 this._setType( type );
505 this._setFormat( format );
509 _create: function () {
512 if ( this.element.is( "input" ) ) {
514 var type, value, format;
516 type = obj.element.get(0).getAttribute( "type" );
517 obj.options.type = type;
519 value = obj.element.get(0).getAttribute( "value" );
521 obj.options.date = new Date( value );
526 if ( !this.options.format ) {
527 switch ( this.options.type ) {
529 this.options.format = this._calendar().patterns.d + "\t" + this._calendar().patterns.t;
532 this.options.format = this._calendar().patterns.d;
535 this.options.format = this._calendar().patterns.t;
540 if ( !this.options.date ) {
541 this.options.date = new Date();
545 this.ui = $('<div class="ui-datefield"></div>');
546 $(this.element).after( this.ui );
548 this._popup_open = false;
549 this.ui.bind('vclick', function ( e ) {
550 obj._showDataSelector( obj, this, e.target );
559 orientationchange: $.proxy( this, "_orientationHandler" )
565 $.each( this._globalHandlers, function( idx, value ) {
566 value.src.bind( value.handler );
569 _orientationHandler: function() {
571 if( self._popup_open ) {
572 self._popup_open = false;
573 self.container.popupwindow( 'close' );
577 _populateDataSelector: function ( field, pat ) {
582 range = window.range,
590 if ( pat == 'H' || pat == 'HH' ) {
592 values = range( 0, 23 );
593 data = range( 0, 23 );
594 current = this.options.date.getHours();
596 values = range( 1, 12 );
597 current = this.options.date.getHours() - 1;//11
598 if ( current >= 11 ) {
599 current = current - 12;
600 data = range( 13, 23 );
601 data.push( 12 ); // consider 12:00 am as 00:00
603 data = range( 1, 11 );
607 current = 11; // 12:00 or 00:00
610 if ( pat.length == 2 ) {
612 values = values.map( this._makeTwoDigits );
614 numItems = values.length;
618 values = range( 0, 59 );
619 if ( pat.length == 2 ) {
620 values = values.map( this._makeTwoDigits );
622 data = range( 0, 59 );
623 current = ( field == 'min' ? this.options.date.getMinutes() : this.options.date.getSeconds() );
624 numItems = values.length;
629 data = range( yearlb, yearhb );
630 current = this.options.date.getFullYear() - yearlb;
631 values = range( yearlb, yearhb );
632 numItems = values.length;
635 switch ( pat.length ) {
637 values = range( 1, 12 );
640 values = range( 1, 12 ).map( this._makeTwoDigits );
643 values = this._calendar().months.namesAbbr.slice();
646 values = this._calendar().months.names.slice();
649 if ( values.length == 13 ) { // @TODO Lunar calendar support
650 if ( values[12] == "" ) { // to remove lunar calendar reserved space
654 data = range( 1, values.length );
655 current = this.options.date.getMonth();
656 numItems = values.length;
659 day = this._daysInMonth[ this.options.date.getMonth() ];
661 day += this._isLeapYear( this.options.date.getFullYear() );
663 values = range( 1, day );
664 if ( pat.length == 2 ) {
665 values = values.map( this._makeTwoDigits );
667 data = range( 1, day );
668 current = this.options.date.getDate() - 1;
682 _showDataSelector: function ( obj, ui, target ) {
685 var attr = target.attr("class"),
686 field = attr ? attr.match(/ui-datefield-([\w]*)/) : undefined,
709 if ( this._popup_open ) {
713 target.not('.ui-datefield-seperator').addClass('ui-datefield-selected');
715 pat = target.jqmData('pat');
716 data = obj._populateDataSelector.call( obj, field[1], pat );
718 values = data.values;
719 numItems = data.numItems;
720 current = data.current;
721 valuesData = data.data;
724 datans = "data-" + ($.mobile.ns ? ($.mobile.ns + '-') : "") + 'val="';
725 for ( i = 0; i < values.length; i++ ) {
726 html += '<li><a class="ui-link" ' + datans + valuesData[i] + '">' + values[i] + '</a></li>';
729 $ul = $("<ul></ul>");
730 $div = $('<div class="ui-datetimepicker-selector" data-transition="fade" data-fade="false"></div>');
731 $div.append( $ul ).appendTo( ui );
732 $ctx = $div.ctxpopup();
733 $ctx.parents('.ui-popupwindow').addClass('ui-datetimepicker');
735 $( $li[current] ).addClass("current");
736 $div.jqmData( "list", $li );
739 obj._reflow = function() {
740 $div.circularview( "reflow" );
741 $div.circularview( 'centerTo', '.current', 0 );
743 $(window).bind( "resize" , obj._reflow );
745 // cause ctxpopup forced to subtract 10
746 if ( $( window ).width() / 2 < target.offset().left ) {
749 $ctx.popupwindow( 'open',
750 target.offset().left + ( target.width() / 2 ) + newLeft - window.pageXOffset ,
751 target.offset().top + target.height() - window.pageYOffset );
753 this.container = $ctx;
754 this._popup_open = true;
756 $div.bind('popupafterclose', function ( e ) {
758 $(window).unbind("resize", obj._reflow);
762 if ( !( target.hasClass("in") || target.hasClass("out") ) ) {
763 target.removeClass("ui-datefield-selected");
766 $div.unbind( 'popupafterclose' );
767 $ul.unbind( 'vclick' );
768 $(obj).unbind( 'update' );
769 $ctx.popupwindow( 'destroy' );
772 self._popup_open = false;
775 $(obj).bind( 'update', function ( e, val ) {
776 var date = new Date( this.options.date ),
778 date_calibration = function () {
780 date.setDate( date.getDate() - 1 );
783 switch ( field[1] ) {
785 date.setMinutes( val );
788 date.setHours( val );
791 date.setSeconds( val );
794 month = date.getMonth();
795 date.setFullYear( val );
797 if ( date.getMonth() != month ) {
802 date.setMonth( val - 1 );
804 if ( date.getMonth() == val ) {
813 obj._setDate( date );
815 $ctx.popupwindow( 'close' );
818 $ul.bind( 'click', function ( e ) {
819 if ( $(e.target).is('a') ) {
820 $ul.find(".current").removeClass("current");
821 $(e.target).parent().addClass('current');
822 var val = $(e.target).jqmData("val");
823 $(obj).trigger( 'update', val ); // close popup, unselect field
827 $div.circularview( 'centerTo', '.current', 500 );
828 $div.bind( 'scrollend' , function ( e ) {
829 if ( !obj._reflow ) {
830 obj._reflow = function () {
831 $div.circularview("reflow");
833 $(window).bind("resize", obj._reflow);
842 $(document).bind("pagecreate create", function ( e ) {
843 $($.tizen.datetimepicker.prototype.options.initSelector, e.target)
844 .not(":jqmData(role='none'), :jqmData(role='nojs')")
848 } ( jQuery, this ) );