schedule.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. - application doesnt handle negative inputs
  3. - to time alwasy > fromtime
  4. */
  5. //for existent elements in the page
  6. updateElements();
  7. makeDeletable();
  8. /*
  9. ON CLICK EVENTS
  10. */
  11. //pop up menu to add class
  12. $(".addNew").on("click", function(){
  13. $(".popUp").fadeToggle();
  14. });
  15. //clicking to add class
  16. $(".addButton").on("click", function(){
  17. var title = $(".nameInput").val();
  18. var checkboxValue = [];
  19. //retrives the values from selected checkboxes and pushes it into array
  20. $.each($("input:checked"), function(){
  21. checkboxValue.push($(this).val());
  22. });
  23. var fromTimeHour = $("#fromTimeHour").val();
  24. var fromTimeHalf = $("#fromTimeHalf").val();
  25. var toTimeHour = $("#toTimeHour").val();
  26. var toTimeHalf = $("#toTimeHalf").val();
  27. var color = $(this).css('backgroundColor');
  28. // alert(color);
  29. // alert(checkboxValue + " " + typeof checkboxValue[0] + " " + checkboxValue.length); //string
  30. //VALIDADE INPUTS
  31. if(title != "") {
  32. //create new div with class element and fadeout popup
  33. //CREATE NEW ELEMENTS
  34. for(var i = 0; i < checkboxValue.length; i++) {
  35. createNew(title, fromTimeHour, fromTimeHalf, toTimeHour, toTimeHalf, Number(checkboxValue[i]), color);
  36. }
  37. makeDeletable();
  38. updateElements()
  39. $(".nameInput").val("");
  40. $(".popUp").fadeToggle();
  41. } else {
  42. //shake textbox
  43. //toggle
  44. shake();
  45. //untoggles after animation is done
  46. setTimeout(shake,310);
  47. }
  48. });
  49. //clicking colors
  50. $(".color").on("click", function() {
  51. let myColor = $(this).css('backgroundColor');
  52. $(".addButton").css('backgroundColor', myColor);
  53. });
  54. function shake () {
  55. $(".nameInput").toggleClass("shake");
  56. }
  57. function createNew(title, fromTimeHour, fromTimeHalf, toTimeHour, toTimeHalf, weekDay, color) {
  58. var unit = 15;
  59. //create new element using title+weekday as ID
  60. var newElement = '<div id=' + title + weekDay + ' class="myClass ui-draggable ui-draggable-handle ui-resizable"><p class="title">' + title + '<i class="fa fa-trash-o" aria-hidden="true"></i></p><div class="ui-resizable-handle ui-resizable-s" style="z-index:90;"></div></div>';
  61. //inserts it
  62. $(newElement).insertBefore(".tableTimes");
  63. //this is how we will control where the elements will appear on the grid
  64. //left for the day of the week
  65. //top for starting time
  66. //height interval of time
  67. $("#" + title + weekDay).css({
  68. "left": weekDay,
  69. "top": getStartHour(fromTimeHour, fromTimeHalf),
  70. "height": getToHour(fromTimeHour, fromTimeHalf, toTimeHour, toTimeHalf),
  71. "background-color": "" + color,
  72. });
  73. updateElements();
  74. }
  75. function makeResizable() {
  76. $( ".myClass" ).resizable({
  77. handles: 's',
  78. grid: [ 0, 15 ]
  79. });
  80. }
  81. function makeDraggable() {
  82. $(".myClass").draggable({
  83. containment: 'parent',
  84. grid: [100,15]
  85. });
  86. console.log("dragable");
  87. }
  88. function updateElements() {
  89. makeDraggable();
  90. makeResizable();
  91. }
  92. function makeDeletable() {
  93. $(".fa-trash-o").on("click", function() {
  94. $(this).parent().parent().remove();
  95. });
  96. }
  97. /*
  98. NOTE: need to make sure inputs are valid on functions bellow and test more
  99. - FROM needs to be smaller than TO
  100. - create function validate input
  101. */
  102. function correctHour(toHour) {
  103. var result;
  104. if(toHour < 7) {
  105. result = 12 + Number(toHour);
  106. } else {
  107. result = toHour;
  108. }
  109. // alert(result);
  110. return result;
  111. }
  112. function getToHour(fromHour, fromHalf, toHour, toHalf) {
  113. //needs to handle 9am to 1pm types of entry
  114. var compensation;
  115. if(fromHalf == 30 && toHalf == 30) {
  116. compensation = 0;
  117. } else if (fromHalf == 30 ){
  118. compensation = -15;
  119. } else if(toHalf == 30){
  120. compensation = 15;
  121. } else {
  122. compensation = 0;
  123. }
  124. var correctedToHour = correctHour(toHour);
  125. var correctedFromHour = correctHour(fromHour);
  126. // alert(correctedHour);
  127. return ((correctedToHour - correctedFromHour) * 30) + compensation;
  128. }
  129. function getStartHour(fromHour, fromHalf) {
  130. let base = 110; //that gives 7am
  131. var unitHalf;
  132. //this rounds down or up
  133. if (fromHalf >= 30) {
  134. unitHalf = 15; //half an hour
  135. } else {
  136. unitHalf = 0;
  137. }
  138. if(fromHour >= 7) {
  139. return base + ((fromHour - 7) * 30) + unitHalf;
  140. } else {
  141. //260 is the base for anything after 12
  142. return 260 + (fromHour * 30) + unitHalf;
  143. }
  144. };
  145. function validateInput (number1, number2) {
  146. //tittle cant be an empty string
  147. //fromHour > toHour
  148. //array of weekdays is not empty
  149. //color with r,g or b lower than 130 makes color of text white
  150. }