dcalendar.picker.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* -- DO NOT REMOVE --
  2. * jQuery DCalendar 1.1 and DCalendar Picker 1.3 plugin
  3. *
  4. * Author: Dionlee Uy
  5. * Email: dionleeuy@gmail.com
  6. *
  7. * Date: Sat Mar 2 2013
  8. *
  9. * @requires jQuery
  10. * -- DO NOT REMOVE --
  11. */
  12. if (typeof jQuery === 'undefined') { throw new Error('DCalendar.Picker: This plugin requires jQuery'); }
  13. +function ($) {
  14. Date.prototype.getDays = function() { return new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate(); };
  15. var months = ['January','February','March','April','May','June','July','August','September','October','November','December'],
  16. short_months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
  17. daysofweek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
  18. DCalendar = function(elem, options) {
  19. this.calendar = $(elem);
  20. this.today = new Date(); //system date
  21. //current selected date, default is today if no value given
  22. if(this.calendar.prev().val() === '') {
  23. this.date = new Date();
  24. } else {
  25. var dateObj = this.reformatDate(this.calendar.prev().val());
  26. this.date = isNaN(parseInt(dateObj.m)) ? new Date(dateObj.m + " " + dateObj.d + ", " + dateObj.y) : new Date(dateObj.y, dateObj.m - 1, dateObj.d);
  27. }
  28. this.viewMode = 'days';
  29. this.options = options;
  30. this.selected = (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" + this.date.getFullYear();
  31. this.minDate = this.calendar.prev().data('mindate');
  32. this.maxDate = this.calendar.prev().data('maxdate');
  33. if(options.mode === 'calendar')
  34. this.tHead = $('<thead><tr><th id="prev">&lsaquo;</th><th colspan="5" id="currM"></th><th id="next">&rsaquo;</th></tr><tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr></thead>');
  35. else if (options.mode === 'datepicker')
  36. this.tHead = $('<thead><tr><th id="prev">&lsaquo;</th><th colspan="5" id="currM"></th><th id="next">&rsaquo;</th></tr><tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr></thead>');
  37. this.tHead.find('#currM').text(months[this.today.getMonth()] +" " + this.today.getFullYear());
  38. this.calendar.prepend(this.tHead);
  39. var that = this;
  40. this.calendar.on('click', '#next', function() { initCreate('next'); })
  41. .on('click', '#prev', function() { initCreate('prev'); })
  42. .on('click', '#today', function() {
  43. that.viewMode = 'days';
  44. var curr = new Date(that.date),
  45. sys = new Date(that.today);
  46. if(curr.toString() != sys.toString()) { that.date = sys; }
  47. that.create('days');
  48. }).on('click', '.date, .pMDate, .nMDate', function() {
  49. var isPrev = $(this).hasClass('pMDate'),
  50. isNext = $(this).hasClass('nMDate'),
  51. sdate = $(this).text(),
  52. cmonth = that.date.getMonth(),
  53. cyear = that.date.getFullYear(),
  54. min = that.minDate === "today" ? new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()) : new Date(that.minDate),
  55. max = that.maxDate === "today" ? new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()) : new Date(that.maxDate);
  56. /* Calculate year */
  57. if(isPrev) { cyear = (cmonth === 0 ? cyear - 1 : cyear); }
  58. else if(isNext) { cyear = (cmonth + 2 === 13 ? cyear + 1 : cyear); }
  59. /* Calculate month */
  60. if(isPrev) { cmonth = (cmonth === 0 ? '12' : cmonth); }
  61. else if (isNext) { cmonth = (cmonth + 2 === 13 ? '1' : cmonth + 2); }
  62. else { cmonth = cmonth + 1; }
  63. // Selected date
  64. var selected = new Date(cyear, cmonth - 1, sdate);
  65. // console.log(cmonth);
  66. // console.log(selected);
  67. if ((that.minDate && selected < min) || (that.maxDate && selected > max)) return;
  68. that.selected = cmonth + '/' + sdate + '/' + cyear;
  69. if(that.options.mode === 'datepicker') {
  70. that.calendar.find('td').removeClass('selected');
  71. $(this).addClass('selected');
  72. }
  73. that.selectDate();
  74. return true;
  75. }).on('click', '#currM', function(){
  76. that.viewMode = 'months';
  77. that.create(that.viewMode);
  78. }).on('click', '.month', function(e){
  79. that.viewMode = 'days';
  80. var curr = new Date(that.date), y = that.calendar.find('#currM').text();
  81. curr.setMonth($(e.currentTarget).attr('num'));
  82. that.date = curr;
  83. that.create(that.viewMode);
  84. });
  85. function initCreate(o){
  86. var curr = new Date(that.date),
  87. currMonth = curr.getMonth(),
  88. currYear = curr.getFullYear();
  89. curr.setDate(1);
  90. if(that.viewMode === 'days') {
  91. curr.setMonth(currMonth + (o === 'next' ? 1 : -1));
  92. } else {
  93. curr.setFullYear(currYear + (o === 'next' ? 1 : - 1));
  94. }
  95. that.date = curr;
  96. that.create(that.viewMode);
  97. }
  98. this.create(this.viewMode);
  99. }
  100. DCalendar.prototype = {
  101. constructor : DCalendar,
  102. setDate : function() {
  103. var that = this,
  104. dateObj = that.reformatDate(that.calendar.prev().val()),
  105. value = isNaN(parseInt(dateObj.m)) ? new Date(dateObj.m + " " + dateObj.d + ", " + dateObj.y) : new Date(dateObj.y, dateObj.m - 1, dateObj.d);
  106. that.selected = (value.getMonth() + 1) + "/" + value.getDate() + "/" + value.getFullYear();
  107. that.selectDate();
  108. that.date = value;
  109. that.create(that.viewMode);
  110. },
  111. selectDate : function () {
  112. var that = this,
  113. newDate = that.formatDate(that.options.format),
  114. e = $.Event('selectdate', {date: newDate});
  115. that.calendar.trigger(e);
  116. },
  117. reformatDate : function (date) {
  118. var that = this,
  119. format = that.options.format;
  120. return {
  121. m: date.substring(format.indexOf('m'), format.lastIndexOf('m') + 1),
  122. d: date.substring(format.indexOf('d'), format.lastIndexOf('d') + 1),
  123. y: date.substring(format.indexOf('y'), format.lastIndexOf('y') + 1)
  124. };
  125. },
  126. formatDate : function (format) {
  127. var that = this;
  128. var d = new Date(that.selected), day = d.getDate(), m = d.getMonth(), y = d.getFullYear();
  129. return format.replace(/(yyyy|yy|mmmm|mmm|mm|m|dd|d)/gi, function (e) {
  130. switch(e.toLowerCase()){
  131. case 'd': return day;
  132. case 'dd': return (day < 10 ? "0"+day: day);
  133. case 'm': return m+1;
  134. case 'mm': return (m+1 < 10 ? "0"+(m+1): (m+1));
  135. case 'mmm': return short_months[m];
  136. case 'mmmm': return months[m];
  137. case 'yy': return y.toString().substr(2,2);
  138. case 'yyyy': return y;
  139. }
  140. });
  141. },
  142. create : function(mode){
  143. var that = this, cal = [],
  144. tBody = $('<tbody></tbody>'),
  145. d = new Date(that.date.getFullYear(), that.date.getMonth(), that.date.getDate()),
  146. days = that.date.getDays(),
  147. day = 1, nStartDate = 1,
  148. selDate = that.selected.split('/'),
  149. selected = new Date(selDate[2], selDate[0] -1, selDate[1]),
  150. today = new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()),
  151. min = that.minDate === "today" ? today : new Date(that.minDate),
  152. max = that.maxDate === "today" ? today : new Date(that.maxDate);
  153. that.calendar.empty();
  154. if(mode === "days") {
  155. if(that.options.mode === 'calendar') {
  156. that.tHead = $('<thead><tr><th id="prev">&lsaquo;</th><th colspan="5" id="currM"></th><th id="next">&rsaquo;</th></tr><tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr></thead>');
  157. } else if (that.options.mode === 'datepicker') {
  158. that.tHead = $('<thead><tr><th id="prev">&lsaquo;</th><th colspan="5" id="currM"></th><th id="next">&rsaquo;</th></tr><tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr></thead>');
  159. }
  160. that.tHead.find('#currM').text(months[that.date.getMonth()] +" " + that.date.getFullYear());
  161. that.calendar.append(that.tHead);
  162. for(var i = 1; i <= 6; i++){
  163. var temp = [$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>')];
  164. while(day <= days){
  165. d.setDate(day);
  166. var dayOfWeek = d.getDay();
  167. if(d.getTime() == today.getTime()) temp[dayOfWeek].attr('id', 'currDay');
  168. if ((that.minDate && d < min) || (that.maxDate && d > max)) temp[dayOfWeek].addClass('disabled');
  169. if(that.options.mode === 'datepicker' && d.getTime() == selected.getTime()) temp[dayOfWeek].addClass('selected');
  170. if(i === 1 && dayOfWeek === 0) break;
  171. else if(dayOfWeek < 6) temp[dayOfWeek].html('<span>'+(day++)+'</span>').addClass('date');
  172. else {
  173. temp[dayOfWeek].html('<span>'+(day++)+'</span>').addClass('date');
  174. break;
  175. }
  176. }
  177. /* For days of previous and next month */
  178. if (i === 1 || i > 4) {
  179. // First week
  180. if (i === 1) {
  181. var p = new Date(that.date.getFullYear(), that.date.getMonth() - 1, 1),
  182. pMonth = p.getMonth(), pDays = p.getDays();
  183. for (var a = 6; a >= 0; a--) {
  184. if (temp[a].text() === ''){
  185. p.setDate(pDays);
  186. temp[a].html('<span>' + (pDays--) + '</span>').addClass('pMDate');
  187. if ((that.minDate && p < min) || (that.maxDate && p > max)) temp[a].addClass('disabled');
  188. if (that.options.mode === 'datepicker' && p.getTime() == selected.getTime()) temp[a].addClass('selected');
  189. }
  190. }
  191. }
  192. // Last week
  193. else if (i > 4) {
  194. var nextMonth = new Date(that.date.getFullYear(), that.date.getMonth() + 1, 1);
  195. for (var a = 0; a <= 6; a++) {
  196. if (temp[a].text() === ''){
  197. nextMonth.setDate(nStartDate);
  198. temp[a].html('<span>' + (nStartDate++) + '</span>').addClass('nMDate');
  199. if ((that.minDate && nextMonth < min) || (that.maxDate && nextMonth > max)) temp[a].addClass('disabled');
  200. if (that.options.mode === 'datepicker' && nextMonth.getTime() == selected.getTime()) temp[a].addClass('selected');
  201. }
  202. }
  203. }
  204. }
  205. cal.push(temp);
  206. }
  207. $.each(cal, function(i, v){
  208. var row = $('<tr></tr>'), l = v.length;
  209. for(var i = 0; i < l; i++) { row.append(v[i]); }
  210. tBody.append(row);
  211. });
  212. var sysDate = "Today: " + daysofweek[that.today.getDay()] + ", " + months[that.today.getMonth()] + " " + that.today.getDate() + ", " + that.today.getFullYear();
  213. tBody.append('<tr><td colspan="7" id="today">' + sysDate + '</td></tr>').appendTo(that.calendar);
  214. } else {
  215. this.tHead = $('<thead><tr><th id="prev">&lsaquo;</th><th colspan="2" id="currM"></th><th id="next">&rsaquo;</th></tr>');
  216. that.tHead.find('#currM').text(that.date.getFullYear());
  217. that.tHead.appendTo(that.calendar);
  218. var currI = 0;
  219. for (var i = 0; i < 3; i++) {
  220. var row = $('<tr></tr>');
  221. for (var x = 0; x < 4; x++) {
  222. var col = $('<td align="center"></td>');
  223. var m = $('<span class="month" num="' + currI + '">' + short_months[currI] + '</span>');
  224. col.append(m).appendTo(row);
  225. currI++;
  226. }
  227. tBody.append(row);
  228. }
  229. var sysDate = "Today: " + daysofweek[that.today.getDay()] + ", "+ months[that.today.getMonth()] + " " + that.today.getDate() + ", " + that.today.getFullYear();
  230. tBody.append('<tr><td colspan="4" id="today">' + sysDate + '</td></tr>').appendTo(that.calendar);
  231. }
  232. }
  233. }
  234. /* DEFINITION FOR DCALENDAR */
  235. $.fn.dcalendar = function(opts){
  236. return $(this).each(function(index, elem){
  237. var that = this;
  238. var $this = $(that),
  239. data = $(that).data('dcalendar'),
  240. options = $.extend({}, $.fn.dcalendar.defaults, $this.data(), typeof opts === 'object' && opts);
  241. if(!data){
  242. $this.data('dcalendar', (data = new DCalendar(this, options)));
  243. }
  244. if(typeof opts === 'string') data[opts]();
  245. });
  246. }
  247. $.fn.dcalendar.defaults = {
  248. mode : 'calendar',
  249. format: 'mm/dd/yyyy',
  250. };
  251. $.fn.dcalendar.Constructor = DCalendar;
  252. /* DEFINITION FOR DCALENDAR PICKER */
  253. $.fn.dcalendarpicker = function(opts){
  254. return $(this).each(function(){
  255. var that = $(this),
  256. hovered = false, selectedDate = false,
  257. cal = null;
  258. if(typeof opts === 'string') {
  259. var data = that.next('.calendar').data('dcalendar');
  260. data[opts]();
  261. } else {
  262. cal = $('<table class="calendar"></table>');
  263. that.wrap($('<div class="datepicker" style="display:inline-block;position:relative;"></div>'));
  264. cal.css({
  265. position:'absolute',
  266. left:0, display:'none',
  267. 'box-shadow':'0 4px 6px 1px rgba(0, 0, 0, 0.14)',
  268. width:'230px',
  269. }).appendTo(that.parent());
  270. if(opts){
  271. opts.mode = 'datepicker';
  272. cal.dcalendar(opts);
  273. } else{
  274. cal.dcalendar({mode: 'datepicker'});
  275. }
  276. cal.hover(function(){
  277. hovered = true;
  278. }, function(){
  279. hovered = false;
  280. }).on('click', function(){
  281. if(!selectedDate)
  282. that.focus();
  283. else {
  284. selectedDate = false;
  285. $(this).hide();
  286. }
  287. }).on('selectdate', function(e){
  288. that.val(e.date).trigger('onchange');
  289. that.trigger($.Event('dateselected', {date: e.date, elem: that}));
  290. selectedDate = true;
  291. });
  292. that.on('keydown', function(e){ if(e.which) return false; })
  293. .on('focus', function(){
  294. $('.datepicker').find('.calendar').not(cal).hide();
  295. cal.show();
  296. })
  297. .on('blur', function(){ if(!hovered) cal.hide(); });
  298. }
  299. });
  300. }
  301. }(jQuery);