Committer: dpetrov
LJSUP-12126: Port choose date widget from the old update.bml pageU trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js
Modified: trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js =================================================================== --- trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js 2012-05-22 13:04:55 UTC (rev 11985) +++ trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js 2012-05-22 13:51:45 UTC (rev 11986) @@ -78,6 +78,8 @@ month: '.cal-nav-month', year: '.cal-nav-year', + monthSelect: '.cal-nav-month-select', + yearSelect: '.cal-nav-year-select', prevMonth: '.cal-nav-month .cal-nav-prev', nextMonth: '.cal-nav-month .cal-nav-next', @@ -93,6 +95,7 @@ inactive : 'other', future : 'other', current : 'current', + weekend: 'weekend', nextDisabled : 'cal-nav-next-dis', prevDisabled : 'cal-nav-prev-dis', cellHover : 'hover' @@ -102,6 +105,7 @@ monthNamesLong: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], monthNamesShort: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], + dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], caption: "Calendar" } }; @@ -142,11 +146,11 @@ } var monthText = o.monthRef - ? $( '<a>', { href: Calendar._formatDate( monthDate, o.monthRef ), text: o.ml.monthNamesShort[ monthDate.getMonth() ] } ) + ? $( '<a>', { href: LJ.Util.Date.format( monthDate, o.monthRef ), text: o.ml.monthNamesShort[ monthDate.getMonth() ] } ) : o.ml.monthNamesShort[ monthDate.getMonth() ]; var yearText = o.yearRef - ? $( '<a>', { href: Calendar._formatDate( monthDate, o.yearRef ), text: monthDate.getFullYear() } ) + ? $( '<a>', { href: LJ.Util.Date.format( monthDate, o.yearRef ), text: monthDate.getFullYear() } ) : monthDate.getFullYear(); nodes.monthLabel.empty().append( monthText ); @@ -272,7 +276,7 @@ cell.removeClass( styles.inactive ); span.html( jQuery( '<a />', { html: d.getDate(), - href: Calendar._formatDate( d, o.dayRef ) + href: LJ.Util.Date.format( d, o.dayRef ) } ) ); } else { cell.removeClass( styles.inactive ); @@ -326,6 +330,9 @@ this._setOption( "startAtSunday", false ); } + this._nodes.monthSelect.val(this.options.displayedMonth.getMonth()); + this._nodes.yearSelect.val(this.options.displayedMonth.getFullYear()); + this._bindEvents(); }, @@ -343,8 +350,8 @@ } } - var displayedMonth; - if( displayedMonth = this._parseDate( this._nodes.table.attr( "data-date" ) ) ) { + var displayedMonth = LJ.Util.Date.parse(this._nodes.table.attr( "data-date"), this.options.dateFormat) + if(displayedMonth) { this.options.displayedMonth = displayedMonth; } }, @@ -370,9 +377,21 @@ }; for (var sws in switcherStates) { - this._nodes[sws].mousedown( switcherMouseDown(sws) ); + this._nodes[sws].click( switcherMouseDown(sws) ); } + this._nodes.monthSelect.change(function(ev) { + var d = new Date(self.options.currentDate); + d.setMonth(this.value); + self._setOption('currentDate', d); + }); + + this._nodes.yearSelect.change(function(ev) { + var d = new Date(self.options.currentDate); + d.setFullYear(this.value); + self._setOption('currentDate', d); + }); + this._nodes.tbody .delegate( 'td', 'click', function( ev ) { self._cellSelectedEvent( $( this ), ev ); @@ -409,7 +428,7 @@ */ _cellSelected: function( date ) { var event = jQuery.Event( "daySelected" ); - this._nodes.root.trigger( event, [ date, this._formatDate(date, this.options.dateFormat) ] ); + this._nodes.root.trigger( event, [ date, LJ.Util.Date.format(date, this.options.dateFormat) ] ); if( !event.isDefaultPrevented() ) { this._setOption( 'currentDate', date ); @@ -472,7 +491,7 @@ var event = jQuery.Event("currentDateChange"), date = new Date(this.options.currentDate); - this._nodes.root.trigger( event, [ date, this._formatDate(date, this.options.dateFormat) ] ); + this._nodes.root.trigger( event, [ date, LJ.Util.Date.format(date, this.options.dateFormat) ] ); this._setOption( 'displayedMonth', value ); break; @@ -499,6 +518,8 @@ if( !isCurrentMonth ) { this.options.displayedMonth = this._fitDate( new Date( value ) ); + this._nodes.monthSelect.val(this.options.displayedMonth.getMonth()); + this._nodes.yearSelect.val(this.options.displayedMonth.getFullYear()); this._invalidateDisplay(); } break; @@ -522,93 +543,6 @@ } else { return null; } - }, - - /** - * Serialize date to string according the format string. - * We suppose that every token takes place in the string only once. - * - * @param string Date Date object. - * @param string format Date format. - * @return Date|null Returns new Date object or null on parse failure. - */ - _formatDate: function( d, format ) { - format = format || "%Y-%M-%D"; - var str = format; - - var subs = { - '%Y' : d.getFullYear(), - '%M' : ( "0" + ( d.getMonth() + 1 ) ).slice( -2 ), - '%D' : ( "0" + d.getDate() ).slice( -2 ), - '%S' : +d - }; - - for( var k in subs ) { - if( !subs.hasOwnProperty(k) ) { - continue; - } - - str = str.replace( k, subs[k] ); - } - - return str; - }, - - /** - * Parse date from string according following format. - * We suppose that every token takes place in the string only once. - * - * @param string str Date string. - * @param string format Date format. - * - * @return Date|null Returns new Date object or null on parse failure. - */ - _parseDate: function( str, format ) { - var testStr = format || "%Y-%M-%D", - positions = [ null ], - pos = 0, token, - regs = { - '%Y' : '(\\d{4})', - '%M' : '(\\d{2})', - '%D' : '(\\d{2})', - '%S' : '(\\d{13})' - }; - - while( ( pos = testStr.indexOf( '%', pos ) ) !== -1 ) { - token = testStr.substr( pos, 2 ); - if( token in regs ) { - testStr = testStr.replace( token, regs[ token ] ); - positions.push( token ); - } else { - positions.push( null ); - } - } - - var r = new RegExp( testStr ), - arr = r.exec( str ); - - if( !arr ) { - return null; - } else { - var d = new Date(); - for( var i = 1; i < arr.length; ++i ) { - if( positions[ i ] ) { - switch( positions[ i ] ) { - case '%D': - d.setDate( arr[ i ] ); - break; - case '%M': - d.setMonth( parseInt( arr[ i ], 10 ) - 1 ); - break; - case '%Y': - d.setFullYear( arr[ i ] ); - break; - } - } - } - - return d; - } } };