Advertisement
NikolayPaskulov

Untitled

Feb 13th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1.  
  2. var Calendar = (function () {
  3. var self,
  4. months = ['Jan', 'Feb', 'Marth', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  5. days = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  6. clickEvent,
  7. isDateCalendar = true,
  8. isHide = true,
  9. isAppended = false,
  10. year,
  11. month,
  12. date;
  13.  
  14. // Takes 2 argument : event - callback to send info over click and type : date or month
  15. function Calendar(event, type) {
  16. this.append = append;
  17. this.toggleType = toggleType;
  18. this.hide = hide;
  19. this.show = showCalendar;
  20. this.getType = getCalendarType;
  21. this.getDate = getDate;
  22. this.isHide = getIsHide;
  23. clickEvent = event;
  24. isDateCalendar = type == 'month' ? false : true;
  25. }
  26.  
  27. // Variables And Styles
  28. var div = document.createElement('div');
  29.  
  30. div.style.display = 'inline-block';
  31. div.style.float = 'left';
  32.  
  33. var calendarConteiner = div.cloneNode(true);
  34. var calendarBtnHolder = div.cloneNode(true);
  35. var calendarBody = div.cloneNode(true);
  36. var clickableObject = div.cloneNode(true);
  37.  
  38. // INLINE-STYLES
  39. calendarConteiner.style.display = 'none';
  40. calendarConteiner.style.position = 'fixed';
  41. calendarConteiner.style.width = '500px';
  42. calendarConteiner.style.zIndex = '1000';
  43.  
  44. calendarBody.style.boxSizing = 'border-box';
  45. calendarBody.style.width = '100%';
  46.  
  47. clickableObject.style.border = '1px solid #dddddd';
  48. clickableObject.style.boxSizing = 'border-box';
  49. clickableObject.style.textAlign = 'center';
  50.  
  51. calendarBtnHolder.style.width = '100%';
  52.  
  53. var middleMonthBtn = clickableObject.cloneNode(true);
  54. var middleYearBtn = clickableObject.cloneNode(true);
  55.  
  56.  
  57. // Static Functions
  58.  
  59. //Appending Calendar to DOM
  60. function append() {
  61. if (isAppended) throw new Error('Already Appended!');
  62. showCalendar();
  63. buildControlPanel('Година', year);
  64. buildControlPanel('Месец', months[month]);
  65. hide();
  66. calendarConteiner.appendChild(calendarBody)
  67. document.body.appendChild(calendarConteiner);
  68. isAppended = true;
  69. }
  70.  
  71. //Change calendar Type from Date to Month and vice versa
  72. function toggleType() {
  73. isDateCalendar = !isDateCalendar;
  74. }
  75.  
  76. //Change Visibility of Calendar to display:none to display:inline-block and vice versa
  77. function hide() {
  78. isHide = true;
  79. calendarConteiner.style.display = 'none';
  80. }
  81.  
  82. // Return Calendar Type - date or month
  83. function getCalendarType() {
  84. if (isDateCalendar) {
  85. return 'date';
  86. } else {
  87. return 'month';
  88. }
  89. }
  90.  
  91. //Return date, month, year, new Date Object, Date as String - format m.d.yyyy
  92. function getDate() {
  93. return { date: date, month: month, year: year, DateAsObject: new Date(year, month, date, 0, 0, 0, 0, 0), DateAsString: date + '.' + (month + 1) + '.' + year };
  94. }
  95.  
  96. //Tells if calendar is hidden(display:none) or visible
  97. function getIsHide() { return isHide };
  98.  
  99. //Show Calendar with current date or by given new Date Object and position relative to HTMLElement or coordinates : x and y
  100. //example : {relativeElement: HTMLElement, coordinates: {x : 123, y: 123}};
  101. function showCalendar(dateObject, position) {
  102. if (dateObject && Object.prototype.toString.call(dateObject).slice(8, -1) != 'Date') throw new TypeError('Only Date Object are allowed!');
  103. if (dateObject == 'Invalid Date') throw new Error('Invalide Date Object');
  104. var d = dateObject || new Date();
  105.  
  106. date = d.getDate();
  107. month = d.getMonth();
  108. year = d.getFullYear();
  109.  
  110. if (position) calculatePosition(position);
  111.  
  112. middleMonthBtn.textContent = 'Месец - ' + months[month];
  113. middleYearBtn.textContent = 'Година - ' + year;
  114.  
  115. calendarConteiner.style.display = '';
  116. isHide = false;
  117. (isDateCalendar) ? buildDates() : buildMonths();
  118. }
  119.  
  120.  
  121. //Calculate Calendar Position relative to HTMLElement or coordinates : x and y
  122. function calculatePosition(position) {
  123. if (position.coordinates) {
  124. calendarConteiner.style.top = (position.coordinates.y || 0) + 'px';
  125. calendarConteiner.style.left = (position.coordinates.x || 0) + 'px';
  126. }
  127. else if (position.relativeElement) {
  128. calendarConteiner.style.top = position.relativeElement.offsetTop + 'px';
  129. calendarConteiner.style.left = (position.relativeElement.offsetLeft + position.relativeElement.offsetWidth) + 'px';
  130. }
  131. }
  132.  
  133. // Build Functions
  134.  
  135. function buildControlPanel(name, data) {
  136. var leftBtn = clickableObject.cloneNode(true),
  137. rightBtn = clickableObject.cloneNode(true);
  138.  
  139. leftBtn.textContent = '<';
  140. rightBtn.textContent = '>';
  141.  
  142. if (name == 'Година') middleYearBtn.textContent = name + ' - ' + year;
  143. else middleMonthBtn.textContent = name + ' - ' + months[month];
  144.  
  145. leftBtn.style.width = '10%'
  146. rightBtn.style.width = '10%';
  147. middleYearBtn.style.width = '80%';
  148. middleMonthBtn.style.width = '80%';
  149.  
  150. AttachEventsToControlPanel(leftBtn, rightBtn, name)
  151.  
  152. calendarBtnHolder.appendChild(leftBtn);
  153. if (name == 'Година') calendarBtnHolder.appendChild(middleYearBtn)
  154. else calendarBtnHolder.appendChild(middleMonthBtn)
  155. calendarBtnHolder.appendChild(rightBtn);
  156. calendarConteiner.appendChild(calendarBtnHolder);
  157. }
  158.  
  159. function buildYears() {
  160. var firstYearToShow = Number(year) - 4,
  161. leftChangeYearBtn = div.cloneNode(true),
  162. rightChangeYearBtn = div.cloneNode(true),
  163. yearClickableObject = clickableObject.cloneNode(true);
  164.  
  165. yearClickableObject.style.width = (100 / 3) + '%';
  166.  
  167. calendarBody.innerHTML = '';
  168.  
  169. calendarBody.appendChild(leftChangeYearBtn);
  170. for (var i = firstYearToShow; i <= firstYearToShow + 8; i++) {
  171. var currentYear = yearClickableObject.cloneNode(true);
  172. currentYear.textContent = i;
  173. currentYear.onclick = YearClickableObjectFunc;
  174. calendarBody.appendChild(currentYear);
  175. }
  176. calendarBody.appendChild(rightChangeYearBtn);
  177. }
  178.  
  179. function buildMonths() {
  180. var monthClickableObject = clickableObject.cloneNode(true);
  181. monthClickableObject.style.width = (100 / 4) + '%';
  182.  
  183. calendarBody.innerHTML = '';
  184. for (var i = 0; i < months.length; i++) {
  185. var currentMonth = monthClickableObject.cloneNode(true);
  186. currentMonth.textContent = months[i];
  187. currentMonth.onclick = MonthClickableObjectFunc;
  188. calendarBody.appendChild(currentMonth);
  189. }
  190. }
  191.  
  192. function buildDates() {
  193. calendarBody.innerHTML = '';
  194.  
  195. var firstMonthDay = new Date(year, month, 1, 0, 0, 0, 0).getDay(),
  196. lastMonthDay = new Date(year, Number(month) + 1, 0, 0, 0, 0, 0).getDay(),
  197. MonthDays = new Date(year, Number(month) + 1, 0, 0, 0, 0, 0).getDate(),
  198. prevMonthDays = new Date(year, Number(month) + 1, 0, 0, 0, 0, 0).getDate(),
  199. dateClickableObject = clickableObject.cloneNode(true);
  200.  
  201. dateClickableObject.style.width = (100 / 7) + '%';
  202.  
  203. for (var i = 0; i < days.length; i++) {
  204. var DaysAsNames = dateClickableObject.cloneNode(true);
  205. DaysAsNames.textContent = days[i];
  206. calendarBody.appendChild(DaysAsNames);
  207. }
  208.  
  209. for (var i = prevMonthDays - firstMonthDay + 1; i <= prevMonthDays; i++) {
  210. var lastMonthBlurryDay = dateClickableObject.cloneNode(true);
  211. lastMonthBlurryDay.textContent = i;
  212. lastMonthBlurryDay.style.color = '#bbbbbb';
  213. calendarBody.appendChild(lastMonthBlurryDay);
  214. }
  215.  
  216. for (var i = 1; i <= MonthDays; i++) {
  217. var thisMonthDay = dateClickableObject.cloneNode(true);
  218. thisMonthDay.textContent = i;
  219. thisMonthDay.onclick = DateClickableObjectFunc;
  220. thisMonthDay.style.cursor = 'pointer';
  221. calendarBody.appendChild(thisMonthDay);
  222. }
  223.  
  224. for (var i = lastMonthDay + 1; i <= 6; i++) {
  225. var lastMonthBlurryDay = dateClickableObject.cloneNode(true);
  226. lastMonthBlurryDay.style.color = '#bbbbbb';
  227. lastMonthBlurryDay.textContent = i - lastMonthDay;
  228. calendarBody.appendChild(lastMonthBlurryDay);
  229. }
  230. }
  231.  
  232.  
  233. // Event Functions
  234.  
  235. function MonthClickableObjectFunc() {
  236. for (var i = 0; i < months.length; i++) {
  237. if (months[i] == this.textContent) {
  238. month = i;
  239. break;
  240. }
  241. }
  242. if (!isDateCalendar) {
  243. clickEvent(new Date(year, month, 1, 0, 0, 0, 0));
  244. } else {
  245. buildDates();
  246. }
  247. middleMonthBtn.textContent = 'Месец - ' + months[month];
  248. }
  249.  
  250. function DateClickableObjectFunc() {
  251. date = this.textContent;
  252. clickEvent(new Date(year, month, date, 0, 0, 0, 0, 0));
  253. }
  254.  
  255. function YearClickableObjectFunc() {
  256. year = this.textContent;
  257. middleYearBtn.textContent = 'Година - ' + year;
  258. buildMonths();
  259. }
  260.  
  261.  
  262. function AttachEventsToControlPanel(leftBtn, rightBtn, name) {
  263. leftBtn.onclick = function () {
  264. if (name == 'Година') {
  265. year = Number(year) - 1;
  266. middleYearBtn.textContent = name + ' - ' + year;
  267. } else if (months[Number(month) - 1]) {
  268. month = month - 1
  269. middleMonthBtn.textContent = name + ' - ' + months[month];
  270. }
  271. if (isDateCalendar) buildDates();
  272. }
  273.  
  274. middleYearBtn.onclick = buildYears;
  275. middleMonthBtn.onclick = buildMonths;
  276.  
  277. rightBtn.onclick = function () {
  278. if (name == 'Година') {
  279. year = Number(year) + 1;
  280. middleYearBtn.textContent = name + ' - ' + year;
  281. } else if (months[Number(month) + 1]) {
  282. month = month + 1
  283. middleMonthBtn.textContent = name + ' - ' + months[month];
  284. }
  285. if (isDateCalendar) buildDates();
  286. }
  287. }
  288.  
  289. return Calendar;
  290. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement