can3p (can3p) wrote in changelog,
can3p
can3p
changelog

[ljcom] r10697: LJSUP-9138: All links are active in cale...

Committer: dpetrov
LJSUP-9138: All links are active in calendar in controlstrip on the user's home page
A   trunk/htdocs/js/jquery/jquery.calendarEvents.js
Added: trunk/htdocs/js/jquery/jquery.calendarEvents.js
===================================================================
--- trunk/htdocs/js/jquery/jquery.calendarEvents.js	                        (rev 0)
+++ trunk/htdocs/js/jquery/jquery.calendarEvents.js	2011-06-24 07:13:15 UTC (rev 10697)
@@ -0,0 +1,118 @@
+( function( $ ) {
+	$.fn.calendarEvents = function( o ) {
+		var defaults = {
+			calendarType: 'calendar',
+			classNames: {
+				idle: 'idle'
+			}
+		};
+
+		function Events( calendarWidget, o ) {
+			var widget = this;
+
+			this.calendar = calendarWidget;
+			this.events = null;
+			this.options = o;
+
+			this._ajaxActive = false;
+
+			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 );
+				}
+			} );
+
+			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() );
+			}
+		}
+
+		Events.prototype = {
+			getEvents: function( year, month, days ) {
+				var result = {};
+				result[ +year ] = {};
+				result[ +year ][ +month -1 ] = days;
+
+				return result;
+			},
+
+			/**
+			 * Get events from the server end point and update the calendar.
+			 *
+			 * @param {Number} year
+			 * @param {Number} month
+			 * @param {Boolean=true} isMonthSwitch Calendar has year and month preloader and it's a flag which preloader to show
+			 */
+			fetchEvents: function( year, month, isMonthSwitch ) {
+				isMonthSwitch = ( arguments.length >= 3 ) ? !!isMonthSwitch : true;
+				var widget = this,
+					curMonth = this.calendar[ this.options.calendarType ]( 'option', 'displayedMonth' ),
+					loaderSpan = this.calendar[ this.options.calendarType ]( 'getElement', ( isMonthSwitch ) ? 'month' : 'year' ),
+					idleClass = this.options.classNames.idle,
+					timerActive = true,
+					checkState = function() {
+						if( !widget._ajaxActive && !timerActive ) {
+							loaderSpan.removeClass( idleClass );
+						}
+					}
+
+				this.calendar[ this.options.calendarType ]( 'getElement', 'year' ).removeClass( idleClass );
+				this.calendar[ this.options.calendarType ]( 'getElement', 'month' ).removeClass( idleClass );
+				loaderSpan.addClass( idleClass );
+				this._ajaxActive = true;
+
+				setTimeout( function() {
+					timerActive = false;
+					checkState();
+				}, 500 );
+
+
+				$.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;
+
+							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 );
+			}
+		}
+
+		return this.each( function() { new Events( $( this ), $.extend( {}, defaults, o ) ) } );
+	}
+} ) ( jQuery );

Tags: can3p, js, ljcom
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments