calendar.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //[calendar Javascript]
  2. //Project: Riday Admin - Responsive Admin Template
  3. //Primary use: Used only for the event calendar
  4. !function($) {
  5. "use strict";
  6. var CalendarApp = function() {
  7. this.$body = $("body")
  8. this.$calendar = $('#calendar'),
  9. this.$event = ('#external-events div.external-event'),
  10. this.$categoryForm = $('#add-new-events form'),
  11. this.$extEvents = $('#external-events'),
  12. this.$modal = $('#my-event'),
  13. this.$saveCategoryBtn = $('.save-category'),
  14. this.$calendarObj = null
  15. };
  16. /* on drop */
  17. CalendarApp.prototype.onDrop = function (eventObj, date) {
  18. var $this = this;
  19. // retrieve the dropped element's stored Event Object
  20. var originalEventObject = eventObj.data('eventObject');
  21. var $categoryClass = eventObj.attr('data-class');
  22. // we need to copy it, so that multiple events don't have a reference to the same object
  23. var copiedEventObject = $.extend({}, originalEventObject);
  24. // assign it the date that was reported
  25. copiedEventObject.start = date;
  26. if ($categoryClass)
  27. copiedEventObject['className'] = [$categoryClass];
  28. // render the event on the calendar
  29. $this.$calendar.fullCalendar('renderEvent', copiedEventObject, true);
  30. // is the "remove after drop" checkbox checked?
  31. if ($('#drop-remove').is(':checked')) {
  32. // if so, remove the element from the "Draggable Events" list
  33. eventObj.remove();
  34. }
  35. },
  36. /* on click on event */
  37. CalendarApp.prototype.onEventClick = function (calEvent, jsEvent, view) {
  38. var $this = this;
  39. var form = $("<form></form>");
  40. form.append("<label>Change event name</label>");
  41. form.append("<div class='input-group'><input class='form-control' type=text value='" + calEvent.title + "' /><span class='input-group-btn'><button type='submit' class='btn btn-success waves-effect waves-light'><i class='fa fa-check'></i> Save</button></span></div>");
  42. $this.$modal.modal({
  43. backdrop: 'static'
  44. });
  45. $this.$modal.find('.delete-event').show().end().find('.save-event').hide().end().find('.modal-body').empty().prepend(form).end().find('.delete-event').unbind('click').click(function () {
  46. $this.$calendarObj.fullCalendar('removeEvents', function (ev) {
  47. return (ev._id == calEvent._id);
  48. });
  49. $this.$modal.modal('hide');
  50. });
  51. $this.$modal.find('form').on('submit', function () {
  52. calEvent.title = form.find("input[type=text]").val();
  53. $this.$calendarObj.fullCalendar('updateEvent', calEvent);
  54. $this.$modal.modal('hide');
  55. return false;
  56. });
  57. },
  58. /* on select */
  59. CalendarApp.prototype.onSelect = function (start, end, allDay) {
  60. var $this = this;
  61. $this.$modal.modal({
  62. backdrop: 'static'
  63. });
  64. var form = $("<form></form>");
  65. form.append("<div class='row'></div>");
  66. form.find(".row")
  67. .append("<div class='col-md-6'><div class='form-group'><label class='form-label'>Event Name</label><input class='form-control' placeholder='Insert Event Name' type='text' name='title'/></div></div>")
  68. .append("<div class='col-md-6'><div class='form-group'><label class='form-label'>Category</label><select class='form-control' name='category'></select></div></div>")
  69. .find("select[name='category']")
  70. .append("<option value='bg-danger'>Danger</option>")
  71. .append("<option value='bg-success'>Success</option>")
  72. .append("<option value='bg-purple'>Purple</option>")
  73. .append("<option value='bg-primary'>Primary</option>")
  74. .append("<option value='bg-pink'>Pink</option>")
  75. .append("<option value='bg-info'>Info</option>")
  76. .append("<option value='bg-warning'>Warning</option></div></div>");
  77. $this.$modal.find('.delete-event').hide().end().find('.save-event').show().end().find('.modal-body').empty().prepend(form).end().find('.save-event').unbind('click').click(function () {
  78. form.submit();
  79. });
  80. $this.$modal.find('form').on('submit', function () {
  81. var title = form.find("input[name='title']").val();
  82. var beginning = form.find("input[name='beginning']").val();
  83. var ending = form.find("input[name='ending']").val();
  84. var categoryClass = form.find("select[name='category'] option:checked").val();
  85. if (title !== null && title.length != 0) {
  86. $this.$calendarObj.fullCalendar('renderEvent', {
  87. title: title,
  88. start:start,
  89. end: end,
  90. allDay: false,
  91. className: categoryClass
  92. }, true);
  93. $this.$modal.modal('hide');
  94. }
  95. else{
  96. alert('You have to give a title to your event');
  97. }
  98. return false;
  99. });
  100. $this.$calendarObj.fullCalendar('unselect');
  101. },
  102. CalendarApp.prototype.enableDrag = function() {
  103. //init events
  104. $(this.$event).each(function () {
  105. // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
  106. // it doesn't need to have a start or end
  107. var eventObject = {
  108. title: $.trim($(this).text()) // use the element's text as the event title
  109. };
  110. // store the Event Object in the DOM element so we can get to it later
  111. $(this).data('eventObject', eventObject);
  112. // make the event draggable using jQuery UI
  113. $(this).draggable({
  114. zIndex: 999999,
  115. revert: true, // will cause the event to go back to its
  116. revertDuration: 0 // original position after the drag
  117. });
  118. });
  119. }
  120. /* Initializing */
  121. CalendarApp.prototype.init = function() {
  122. this.enableDrag();
  123. /* Initialize the calendar */
  124. var date = new Date();
  125. var d = date.getDate();
  126. var m = date.getMonth();
  127. var y = date.getFullYear();
  128. var form = '';
  129. var today = new Date($.now());
  130. var defaultEvents = [{
  131. title: 'Released Ample Admin!',
  132. start: '2017-08-08',
  133. end: '2017-08-08',
  134. className: 'bg-info'
  135. }, {
  136. title: 'This is today check date',
  137. start: today,
  138. end: today,
  139. className: 'bg-danger'
  140. }, {
  141. title: 'This is your birthday',
  142. start: '2017-09-08',
  143. end: '2017-09-08',
  144. className: 'bg-info'
  145. },
  146. {
  147. title: 'Hanns birthday',
  148. start: '2017-10-08',
  149. end: '2017-10-08',
  150. className: 'bg-danger'
  151. },{
  152. title: 'Like it?',
  153. start: new Date($.now() + 784800000),
  154. className: 'bg-success'
  155. }];
  156. var $this = this;
  157. $this.$calendarObj = $this.$calendar.fullCalendar({
  158. slotDuration: '00:15:00', /* If we want to split day time each 15minutes */
  159. minTime: '08:00:00',
  160. maxTime: '19:00:00',
  161. defaultView: 'month',
  162. handleWindowResize: true,
  163. header: {
  164. left: 'prev,next today',
  165. center: 'title',
  166. right: 'month,agendaWeek,agendaDay'
  167. },
  168. events: defaultEvents,
  169. editable: true,
  170. droppable: true, // this allows things to be dropped onto the calendar !!!
  171. eventLimit: true, // allow "more" link when too many events
  172. selectable: true,
  173. drop: function(date) { $this.onDrop($(this), date); },
  174. select: function (start, end, allDay) { $this.onSelect(start, end, allDay); },
  175. eventClick: function(calEvent, jsEvent, view) { $this.onEventClick(calEvent, jsEvent, view); }
  176. });
  177. //on new event
  178. this.$saveCategoryBtn.on('click', function(){
  179. var categoryName = $this.$categoryForm.find("input[name='category-name']").val();
  180. var categoryColor = $this.$categoryForm.find("select[name='category-color']").val();
  181. if (categoryName !== null && categoryName.length != 0) {
  182. $this.$extEvents.append('<div class="m-15 external-event bg-' + categoryColor + '" data-class="bg-' + categoryColor + '" style="position: relative;"><i class="fa fa-hand-o-right"></i>' + categoryName + '</div>')
  183. $this.enableDrag();
  184. }
  185. });
  186. },
  187. //init CalendarApp
  188. $.CalendarApp = new CalendarApp, $.CalendarApp.Constructor = CalendarApp
  189. }(window.jQuery),// End of use strict
  190. //initializing CalendarApp
  191. function($) {
  192. "use strict";
  193. $.CalendarApp.init()
  194. }(window.jQuery);// End of use strict