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 @class DateTimePicker
107 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:
109 <li class="ui-li-dialogue ui-datetime">
110 <div class="ui-datetime-text-main">
111 <input type="datetime" data-format="MMM dd yyyy hh:mm:ss" name="demo-date" id="demo-date" />
113 <div class="ui-li-text-sub">Date/Time Picker
114 <span id="selected-date1"><em>(select a date first)</em></span>
120 ( function ( $, window, undefined ) {
121 $.widget( "tizen.datetimepicker", $.tizen.widgetex, {
124 type: null, // date, time, datetime applicable
127 initSelector: "input[type='date'], input[type='datetime'], input[type='time'], :jqmData(role='datetimepicker')"
130 _calendar: function () {
131 return window.Globalize.culture().calendars.standard;
135 attr: "data-" + ( $.mobile.ns || "" ) + "date",
136 signal: "date-changed"
139 _daysInMonth: [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ],
141 _isLeapYear: function ( year ) {
142 return year % 4 ? 0 : ( year % 100 ? 1 : ( year % 400 ? 0 : 1 ) );
145 _makeTwoDigits: function ( val ) {
146 var ret = val.toString(10);
153 _setType: function ( type ) {
154 //datetime, date, time
159 this.options.type = type;
162 this.options.type = 'datetime';
166 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "type", this.options.type );
167 return this.options.type;
170 _setFormat: function ( format ) {
171 if ( this.options.format != format ) {
172 this.options.format = format;
177 this.ui.children().remove();
179 var token = this._parsePattern( format ),
180 div = document.createElement('div'),
187 while ( token.length > 0 ) {
189 tpl = '<span class="ui-datefield-%1" data-pat="' + pat + '">%2</span>';
191 case 'H': //0 1 2 3 ... 21 22 23
192 case 'HH': //00 01 02 ... 21 22 23
193 case 'h': //0 1 2 3 ... 11 12
194 case 'hh': //00 01 02 ... 11 12
195 $(div).append( tpl.replace('%1', 'hour') );
197 case 'mm': //00 01 ... 59
198 case 'm': //0 1 2 ... 59
199 if ( this.options.type == 'date' ) {
200 $(div).append( tpl.replace('%1', 'month') );
202 $(div).append( tpl.replace('%1', 'min') );
207 $(div).append( tpl.replace('%1', 'sec') );
209 case 'd': // day of month 5
210 case 'dd': // day of month(leading zero) 05
211 $(div).append( tpl.replace('%1', 'day') );
213 case 'M': // Month of year 9
214 case 'MM': // Month of year(leading zero) 09
217 $(div).append( tpl.replace('%1', 'month') );
219 case 'yy': // year two digit
220 case 'yyyy': // year four digit
221 $(div).append( tpl.replace('%1', 'year') );
223 case 't': //AM / PM indicator(first letter) A, P
225 case 'tt': //AM / PM indicator AM/PM
227 btn = '<a href="#" class="ui-datefield-period"' +
228 ' data-role="button" data-inline="true">period</a>';
229 $(div).append( btn );
233 $(div).append( tpl.replace('%1', 'era').replace('%2', this._calendar().eras.name) );
236 $(div).append( tpl.replace('%1', 'tab').replace('%2', pat) );
238 default : // string or any non-clickable object
239 $(div).append( tpl.replace('%1', 'seperator').replace('%2', pat) );
244 this.ui.append( div );
245 if ( this.options.date ) {
246 this._setDate( this.options.date );
249 this.ui.find('.ui-datefield-period').buttonMarkup().bind( 'vclick', function ( e ) {
250 obj._switchAmPm( obj );
253 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "format", this.options.format );
254 return this.options.format;
257 _setDate: function ( newdate ) {
258 if ( typeof ( newdate ) == "string" ) {
259 newdate = new Date( newdate );
262 var fields = $('span,a', this.ui),
269 function getMonth() {
270 return newdate.getMonth() + 1;
273 for ( i = 0; i < fields.length; i++ ) {
274 $field = $(fields[i]);
275 type = $field.attr("class").match(/ui-datefield-([\w]*)/);
281 fn = newdate.getHours;
284 fn = newdate.getMinutes;
287 fn = newdate.getSeconds;
290 fn = newdate.getFullYear;
296 fn = newdate.getDate;
299 fn = newdate.getHours() < 12 ? this._calendar().AM[0] : this._calendar().PM[0];
300 btn = $field.find( '.ui-btn-text' );
301 if ( btn.length == 0 ) {
303 } else if ( btn.text() != fn ) {
313 this._updateField( $field, fn.call( newdate ) );
317 this.options.date = newdate;
319 this._setValue( newdate );
321 this.element.attr( "data-" + ( $.mobile.ns ? $.mobile.ns + "-" : "" ) + "date", this.options.date );
322 return this.options.date;
325 destroy: function () {
330 if ( this.element ) {
335 value: function ( val ) {
336 function timeStr( t, obj ) {
337 return obj._makeTwoDigits( t.getHours() ) + ':' +
338 obj._makeTwoDigits( t.getMinutes() ) + ':' +
339 obj._makeTwoDigits( t.getSeconds() );
342 function dateStr( d, obj ) {
343 return ( ( d.getFullYear() % 10000 ) + 10000 ).toString().substr(1) + '-' +
344 obj._makeTwoDigits( d.getMonth() + 1 ) + '-' +
345 obj._makeTwoDigits( d.getDate() );
350 rvalue = this._setDate( val );
352 switch ( this.options.type ) {
354 rvalue = timeStr( this.options.date, this );
357 rvalue = dateStr( this.options.date, this );
360 rvalue = dateStr( this.options.date, this ) + 'T' + timeStr( this.options.date, this );
367 setValue: function ( newdate ) {
368 console.warn( "setValue was deprecated. use datetimepicker('option', 'date', value) instead." );
369 return this.value( newdate );
373 * return W3C DTF string
375 getValue: function () {
376 console.warn("getValue() was deprecated. use datetimepicker('value') instead.");
380 _updateField: function ( target, value ) {
381 if ( !target || target.length == 0 ) {
389 var pat = target.jqmData( 'pat' ),
398 if ( pat.charAt(0) == 'h' ) {
401 } else if ( hour == 0 ) {
405 hour = this._makeTwoDigits( hour );
418 text = this._makeTwoDigits( value );
421 text = this._calendar().months.namesAbbr[ value - 1];
424 text = this._calendar().months.names[ value - 1 ];
427 text = this._makeTwoDigits( value % 100 );
431 value = '000' + value;
432 } else if ( value < 100 ) {
433 value = '00' + value;
434 } else if ( value < 1000 ) {
440 // to avoid reflow where its value isn't out-dated
441 if ( target.text() != text ) {
446 _switchAmPm: function ( obj ) {
447 if ( this._calendar().AM != null ) {
448 var date = new Date( this.options.date ),
450 change = 1000 * 60 * 60 * 12;
451 if ( date.getHours() > 11 ) {
454 date.setTime( date.getTime() + change );
455 this._setDate( date );
459 _parsePattern: function ( pattern ) {
460 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,
464 matches = pattern.match( regex );
466 for ( i = 0; i < matches.length; i++ ) {
467 if ( matches[i].charAt(0) == "'" ) {
468 matches[i] = matches[i].substr( 1, matches[i].length - 2 );
475 changeTypeFormat: function ( type, format ) {
476 console.warn('changeTypeFormat() was deprecated. use datetimepicker("option", "type"|"format", value) instead');
478 this._setType( type );
482 this._setFormat( format );
486 _create: function () {
489 if ( this.element.is( "input" ) ) {
491 var type, value, format;
493 type = obj.element.attr( "type" );
494 obj.options.type = type;
496 value = obj.element.attr( "value" );
498 obj.options.date = new Date( value );
503 if ( !this.options.format ) {
504 switch ( this.options.type ) {
506 this.options.format = this._calendar().patterns.d + "\t" + this._calendar().patterns.t;
509 this.options.format = this._calendar().patterns.d;
512 this.options.format = this._calendar().patterns.t;
517 if ( !this.options.date ) {
518 this.options.date = new Date();
522 this.ui = $('<div class="ui-datefield"></div>');
523 $(this.element).after( this.ui );
525 this.ui.bind('vclick', function ( e ) {
526 obj._showDataSelector( obj, this, e.target );
530 _populateDataSelector: function ( field, pat ) {
535 range = window.range,
543 if ( pat == 'H' || pat == 'HH' ) {
545 values = range( 0, 23 );
546 data = range( 0, 23 );
547 current = this.options.date.getHours();
549 values = range( 1, 12 );
550 current = this.options.date.getHours() - 1;//11
551 if ( current >= 11 ) {
552 current = current - 12;
553 data = range( 13, 23 );
554 data.push( 12 ); // consider 12:00 am as 00:00
556 data = range( 1, 11 );
560 current = 11; // 12:00 or 00:00
563 if ( pat.length == 2 ) {
565 values = values.map( this._makeTwoDigits );
567 numItems = values.length;
571 values = range( 0, 59 );
572 if ( pat.length == 2 ) {
573 values = values.map( this._makeTwoDigits );
575 data = range( 0, 59 );
576 current = ( field == 'min' ? this.options.date.getMinutes() : this.options.date.getSeconds() );
577 numItems = values.length;
582 data = range( yearlb, yearhb );
583 current = this.options.date.getFullYear() - yearlb;
584 values = range( yearlb, yearhb );
585 numItems = values.length;
588 switch ( pat.length ) {
590 values = range( 1, 12 );
593 values = range( 1, 12 ).map( this._makeTwoDigits );
596 values = this._calendar().months.namesAbbr.slice();
599 values = this._calendar().months.names.slice();
602 if ( values.length == 13 ) { // @TODO Lunar calendar support
603 if ( values[12] == "" ) { // to remove lunar calendar reserved space
607 data = range( 1, values.length );
608 current = this.options.date.getMonth();
609 numItems = values.length;
612 day = this._daysInMonth[ this.options.date.getMonth() ];
614 day += this._isLeapYear( this.options.date.getFullYear() );
616 values = range( 1, day );
617 if ( pat.length == 2 ) {
618 values = values.map( this._makeTwoDigits );
620 data = range( 1, day );
621 current = this.options.date.getDate() - 1;
635 _showDataSelector: function ( obj, ui, target ) {
638 var attr = target.attr("class"),
639 field = attr ? attr.match(/ui-datefield-([\w]*)/) : undefined,
662 target.not('.ui-datefield-seperator').addClass('ui-datefield-selected');
664 pat = target.jqmData('pat');
665 data = obj._populateDataSelector.call( obj, field[1], pat );
667 values = data.values;
668 numItems = data.numItems;
669 current = data.current;
670 valuesData = data.data;
673 datans = "data-" + ($.mobile.ns ? ($.mobile.ns + '-') : "") + 'val="';
674 for ( i = 0; i < values.length; i++ ) {
675 html += '<li><a class="ui-link" ' + datans + valuesData[i] + '">' + values[i] + '</a></li>';
678 $ul = $("<ul></ul>");
679 $div = $('<div class="ui-datetimepicker-selector" data-transition="none" data-fade="false"></div>');
680 $div.append( $ul ).appendTo( ui );
681 $ctx = $div.ctxpopup();
682 $ctx.parents('.ui-popupwindow').addClass('ui-datetimepicker');
684 $( $li[current] ).addClass("current");
685 $div.jqmData( "list", $li );
687 // cause ctxpopup forced to subtract 10
688 if ( $( window ).width() / 2 < target.offset().left ) {
691 $ctx.popupwindow( 'open',
692 target.offset().left + ( target.width() / 2 ) + newLeft - window.pageXOffset ,
693 target.offset().top + target.height() - window.pageYOffset );
694 $div.bind('popupafterclose', function ( e ) {
696 $(window).unbind("resize", obj._reflow);
699 $div.unbind( 'popupafterclose' );
700 $ul.unbind( 'vclick' );
701 $(obj).unbind( 'update' );
702 $(ui).find('.ui-datefield-selected').removeClass('ui-datefield-selected');
703 $ctx.popupwindow( 'destroy' );
707 $(obj).bind( 'update', function ( e, val ) {
708 $ctx.popupwindow( 'close' );
709 var date = new Date( this.options.date );
710 switch ( field[1] ) {
712 date.setMinutes( val );
715 date.setHours( val );
718 date.setSeconds( val );
721 date.setFullYear( val );
724 date.setMonth( val - 1 );
726 if ( date.getMonth() == val ) {
728 date.setDate( date.getDate() - 1 );
735 obj._setDate( date );
738 $ul.bind( 'click', function ( e ) {
739 if ( $(e.target).is('a') ) {
740 $ul.find(".current").removeClass("current");
741 $(e.target).parent().addClass('current');
742 var val = $(e.target).jqmData("val");
743 $(obj).trigger( 'update', val ); // close popup, unselect field
747 $div.circularview( 'centerTo', '.current', 200 );
748 $div.bind( 'scrollend' , function ( e ) {
749 if ( !obj._reflow ) {
750 obj._reflow = function () {
751 $div.circularview("reflow");
753 $(window).bind("resize", obj._reflow);
762 $(document).bind("pagecreate create", function ( e ) {
763 $($.tizen.datetimepicker.prototype.options.initSelector, e.target)
764 .not(":jqmData(role='none'), :jqmData(role='nojs')")
768 } ( jQuery, this ) );