main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. $(document).ready(function() {
  2. $(document).on('keypress',function(e){
  3. if(e.keyCode!=46 || $('.fc-event[data-selected]').length==0 ) return;
  4. var toDelete = [];
  5. $('.fc-event[data-selected]').each(function(i,element){
  6. if($.inArray($(element).attr('data-ics'),toDelete) == -1)
  7. toDelete.push($(element).attr('data-ics'));
  8. });
  9. $.getJSON('action.php?action=caldav_delete_event',{events:toDelete,calendar:$('#calendarSelect').val()},function(r){
  10. $('.fc-event[data-selected]').each(function(i,element){
  11. $('#calendar').fullCalendar( 'removeEvents' ,$(element).attr('data-ics') );
  12. });
  13. });
  14. });
  15. $('#calendar').fullCalendar({
  16. locale: 'fr',
  17. header: {
  18. left: 'prev,next today',
  19. center: 'title',
  20. right: 'month,agendaWeek,agendaDay,listWeek'
  21. },
  22. //defaultDate: '2017-02-12',
  23. navLinks: true, // can click day/week names to navigate views
  24. editable: true,
  25. eventLimit: true, // allow "more" link when too many events
  26. events: {
  27. url: 'action.php?action=caldav_get_events&calendar='+$('#calendarSelect').val(),
  28. error: function() {
  29. $('#script-warning').show();
  30. }
  31. },
  32. dayRender: function(event, element,view) {
  33. element.bind('dblclick', function() {
  34. var calculatedEnd = parseInt(event.format("HH"))+1;
  35. var newEvent = {
  36. label : 'Rendez vous',
  37. startDay : event.format("DD/MM/YYYY"),
  38. endDay : event.format("DD/MM/YYYY"),
  39. startHour : event.format("HH"),
  40. endHour : "00".substring(0, 2 - calculatedEnd.length) + ''+ calculatedEnd,
  41. startMinut : event.format("mm"),
  42. endMinut : event.format("mm")
  43. };
  44. if(view.name=='month'){
  45. newEvent.startHour = '08';
  46. newEvent.endHour = '12';
  47. newEvent.startMinut = '00';
  48. newEvent.endMinut = '00';
  49. }
  50. edit_event(newEvent);
  51. });
  52. },
  53. eventRender: function(event, element) {
  54. element.bind('dblclick', function() {
  55. edit_event({
  56. id : event.id,
  57. label : event.title,
  58. startDay : event.start.format("DD/MM/YYYY"),
  59. endDay : event.end.format("DD/MM/YYYY"),
  60. startHour : event.start.format("HH"),
  61. endHour : event.end.format("HH"),
  62. startMinut : event.start.format("mm"),
  63. endMinut : event.end.format("mm")
  64. });
  65. });
  66. },
  67. eventAfterRender:function( event, element, view ) {
  68. $(element).attr("data-ics",event._id);
  69. },
  70. eventClick: function(event,jsevent,view) {
  71. $('[data-selected]').removeAttr('data-selected');
  72. $(this).attr('data-selected','selected');
  73. $('[data-ics="'+$(this).attr('data-ics')+'"]').attr('data-selected','selected');
  74. },
  75. eventResize: function(a) {
  76. alert('a day has been eventResize: '+a);
  77. },
  78. eventDrop: function(event, delta, revertFunc) {
  79. $.getJSON('action.php?action=caldav_save_event',{
  80. ics : event.id,
  81. label : event.title,
  82. startDay : event.start.format("DD/MM/YYYY"),
  83. endDay : event.end.format("DD/MM/YYYY"),
  84. startHour : event.start.format("HH"),
  85. endHour : parseInt(event.end.format("HH")),
  86. startMinut : event.start.format("mm"),
  87. endMinut : event.end.format("mm"),
  88. calendar : $('#calendarSelect').val()
  89. },function(r){
  90. // if(r.error) revertFunc();
  91. });
  92. }
  93. });
  94. });
  95. function edit_event(event){
  96. $('#eventModal').modal('show');
  97. $('#eventModal').attr('data-ics',event.id);
  98. $('#eventModal #label').val(event.label);
  99. $('#eventModal #startDay').val(event.startDay);
  100. $('#eventModal #endDay').val(event.endDay);
  101. $('#eventModal #startHour').val(event.startHour);
  102. $('#eventModal #endHour').val(event.endHour);
  103. $('#eventModal #startMinut').val(event.startMinut);
  104. $('#eventModal #endMinut').val(event.endMinut);
  105. }
  106. function caldav_save_event(){
  107. var event = $('#eventModal').toData();
  108. event.ics = $('#eventModal').attr('data-ics');
  109. event.calendar = $('#calendarSelect').val();
  110. $.getJSON('action.php?action=caldav_save_event',event,function(r){
  111. //modification
  112. if($('#eventModal').attr('data-ics')!=null){
  113. var originalEvent = $('#calendar').fullCalendar( 'clientEvents',r.event.id );
  114. originalEvent = originalEvent[0];
  115. //originalEvent.title = r.event.title;
  116. originalEvent = $.extend(originalEvent,r.event);
  117. $('#calendar').fullCalendar('updateEvent', originalEvent);
  118. //creation
  119. }else{
  120. $('#calendar').fullCalendar( 'renderEvent',r.event);
  121. }
  122. $('#eventModal').removeAttr('data-ics','');
  123. });
  124. $('#eventModal').modal('hide');
  125. return;
  126. }