diff --git a/fullcalendar_legend/js/fullcalendar_legend.js b/fullcalendar_legend/js/fullcalendar_legend.js index eac7cfc..75b3982 100644 --- a/fullcalendar_legend/js/fullcalendar_legend.js +++ b/fullcalendar_legend/js/fullcalendar_legend.js @@ -2,7 +2,7 @@ * @file * Javascript functionality for FullCalendar Legend. Filters events by legend. */ -(function ($) { +(function ($, Drupal) { Drupal.fullcalendarLegend = Drupal.fullcalendarLegend || {} Drupal.fullcalendarLegend.state = Drupal.fullcalendarLegend.state || {} @@ -14,56 +14,89 @@ // Make sure we update the calendar on every ajax request. Drupal.fullcalendarLegend.update(settings); - // Toggle event visibility and checkbox state when clicking on a legend. - $('.fullcalendar-legend .fc-event-default', context).click(function(event) { + // Toggle event visibility when clicking on a legend. + $('.fullcalendar-legend .fc-event-default a', context).click(function(event) { event.preventDefault(); + Drupal.fullcalendarLegend.filterByLegend(this, settings); + }); + + // Check event visibility by searching the url for a legend. + $('.fullcalendar-legend', context).once('filter-by-url-done', function() { + var $legendLinks = $(this).find('a'), + currentLegend = location.pathname.split('/')[1], + $legendToNotFilter = false; - // Retrieve the class defined by the Colors module. - var colorClass = $(this).attr('class').match(/colors-[^\s]+/); + // Loop over all legends to find a valid filter. + for (var i = $legendLinks.length - 1; i >= 0; i--) { + var $legendLink = $($legendLinks[i]); + if ($legendLink.attr('href').split('/')[1] === currentLegend) { + $legendToNotFilter = $legendLink; + }; + }; - // Toggle the state and update the calendar. - Drupal.fullcalendarLegend.state[colorClass[0]] = !Drupal.fullcalendarLegend.state[colorClass[0]]; - Drupal.fullcalendarLegend.update(settings); + // Only filter if a valid legend has been found. + if ($legendToNotFilter) { + $legendLinksToFilter = $legendLinks.not($legendToNotFilter); + Drupal.fullcalendarLegend.filterByLegends($legendLinksToFilter, settings); + }; }); } }; /** + * Toggle event visibility depending on multiple legends. + */ + Drupal.fullcalendarLegend.filterByLegends = function(links, settings) { + for (var i = links.length - 1; i >= 0; i--) { + Drupal.fullcalendarLegend.filterByLegend(links[i], settings); + }; + } + + /** + * Toggle event visibility depending on a legend. + */ + Drupal.fullcalendarLegend.filterByLegend = function(link, settings) { + // Retrieve the class defined by the Colors module. + var colorClass = $(link).parent().attr('class').match(/colors-[^\s]+/); + + // Toggle the state and update the calendar. + Drupal.fullcalendarLegend.state[colorClass[0]] = !Drupal.fullcalendarLegend.state[colorClass[0]]; + Drupal.fullcalendarLegend.update(settings); + } + + /** * Update the calendar event visibility and legend checkboxes. */ Drupal.fullcalendarLegend.update = function(settings) { $('.fullcalendar-legend .fc-event-default').each(function() { // Retrieve the class defined by the Colors module. - var colorClass = $(this).attr('class').match(/colors-[^\s]+/); + var $legend = $(this), + colorClass = $legend.attr('class').match(/colors-[^\s]+/); + if (colorClass) { + // Hide or show events and checkboxes according to the state. + // Events are hidden by placing an inline style declaration in the HTML + // head, this makes sure the events are not momentarily visible during + // ajax page loads. + if (typeof Drupal.fullcalendarLegend.state[colorClass[0]] == 'undefined' || Drupal.fullcalendarLegend.state[colorClass[0]] == 0) { + // Toggle classes on the legends to aid in theming. + $legend + .addClass('fc-event-visible') + .removeClass('fc-event-hidden'); - // Insert a checkbox before the legend if it does not exist yet. - if ($(this).children('input').length === 0) { - $(this).prepend($(document.createElement('input')).attr('type', 'checkbox')); - } + // Show events. + $('#fullcalendar-legend-' + colorClass[0]).remove(); + } + else { + // Toggle classes on the legends to aid in theming. + $legend + .addClass('fc-event-hidden') + .removeClass('fc-event-visible'); - // Hide or show events and checkboxes according to the state. - // Events are hidden by placing an inline style declaration in the HTML - // head, this makes sure the events are not momentarily visible during - // ajax page loads. - if (typeof Drupal.fullcalendarLegend.state[colorClass[0]] == 'undefined' || Drupal.fullcalendarLegend.state[colorClass[0]] == 0) { - // Toggle classes on the legends to aid in theming. - $(this).addClass('fc-event-visible'); - $(this).removeClass('fc-event-hidden'); - // Check checkbox. - $(this).children('input').first().attr('checked', 'checked'); - // Show events. - $('#fullcalendar-legend-' + colorClass[0]).remove(); - } - else { - // Toggle classes on the legends to aid in theming. - $(this).addClass('fc-event-hidden'); - $(this).removeClass('fc-event-visible'); - // Uncheck checkbox. - $(this).children('input').first().removeAttr('checked'); - // Hide events by inserting a style declaration, if it is not already - // hidden. - if ($('#fullcalendar-legend-' + colorClass[0]).length === 0) { - $('').appendTo('head'); + // Hide events by inserting a style declaration, if it is not already + // hidden. + if ($('#fullcalendar-legend-' + colorClass[0]).length === 0) { + $('').appendTo('head'); + } } } }); @@ -73,4 +106,4 @@ $(dom_id).find('.fullcalendar').fullCalendar('rerenderEvents'); } } -})(jQuery); +})(jQuery, Drupal);