PageSourceSearch

http://aend.org/Calendario/js/jquery.calendario.js

js aend.org collected 2026-10-02 19:11:00 UTC 9,893 bytes, 388 lines download raw bytes

1/**
2 * jquery.calendario.js v1.0.0
3 * http://www.codrops.com
4 *
5 * Licensed under the MIT license.
6 * http://www.opensource.org/licenses/mit-license.php
7 * 
8 * Copyright 2012, Codrops
9 * http://www.codrops.com
10 */
11;( function( $, window, undefined ) {
12	
13	'use strict';
14
15	$.Calendario = function( options, element ) {
16		
17		this.$el = $( element );
18		this._init( options );
19		
20	};
21
22	// the options
23	$.Calendario.defaults = {
24		/*
25		you can also pass:
26		month : initialize calendar with this month (1-12). Default is today.
27		year : initialize calendar with this year. Default is today.
28		caldata : initial data/content for the calendar.
29		caldata format:
30		{
31			'MM-DD-YYYY' : 'HTML Content',
32			'MM-DD-YYYY' : 'HTML Content',
33			'MM-DD-YYYY' : 'HTML Content'
34			...
35		}
36		*/
37		weeks : [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
38		weekabbrs : [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
39		months : [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ],
40		monthabbrs : [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
41		// choose between values in options.weeks or options.weekabbrs
42		displayWeekAbbr : false,
43		// choose between values in options.months or options.monthabbrs
44		displayMonthAbbr : false,
45		// left most day in the calendar
46		// 0 - Sunday, 1 - Monday, ... , 6 - Saturday
47		startIn : 1,
48		onDayClick : function( $el, $content, dateProperties ) { return false; }
49	};
50
51	$.Calendario.prototype = {
52
53		_init : function( options ) {
54			
55			// options
56			this.options = $.extend( true, {}, $.Calendario.defaults, options );
57
58			this.today = new Date();
59			this.month = ( isNaN( this.options.month ) || this.options.month == null) ? this.today.getMonth() : this.options.month - 1;
60			this.year = ( isNaN( this.options.year ) || this.options.year == null) ? this.today.getFullYear() : this.options.year;
61			this.caldata = this.options.caldata || {};
62			this._generateTemplate();
63			this._initEvents();
64
65		},
66		_initEvents : function() {
67
68			var self = this;
69
70			this.$el.on( 'click.calendario', 'div.fc-row > div', function() {
71
72				var $cell = $( this ),
73					idx = $cell.index(),
74					$content = $cell.children( 'div' ),
75					dateProp = {
76						day : $cell.children( 'span.fc-date' ).text(),
77						month : self.month + 1,
78						monthname : self.options.displayMonthAbbr ? self.options.monthabbrs[ self.month ] : self.options.months[ self.month ],
79						year : self.year,
80						weekday : idx + self.options.startIn,
81						weekdayname : self.options.weeks[ idx + self.options.startIn ]
82					};
83
84				if( dateProp.day ) {
85					self.options.onDayClick( $cell, $content, dateProp );
86				}
87
88			} );
89
90		},
91		// Calendar logic based on http://jszen.blogspot.pt/2007/03/how-to-build-simple-calendar-with.html
92		_generateTemplate : function( callback ) {
93
94			var head = this._getHead(),
95				body = this._getBody(),
96				rowClass;
97
98			switch( this.rowTotal ) {
99				case 4 : rowClass = 'fc-four-rows'; break;
100				case 5 : rowClass = 'fc-five-rows'; break;
101				case 6 : rowClass = 'fc-six-rows'; break;
102			}
103
104			this.$cal = $( '<div class="fc-calendar ' + rowClass + '">' ).append( head, body );
105
106			this.$el.find( 'div.fc-calendar' ).remove().end().append( this.$cal );
107
108			if( callback ) { callback.call(); }
109
110		},
111		_getHead : function() {
112
113			var html = '<div class="fc-head">';
114		
115			for ( var i = 0; i <= 6; i++ ) {
116
117				var pos = i + this.options.startIn,
118					j = pos > 6 ? pos - 6 - 1 : pos;
119
120				html += '<div>';
121				html += this.options.displayWeekAbbr ? this.options.weekabbrs[ j ] : this.options.weeks[ j ];
122				html += '</div>';
123
124			}
125
126			html += '</div>';
127
128			return html;
129
130		},
131		_getBody : function() {
132
133			var d = new Date( this.year, this.month + 1, 0 ),
134				// number of days in the month
135				monthLength = d.getDate(),
136				firstDay = new Date( this.year, this.month, 1 );
137
138			// day of the week
139			this.startingDay = firstDay.getDay();
140
141			var html = '<div class="fc-body"><div class="fc-row">',
142				// fill in the days
143				day = 1;
144
145			// this loop is for weeks (rows)
146			for ( var i = 0; i < 7; i++ ) {
147
148				// this loop is for weekdays (cells)
149				for ( var j = 0; j <= 6; j++ ) {
150
151					var pos = this.startingDay - this.options.startIn,
152						p = pos < 0 ? 6 + pos + 1 : pos,
153						inner = '',
154						today = this.month === this.today.getMonth() && this.year === this.today.getFullYear() && day === this.today.getDate(),
155						content = '';
156					
157					if ( day <= monthLength && ( i > 0 || j >= p ) ) {
158
159						inner += '<span class="fc-date">' + day + '</span><span class="fc-weekday">' + this.options.weekabbrs[ j + this.options.startIn > 6 ? j + this.options.startIn - 6 - 1 : j + this.options.startIn ] + '</span>';
160
161						// this day is:
162						var strdate = ( this.month + 1 < 10 ? '0' + ( this.month + 1 ) : this.month + 1 ) + '-' + ( day < 10 ? '0' + day : day ) + '-' + this.year,
163							dayData = this.caldata[ strdate ];
164
165						if( dayData ) {
166							content = dayData;
167						}
168
169						if( content !== '' ) {
170							inner += '<div>' + content + '</div>';
171						}
172
173						++day;
174
175					}
176					else {
177						today = false;
178					}
179
180					var cellClasses = today ? 'fc-today ' : '';
181					if( content !== '' ) {
182						cellClasses += 'fc-content';
183					}
184
185					html += cellClasses !== '' ? '<div class="' + cellClasses + '">' : '<div>';
186					html += inner;
187					html += '</div>';
188
189				}
190
191				// stop making rows if we've run out of days
192				if (day > monthLength) {
193					this.rowTotal = i + 1;
194					break;
195				} 
196				else {
197					html += '</div><div class="fc-row">';
198				}
199
200			}
201			html += '</div></div>';
202
203			return html;
204
205		},
206		// based on http://stackoverflow.com/a/8390325/989439
207		_isValidDate : function( date ) {
208
209			date = date.replace(/-/gi,'');
210			var month = parseInt( date.substring( 0, 2 ), 10 ),
211				day = parseInt( date.substring( 2, 4 ), 10 ),
212				year = parseInt( date.substring( 4, 8 ), 10 );
213
214			if( ( month < 1 ) || ( month > 12 ) ) {
215				return false;
216			}
217			else if( ( day < 1 ) || ( day > 31 ) )  {
218				return false;
219			}
220			else if( ( ( month == 4 ) || ( month == 6 ) || ( month == 9 ) || ( month == 11 ) ) && ( day > 30 ) )  {
221				return false;
222			}
223			else if( ( month == 2 ) && ( ( ( year % 400 ) == 0) || ( ( year % 4 ) == 0 ) ) && ( ( year % 100 ) != 0 ) && ( day > 29 ) )  {
224				return false;
225			}
226			else if( ( month == 2 ) && ( ( year % 100 ) == 0 ) && ( day > 29 ) )  {
227				return false;
228			}
229
230			return {
231				day : day,
232				month : month,
233				year : year
234			};
235
236		},
237		_move : function( period, dir, callback ) {
238
239			if( dir === 'previous' ) {
240				
241				if( period === 'month' ) {
242					this.year = this.month > 0 ? this.year : --this.year;
243					this.month = this.month > 0 ? --this.month : 11;
244				}
245				else if( period === 'year' ) {
246					this.year = --this.year;
247				}
248
249			}
250			else if( dir === 'next' ) {
251
252				if( period === 'month' ) {
253					this.year = this.month < 11 ? this.year : ++this.year;
254					this.month = this.month < 11 ? ++this.month : 0;
255				}
256				else if( period === 'year' ) {
257					this.year = ++this.year;
258				}
259
260			}
261
262			this._generateTemplate( callback );
263
264		},
265		/************************* 
266		******PUBLIC METHODS *****
267		**************************/
268		getYear : function() {
269			return this.year;
270		},
271		getMonth : function() {
272			return this.month + 1;
273		},
274		getMonthName : function() {
275			return this.options.displayMonthAbbr ? this.options.monthabbrs[ this.month ] : this.options.months[ this.month ];
276		},
277		// gets the cell's content div associated to a day of the current displayed month
278		// day : 1 - [28||29||30||31]
279		getCell : function( day ) {
280
281			var row = Math.floor( ( day + this.startingDay - this.options.startIn ) / 7 ),
282				pos = day + this.startingDay - this.options.startIn - ( row * 7 ) - 1;
283
284			return this.$cal.find( 'div.fc-body' ).children( 'div.fc-row' ).eq( row ).children( 'div' ).eq( pos ).children( 'div' );
285
286		},
287		setData : function( caldata ) {
288
289			caldata = caldata || {};
290			$.extend( this.caldata, caldata );
291			this._generateTemplate();
292
293		},
294		// goes to today's month/year
295		gotoNow : function( callback ) {
296
297			this.month = this.today.getMonth();
298			this.year = this.today.getFullYear();
299			this._generateTemplate( callback );
300
301		},
302		// goes to month/year
303		goto : function( month, year, callback ) {
304
305			this.month = month;
306			this.year = year;
307			this._generateTemplate( callback );
308
309		},
310		gotoPreviousMonth : function( callback ) {
311			this._move( 'month', 'previous', callback );
312		},
313		gotoPreviousYear : function( callback ) {
314			this._move( 'year', 'previous', callback );
315		},
316		gotoNextMonth : function( callback ) {
317			this._move( 'month', 'next', callback );
318		},
319		gotoNextYear : function( callback ) {
320			this._move( 'year', 'next', callback );
321		}
322
323	};
324	
325	var logError = function( message ) {
326
327		if ( window.console ) {
328
329			window.console.error( message );
330		
331		}
332
333	};
334	
335	$.fn.calendario = function( options ) {
336
337		var instance = $.data( this, 'calendario' );
338		
339		if ( typeof options === 'string' ) {
340			
341			var args = Array.prototype.slice.call( arguments, 1 );
342			
343			this.each(function() {
344			
345				if ( !instance ) {
346
347					logError( "cannot call methods on calendario prior to initialization; " +
348					"attempted to call method '" + options + "'" );
349					return;
350				
351				}
352				
353				if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
354
355					logError( "no such method '" + options + "' for calendario instance" );
356					return;
357				
358				}
359				
360				instance[ options ].apply( instance, args );
361			
362			});
363		
364		} 
365		else {
366		
367			this.each(function() {
368				
369				if ( instance ) {
370
371					instance._init();
372				
373				}
374				else {
375
376					instance = $.data( this, 'calendario', new $.Calendario( options, this ) );
377				
378				}
379
380			});
381		
382		}
383		
384		return instance;
385		
386	};
387	
388} )( jQuery, window );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.