Açıklama Yok

monthly.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Monthly 2.0.7 by Kevin Thornbloom is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
  3. */
  4. (function($) {
  5. $.fn.extend({
  6. monthly: function(options) {
  7. // These are overridden by options declared in footer
  8. var defaults = {
  9. weekStart: 'Sun',
  10. mode: '',
  11. xmlUrl: '',
  12. target: '',
  13. eventList: true,
  14. maxWidth: false,
  15. setWidth: false,
  16. startHidden: false,
  17. showTrigger: '',
  18. stylePast: false,
  19. disablePast: false
  20. }
  21. var options = $.extend(defaults, options),
  22. that = this,
  23. uniqueId = $(this).attr('id'),
  24. d = new Date(),
  25. currentMonth = d.getMonth() + 1,
  26. currentYear = d.getFullYear(),
  27. currentDay = d.getDate(),
  28. monthNames = options.monthNames || ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  29. dayNames = options.dayNames || ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  30. if (options.maxWidth != false){
  31. $('#'+uniqueId).css('maxWidth',options.maxWidth);
  32. }
  33. if (options.setWidth != false){
  34. $('#'+uniqueId).css('width',options.setWidth);
  35. }
  36. if (options.startHidden == true){
  37. $('#'+uniqueId).addClass('monthly-pop').css({
  38. 'position' : 'absolute',
  39. 'display' : 'none'
  40. });
  41. $(document).on('focus', ''+options.showTrigger+'', function (e) {
  42. $('#'+uniqueId).show();
  43. e.preventDefault();
  44. });
  45. $(document).on('click', ''+options.showTrigger+', .monthly-pop', function (e) {
  46. e.stopPropagation();
  47. e.preventDefault();
  48. });
  49. $(document).on('click', function (e) {
  50. $('#'+uniqueId).hide();
  51. });
  52. }
  53. // Add Day Of Week Titles
  54. if (options.weekStart == 'Sun') {
  55. $('#' + uniqueId).append('<div class="monthly-day-title-wrap"><div>'+dayNames[0]+'</div><div>'+dayNames[1]+'</div><div>'+dayNames[2]+'</div><div>'+dayNames[3]+'</div><div>'+dayNames[4]+'</div><div>'+dayNames[5]+'</div><div>'+dayNames[6]+'</div></div><div class="monthly-day-wrap"></div>');
  56. } else if (options.weekStart == 'Mon') {
  57. $('#' + uniqueId).append('<div class="monthly-day-title-wrap"><div>'+dayNames[1]+'</div><div>'+dayNames[2]+'</div><div>'+dayNames[3]+'</div><div>'+dayNames[4]+'</div><div>'+dayNames[5]+'</div><div>'+dayNames[6]+'</div><div>'+dayNames[0]+'</div></div><div class="monthly-day-wrap"></div>');
  58. } else {
  59. console.error('Monthly.js has an incorrect entry for the weekStart variable');
  60. }
  61. // Add Header & event list markup
  62. $('#' + uniqueId).prepend('<div class="monthly-header"><div class="monthly-header-title"></div><a href="#" class="monthly-prev"></a><a href="#" class="monthly-next"></a></div>').append('<div class="monthly-event-list"></div>');
  63. // How many days are in this month?
  64. function daysInMonth(m, y){
  65. return m===2?y&3||!(y%25)&&y&15?28:29:30+(m+(m>>3)&1);
  66. }
  67. // Massive function to build the month
  68. function setMonthly(m, y){
  69. $('#' + uniqueId).data('setMonth', m).data('setYear', y);
  70. // Get number of days
  71. var dayQty = daysInMonth(m, y),
  72. // Get day of the week the first day is
  73. mZeroed = m -1,
  74. firstDay = new Date(y, mZeroed, 1, 0, 0, 0, 0).getDay();
  75. // Remove old days
  76. $('#' + uniqueId + ' .monthly-day, #' + uniqueId + ' .monthly-day-blank').remove();
  77. $('#'+uniqueId+' .monthly-event-list').empty();
  78. $('#'+uniqueId+' .monthly-day-wrap').empty();
  79. // Print out the days
  80. if (options.mode == 'event') {
  81. for(var i = 0; i < dayQty; i++) {
  82. var day = i + 1; // Fix 0 indexed days
  83. var dayNamenum = new Date(y, mZeroed, day, 0, 0, 0, 0).getDay()
  84. $('#' + uniqueId + ' .monthly-day-wrap').append('<a href="#" class="m-d monthly-day monthly-day-event" data-number="'+day+'"><div class="monthly-day-number">'+day+'</div><div class="monthly-indicator-wrap"></div></a>');
  85. $('#' + uniqueId + ' .monthly-event-list').append('<div class="monthly-list-item" id="'+uniqueId+'day'+day+'" data-number="'+day+'"><div class="monthly-event-list-date">'+dayNames[dayNamenum]+'<br>'+day+'</div></div>');
  86. }
  87. } else {
  88. for(var i = 0; i < dayQty; i++) {
  89. // Fix 0 indexed days
  90. var day = i + 1;
  91. // Check if it's a day in the past
  92. if(((day < currentDay && m === currentMonth) || y < currentYear || (m < currentMonth && y == currentYear)) && options.stylePast == true){
  93. $('#' + uniqueId + ' .monthly-day-wrap').append('<a href="#" data-toggle="modal" data-target="#schedule-box" class="m-d monthly-day monthly-day-pick monthly-past-day" data-number="'+day+'"><div class="monthly-day-number">'+day+'</div><div class="monthly-indicator-wrap"></div></a>');
  94. } else {
  95. $('#' + uniqueId + ' .monthly-day-wrap').append('<a href="#" data-toggle="modal" data-target="#schedule-box" class="m-d monthly-day monthly-day-pick" data-number="'+day+'"><div class="monthly-day-number">'+day+'</div><div class="monthly-indicator-wrap"></div></a>');
  96. }
  97. }
  98. }
  99. // Set Today
  100. var setMonth = $('#' + uniqueId).data('setMonth'),
  101. setYear = $('#' + uniqueId).data('setYear');
  102. if (setMonth == currentMonth && setYear == currentYear) {
  103. $('#' + uniqueId + ' *[data-number="'+currentDay+'"]').addClass('monthly-today');
  104. }
  105. // Reset button
  106. if (setMonth == currentMonth && setYear == currentYear) {
  107. $('#' + uniqueId + ' .monthly-header-title').html(monthNames[m - 1] +' '+ y);
  108. } else {
  109. $('#' + uniqueId + ' .monthly-header-title').html(monthNames[m - 1] +' '+ y +'<a href="#" class="monthly-reset" title="Back To This Month"></a> ');
  110. }
  111. // Account for empty days at start
  112. if(options.weekStart == 'Sun' && firstDay != 7) {
  113. for(var i = 0; i < firstDay; i++) {
  114. $('#' + uniqueId + ' .monthly-day-wrap').prepend('<div class="m-d monthly-day-blank"><div class="monthly-day-number"></div></div>');
  115. }
  116. } else if (options.weekStart == 'Mon' && firstDay == 0) {
  117. for(var i = 0; i < 6; i++) {
  118. $('#' + uniqueId + ' .monthly-day-wrap').prepend('<div class="m-d monthly-day-blank" ><div class="monthly-day-number"></div></div>');
  119. }
  120. } else if (options.weekStart == 'Mon' && firstDay != 1) {
  121. for(var i = 0; i < (firstDay - 1); i++) {
  122. $('#' + uniqueId + ' .monthly-day-wrap').prepend('<div class="m-d monthly-day-blank" ><div class="monthly-day-number"></div></div>');
  123. }
  124. }
  125. //Account for empty days at end
  126. var numdays = $('#' + uniqueId + ' .monthly-day').length,
  127. numempty = $('#' + uniqueId + ' .monthly-day-blank').length,
  128. totaldays = numdays + numempty,
  129. roundup = Math.ceil(totaldays/7) * 7,
  130. daysdiff = roundup - totaldays;
  131. if(totaldays % 7 != 0) {
  132. for(var i = 0; i < daysdiff; i++) {
  133. $('#' + uniqueId + ' .monthly-day-wrap').append('<div class="m-d monthly-day-blank"><div class="monthly-day-number"></div></div>');
  134. }
  135. }
  136. // Events
  137. if (options.mode == 'event') {
  138. // Remove previous events
  139. // Add Events
  140. $.get(''+options.xmlUrl+'', function(d){
  141. $(d).find('event').each(function(){
  142. // Year [0] Month [1] Day [2]
  143. var fullstartDate = $(this).find('startdate').text(),
  144. startArr = fullstartDate.split("-"),
  145. startYear = startArr[0],
  146. startMonth = parseInt(startArr[1], 10),
  147. startDay = parseInt(startArr[2], 10),
  148. fullendDate = $(this).find('enddate').text(),
  149. endArr = fullendDate.split("-"),
  150. endYear = endArr[0],
  151. endMonth = parseInt(endArr[1], 10),
  152. endDay = parseInt(endArr[2], 10),
  153. eventURL = $(this).find('url').text(),
  154. eventTitle = $(this).find('name').text(),
  155. eventColor = $(this).find('color').text(),
  156. eventId = $(this).find('id').text(),
  157. startTime = $(this).find('starttime').text(),
  158. startSplit = startTime.split(":");
  159. endTime = $(this).find('endtime').text(),
  160. endSplit = endTime.split(":");
  161. eventLink = '',
  162. startPeriod = 'AM',
  163. endPeriod = 'PM';
  164. /* Convert times to 12 hour & determine AM or PM */
  165. if(parseInt(startSplit[0]) >= 12) {
  166. var startTime = (startSplit[0] - 12)+':'+startSplit[1]+'';
  167. var startPeriod = 'PM'
  168. }
  169. if(parseInt(startTime) == 0) {
  170. var startTime = '12:'+startSplit[1]+'';
  171. }
  172. if(parseInt(endSplit[0]) >= 12) {
  173. var endTime = (endSplit[0] - 12)+':'+endSplit[1]+'';
  174. var endPeriod = 'PM'
  175. }
  176. if(parseInt(endTime) == 0) {
  177. var endTime = '12:'+endSplit[1]+'';
  178. }
  179. if (eventURL){
  180. var eventLink = 'href="'+eventURL+'"';
  181. }
  182. // function to print out list for multi day events
  183. function multidaylist(){
  184. var timeHtml = '';
  185. if (startTime){
  186. var startTimehtml = '<div><div class="monthly-list-time-start">'+startTime+' '+startPeriod+'</div>';
  187. var endTimehtml = '';
  188. if (endTime){
  189. var endTimehtml = '<div class="monthly-list-time-end">'+endTime+' '+endPeriod+'</div>';
  190. }
  191. var timeHtml = startTimehtml + endTimehtml + '</div>';
  192. }
  193. $('#'+uniqueId+' .monthly-list-item[data-number="'+i+'"]').addClass('item-has-event').append('<a href="'+eventURL+'" class="listed-event" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+' '+timeHtml+'</a>');
  194. }
  195. // If event is one day & within month
  196. if (!fullendDate && startMonth == setMonth && startYear == setYear) {
  197. // Add Indicators
  198. $('#'+uniqueId+' *[data-number="'+startDay+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+'</div>');
  199. // Print out event list for single day event
  200. var timeHtml = '';
  201. if (startTime){
  202. var startTimehtml = '<div><div class="monthly-list-time-start">'+startTime+' '+startPeriod+'</div>';
  203. var endTimehtml = '';
  204. if (endTime){
  205. var endTimehtml = '<div class="monthly-list-time-end">'+endTime+' '+endPeriod+'</div>';
  206. }
  207. var timeHtml = startTimehtml + endTimehtml + '</div>';
  208. }
  209. $('#'+uniqueId+' .monthly-list-item[data-number="'+startDay+'"]').addClass('item-has-event').append('<a href="'+eventURL+'" class="listed-event" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+' '+timeHtml+'</a>');
  210. // If event is multi day & within month
  211. } else if (startMonth == setMonth && startYear == setYear && endMonth == setMonth && endYear == setYear){
  212. for(var i = parseInt(startDay); i <= parseInt(endDay); i++) {
  213. // If first day, add title
  214. if (i == parseInt(startDay)) {
  215. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+'</div>');
  216. } else {
  217. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'"></div>');
  218. }
  219. multidaylist();
  220. }
  221. // If event is multi day, starts in prev month, and ends in current month
  222. } else if ((endMonth == setMonth && endYear == setYear) && ((startMonth < setMonth && startYear == setYear) || (startYear < setYear))) {
  223. for(var i = 0; i <= parseInt(endDay); i++) {
  224. // If first day, add title
  225. if (i==1){
  226. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+'</div>');
  227. } else {
  228. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'"></div>');
  229. }
  230. multidaylist();
  231. }
  232. // If event is multi day, starts in this month, but ends in next
  233. } else if ((startMonth == setMonth && startYear == setYear) && ((endMonth > setMonth && endYear == setYear) || (endYear > setYear))){
  234. for(var i = parseInt(startDay); i <= dayQty; i++) {
  235. // If first day, add title
  236. if (i == parseInt(startDay)) {
  237. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+'</div>');
  238. } else {
  239. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'"></div>');
  240. }
  241. multidaylist();
  242. }
  243. // If event is multi day, starts in a prev month, ends in a future month
  244. } else if (((startMonth < setMonth && startYear == setYear) || (startYear < setYear)) && ((endMonth > setMonth && endYear == setYear) || (endYear > setYear))){
  245. for(var i = 0; i <= dayQty; i++) {
  246. // If first day, add title
  247. if (i == 1){
  248. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'">'+eventTitle+'</div>');
  249. } else {
  250. $('#'+uniqueId+' *[data-number="'+i+'"] .monthly-indicator-wrap').append('<div class="monthly-event-indicator" data-eventid="'+ eventId +'" style="background:'+eventColor+'" title="'+eventTitle+'"></div>');
  251. }
  252. multidaylist();
  253. }
  254. }
  255. });
  256. }).fail(function() {
  257. console.error('Monthly.js failed to import '+options.xmlUrl+'. Please check for the correct path & XML syntax.');
  258. });
  259. }
  260. var divs = $("#"+uniqueId+" .m-d");
  261. for(var i = 0; i < divs.length; i+=7) {
  262. divs.slice(i, i+7).wrapAll("<div class='monthly-week'></div>");
  263. }
  264. }
  265. // Set the calendar the first time
  266. setMonthly(currentMonth, currentYear);
  267. // Function to go back to the month view
  268. function viewToggleButton(){
  269. if($('#'+uniqueId+' .monthly-event-list').is(":visible")) {
  270. $('#'+uniqueId+' .monthly-cal').remove();
  271. $('#'+uniqueId+' .monthly-header-title').prepend('<a href="#" class="monthly-cal" title="Back To Month View"><div></div></a>');
  272. }
  273. }
  274. // Advance months
  275. $(document.body).on('click', '#'+uniqueId+' .monthly-next', function (e) {
  276. var setMonth = $('#' + uniqueId).data('setMonth'),
  277. setYear = $('#' + uniqueId).data('setYear');
  278. if (setMonth == 12) {
  279. var newMonth = 1,
  280. newYear = setYear + 1;
  281. setMonthly(newMonth, newYear);
  282. } else {
  283. var newMonth = setMonth + 1,
  284. newYear = setYear;
  285. setMonthly(newMonth, newYear);
  286. }
  287. viewToggleButton();
  288. e.preventDefault();
  289. });
  290. // Go back in months
  291. $(document.body).on('click', '#'+uniqueId+' .monthly-prev', function (e) {
  292. var setMonth = $('#' + uniqueId).data('setMonth'),
  293. setYear = $('#' + uniqueId).data('setYear');
  294. if (setMonth == 1) {
  295. var newMonth = 12,
  296. newYear = setYear - 1;
  297. setMonthly(newMonth, newYear);
  298. } else {
  299. var newMonth = setMonth - 1,
  300. newYear = setYear;
  301. setMonthly(newMonth, newYear);
  302. }
  303. viewToggleButton();
  304. e.preventDefault();
  305. });
  306. // Reset Month
  307. $(document.body).on('click', '#'+uniqueId+' .monthly-reset', function (e) {
  308. setMonthly(currentMonth, currentYear);
  309. viewToggleButton();
  310. e.preventDefault();
  311. e.stopPropagation();
  312. });
  313. // Back to month view
  314. $(document.body).on('click', '#'+uniqueId+' .monthly-cal', function (e) {
  315. $(this).remove();
  316. $('#' + uniqueId+' .monthly-event-list').css('transform','scale(0)').delay('800').hide();
  317. e.preventDefault();
  318. });
  319. // Click A Day
  320. $(document.body).on('click', '#'+uniqueId+' a.monthly-day', function (e) {
  321. // If events, show events list
  322. if(options.mode == 'event' && options.eventList == true) {
  323. var whichDay = $(this).data('number');
  324. $('#' + uniqueId+' .monthly-event-list').show();
  325. $('#' + uniqueId+' .monthly-event-list').css('transform');
  326. $('#' + uniqueId+' .monthly-event-list').css('transform','scale(1)');
  327. $('#'+uniqueId+' .monthly-list-item[data-number="'+whichDay+'"]').show();
  328. var myElement = document.getElementById(uniqueId+'day'+whichDay);
  329. var topPos = myElement.offsetTop;
  330. //document.getElementByClassname('scrolling_div').scrollTop = topPos;
  331. $('#'+uniqueId+' .monthly-event-list').scrollTop(topPos);
  332. viewToggleButton();
  333. // If picker, pick date
  334. } else if (options.mode == 'picker') {
  335. var whichDay = $(this).data('number'),
  336. setMonth = $('#' + uniqueId).data('setMonth'),
  337. setYear = $('#' + uniqueId).data('setYear');
  338. // Should days in the past be disabled?
  339. if($(this).hasClass('monthly-past-day') && options.disablePast == true) {
  340. // If so, don't do anything.
  341. e.preventDefault();
  342. } else {
  343. // Otherwise, select the date ...
  344. $(''+options.target+'').val(setMonth+'/'+whichDay+'/'+setYear);
  345. // ... and then hide the calendar if it started that way
  346. if(options.startHidden == true) {
  347. $('#'+uniqueId).hide();
  348. }
  349. }
  350. }
  351. e.preventDefault();
  352. });
  353. // Clicking an event within the list
  354. $(document.body).on('click', '#'+uniqueId+' .listed-event', function (e) {
  355. var href = $(this).attr('href');
  356. // If there isn't a link, don't go anywhere
  357. if(!href) {
  358. e.preventDefault();
  359. }
  360. });
  361. }
  362. });
  363. })(jQuery);