jquery.datePicker.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. /*
  2. * Date prototype extensions. Doesn't depend on any
  3. * other code. Doens't overwrite existing methods.
  4. * Download by http://www.codefans.net
  5. * Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
  6. * isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
  7. * setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
  8. *
  9. * Copyright (c) 2006 J枚rn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
  10. *
  11. * Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
  12. * I've added my name to these methods so you know who to blame if they are broken!
  13. *
  14. * Dual licensed under the MIT and GPL licenses:
  15. * http://www.opensource.org/licenses/mit-license.php
  16. * http://www.gnu.org/licenses/gpl.html
  17. *
  18. */
  19. /**
  20. * An Array of day names starting with Sunday.
  21. *
  22. * @example dayNames[0]
  23. * @result 'Sunday'
  24. *
  25. * @name dayNames
  26. * @type Array
  27. * @cat Plugins/Methods/Date
  28. */
  29. Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  30. /**
  31. * An Array of abbreviated day names starting with Sun.
  32. *
  33. * @example abbrDayNames[0]
  34. * @result 'Sun'
  35. *
  36. * @name abbrDayNames
  37. * @type Array
  38. * @cat Plugins/Methods/Date
  39. */
  40. Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  41. /**
  42. * An Array of month names starting with Janurary.
  43. *
  44. * @example monthNames[0]
  45. * @result 'January'
  46. *
  47. * @name monthNames
  48. * @type Array
  49. * @cat Plugins/Methods/Date
  50. */
  51. Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  52. /**
  53. * An Array of abbreviated month names starting with Jan.
  54. *
  55. * @example abbrMonthNames[0]
  56. * @result 'Jan'
  57. *
  58. * @name monthNames
  59. * @type Array
  60. * @cat Plugins/Methods/Date
  61. */
  62. Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  63. /**
  64. * The first day of the week for this locale.
  65. *
  66. * @name firstDayOfWeek
  67. * @type Number
  68. * @cat Plugins/Methods/Date
  69. * @author Kelvin Luck
  70. */
  71. Date.firstDayOfWeek = 1;
  72. /**
  73. * The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
  74. *
  75. * @name format
  76. * @type String
  77. * @cat Plugins/Methods/Date
  78. * @author Kelvin Luck
  79. */
  80. Date.format = 'dd/mm/yyyy';
  81. //Date.format = 'mm/dd/yyyy';
  82. //Date.format = 'yyyy-mm-dd';
  83. //Date.format = 'dd mmm yy';
  84. /**
  85. * The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
  86. * only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
  87. *
  88. * @name format
  89. * @type String
  90. * @cat Plugins/Methods/Date
  91. * @author Kelvin Luck
  92. */
  93. Date.fullYearStart = '20';
  94. (function() {
  95. /**
  96. * Adds a given method under the given name
  97. * to the Date prototype if it doesn't
  98. * currently exist.
  99. *
  100. * @private
  101. */
  102. function add(name, method) {
  103. if( !Date.prototype[name] ) {
  104. Date.prototype[name] = method;
  105. }
  106. };
  107. /**
  108. * Checks if the year is a leap year.
  109. *
  110. * @example var dtm = new Date("01/12/2008");
  111. * dtm.isLeapYear();
  112. * @result true
  113. *
  114. * @name isLeapYear
  115. * @type Boolean
  116. * @cat Plugins/Methods/Date
  117. */
  118. add("isLeapYear", function() {
  119. var y = this.getFullYear();
  120. return (y%4==0 && y%100!=0) || y%400==0;
  121. });
  122. /**
  123. * Checks if the day is a weekend day (Sat or Sun).
  124. *
  125. * @example var dtm = new Date("01/12/2008");
  126. * dtm.isWeekend();
  127. * @result false
  128. *
  129. * @name isWeekend
  130. * @type Boolean
  131. * @cat Plugins/Methods/Date
  132. */
  133. add("isWeekend", function() {
  134. return this.getDay()==0 || this.getDay()==6;
  135. });
  136. /**
  137. * Check if the day is a day of the week (Mon-Fri)
  138. *
  139. * @example var dtm = new Date("01/12/2008");
  140. * dtm.isWeekDay();
  141. * @result false
  142. *
  143. * @name isWeekDay
  144. * @type Boolean
  145. * @cat Plugins/Methods/Date
  146. */
  147. add("isWeekDay", function() {
  148. return !this.isWeekend();
  149. });
  150. /**
  151. * Gets the number of days in the month.
  152. *
  153. * @example var dtm = new Date("01/12/2008");
  154. * dtm.getDaysInMonth();
  155. * @result 31
  156. *
  157. * @name getDaysInMonth
  158. * @type Number
  159. * @cat Plugins/Methods/Date
  160. */
  161. add("getDaysInMonth", function() {
  162. return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
  163. });
  164. /**
  165. * Gets the name of the day.
  166. *
  167. * @example var dtm = new Date("01/12/2008");
  168. * dtm.getDayName();
  169. * @result 'Saturday'
  170. *
  171. * @example var dtm = new Date("01/12/2008");
  172. * dtm.getDayName(true);
  173. * @result 'Sat'
  174. *
  175. * @param abbreviated Boolean When set to true the name will be abbreviated.
  176. * @name getDayName
  177. * @type String
  178. * @cat Plugins/Methods/Date
  179. */
  180. add("getDayName", function(abbreviated) {
  181. return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
  182. });
  183. /**
  184. * Gets the name of the month.
  185. *
  186. * @example var dtm = new Date("01/12/2008");
  187. * dtm.getMonthName();
  188. * @result 'Janurary'
  189. *
  190. * @example var dtm = new Date("01/12/2008");
  191. * dtm.getMonthName(true);
  192. * @result 'Jan'
  193. *
  194. * @param abbreviated Boolean When set to true the name will be abbreviated.
  195. * @name getDayName
  196. * @type String
  197. * @cat Plugins/Methods/Date
  198. */
  199. add("getMonthName", function(abbreviated) {
  200. return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
  201. });
  202. /**
  203. * Get the number of the day of the year.
  204. *
  205. * @example var dtm = new Date("01/12/2008");
  206. * dtm.getDayOfYear();
  207. * @result 11
  208. *
  209. * @name getDayOfYear
  210. * @type Number
  211. * @cat Plugins/Methods/Date
  212. */
  213. add("getDayOfYear", function() {
  214. var tmpdtm = new Date("1/1/" + this.getFullYear());
  215. return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
  216. });
  217. /**
  218. * Get the number of the week of the year.
  219. *
  220. * @example var dtm = new Date("01/12/2008");
  221. * dtm.getWeekOfYear();
  222. * @result 2
  223. *
  224. * @name getWeekOfYear
  225. * @type Number
  226. * @cat Plugins/Methods/Date
  227. */
  228. add("getWeekOfYear", function() {
  229. return Math.ceil(this.getDayOfYear() / 7);
  230. });
  231. /**
  232. * Set the day of the year.
  233. *
  234. * @example var dtm = new Date("01/12/2008");
  235. * dtm.setDayOfYear(1);
  236. * dtm.toString();
  237. * @result 'Tue Jan 01 2008 00:00:00'
  238. *
  239. * @name setDayOfYear
  240. * @type Date
  241. * @cat Plugins/Methods/Date
  242. */
  243. add("setDayOfYear", function(day) {
  244. this.setMonth(0);
  245. this.setDate(day);
  246. return this;
  247. });
  248. /**
  249. * Add a number of years to the date object.
  250. *
  251. * @example var dtm = new Date("01/12/2008");
  252. * dtm.addYears(1);
  253. * dtm.toString();
  254. * @result 'Mon Jan 12 2009 00:00:00'
  255. *
  256. * @name addYears
  257. * @type Date
  258. * @cat Plugins/Methods/Date
  259. */
  260. add("addYears", function(num) {
  261. this.setFullYear(this.getFullYear() + num);
  262. return this;
  263. });
  264. /**
  265. * Add a number of months to the date object.
  266. *
  267. * @example var dtm = new Date("01/12/2008");
  268. * dtm.addMonths(1);
  269. * dtm.toString();
  270. * @result 'Tue Feb 12 2008 00:00:00'
  271. *
  272. * @name addMonths
  273. * @type Date
  274. * @cat Plugins/Methods/Date
  275. */
  276. add("addMonths", function(num) {
  277. var tmpdtm = this.getDate();
  278. this.setMonth(this.getMonth() + num);
  279. if (tmpdtm > this.getDate())
  280. this.addDays(-this.getDate());
  281. return this;
  282. });
  283. /**
  284. * Add a number of days to the date object.
  285. *
  286. * @example var dtm = new Date("01/12/2008");
  287. * dtm.addDays(1);
  288. * dtm.toString();
  289. * @result 'Sun Jan 13 2008 00:00:00'
  290. *
  291. * @name addDays
  292. * @type Date
  293. * @cat Plugins/Methods/Date
  294. */
  295. add("addDays", function(num) {
  296. //this.setDate(this.getDate() + num);
  297. this.setTime(this.getTime() + (num*86400000) );
  298. return this;
  299. });
  300. /**
  301. * Add a number of hours to the date object.
  302. *
  303. * @example var dtm = new Date("01/12/2008");
  304. * dtm.addHours(24);
  305. * dtm.toString();
  306. * @result 'Sun Jan 13 2008 00:00:00'
  307. *
  308. * @name addHours
  309. * @type Date
  310. * @cat Plugins/Methods/Date
  311. */
  312. add("addHours", function(num) {
  313. this.setHours(this.getHours() + num);
  314. return this;
  315. });
  316. /**
  317. * Add a number of minutes to the date object.
  318. *
  319. * @example var dtm = new Date("01/12/2008");
  320. * dtm.addMinutes(60);
  321. * dtm.toString();
  322. * @result 'Sat Jan 12 2008 01:00:00'
  323. *
  324. * @name addMinutes
  325. * @type Date
  326. * @cat Plugins/Methods/Date
  327. */
  328. add("addMinutes", function(num) {
  329. this.setMinutes(this.getMinutes() + num);
  330. return this;
  331. });
  332. /**
  333. * Add a number of seconds to the date object.
  334. *
  335. * @example var dtm = new Date("01/12/2008");
  336. * dtm.addSeconds(60);
  337. * dtm.toString();
  338. * @result 'Sat Jan 12 2008 00:01:00'
  339. *
  340. * @name addSeconds
  341. * @type Date
  342. * @cat Plugins/Methods/Date
  343. */
  344. add("addSeconds", function(num) {
  345. this.setSeconds(this.getSeconds() + num);
  346. return this;
  347. });
  348. /**
  349. * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
  350. *
  351. * @example var dtm = new Date();
  352. * dtm.zeroTime();
  353. * dtm.toString();
  354. * @result 'Sat Jan 12 2008 00:01:00'
  355. *
  356. * @name zeroTime
  357. * @type Date
  358. * @cat Plugins/Methods/Date
  359. * @author Kelvin Luck
  360. */
  361. add("zeroTime", function() {
  362. this.setMilliseconds(0);
  363. this.setSeconds(0);
  364. this.setMinutes(0);
  365. this.setHours(0);
  366. return this;
  367. });
  368. /**
  369. * Returns a string representation of the date object according to Date.format.
  370. * (Date.toString may be used in other places so I purposefully didn't overwrite it)
  371. *
  372. * @example var dtm = new Date("01/12/2008");
  373. * dtm.asString();
  374. * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
  375. *
  376. * @name asString
  377. * @type Date
  378. * @cat Plugins/Methods/Date
  379. * @author Kelvin Luck
  380. */
  381. add("asString", function(format) {
  382. var r = format || Date.format;
  383. if (r.split('mm').length>1) { // ugly workaround to make sure we don't replace the m's in e.g. noveMber
  384. r = r.split('mmmm').join(this.getMonthName(false))
  385. .split('mmm').join(this.getMonthName(true))
  386. .split('mm').join(_zeroPad(this.getMonth()+1))
  387. } else {
  388. r = r.split('m').join(this.getMonth()+1);
  389. }
  390. r = r.split('yyyy').join(this.getFullYear())
  391. .split('yy').join((this.getFullYear() + '').substring(2))
  392. .split('dd').join(_zeroPad(this.getDate()))
  393. .split('d').join(this.getDate());
  394. return r;
  395. });
  396. /**
  397. * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
  398. * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
  399. *
  400. * @example var dtm = Date.fromString("12/01/2008");
  401. * dtm.toString();
  402. * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
  403. *
  404. * @name fromString
  405. * @type Date
  406. * @cat Plugins/Methods/Date
  407. * @author Kelvin Luck
  408. */
  409. Date.fromString = function(s)
  410. {
  411. var f = Date.format;
  412. var d = new Date('01/01/1970');
  413. if (s == '') return d;
  414. s = s.toLowerCase();
  415. var matcher = '';
  416. var order = [];
  417. var r = /(dd?d?|mm?m?|yy?yy?)+([^(m|d|y)])?/g;
  418. var results;
  419. while ((results = r.exec(f)) != null)
  420. {
  421. switch (results[1]) {
  422. case 'd':
  423. case 'dd':
  424. case 'm':
  425. case 'mm':
  426. case 'yy':
  427. case 'yyyy':
  428. matcher += '(\\d+\\d?\\d?\\d?)+';
  429. order.push(results[1].substr(0, 1));
  430. break;
  431. case 'mmm':
  432. matcher += '([a-z]{3})';
  433. order.push('M');
  434. break;
  435. }
  436. if (results[2]) {
  437. matcher += results[2];
  438. }
  439. }
  440. var dm = new RegExp(matcher);
  441. var result = s.match(dm);
  442. for (var i=0; i<order.length; i++) {
  443. var res = result[i+1];
  444. switch(order[i]) {
  445. case 'd':
  446. d.setDate(res);
  447. break;
  448. case 'm':
  449. d.setMonth(Number(res)-1);
  450. break;
  451. case 'M':
  452. for (var j=0; j<Date.abbrMonthNames.length; j++) {
  453. if (Date.abbrMonthNames[j].toLowerCase() == res) break;
  454. }
  455. d.setMonth(j);
  456. break;
  457. case 'y':
  458. d.setYear(res);
  459. break;
  460. }
  461. }
  462. return d;
  463. };
  464. // utility method
  465. var _zeroPad = function(num) {
  466. var s = '0'+num;
  467. return s.substring(s.length-2)
  468. //return ('0'+num).substring(-2); // doesn't work on IE :(
  469. };
  470. })();
  471. /**
  472. * Copyright (c) 2008 Kelvin Luck (http://www.kelvinluck.com/)
  473. * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  474. * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  475. *
  476. * $Id: jquery.datePicker.js,v 1.2 2012/12/15 05:36:04 ghp Exp $
  477. **/
  478. (function($){
  479. $.fn.extend({
  480. /**
  481. * Render a calendar table into any matched elements.
  482. *
  483. * @param Object s (optional) Customize your calendars.
  484. * @option Number month The month to render (NOTE that months are zero based). Default is today's month.
  485. * @option Number year The year to render. Default is today's year.
  486. * @option Function renderCallback A reference to a function that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Default is no callback.
  487. * @option Number showHeader Whether or not to show the header row, possible values are: $.dpConst.SHOW_HEADER_NONE (no header), $.dpConst.SHOW_HEADER_SHORT (first letter of each day) and $.dpConst.SHOW_HEADER_LONG (full name of each day). Default is $.dpConst.SHOW_HEADER_SHORT.
  488. * @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
  489. * @type jQuery
  490. * @name renderCalendar
  491. * @cat plugins/datePicker
  492. * @author Kelvin Luck (http://www.kelvinluck.com/)
  493. *
  494. * @example $('#calendar-me').renderCalendar({month:0, year:2007});
  495. * @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me.
  496. *
  497. * @example
  498. * var testCallback = function($td, thisDate, month, year)
  499. * {
  500. * if ($td.is('.current-month') && thisDate.getDay() == 4) {
  501. * var d = thisDate.getDate();
  502. * $td.bind(
  503. * 'click',
  504. * function()
  505. * {
  506. * alert('You clicked on ' + d + '/' + (Number(month)+1) + '/' + year);
  507. * }
  508. * ).addClass('thursday');
  509. * } else if (thisDate.getDay() == 5) {
  510. * $td.html('Friday the ' + $td.html() + 'th');
  511. * }
  512. * }
  513. * $('#calendar-me').renderCalendar({month:0, year:2007, renderCallback:testCallback});
  514. *
  515. * @desc Renders a calendar displaying January 2007 into the element with an id of calendar-me. Every Thursday in the current month has a class of "thursday" applied to it, is clickable and shows an alert when clicked. Every Friday on the calendar has the number inside replaced with text.
  516. **/
  517. renderCalendar : function(s)
  518. {
  519. var dc = function(a)
  520. {
  521. return document.createElement(a);
  522. };
  523. s = $.extend({}, $.fn.datePicker.defaults, s);
  524. if (s.showHeader != $.dpConst.SHOW_HEADER_NONE) {
  525. var headRow = $(dc('tr'));
  526. for (var i=Date.firstDayOfWeek; i<Date.firstDayOfWeek+7; i++) {
  527. var weekday = i%7;
  528. var day = Date.dayNames[weekday];
  529. headRow.append(
  530. jQuery(dc('th')).attr({'scope':'col', 'abbr':day, 'title':day, 'class':(weekday == 0 || weekday == 6 ? 'weekend' : 'weekday')}).html(s.showHeader == $.dpConst.SHOW_HEADER_SHORT ? day.substr(0, 1) : day)
  531. );
  532. }
  533. };
  534. var calendarTable = $(dc('table'))
  535. .attr(
  536. {
  537. 'cellspacing':2
  538. }
  539. )
  540. .addClass('jCalendar')
  541. .append(
  542. (s.showHeader != $.dpConst.SHOW_HEADER_NONE ?
  543. $(dc('thead'))
  544. .append(headRow)
  545. :
  546. dc('thead')
  547. )
  548. );
  549. var tbody = $(dc('tbody'));
  550. var today = (new Date()).zeroTime();
  551. var month = s.month == undefined ? today.getMonth() : s.month;
  552. var year = s.year || today.getFullYear();
  553. var currentDate = new Date(year, month, 1);
  554. var firstDayOffset = Date.firstDayOfWeek - currentDate.getDay() + 1;
  555. if (firstDayOffset > 1) firstDayOffset -= 7;
  556. var weeksToDraw = Math.ceil(( (-1*firstDayOffset+1) + currentDate.getDaysInMonth() ) /7);
  557. currentDate.addDays(firstDayOffset-1);
  558. var doHover = function(firstDayInBounds)
  559. {
  560. return function()
  561. {
  562. if (s.hoverClass) {
  563. var $this = $(this);
  564. if (!s.selectWeek) {
  565. $this.addClass(s.hoverClass);
  566. } else if (firstDayInBounds && !$this.is('.disabled')) {
  567. $this.parent().addClass('activeWeekHover');
  568. }
  569. }
  570. }
  571. };
  572. var unHover = function()
  573. {
  574. if (s.hoverClass) {
  575. var $this = $(this);
  576. $this.removeClass(s.hoverClass);
  577. $this.parent().removeClass('activeWeekHover');
  578. }
  579. };
  580. var w = 0;
  581. while (w++<weeksToDraw) {
  582. var r = jQuery(dc('tr'));
  583. var firstDayInBounds = s.dpController ? currentDate > s.dpController.startDate : false;
  584. for (var i=0; i<7; i++) {
  585. var thisMonth = currentDate.getMonth() == month;
  586. var d = $(dc('td'))
  587. .text(currentDate.getDate() + '')
  588. .addClass((thisMonth ? 'current-month ' : 'other-month ') +
  589. (currentDate.isWeekend() ? 'weekend ' : 'weekday ') +
  590. (thisMonth && currentDate.getTime() == today.getTime() ? 'today ' : '')
  591. )
  592. .data('datePickerDate', currentDate.asString())
  593. .hover(doHover(firstDayInBounds), unHover)
  594. ;
  595. r.append(d);
  596. if (s.renderCallback) {
  597. s.renderCallback(d, currentDate, month, year);
  598. }
  599. // addDays(1) fails in some locales due to daylight savings. See issue 39.
  600. //currentDate.addDays(1);
  601. currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()+1);
  602. }
  603. tbody.append(r);
  604. }
  605. calendarTable.append(tbody);
  606. return this.each(
  607. function()
  608. {
  609. $(this).empty().append(calendarTable);
  610. }
  611. );
  612. },
  613. /**
  614. * Create a datePicker associated with each of the matched elements.
  615. *
  616. * The matched element will receive a few custom events with the following signatures:
  617. *
  618. * dateSelected(event, date, $td, status)
  619. * Triggered when a date is selected. event is a reference to the event, date is the Date selected, $td is a jquery object wrapped around the TD that was clicked on and status is whether the date was selected (true) or deselected (false)
  620. *
  621. * dpClosed(event, selected)
  622. * Triggered when the date picker is closed. event is a reference to the event and selected is an Array containing Date objects.
  623. *
  624. * dpMonthChanged(event, displayedMonth, displayedYear)
  625. * Triggered when the month of the popped up calendar is changed. event is a reference to the event, displayedMonth is the number of the month now displayed (zero based) and displayedYear is the year of the month.
  626. *
  627. * dpDisplayed(event, $datePickerDiv)
  628. * Triggered when the date picker is created. $datePickerDiv is the div containing the date picker. Use this event to add custom content/ listeners to the popped up date picker.
  629. *
  630. * @param Object s (optional) Customize your date pickers.
  631. * @option Number month The month to render when the date picker is opened (NOTE that months are zero based). Default is today's month.
  632. * @option Number year The year to render when the date picker is opened. Default is today's year.
  633. * @option String startDate The first date date can be selected.
  634. * @option String endDate The last date that can be selected.
  635. * @option Boolean inline Whether to create the datePicker as inline (e.g. always on the page) or as a model popup. Default is false (== modal popup)
  636. * @option Boolean createButton Whether to create a .dp-choose-date anchor directly after the matched element which when clicked will trigger the showing of the date picker. Default is true.
  637. * @option Boolean showYearNavigation Whether to display buttons which allow the user to navigate through the months a year at a time. Default is true.
  638. * @option Boolean closeOnSelect Whether to close the date picker when a date is selected. Default is true.
  639. * @option Boolean displayClose Whether to create a "Close" button within the date picker popup. Default is false.
  640. * @option Boolean selectMultiple Whether a user should be able to select multiple dates with this date picker. Default is false.
  641. * @option Number numSelectable The maximum number of dates that can be selected where selectMultiple is true. Default is a very high number.
  642. * @option Boolean clickInput If the matched element is an input type="text" and this option is true then clicking on the input will cause the date picker to appear.
  643. * @option Boolean rememberViewedMonth Whether the datePicker should remember the last viewed month and open on it. If false then the date picker will always open with the month for the first selected date visible.
  644. * @option Boolean selectWeek Whether to select a complete week at a time...
  645. * @option Number verticalPosition The vertical alignment of the popped up date picker to the matched element. One of $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM. Default is $.dpConst.POS_TOP.
  646. * @option Number horizontalPosition The horizontal alignment of the popped up date picker to the matched element. One of $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT.
  647. * @option Number verticalOffset The number of pixels offset from the defined verticalPosition of this date picker that it should pop up in. Default in 0.
  648. * @option Number horizontalOffset The number of pixels offset from the defined horizontalPosition of this date picker that it should pop up in. Default in 0.
  649. * @option (Function|Array) renderCallback A reference to a function (or an array of seperate functions) that is called as each cell is rendered and which can add classes and event listeners to the created nodes. Each callback function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year. Default is no callback.
  650. * @option String hoverClass The class to attach to each cell when you hover over it (to allow you to use hover effects in IE6 which doesn't support the :hover pseudo-class on elements other than links). Default is dp-hover. Pass false if you don't want a hover class.
  651. * @type jQuery
  652. * @name datePicker
  653. * @cat plugins/datePicker
  654. * @author Kelvin Luck (http://www.kelvinluck.com/)
  655. *
  656. * @example $('input.date-picker').datePicker();
  657. * @desc Creates a date picker button next to all matched input elements. When the button is clicked on the value of the selected date will be placed in the corresponding input (formatted according to Date.format).
  658. *
  659. * @example demo/index.html
  660. * @desc See the projects homepage for many more complex examples...
  661. **/
  662. datePicker : function(s)
  663. {
  664. if (!$.event._dpCache) $.event._dpCache = [];
  665. // initialise the date picker controller with the relevant settings...
  666. s = $.extend({}, $.fn.datePicker.defaults, s);
  667. return this.each(
  668. function()
  669. {
  670. var $this = $(this);
  671. var alreadyExists = true;
  672. if (!this._dpId) {
  673. this._dpId = $.event.guid++;
  674. $.event._dpCache[this._dpId] = new DatePicker(this);
  675. alreadyExists = false;
  676. }
  677. if (s.inline) {
  678. s.createButton = false;
  679. s.displayClose = false;
  680. s.closeOnSelect = false;
  681. $this.empty();
  682. }
  683. var controller = $.event._dpCache[this._dpId];
  684. controller.init(s);
  685. if (!alreadyExists && s.createButton) {
  686. // create it!
  687. controller.button = $('<a href="#" class="dp-choose-date" title="' + $.dpText.TEXT_CHOOSE_DATE + '">' + $.dpText.TEXT_CHOOSE_DATE + '</a>')
  688. .bind(
  689. 'click',
  690. function()
  691. {
  692. $this.dpDisplay(this);
  693. this.blur();
  694. return false;
  695. }
  696. );
  697. $this.after(controller.button);
  698. }
  699. if (!alreadyExists && $this.is(':text')) {
  700. $this
  701. .bind(
  702. 'dateSelected',
  703. function(e, selectedDate, $td)
  704. {
  705. this.value = selectedDate.asString();
  706. }
  707. ).bind(
  708. 'change',
  709. function()
  710. {
  711. if (this.value == '') {
  712. controller.clearSelected();
  713. } else {
  714. var d = Date.fromString(this.value);
  715. if (d) {
  716. controller.setSelected(d, true, true);
  717. }
  718. }
  719. }
  720. );
  721. if (s.clickInput) {
  722. $this.bind(
  723. 'click',
  724. function()
  725. {
  726. // The change event doesn't happen until the input loses focus so we need to manually trigger it...
  727. $this.trigger('change');
  728. $this.dpDisplay();
  729. }
  730. );
  731. }
  732. var d = Date.fromString(this.value);
  733. if (this.value != '' && d) {
  734. controller.setSelected(d, true, true);
  735. }
  736. }
  737. $this.addClass('dp-applied');
  738. }
  739. )
  740. },
  741. /**
  742. * Disables or enables this date picker
  743. *
  744. * @param Boolean s Whether to disable (true) or enable (false) this datePicker
  745. * @type jQuery
  746. * @name dpSetDisabled
  747. * @cat plugins/datePicker
  748. * @author Kelvin Luck (http://www.kelvinluck.com/)
  749. *
  750. * @example $('.date-picker').datePicker();
  751. * $('.date-picker').dpSetDisabled(true);
  752. * @desc Prevents this date picker from displaying and adds a class of dp-disabled to it (and it's associated button if it has one) for styling purposes. If the matched element is an input field then it will also set the disabled attribute to stop people directly editing the field.
  753. **/
  754. dpSetDisabled : function(s)
  755. {
  756. return _w.call(this, 'setDisabled', s);
  757. },
  758. /**
  759. * Updates the first selectable date for any date pickers on any matched elements.
  760. *
  761. * @param String d A string representing the first selectable date (formatted according to Date.format).
  762. * @type jQuery
  763. * @name dpSetStartDate
  764. * @cat plugins/datePicker
  765. * @author Kelvin Luck (http://www.kelvinluck.com/)
  766. *
  767. * @example $('.date-picker').datePicker();
  768. * $('.date-picker').dpSetStartDate('01/01/2000');
  769. * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the first selectable date for each of these to the first day of the millenium.
  770. **/
  771. dpSetStartDate : function(d)
  772. {
  773. return _w.call(this, 'setStartDate', d);
  774. },
  775. /**
  776. * Updates the last selectable date for any date pickers on any matched elements.
  777. *
  778. * @param String d A string representing the last selectable date (formatted according to Date.format).
  779. * @type jQuery
  780. * @name dpSetEndDate
  781. * @cat plugins/datePicker
  782. * @author Kelvin Luck (http://www.kelvinluck.com/)
  783. *
  784. * @example $('.date-picker').datePicker();
  785. * $('.date-picker').dpSetEndDate('01/01/2010');
  786. * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the last selectable date for each of these to the first Janurary 2010.
  787. **/
  788. dpSetEndDate : function(d)
  789. {
  790. return _w.call(this, 'setEndDate', d);
  791. },
  792. /**
  793. * Gets a list of Dates currently selected by this datePicker. This will be an empty array if no dates are currently selected or NULL if there is no datePicker associated with the matched element.
  794. *
  795. * @type Array
  796. * @name dpGetSelected
  797. * @cat plugins/datePicker
  798. * @author Kelvin Luck (http://www.kelvinluck.com/)
  799. *
  800. * @example $('.date-picker').datePicker();
  801. * alert($('.date-picker').dpGetSelected());
  802. * @desc Will alert an empty array (as nothing is selected yet)
  803. **/
  804. dpGetSelected : function()
  805. {
  806. var c = _getController(this[0]);
  807. if (c) {
  808. return c.getSelected();
  809. }
  810. return null;
  811. },
  812. /**
  813. * Selects or deselects a date on any matched element's date pickers. Deselcting is only useful on date pickers where selectMultiple==true. Selecting will only work if the passed date is within the startDate and endDate boundries for a given date picker.
  814. *
  815. * @param String d A string representing the date you want to select (formatted according to Date.format).
  816. * @param Boolean v Whether you want to select (true) or deselect (false) this date. Optional - default = true.
  817. * @param Boolean m Whether you want the date picker to open up on the month of this date when it is next opened. Optional - default = true.
  818. * @param Boolean e Whether you want the date picker to dispatch events related to this change of selection. Optional - default = true.
  819. * @type jQuery
  820. * @name dpSetSelected
  821. * @cat plugins/datePicker
  822. * @author Kelvin Luck (http://www.kelvinluck.com/)
  823. *
  824. * @example $('.date-picker').datePicker();
  825. * $('.date-picker').dpSetSelected('01/01/2010');
  826. * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the selected date on these date pickers to the first Janurary 2010. When the date picker is next opened it will display Janurary 2010.
  827. **/
  828. dpSetSelected : function(d, v, m, e)
  829. {
  830. if (v == undefined) v=true;
  831. if (m == undefined) m=true;
  832. if (e == undefined) e=true;
  833. return _w.call(this, 'setSelected', Date.fromString(d), v, m, e);
  834. },
  835. /**
  836. * Sets the month that will be displayed when the date picker is next opened. If the passed month is before startDate then the month containing startDate will be displayed instead. If the passed month is after endDate then the month containing the endDate will be displayed instead.
  837. *
  838. * @param Number m The month you want the date picker to display. Optional - defaults to the currently displayed month.
  839. * @param Number y The year you want the date picker to display. Optional - defaults to the currently displayed year.
  840. * @type jQuery
  841. * @name dpSetDisplayedMonth
  842. * @cat plugins/datePicker
  843. * @author Kelvin Luck (http://www.kelvinluck.com/)
  844. *
  845. * @example $('.date-picker').datePicker();
  846. * $('.date-picker').dpSetDisplayedMonth(10, 2008);
  847. * @desc Creates a date picker associated with all elements with a class of "date-picker" then sets the selected date on these date pickers to the first Janurary 2010. When the date picker is next opened it will display Janurary 2010.
  848. **/
  849. dpSetDisplayedMonth : function(m, y)
  850. {
  851. return _w.call(this, 'setDisplayedMonth', Number(m), Number(y), true);
  852. },
  853. /**
  854. * Displays the date picker associated with the matched elements. Since only one date picker can be displayed at once then the date picker associated with the last matched element will be the one that is displayed.
  855. *
  856. * @param HTMLElement e An element that you want the date picker to pop up relative in position to. Optional - default behaviour is to pop up next to the element associated with this date picker.
  857. * @type jQuery
  858. * @name dpDisplay
  859. * @cat plugins/datePicker
  860. * @author Kelvin Luck (http://www.kelvinluck.com/)
  861. *
  862. * @example $('#date-picker').datePicker();
  863. * $('#date-picker').dpDisplay();
  864. * @desc Creates a date picker associated with the element with an id of date-picker and then causes it to pop up.
  865. **/
  866. dpDisplay : function(e)
  867. {
  868. return _w.call(this, 'display', e);
  869. },
  870. /**
  871. * Sets a function or array of functions that is called when each TD of the date picker popup is rendered to the page
  872. *
  873. * @param (Function|Array) a A function or an array of functions that are called when each td is rendered. Each function will receive four arguments; a jquery object wrapping the created TD, a Date object containing the date this TD represents, a number giving the currently rendered month and a number giving the currently rendered year.
  874. * @type jQuery
  875. * @name dpSetRenderCallback
  876. * @cat plugins/datePicker
  877. * @author Kelvin Luck (http://www.kelvinluck.com/)
  878. *
  879. * @example $('#date-picker').datePicker();
  880. * $('#date-picker').dpSetRenderCallback(function($td, thisDate, month, year)
  881. * {
  882. * // do stuff as each td is rendered dependant on the date in the td and the displayed month and year
  883. * });
  884. * @desc Creates a date picker associated with the element with an id of date-picker and then creates a function which is called as each td is rendered when this date picker is displayed.
  885. **/
  886. dpSetRenderCallback : function(a)
  887. {
  888. return _w.call(this, 'setRenderCallback', a);
  889. },
  890. /**
  891. * Sets the position that the datePicker will pop up (relative to it's associated element)
  892. *
  893. * @param Number v The vertical alignment of the created date picker to it's associated element. Possible values are $.dpConst.POS_TOP and $.dpConst.POS_BOTTOM
  894. * @param Number h The horizontal alignment of the created date picker to it's associated element. Possible values are $.dpConst.POS_LEFT and $.dpConst.POS_RIGHT
  895. * @type jQuery
  896. * @name dpSetPosition
  897. * @cat plugins/datePicker
  898. * @author Kelvin Luck (http://www.kelvinluck.com/)
  899. *
  900. * @example $('#date-picker').datePicker();
  901. * $('#date-picker').dpSetPosition($.dpConst.POS_BOTTOM, $.dpConst.POS_RIGHT);
  902. * @desc Creates a date picker associated with the element with an id of date-picker and makes it so that when this date picker pops up it will be bottom and right aligned to the #date-picker element.
  903. **/
  904. dpSetPosition : function(v, h)
  905. {
  906. return _w.call(this, 'setPosition', v, h);
  907. },
  908. /**
  909. * Sets the offset that the popped up date picker will have from it's default position relative to it's associated element (as set by dpSetPosition)
  910. *
  911. * @param Number v The vertical offset of the created date picker.
  912. * @param Number h The horizontal offset of the created date picker.
  913. * @type jQuery
  914. * @name dpSetOffset
  915. * @cat plugins/datePicker
  916. * @author Kelvin Luck (http://www.kelvinluck.com/)
  917. *
  918. * @example $('#date-picker').datePicker();
  919. * $('#date-picker').dpSetOffset(-20, 200);
  920. * @desc Creates a date picker associated with the element with an id of date-picker and makes it so that when this date picker pops up it will be 20 pixels above and 200 pixels to the right of it's default position.
  921. **/
  922. dpSetOffset : function(v, h)
  923. {
  924. return _w.call(this, 'setOffset', v, h);
  925. },
  926. /**
  927. * Closes the open date picker associated with this element.
  928. *
  929. * @type jQuery
  930. * @name dpClose
  931. * @cat plugins/datePicker
  932. * @author Kelvin Luck (http://www.kelvinluck.com/)
  933. *
  934. * @example $('.date-pick')
  935. * .datePicker()
  936. * .bind(
  937. * 'focus',
  938. * function()
  939. * {
  940. * $(this).dpDisplay();
  941. * }
  942. * ).bind(
  943. * 'blur',
  944. * function()
  945. * {
  946. * $(this).dpClose();
  947. * }
  948. * );
  949. * @desc Creates a date picker and makes it appear when the relevant element is focused and disappear when it is blurred.
  950. **/
  951. dpClose : function()
  952. {
  953. return _w.call(this, '_closeCalendar', false, this[0]);
  954. },
  955. // private function called on unload to clean up any expandos etc and prevent memory links...
  956. _dpDestroy : function()
  957. {
  958. // TODO - implement this?
  959. }
  960. });
  961. // private internal function to cut down on the amount of code needed where we forward
  962. // dp* methods on the jQuery object on to the relevant DatePicker controllers...
  963. var _w = function(f, a1, a2, a3, a4)
  964. {
  965. return this.each(
  966. function()
  967. {
  968. var c = _getController(this);
  969. if (c) {
  970. c[f](a1, a2, a3, a4);
  971. }
  972. }
  973. );
  974. };
  975. function DatePicker(ele)
  976. {
  977. this.ele = ele;
  978. // initial values...
  979. this.displayedMonth = null;
  980. this.displayedYear = null;
  981. this.startDate = null;
  982. this.endDate = null;
  983. this.showYearNavigation = null;
  984. this.closeOnSelect = null;
  985. this.displayClose = null;
  986. this.rememberViewedMonth= null;
  987. this.selectMultiple = null;
  988. this.numSelectable = null;
  989. this.numSelected = null;
  990. this.verticalPosition = null;
  991. this.horizontalPosition = null;
  992. this.verticalOffset = null;
  993. this.horizontalOffset = null;
  994. this.button = null;
  995. this.renderCallback = [];
  996. this.selectedDates = {};
  997. this.inline = null;
  998. this.context = '#dp-popup';
  999. this.settings = {};
  1000. };
  1001. $.extend(
  1002. DatePicker.prototype,
  1003. {
  1004. init : function(s)
  1005. {
  1006. this.setStartDate(s.startDate);
  1007. this.setEndDate(s.endDate);
  1008. this.setDisplayedMonth(Number(s.month), Number(s.year));
  1009. this.setRenderCallback(s.renderCallback);
  1010. this.showYearNavigation = s.showYearNavigation;
  1011. this.closeOnSelect = s.closeOnSelect;
  1012. this.displayClose = s.displayClose;
  1013. this.rememberViewedMonth = s.rememberViewedMonth;
  1014. this.selectMultiple = s.selectMultiple;
  1015. this.numSelectable = s.selectMultiple ? s.numSelectable : 1;
  1016. this.numSelected = 0;
  1017. this.verticalPosition = s.verticalPosition;
  1018. this.horizontalPosition = s.horizontalPosition;
  1019. this.hoverClass = s.hoverClass;
  1020. this.setOffset(s.verticalOffset, s.horizontalOffset);
  1021. this.inline = s.inline;
  1022. this.settings = s;
  1023. if (this.inline) {
  1024. this.context = this.ele;
  1025. this.display();
  1026. }
  1027. },
  1028. setStartDate : function(d)
  1029. {
  1030. if (d) {
  1031. this.startDate = Date.fromString(d);
  1032. }
  1033. if (!this.startDate) {
  1034. this.startDate = (new Date()).zeroTime();
  1035. }
  1036. this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
  1037. },
  1038. setEndDate : function(d)
  1039. {
  1040. if (d) {
  1041. this.endDate = Date.fromString(d);
  1042. }
  1043. if (!this.endDate) {
  1044. this.endDate = (new Date('12/31/2999')); // using the JS Date.parse function which expects mm/dd/yyyy
  1045. }
  1046. if (this.endDate.getTime() < this.startDate.getTime()) {
  1047. this.endDate = this.startDate;
  1048. }
  1049. this.setDisplayedMonth(this.displayedMonth, this.displayedYear);
  1050. },
  1051. setPosition : function(v, h)
  1052. {
  1053. this.verticalPosition = v;
  1054. this.horizontalPosition = h;
  1055. },
  1056. setOffset : function(v, h)
  1057. {
  1058. this.verticalOffset = parseInt(v) || 0;
  1059. this.horizontalOffset = parseInt(h) || 0;
  1060. },
  1061. setDisabled : function(s)
  1062. {
  1063. $e = $(this.ele);
  1064. $e[s ? 'addClass' : 'removeClass']('dp-disabled');
  1065. if (this.button) {
  1066. $but = $(this.button);
  1067. $but[s ? 'addClass' : 'removeClass']('dp-disabled');
  1068. $but.attr('title', s ? '' : $.dpText.TEXT_CHOOSE_DATE);
  1069. }
  1070. if ($e.is(':text')) {
  1071. $e.attr('disabled', s ? 'disabled' : '');
  1072. }
  1073. },
  1074. setDisplayedMonth : function(m, y, rerender)
  1075. {
  1076. if (this.startDate == undefined || this.endDate == undefined) {
  1077. return;
  1078. }
  1079. var s = new Date(this.startDate.getTime());
  1080. s.setDate(1);
  1081. var e = new Date(this.endDate.getTime());
  1082. e.setDate(1);
  1083. var t;
  1084. if ((!m && !y) || (isNaN(m) && isNaN(y))) {
  1085. // no month or year passed - default to current month
  1086. t = new Date().zeroTime();
  1087. t.setDate(1);
  1088. } else if (isNaN(m)) {
  1089. // just year passed in - presume we want the displayedMonth
  1090. t = new Date(y, this.displayedMonth, 1);
  1091. } else if (isNaN(y)) {
  1092. // just month passed in - presume we want the displayedYear
  1093. t = new Date(this.displayedYear, m, 1);
  1094. } else {
  1095. // year and month passed in - that's the date we want!
  1096. t = new Date(y, m, 1)
  1097. }
  1098. // check if the desired date is within the range of our defined startDate and endDate
  1099. if (t.getTime() < s.getTime()) {
  1100. t = s;
  1101. } else if (t.getTime() > e.getTime()) {
  1102. t = e;
  1103. }
  1104. var oldMonth = this.displayedMonth;
  1105. var oldYear = this.displayedYear;
  1106. this.displayedMonth = t.getMonth();
  1107. this.displayedYear = t.getFullYear();
  1108. if (rerender && (this.displayedMonth != oldMonth || this.displayedYear != oldYear))
  1109. {
  1110. this._rerenderCalendar();
  1111. $(this.ele).trigger('dpMonthChanged', [this.displayedMonth, this.displayedYear]);
  1112. }
  1113. },
  1114. setSelected : function(d, v, moveToMonth, dispatchEvents)
  1115. {
  1116. if (d < this.startDate || d > this.endDate) {
  1117. // Don't allow people to select dates outside range...
  1118. return;
  1119. }
  1120. var s = this.settings;
  1121. if (s.selectWeek)
  1122. {
  1123. d = d.addDays(- (d.getDay() - Date.firstDayOfWeek + 7) % 7);
  1124. if (d < this.startDate) // The first day of this week is before the start date so is unselectable...
  1125. {
  1126. return;
  1127. }
  1128. }
  1129. if (v == this.isSelected(d)) // this date is already un/selected
  1130. {
  1131. return;
  1132. }
  1133. if (this.selectMultiple == false) {
  1134. this.clearSelected();
  1135. } else if (v && this.numSelected == this.numSelectable) {
  1136. // can't select any more dates...
  1137. return;
  1138. }
  1139. if (moveToMonth && (this.displayedMonth != d.getMonth() || this.displayedYear != d.getFullYear())) {
  1140. this.setDisplayedMonth(d.getMonth(), d.getFullYear(), true);
  1141. }
  1142. this.selectedDates[d.toString()] = v;
  1143. this.numSelected += v ? 1 : -1;
  1144. var selectorString = 'td.' +( d.getMonth() == this.displayedMonth ? 'current-month' : 'other-month');
  1145. var $td;
  1146. $(selectorString, this.context).each(
  1147. function()
  1148. {
  1149. if ($(this).data('datePickerDate') == d.asString()) {
  1150. $td = $(this);
  1151. if (s.selectWeek)
  1152. {
  1153. $td.parent()[v ? 'addClass' : 'removeClass']('selectedWeek');
  1154. }
  1155. $td[v ? 'addClass' : 'removeClass']('selected');
  1156. }
  1157. }
  1158. );
  1159. $('td', this.context).not('.selected')[this.selectMultiple && this.numSelected == this.numSelectable ? 'addClass' : 'removeClass']('unselectable');
  1160. if (dispatchEvents)
  1161. {
  1162. var s = this.isSelected(d);
  1163. $e = $(this.ele);
  1164. var dClone = Date.fromString(d.asString());
  1165. $e.trigger('dateSelected', [dClone, $td, s]);
  1166. $e.trigger('change');
  1167. }
  1168. },
  1169. isSelected : function(d)
  1170. {
  1171. return this.selectedDates[d.toString()];
  1172. },
  1173. getSelected : function()
  1174. {
  1175. var r = [];
  1176. for(s in this.selectedDates) {
  1177. if (this.selectedDates[s] == true) {
  1178. r.push(Date.parse(s));
  1179. }
  1180. }
  1181. return r;
  1182. },
  1183. clearSelected : function()
  1184. {
  1185. this.selectedDates = {};
  1186. this.numSelected = 0;
  1187. $('td.selected', this.context).removeClass('selected').parent().removeClass('selectedWeek');
  1188. },
  1189. display : function(eleAlignTo)
  1190. {
  1191. if ($(this.ele).is('.dp-disabled')) return;
  1192. eleAlignTo = eleAlignTo || this.ele;
  1193. var c = this;
  1194. var $ele = $(eleAlignTo);
  1195. var eleOffset = $ele.offset();
  1196. var $createIn;
  1197. var attrs;
  1198. var attrsCalendarHolder;
  1199. var cssRules;
  1200. if (c.inline) {
  1201. $createIn = $(this.ele);
  1202. attrs = {
  1203. 'id' : 'calendar-' + this.ele._dpId,
  1204. 'class' : 'dp-popup dp-popup-inline'
  1205. };
  1206. $('.dp-popup', $createIn).remove();
  1207. cssRules = {
  1208. };
  1209. } else {
  1210. $createIn = $('body');
  1211. attrs = {
  1212. 'id' : 'dp-popup',
  1213. 'class' : 'dp-popup'
  1214. };
  1215. cssRules = {
  1216. 'top' : eleOffset.top + c.verticalOffset,
  1217. 'left' : eleOffset.left + c.horizontalOffset
  1218. };
  1219. var _checkMouse = function(e)
  1220. {
  1221. var el = e.target;
  1222. var cal = $('#dp-popup')[0];
  1223. while (true){
  1224. if (el == cal) {
  1225. return true;
  1226. } else if (el == document) {
  1227. c._closeCalendar();
  1228. return false;
  1229. } else {
  1230. el = $(el).parent()[0];
  1231. }
  1232. }
  1233. };
  1234. this._checkMouse = _checkMouse;
  1235. c._closeCalendar(true);
  1236. $(document).bind(
  1237. 'keydown.datepicker',
  1238. function(event)
  1239. {
  1240. if (event.keyCode == 27) {
  1241. c._closeCalendar();
  1242. }
  1243. }
  1244. );
  1245. }
  1246. if (!c.rememberViewedMonth)
  1247. {
  1248. var selectedDate = this.getSelected()[0];
  1249. if (selectedDate) {
  1250. selectedDate = new Date(selectedDate);
  1251. this.setDisplayedMonth(selectedDate.getMonth(), selectedDate.getFullYear(), false);
  1252. }
  1253. }
  1254. $createIn
  1255. .append(
  1256. $('<div></div>')
  1257. .attr(attrs)
  1258. .css(cssRules)
  1259. .append(
  1260. // $('<a href="#" class="selecteee">aaa</a>'),
  1261. $('<h2></h2>'),
  1262. $('<div class="dp-nav-prev"></div>')
  1263. .append(
  1264. $('<a class="dp-nav-prev-year" href="#" title="' + $.dpText.TEXT_PREV_YEAR + '">&lt;&lt;</a>')
  1265. .bind(
  1266. 'click',
  1267. function()
  1268. {
  1269. return c._displayNewMonth.call(c, this, 0, -1);
  1270. }
  1271. ),
  1272. $('<a class="dp-nav-prev-month" href="#" title="' + $.dpText.TEXT_PREV_MONTH + '">&lt;</a>')
  1273. .bind(
  1274. 'click',
  1275. function()
  1276. {
  1277. return c._displayNewMonth.call(c, this, -1, 0);
  1278. }
  1279. )
  1280. ),
  1281. $('<div class="dp-nav-next"></div>')
  1282. .append(
  1283. $('<a class="dp-nav-next-year" href="#" title="' + $.dpText.TEXT_NEXT_YEAR + '">&gt;&gt;</a>')
  1284. .bind(
  1285. 'click',
  1286. function()
  1287. {
  1288. return c._displayNewMonth.call(c, this, 0, 1);
  1289. }
  1290. ),
  1291. $('<a class="dp-nav-next-month" href="#" title="' + $.dpText.TEXT_NEXT_MONTH + '">&gt;</a>')
  1292. .bind(
  1293. 'click',
  1294. function()
  1295. {
  1296. return c._displayNewMonth.call(c, this, 1, 0);
  1297. }
  1298. )
  1299. ),
  1300. $('<div class="dp-calendar"></div>')
  1301. )
  1302. .bgIframe()
  1303. );
  1304. var $pop = this.inline ? $('.dp-popup', this.context) : $('#dp-popup');
  1305. if (this.showYearNavigation == false) {
  1306. $('.dp-nav-prev-year, .dp-nav-next-year', c.context).css('display', 'none');
  1307. }
  1308. if (this.displayClose) {
  1309. $pop.append(
  1310. $('<a href="#" id="dp-close">' + $.dpText.TEXT_CLOSE + '</a>')
  1311. .bind(
  1312. 'click',
  1313. function()
  1314. {
  1315. c._closeCalendar();
  1316. return false;
  1317. }
  1318. )
  1319. );
  1320. }
  1321. c._renderCalendar();
  1322. $(this.ele).trigger('dpDisplayed', $pop);
  1323. if (!c.inline) {
  1324. if (this.verticalPosition == $.dpConst.POS_BOTTOM) {
  1325. $pop.css('top', eleOffset.top + $ele.height() - $pop.height() + c.verticalOffset);
  1326. }
  1327. if (this.horizontalPosition == $.dpConst.POS_RIGHT) {
  1328. $pop.css('left', eleOffset.left + $ele.width() - $pop.width() + c.horizontalOffset);
  1329. }
  1330. // $('.selectee', this.context).focus();
  1331. $(document).bind('mousedown.datepicker', this._checkMouse);
  1332. }
  1333. },
  1334. setRenderCallback : function(a)
  1335. {
  1336. if (a == null) return;
  1337. if (a && typeof(a) == 'function') {
  1338. a = [a];
  1339. }
  1340. this.renderCallback = this.renderCallback.concat(a);
  1341. },
  1342. cellRender : function ($td, thisDate, month, year) {
  1343. var c = this.dpController;
  1344. var d = new Date(thisDate.getTime());
  1345. // add our click handlers to deal with it when the days are clicked...
  1346. $td.bind(
  1347. 'click',
  1348. function()
  1349. {
  1350. var $this = $(this);
  1351. if (!$this.is('.disabled')) {
  1352. c.setSelected(d, !$this.is('.selected') || !c.selectMultiple, false, true);
  1353. if (c.closeOnSelect) {
  1354. c._closeCalendar();
  1355. }
  1356. // TODO: Instead of this which doesn't work in IE anyway we should find the next focusable element in the document
  1357. // and pass the focus onto that. That would allow the user to continue on the form as expected...
  1358. if (!$.browser.msie)
  1359. {
  1360. $(c.ele).trigger('focus', [$.dpConst.DP_INTERNAL_FOCUS]);
  1361. }
  1362. }
  1363. }
  1364. );
  1365. if (c.isSelected(d)) {
  1366. $td.addClass('selected');
  1367. if (c.settings.selectWeek)
  1368. {
  1369. $td.parent().addClass('selectedWeek');
  1370. }
  1371. } else if (c.selectMultiple && c.numSelected == c.numSelectable) {
  1372. $td.addClass('unselectable');
  1373. }
  1374. },
  1375. _applyRenderCallbacks : function()
  1376. {
  1377. var c = this;
  1378. $('td', this.context).each(
  1379. function()
  1380. {
  1381. for (var i=0; i<c.renderCallback.length; i++) {
  1382. $td = $(this);
  1383. c.renderCallback[i].apply(this, [$td, Date.fromString($td.data('datePickerDate')), c.displayedMonth, c.displayedYear]);
  1384. }
  1385. }
  1386. );
  1387. return;
  1388. },
  1389. // ele is the clicked button - only proceed if it doesn't have the class disabled...
  1390. // m and y are -1, 0 or 1 depending which direction we want to go in...
  1391. _displayNewMonth : function(ele, m, y)
  1392. {
  1393. if (!$(ele).is('.disabled')) {
  1394. this.setDisplayedMonth(this.displayedMonth + m, this.displayedYear + y, true);
  1395. }
  1396. ele.blur();
  1397. return false;
  1398. },
  1399. _rerenderCalendar : function()
  1400. {
  1401. this._clearCalendar();
  1402. this._renderCalendar();
  1403. },
  1404. _renderCalendar : function()
  1405. {
  1406. // set the title...
  1407. $('h2', this.context).html((new Date(this.displayedYear, this.displayedMonth, 1)).asString($.dpText.HEADER_FORMAT));
  1408. // render the calendar...
  1409. $('.dp-calendar', this.context).renderCalendar(
  1410. $.extend(
  1411. {},
  1412. this.settings,
  1413. {
  1414. month : this.displayedMonth,
  1415. year : this.displayedYear,
  1416. renderCallback : this.cellRender,
  1417. dpController : this,
  1418. hoverClass : this.hoverClass
  1419. })
  1420. );
  1421. // update the status of the control buttons and disable dates before startDate or after endDate...
  1422. // TODO: When should the year buttons be disabled? When you can't go forward a whole year from where you are or is that annoying?
  1423. if (this.displayedYear == this.startDate.getFullYear() && this.displayedMonth == this.startDate.getMonth()) {
  1424. $('.dp-nav-prev-year', this.context).addClass('disabled');
  1425. $('.dp-nav-prev-month', this.context).addClass('disabled');
  1426. $('.dp-calendar td.other-month', this.context).each(
  1427. function()
  1428. {
  1429. var $this = $(this);
  1430. if (Number($this.text()) > 20) {
  1431. $this.addClass('disabled');
  1432. }
  1433. }
  1434. );
  1435. var d = this.startDate.getDate();
  1436. $('.dp-calendar td.current-month', this.context).each(
  1437. function()
  1438. {
  1439. var $this = $(this);
  1440. if (Number($this.text()) < d) {
  1441. $this.addClass('disabled');
  1442. }
  1443. }
  1444. );
  1445. } else {
  1446. $('.dp-nav-prev-year', this.context).removeClass('disabled');
  1447. $('.dp-nav-prev-month', this.context).removeClass('disabled');
  1448. var d = this.startDate.getDate();
  1449. if (d > 20) {
  1450. // check if the startDate is last month as we might need to add some disabled classes...
  1451. var st = this.startDate.getTime();
  1452. var sd = new Date(st);
  1453. sd.addMonths(1);
  1454. if (this.displayedYear == sd.getFullYear() && this.displayedMonth == sd.getMonth()) {
  1455. $('.dp-calendar td.other-month', this.context).each(
  1456. function()
  1457. {
  1458. var $this = $(this);
  1459. if (Date.fromString($this.data('datePickerDate')).getTime() < st) {
  1460. $this.addClass('disabled');
  1461. }
  1462. }
  1463. );
  1464. }
  1465. }
  1466. }
  1467. if (this.displayedYear == this.endDate.getFullYear() && this.displayedMonth == this.endDate.getMonth()) {
  1468. $('.dp-nav-next-year', this.context).addClass('disabled');
  1469. $('.dp-nav-next-month', this.context).addClass('disabled');
  1470. $('.dp-calendar td.other-month', this.context).each(
  1471. function()
  1472. {
  1473. var $this = $(this);
  1474. if (Number($this.text()) < 14) {
  1475. $this.addClass('disabled');
  1476. }
  1477. }
  1478. );
  1479. var d = this.endDate.getDate();
  1480. $('.dp-calendar td.current-month', this.context).each(
  1481. function()
  1482. {
  1483. var $this = $(this);
  1484. if (Number($this.text()) > d) {
  1485. $this.addClass('disabled');
  1486. }
  1487. }
  1488. );
  1489. } else {
  1490. $('.dp-nav-next-year', this.context).removeClass('disabled');
  1491. $('.dp-nav-next-month', this.context).removeClass('disabled');
  1492. var d = this.endDate.getDate();
  1493. if (d < 13) {
  1494. // check if the endDate is next month as we might need to add some disabled classes...
  1495. var ed = new Date(this.endDate.getTime());
  1496. ed.addMonths(-1);
  1497. if (this.displayedYear == ed.getFullYear() && this.displayedMonth == ed.getMonth()) {
  1498. $('.dp-calendar td.other-month', this.context).each(
  1499. function()
  1500. {
  1501. var $this = $(this);
  1502. if (Number($this.text()) > d) {
  1503. $this.addClass('disabled');
  1504. }
  1505. }
  1506. );
  1507. }
  1508. }
  1509. }
  1510. this._applyRenderCallbacks();
  1511. },
  1512. _closeCalendar : function(programatic, ele)
  1513. {
  1514. if (!ele || ele == this.ele)
  1515. {
  1516. $(document).unbind('mousedown.datepicker');
  1517. $(document).unbind('keydown.datepicker');
  1518. this._clearCalendar();
  1519. $('#dp-popup a').unbind();
  1520. $('#dp-popup').empty().remove();
  1521. if (!programatic) {
  1522. $(this.ele).trigger('dpClosed', [this.getSelected()]);
  1523. }
  1524. }
  1525. },
  1526. // empties the current dp-calendar div and makes sure that all events are unbound
  1527. // and expandos removed to avoid memory leaks...
  1528. _clearCalendar : function()
  1529. {
  1530. // TODO.
  1531. $('.dp-calendar td', this.context).unbind();
  1532. $('.dp-calendar', this.context).empty();
  1533. }
  1534. }
  1535. );
  1536. // static constants
  1537. $.dpConst = {
  1538. SHOW_HEADER_NONE : 0,
  1539. SHOW_HEADER_SHORT : 1,
  1540. SHOW_HEADER_LONG : 2,
  1541. POS_TOP : 0,
  1542. POS_BOTTOM : 1,
  1543. POS_LEFT : 0,
  1544. POS_RIGHT : 1,
  1545. DP_INTERNAL_FOCUS : 'dpInternalFocusTrigger'
  1546. };
  1547. // localisable text
  1548. $.dpText = {
  1549. TEXT_PREV_YEAR : 'Previous year',
  1550. TEXT_PREV_MONTH : 'Previous month',
  1551. TEXT_NEXT_YEAR : 'Next year',
  1552. TEXT_NEXT_MONTH : 'Next month',
  1553. TEXT_CLOSE : 'Close',
  1554. TEXT_CHOOSE_DATE : 'Choose date',
  1555. HEADER_FORMAT : 'mmmm yyyy'
  1556. };
  1557. // version
  1558. $.dpVersion = '$Id: jquery.datePicker.js,v 1.2 2012/12/15 05:36:04 ghp Exp $';
  1559. $.fn.datePicker.defaults = {
  1560. month : undefined,
  1561. year : undefined,
  1562. showHeader : $.dpConst.SHOW_HEADER_SHORT,
  1563. startDate : undefined,
  1564. endDate : undefined,
  1565. inline : false,
  1566. renderCallback : null,
  1567. createButton : true,
  1568. showYearNavigation : true,
  1569. closeOnSelect : true,
  1570. displayClose : false,
  1571. selectMultiple : false,
  1572. numSelectable : Number.MAX_VALUE,
  1573. clickInput : false,
  1574. rememberViewedMonth : true,
  1575. selectWeek : false,
  1576. verticalPosition : $.dpConst.POS_TOP,
  1577. horizontalPosition : $.dpConst.POS_LEFT,
  1578. verticalOffset : 0,
  1579. horizontalOffset : 0,
  1580. hoverClass : 'dp-hover'
  1581. };
  1582. function _getController(ele)
  1583. {
  1584. if (ele._dpId) return $.event._dpCache[ele._dpId];
  1585. return false;
  1586. };
  1587. // make it so that no error is thrown if bgIframe plugin isn't included (allows you to use conditional
  1588. // comments to only include bgIframe where it is needed in IE without breaking this plugin).
  1589. if ($.fn.bgIframe == undefined) {
  1590. $.fn.bgIframe = function() {return this; };
  1591. };
  1592. // clean-up
  1593. $(window)
  1594. .bind('unload', function() {
  1595. var els = $.event._dpCache || [];
  1596. for (var i in els) {
  1597. $(els[i].ele)._dpDestroy();
  1598. }
  1599. });
  1600. })(jQuery);