You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

336 lines
12 KiB

  1. /*
  2. Copyright (c) 2009 Open Lab
  3. Written by Roberto Bicchierai http://roberto.open-lab.com
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. jQuery.fn.dateField = function(options) {
  22. //console.debug("dateField",options);
  23. //check if the input field is passed correctly
  24. if (!options.inputField){
  25. console.error("You must supply an input field");
  26. return false;
  27. }
  28. // -------------------------- start default option values --------------------------
  29. if (typeof(options.firstDayOfWeek) == "undefined")
  30. options.firstDayOfWeek=Date.firstDayOfWeek;
  31. if (typeof(options.useWheel) == "undefined")
  32. options.useWheel=true;
  33. if (typeof(options.dateFormat) == "undefined")
  34. options.dateFormat=Date.defaultFormat;
  35. if (typeof(options.todayLabel) == "undefined")
  36. options.todayLabel=Date.today;
  37. /* optional
  38. options.notBeforeMillis //disable buttons if before millis
  39. options.notAfterMillis //disable buttons if after millis
  40. options.width // imposta una larghezza al calendario
  41. options.height
  42. options.showToday // show "today" on the year or month bar
  43. options.centerOnScreen //se true centra invece che usa nearBestPosition
  44. options.useYears:0 // se >0 non disegna prev-next ma n anni prima e n anni dopo quello corrente
  45. options.useMonths:0 // se >0 non disegna prev-next ma n mesi prima e n mesi dopo quello corrente
  46. */
  47. // -------------------------- end default option values --------------------------
  48. // ------------------ start
  49. if(options.inputField.is("[readonly]") && !options.inputField.is(".noFocus") || options.inputField.is("[disabled]"))
  50. return;
  51. var calendar = {currentDate: new Date()};
  52. calendar.options = options;
  53. //build the calendar on the first element in the set of matched elements.
  54. var theOpener = this.eq(0);
  55. var theDiv=$("<div>").addClass("calBox");
  56. if(options.width)
  57. theDiv.css("width",options.width);
  58. if(options.height)
  59. theDiv.css("height",options.height);
  60. //create calendar elements elements
  61. var divNavBar = $("<div>").addClass("calNav");
  62. var divDays = $("<div>").addClass("calDay");
  63. divDays.addClass("calFullMonth");
  64. theDiv.append(divNavBar).append(divDays);
  65. if (options.isSearchField){
  66. var divShortcuts=$("<div>").addClass("shortCuts").html("<span title='last quarter'>LQ</span> <span title='last month'>LM</span> <span title='this month'>M</span> <span title='last week'>LW</span> <span title='this week'>W</span> <span title='yesterday'>Y</span> <span title='today'>T</span><span title='tomorrow'>TO</span><span title='next week'>NW</span> <span title='next month'>NM</span> <span title='this quarter'>Q</span> <span title='next quarter'>NQ</span>");
  67. divShortcuts.click(function(ev){
  68. var el=$(ev.target);
  69. if(el.is("span")){
  70. if (!options.isSearchField)
  71. options.inputField.val(Date.parseString(el.text().trim(),options.dateFormat,true).format(options.dateFormat));
  72. else
  73. options.inputField.val(el.text().trim());
  74. calendar.closeCalendar()
  75. }
  76. });
  77. theDiv.append(divShortcuts);
  78. }
  79. //mobile management
  80. if ($("body").is(".mobile")){
  81. enableComponentOverlay(options.inputField,theDiv);
  82. }
  83. $("body").append(theDiv);
  84. if (options.centerOnScreen){
  85. theDiv.oneTime(10,"ce",function(){$(this).centerOnScreen()});
  86. } else {
  87. nearBestPosition(theOpener,theDiv);
  88. }
  89. theDiv.css("z-index",10000);
  90. //register for click outside. Delayed to avoid it run immediately
  91. $("body").oneTime(100, "regclibodcal", function() {
  92. $("body").bind("click.dateField", function() {
  93. calendar.closeCalendar();
  94. });
  95. });
  96. calendar.drawCalendar = function(date) {
  97. calendar.currentDate = date;
  98. //console.debug("drawCalendar",date);
  99. var fillNavBar = function(date) {
  100. //console.debug("fillNavBar",date);
  101. var today = new Date();//today
  102. divNavBar.empty();
  103. var showToday = options.showToday;
  104. //use the classic prev next bar
  105. if (!options.useYears && !options.useMonths) {
  106. var t = new Date(date.getTime());
  107. t.setDate(1);
  108. t.setMonth(t.getMonth() - 1);
  109. var spanPrev = $("<span>").addClass("calElement noCallback prev").attr("millis", t.getTime());
  110. var spanToday = $("<span>").addClass("calElement noCallback goToday").attr("millis", new Date().getTime()).attr("title", "today");
  111. t.setMonth(t.getMonth() + 1);
  112. var spanMonth = $("<span>").html(t.format("MMMM yyyy"));
  113. t.setMonth(t.getMonth() + 1);
  114. var spanNext = $("<span>").addClass("calElement noCallback next").attr("millis", t.getTime());
  115. divNavBar.append(spanPrev, spanToday, spanMonth, spanNext);
  116. // use the year month bar
  117. } else {
  118. if (options.useYears>0){
  119. options.useMonths=options.useMonths||1; //if shows years -> shows also months
  120. t = new Date(date.getTime());
  121. var yB= $("<div class='calYear'>");
  122. var w=100/(2*options.useYears+1+(showToday?1:0));
  123. t.setFullYear(t.getFullYear()-options.useYears);
  124. if(showToday){
  125. var s = $("<span>").addClass("calElement noCallback goToday").attr("millis", today.getTime()).append(options.todayLabel).css("width",w+"%");
  126. showToday=false;
  127. yB.append(s);
  128. }
  129. for (var i=-options.useYears;i<=options.useYears;i++){
  130. var s = $("<span>").addClass("calElement noCallback").attr("millis", t.getTime()).append(t.getFullYear()).css("width",w+"%");
  131. if (today.getFullYear()== t.getFullYear()) //current year
  132. s.addClass("today");
  133. if (i==0) //selected year
  134. s.addClass("selected");
  135. yB.append(s);
  136. t.setFullYear(t.getFullYear()+1);
  137. }
  138. divNavBar.append(yB);
  139. }
  140. if (options.useMonths>0){
  141. t = new Date(date.getTime());
  142. t.setDate(1);
  143. var w=100/(2*options.useMonths+1+(showToday?1:0));
  144. t.setMonth(t.getMonth()-options.useMonths);
  145. var yB= $("<div class='calMonth'>");
  146. if(showToday){
  147. var s = $("<span>").addClass("calElement noCallback goToday").attr("millis", today.getTime()).append(options.todayLabel).css("width",w+"%");
  148. yB.append(s);
  149. }
  150. for (var i=-options.useMonths;i<=options.useMonths;i++){
  151. var s = $("<span>").addClass("calElement noCallback").attr("millis", t.getTime()).append(t.format("MMM")).css("width",w+"%");
  152. if (today.getFullYear()== t.getFullYear() && today.getMonth()== t.getMonth()) //current year
  153. s.addClass("today");
  154. if (i==0) //selected month
  155. s.addClass("selected");
  156. yB.append(s);
  157. t.setMonth(t.getMonth()+1);
  158. }
  159. divNavBar.append(yB);
  160. }
  161. }
  162. };
  163. var fillDaysFullMonth = function(date) {
  164. divDays.empty();
  165. var today = new Date();//today
  166. var w = 100/7;
  167. // draw day headers
  168. var d = new Date(date);
  169. d.setFirstDayOfThisWeek(options.firstDayOfWeek);
  170. for (var i = 0; i < 7; i++) {
  171. var span = $("<span>").addClass("calDayHeader").attr("day", d.getDay());
  172. if (d.isHoliday())
  173. span.addClass("holy");
  174. span.css("width",w+"%");
  175. span.html(Date.dayAbbreviations[d.getDay()]);
  176. //call the dayHeaderRenderer
  177. if (typeof(options.dayHeaderRenderer) == "function")
  178. options.dayHeaderRenderer(span,d.getDay());
  179. divDays.append(span);
  180. d.setDate(d.getDate()+1);
  181. }
  182. //draw cells
  183. d = new Date(date);
  184. d.setDate(1); // set day to start of month
  185. d.setFirstDayOfThisWeek(options.firstDayOfWeek);//go to first day of week
  186. var i=0;
  187. while ((d.getMonth()<=date.getMonth() && d.getFullYear()<=date.getFullYear()) || d.getFullYear()<date.getFullYear() || (i%7!=0)) {
  188. var span = $("<span>").addClass("calElement day").attr("millis", d.getTime());
  189. span.html("<span class=dayNumber>" + d.getDate() + "</span>").css("width",w+"%");
  190. if (d.getYear() == today.getYear() && d.getMonth() == today.getMonth() && d.getDate() == today.getDate())
  191. span.addClass("today");
  192. if (d.getYear() == date.getYear() && d.getMonth() == date.getMonth() && d.getDate() == date.getDate())
  193. span.addClass("selected");
  194. if (d.isHoliday())
  195. span.addClass("holy");
  196. if(d.getMonth()!=date.getMonth())
  197. span.addClass("calOutOfScope");
  198. //call the dayRenderer
  199. if (typeof(options.dayRenderer) == "function")
  200. options.dayRenderer(span,d);
  201. divDays.append(span);
  202. d.setDate(d.getDate()+1);
  203. i++;
  204. }
  205. };
  206. fillNavBar(date);
  207. fillDaysFullMonth(date);
  208. //disable all buttons out of validity period
  209. if (options.notBeforeMillis ||options.notAfterMillis) {
  210. var notBefore = options.notBeforeMillis ? options.notBeforeMillis : Number.MIN_VALUE;
  211. var notAfter = options.notAfterMillis ? options.notAfterMillis : Number.MAX_VALUE;
  212. divDays.find(".calElement[millis]").each(function(){
  213. var el=$(this);
  214. var m=parseInt(el.attr("millis"));
  215. if (m>notAfter || m<notBefore)
  216. el.addClass("disabled");
  217. })
  218. }
  219. };
  220. calendar.closeCalendar=function(){
  221. //mobile management
  222. if ($("body").is(".mobile")){
  223. disableComponentOverlay();
  224. }
  225. theDiv.remove();
  226. $("body").unbind("click.dateField");
  227. };
  228. theDiv.click(function(ev) {
  229. var el = $(ev.target).closest(".calElement");
  230. if (el.length > 0) {
  231. var millis = parseInt(el.attr("millis"));
  232. var date = new Date(millis);
  233. if (el.is(".disabled")) {
  234. ev.stopPropagation();
  235. return;
  236. }
  237. if (el.hasClass("day")) {
  238. calendar.closeCalendar();
  239. if (!el.is(".noCallback")) {
  240. options.inputField.val(date.format(options.dateFormat)).attr("millis", date.getTime()).focus();
  241. if (typeof(options.callback) == "function")
  242. options.callback.apply(options.inputField,[date]); // in callBack you can use "this" that refers to the input
  243. }
  244. } else {
  245. calendar.drawCalendar(date);
  246. }
  247. }
  248. ev.stopPropagation();
  249. });
  250. //if mousewheel
  251. if ($.event.special.mousewheel && options.useWheel) {
  252. divDays.mousewheel(function(event, delta) {
  253. var d = new Date(calendar.currentDate.getTime());
  254. d.setMonth(d.getMonth() + delta);
  255. calendar.drawCalendar(d);
  256. return false;
  257. });
  258. }
  259. // start calendar to the date in the input
  260. var dateStr=options.inputField.val();
  261. if (!dateStr || !Date.isValid(dateStr,options.dateFormat,true)){
  262. calendar.drawCalendar(new Date());
  263. } else {
  264. var date = Date.parseString(dateStr,options.dateFormat,true);
  265. var newDateStr=date.format(options.dateFormat);
  266. //set date string formatted if not equals
  267. if (!options.isSearchField) {
  268. options.inputField.attr("millis", date.getTime());
  269. if (dateStr != newDateStr)
  270. options.inputField.val(newDateStr);
  271. }
  272. calendar.drawCalendar(date);
  273. }
  274. return calendar;
  275. };