Advertisement
NikolayPaskulov

Untitled

Feb 27th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. Element.prototype.addStyles = function (styles) {
  2. if (!styles || Object.prototype.toString.call(styles).slice(8, -1) != 'Object') throw new Error('Styles accept object');
  3. for (var style in styles) { this.style[style] = styles[style]; }
  4. }
  5.  
  6. var Schedule = (function () {
  7. var _conteinerHeight,
  8. _conteinerWidth,
  9. _FromDate,
  10. _ToDate,
  11. _userName,
  12. SCHEDULE_START_HOUR = 9,
  13. self;
  14.  
  15. function Schedule(sportClassesArray, width, height, FromDate, ToDate, userName) {
  16. _conteinerHeight = height;
  17. _conteinerWidth = width;
  18. _FromDate = new Date(FromDate);
  19. _ToDate = new Date(ToDate);
  20. _userName = userName;
  21. self = this;
  22. addStylesToConteiners();
  23. buildSchedule(sportClassesArray);
  24. this.append = append;
  25. this.RebuildSchedule = buildSchedule;
  26. }
  27.  
  28. var div = document.createElement('div');
  29.  
  30. div.addStyles({ display: 'inline-block', float: 'left', boxSizing: 'border-box' });
  31.  
  32. var ScheduleConteiner = div.cloneNode(true),
  33. ScheduleColumn = div.cloneNode(true),
  34. ScheduleClass = div.cloneNode(true),
  35. onHoverWindow = div.cloneNode(true);
  36.  
  37. //Static Functions
  38.  
  39. function append(conteiner) {
  40. fillConteinerWithColumns();
  41. conteiner.appendChild(ScheduleConteiner);
  42. return self;
  43. }
  44.  
  45.  
  46. // Help Functions
  47.  
  48. function addStylesToConteiners() {
  49. ScheduleConteiner.addStyles({ width: (_conteinerWidth + 'px'), height: (_conteinerHeight + 'px'), border: '1px solid green', position: 'relative' });
  50. ScheduleColumn.addStyles({ width: (100 / 7) + '%', height: _conteinerHeight + 'px', border: '1px solid red' });
  51.  
  52. ScheduleClass.addStyles({ width: (100 / 7) + '%', height: (_conteinerHeight / 12) + 'px', borderRadius: '7px', position: 'absolute', border: '1px solid black' });
  53.  
  54. onHoverWindow.addStyles({ width: ((100 / 7) * 2) + '%', borderRadius: '10px', position: 'absolute', border: '1px solid black' });
  55.  
  56. }
  57.  
  58. //Build Functions
  59.  
  60. function buildSchedule(sportClassesArray) {
  61. for (var a = 0; a < sportClassesArray.length; a++) {
  62. for (var b = 0; b < sportClassesArray[a].Dates.length; b++) {
  63. buildScheduleClass(sportClassesArray[a], sportClassesArray[a].Dates[b], sportClassesArray[a].UserNames);
  64. }
  65. }
  66. }
  67.  
  68. function buildScheduleClass(sportClass, date, usersArray) {
  69. var d = new Date(date);
  70. var currentClass = ScheduleClass.cloneNode(true);
  71. var Name = sportClass.Name;
  72. var ShortDisc = sportClass.ShortDiscription;
  73. var ClassImg = sportClass.ImageData;
  74. var row = div.cloneNode(true);
  75.  
  76. var daysFromBegginingOfWeek = Math.floor((d.valueOf() - _FromDate.valueOf()) / 86400000),
  77. minutePercent = _conteinerHeight / 720 * 100 / 720;
  78.  
  79. currentClass.style.left = (daysFromBegginingOfWeek * (100 / 7)) + '%';
  80. currentClass.style.top = ((((d.getHours() - SCHEDULE_START_HOUR) * 60) + d.getMinutes()) * minutePercent) + '%';
  81.  
  82. currentClass.innerHTML = '<div style="width:100%;height:50%;overflow:hidden;margin:0 2px;" title="' + Name + '">' + Name + '</div>' +
  83. '<div style="width:100%;height:50%;overflow:hidden;margin:0 2px;">' + d.toDateString().split(' ').splice(1).join(' ') + '</div>';
  84.  
  85. currentClass.onmouseover = function (e) {
  86. buildOnHoverWindow(Name, d, ShortDisc, '', usersArray);
  87. }
  88.  
  89. currentClass.onmouseleave = function () {
  90. try {
  91. document.body.removeChild(onHoverWindow);
  92. } catch (e) {
  93.  
  94. }
  95. }
  96.  
  97. ScheduleConteiner.appendChild(currentClass);
  98. // Add onmouseover and onmouseleave
  99. }
  100.  
  101. function buildOnHoverWindow(name, date, shortDisc, imgStr) {
  102. var d = date.toDateString().split(' ').splice(1).join(' ') + ' ' + date.getHours() + ':' + date.getMinutes() + ':00';
  103. onHoverWindow.innerHTML = '<div style="width:100%;margin-bottom:15px;"><img src="' + imgStr + '" style="width:20%;height:20%"/><div style="width:30%;display:inline-block;">' + name + '</div>' +
  104. '<div style="float:right; margin: 0 3px;">' + d + '</div></div><div style="width:100%;margin-bottom:15px;"><div style="width:35%;display:inline-block;">Кратко Описание:</div>' +
  105. '<div style="width:60%;display:inline-block;">' + shortDisc + '</div></div>';
  106.  
  107. document.body.appendChild(onHoverWindow);
  108. }
  109.  
  110. function fillConteinerWithColumns() {
  111. for (var i = 0; i < 7; i++) {
  112. var columnCoppy = ScheduleColumn.cloneNode(true);
  113. ScheduleConteiner.appendChild(columnCoppy);
  114. }
  115. }
  116.  
  117. return Schedule;
  118. })();
  119.  
  120. var obj = [
  121. {
  122. Name: 'Kangoo',
  123. ShortDiscription: 'PROSTO NQKWO OPISANIE ZA TEST',
  124. Dates: [
  125. '2015-02-22 13:30:00', '2015-02-23 15:00:00', '2015-02-25 12:30:00'
  126. ]
  127. },
  128. {
  129. Name: 'Aerobic',
  130. ShortDiscription: 'Aerobic',
  131. Dates: [
  132. '2015-02-22 14:30:00', '2015-02-23 16:00:00', '2015-02-25 13:30:00'
  133. ]
  134. },
  135. {
  136. Name: 'OMG',
  137. ShortDiscription: 'OMG',
  138. Dates: [
  139. '2015-02-22 17:30:00', '2015-02-23 18:00:00', '2015-02-25 15:30:00'
  140. ]
  141. },
  142. {
  143. Name: 'ASD',
  144. ShortDiscription: 'ASD',
  145. Dates: [
  146. '2015-02-26 17:30:00', '2015-02-27 18:00:00', '2015-02-28 15:30:00'
  147. ]
  148. },
  149. ]
  150.  
  151.  
  152. var sc = new Schedule(obj, 800, 700, '2015-02-22', '2015-02-28').append(document.body);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement