calendar/amd/src/calendar_mini.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * This module is the highest level module for the calendar. It is
  17. * responsible for initialising all of the components required for
  18. * the calendar to run. It also coordinates the interaction between
  19. * components by listening for and responding to different events
  20. * triggered within the calendar UI.
  21. *
  22. * @module core_calendar/calendar_mini
  23. * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. define([
  27. 'jquery',
  28. 'core_calendar/selectors',
  29. 'core_calendar/events',
  30. 'core_calendar/view_manager',
  31. ],
  32. function(
  33. $,
  34. CalendarSelectors,
  35. CalendarEvents,
  36. CalendarViewManager
  37. ) {
  38. /**
  39. * Listen to and handle any calendar events fired by the calendar UI.
  40. *
  41. * @method registerCalendarEventListeners
  42. * @param {object} root The calendar root element
  43. */
  44. var registerCalendarEventListeners = function(root) {
  45. var body = $('body');
  46. var namespace = '.' + root.attr('id');
  47. body.on(CalendarEvents.created + namespace, root, reloadMonth);
  48. body.on(CalendarEvents.deleted + namespace, root, reloadMonth);
  49. body.on(CalendarEvents.updated + namespace, root, reloadMonth);
  50. body.on(CalendarEvents.eventMoved + namespace, root, reloadMonth);
  51. };
  52. /**
  53. * Reload the month view in this month.
  54. *
  55. * @param {EventFacade} e
  56. */
  57. var reloadMonth = function(e) {
  58. var root = e.data;
  59. var body = $('body');
  60. var namespace = '.' + root.attr('id');
  61. if (root.is(':visible')) {
  62. CalendarViewManager.reloadCurrentMonth(root);
  63. } else {
  64. // The root has been removed.
  65. // Remove all events in the namespace.
  66. body.off(CalendarEvents.created + namespace);
  67. body.off(CalendarEvents.deleted + namespace);
  68. body.off(CalendarEvents.updated + namespace);
  69. body.off(CalendarEvents.eventMoved + namespace);
  70. }
  71. };
  72. var registerEventListeners = function(root) {
  73. $('body').on(CalendarEvents.filterChanged, function(e, data) {
  74. var daysWithEvent = root.find(CalendarSelectors.eventType[data.type]);
  75. daysWithEvent.toggleClass('calendar_event_' + data.type, !data.hidden);
  76. });
  77. var namespace = '.' + root.attr('id');
  78. $('body').on('change' + namespace, CalendarSelectors.elements.courseSelector, function() {
  79. if (root.is(':visible')) {
  80. var selectElement = $(this);
  81. var courseId = selectElement.val();
  82. var categoryId = null;
  83. CalendarViewManager.reloadCurrentMonth(root, courseId, categoryId);
  84. } else {
  85. $('body').off('change' + namespace);
  86. }
  87. });
  88. };
  89. return {
  90. init: function(root, loadOnInit) {
  91. root = $(root);
  92. CalendarViewManager.init(root);
  93. registerEventListeners(root);
  94. registerCalendarEventListeners(root);
  95. if (loadOnInit) {
  96. // The calendar hasn't yet loaded it's events so we
  97. // should load them as soon as we've initialised.
  98. CalendarViewManager.reloadCurrentMonth(root);
  99. }
  100. }
  101. };
  102. });