Committer: dpetrov
LJSUP-9182: journal calendar links mast lead to the next/prev months/yearsU trunk/htdocs/js/jquery/jquery.calendarEvents.js U trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js
Modified: trunk/htdocs/js/jquery/jquery.calendarEvents.js =================================================================== --- trunk/htdocs/js/jquery/jquery.calendarEvents.js 2011-06-29 07:12:08 UTC (rev 10709) +++ trunk/htdocs/js/jquery/jquery.calendarEvents.js 2011-06-29 07:33:38 UTC (rev 10710) @@ -1,12 +1,20 @@ ( function( $ ) { + //we think that all such calendars on the page contain the same information + var ajaxCache = {}; + $.fn.calendarEvents = function( o ) { var defaults = { calendarType: 'calendar', classNames: { idle: 'idle' - } + }, + fetchOnFirstDisplay: false }; + function getCacheKey( year, month ) { + return "cache_" + year + month; + } + function Events( calendarWidget, o ) { var widget = this; @@ -15,38 +23,29 @@ this.options = o; this._ajaxActive = false; + this._currentDate; + var ajaxTimer, + loadData = function( date, isMonthSwitch ) { + clearTimeout( ajaxTimer ); + widget._currentDate = getCacheKey( date.getFullYear(), date.getMonth() + 1 ); + + ajaxTimer = setTimeout( function() { + widget.fetchEvents( date.getFullYear(), date.getMonth() + 1, isMonthSwitch ); + }, 200 ); + }; this.calendar.bind( 'dateChange', function( ev ) { var curDate = widget.calendar[ widget.options.calendarType ]( 'option', 'displayedMonth' ), isMonthSwitch = Math.abs( 12 * ( ev.date.getFullYear() - curDate.getFullYear() ) + ( ev.date.getMonth() - curDate.getMonth() ) ) === 1; - - //if request has not been procceeded yet, we force calendar to stay on current date - if( widget._ajaxActive ) { - ev.date = curDate; - return; - } - - var type = ( ev.moveForward ) ? "next_" : "prev_"; - type += ( ev.switchType === "year" ) ? "year" : "month"; - - if( widget.events[ type ] ) { - ev.date.setFullYear( widget.events[ type ][ 0 ] ); - ev.date.setMonth( widget.events[ type ][ 1 ] -1 ); - - widget.fetchEvents( widget.events[ type ][ 0 ], widget.events[ type ][ 1 ], isMonthSwitch ); - } + loadData( ev.date, isMonthSwitch ); } ); - if( Site.journal_calendar ) { - this.events = Site.journal_calendar; - this.fixBounds(); - this.calendar[ this.options.calendarType ]( 'option', 'events', - this.getEvents( this.events.year, this.events.month, this.events.days ) ); - } else { - var d = new Date(); - - this.fetchEvents( d.getFullYear(), d.getMonth() ); + if( this.options.fetchOnFirstDisplay ) { + this.calendar.one( 'calendarshow', function( ev ) { + loadData( new Date, true ); + } ); } + } Events.prototype = { @@ -88,28 +87,27 @@ checkState(); }, 500 ); + var cacheKey = getCacheKey( year, month ); + processResults = function( answer ) { + if( cacheKey !== widget._currentDate ) { return; } - $.getJSON( LiveJournal.getAjaxUrl( 'calendar' ), { year: year, month: month }, - function( answer ) { - widget.events = answer; - widget.fixBounds(); - widget.calendar[ widget.options.calendarType ]( 'option', 'events', - widget.getEvents( widget.events.year, widget.events.month, widget.events.days ) ); - widget._ajaxActive = false; + widget.events = ajaxCache[ cacheKey ]; + // widget.fixBounds(); + widget.calendar[ widget.options.calendarType ]( 'option', 'events', + widget.getEvents( widget.events.year, widget.events.month, widget.events.days ) ); + widget._ajaxActive = false; - checkState(); - } - ); - }, + checkState(); + }; - fixBounds: function() { - var curMonth = this.calendar[ this.options.calendarType ]( 'option', 'displayedMonth' ); - var next = this.events.next_year || this.events.next_month; - var prev = this.events.prev_year || this.events.prev_month; - this.calendar[ this.options.calendarType ]( 'option', 'endMonth', ( next ) ? - new Date( next[0], next[1] - 1, 1 ) : curMonth ); - this.calendar[ this.options.calendarType ]( 'option', 'startMonth', ( prev ) ? - new Date( prev[0], prev[1] - 1, 1 ) : curMonth ); + if( cacheKey in ajaxCache ) { + processResults( ajaxCache[ cacheKey ] ); + } else { + $.getJSON( LiveJournal.getAjaxUrl( 'calendar' ), { year: year, month: month }, function( answer ) { + ajaxCache[ cacheKey ] = answer; + processResults( answer ); + } ); + } } } Modified: trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js =================================================================== --- trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js 2011-06-29 07:12:08 UTC (rev 10709) +++ trunk/htdocs/js/jquery/jquery.lj.inlineCalendar.js 2011-06-29 07:33:38 UTC (rev 10710) @@ -356,8 +356,6 @@ this._view = new (this._getView())( this._nodes, this.options.classNames, this.options ); this._view.initialize(); - this._invalidateDisplay(); - this._bindEvents(); },