From 92da4d5677aa13112b5007f66e35fffd8f9e5dc7 Mon Sep 17 00:00:00 2001 From: Kristiaan Van den Eynde Date: Mon, 26 Aug 2013 16:51:02 +0200 Subject: [PATCH] Issue #1880252 by pfrenssen, kristiaanvandeneynde: Added Allow to show/hide events by clicking on the legends. --- fullcalendar_legend/fullcalendar_legend.install | 1 + fullcalendar_legend/fullcalendar_legend.module | 11 +++++ fullcalendar_legend/js/fullcalendar_legend.js | 51 ++++++++++++++++++++ .../plugins/content_types/fullcalendar_legend.inc | 12 ++++- 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 fullcalendar_legend/js/fullcalendar_legend.js diff --git a/fullcalendar_legend/fullcalendar_legend.install b/fullcalendar_legend/fullcalendar_legend.install index 60c17c7..a4175b7 100644 --- a/fullcalendar_legend/fullcalendar_legend.install +++ b/fullcalendar_legend/fullcalendar_legend.install @@ -10,4 +10,5 @@ */ function fullcalendar_legend_uninstall() { variable_del('fullcalendar_legend_type'); + variable_del('fullcalendar_legend_filter'); } diff --git a/fullcalendar_legend/fullcalendar_legend.module b/fullcalendar_legend/fullcalendar_legend.module index 26518b0..2cd5953 100644 --- a/fullcalendar_legend/fullcalendar_legend.module +++ b/fullcalendar_legend/fullcalendar_legend.module @@ -48,6 +48,11 @@ function fullcalendar_legend_block_configure($delta = '') { '#default_value' => variable_get('fullcalendar_legend_type', 'bundle'), '#description' => t('Select the type of legend to display.'), ); + $form['fullcalendar_legend_filter'] = array( + '#type' => 'checkbox', + '#title' => t('Allow to filter events by clicking on the legends.'), + '#default_value' => variable_get('fullcalendar_legend_filter', 0), + ); } return $form; @@ -59,6 +64,7 @@ function fullcalendar_legend_block_configure($delta = '') { function fullcalendar_legend_block_save($delta = '', $edit = array()) { if ($delta == 'fullcalendar_legend') { variable_set('fullcalendar_legend_type', $edit['fullcalendar_legend_type']); + variable_set('fullcalendar_legend_filter', $edit['fullcalendar_legend_filter']); } } @@ -79,6 +85,11 @@ function fullcalendar_legend_block_view($delta = '') { // Build the block structure. $block['content'] = fullcalendar_legend_build_legend($view, variable_get('fullcalendar_legend_type', 'bundle')); + + // Load the javascript for filtering by legend if needed. + if (variable_get('fullcalendar_legend_filter', 0)) { + drupal_add_js(drupal_get_path('module', 'fullcalendar_legend') . '/js/fullcalendar_legend.js'); + } } return $block; diff --git a/fullcalendar_legend/js/fullcalendar_legend.js b/fullcalendar_legend/js/fullcalendar_legend.js new file mode 100644 index 0000000..ccfec4b --- /dev/null +++ b/fullcalendar_legend/js/fullcalendar_legend.js @@ -0,0 +1,51 @@ +/** + * @file + * Javascript functionality for FullCalendar Legend. Filters events by legend. + */ +(function ($, Drupal, window, document, undefined) { + + $(function() { + // Used to store CSS to append to the head. + var headCss = '.filter-default .fc-event-default:not([class*=colors]){display:none;}'; + + $('.fullcalendar-legend .fc-event-default').each(function() { + var legend = $(this); + + // Retrieve the class defined by the Colors module. + var colorClass = legend.attr('class').match(/colors-[^\s]+/); + colorClass = colorClass ? colorClass[0] : 'default'; + + // Set the class that will be used for filtering. + var filterClass = 'filter-' + colorClass; + + // If there is a color class we add its filter to the head. + if (colorClass != 'default') { + headCss += '.' + filterClass + ' .' + colorClass + '{display:none;}'; + } + + // Store the filter class on the legend element. + legend.data('filter-class', filterClass); + + // If the filter class is already present, we add it to the calendar. + if (legend.hasClass(filterClass)) { + $('.fc-content').addClass(filterClass); + } + }).click(function(event) { + // Prevent a page load. + event.preventDefault(); + + var legend = $(this); + + // Toggle both the legend and the calendar's filter class. + legend.toggleClass('fc-event-hidden'); + $('.fc-content').toggleClass(legend.data('filter-class')); + + // Refresh the calendar display. + $('.fullcalendar').fullCalendar('rerenderEvents'); + }); + + // Add the combined CSS to the head so it applies to loaded events as well. + $('').appendTo('head'); + }); + +})(jQuery, Drupal, this, document); diff --git a/fullcalendar_legend/plugins/content_types/fullcalendar_legend.inc b/fullcalendar_legend/plugins/content_types/fullcalendar_legend.inc index 6063990..a508880 100644 --- a/fullcalendar_legend/plugins/content_types/fullcalendar_legend.inc +++ b/fullcalendar_legend/plugins/content_types/fullcalendar_legend.inc @@ -28,6 +28,11 @@ function fullcalendar_legend_content_type_render($subtype, $conf, $panel_args, $ } $view->set_display($display); + // Load the javascript for filtering by legend if needed. + if (!empty($conf['filter'])) { + drupal_add_js(drupal_get_path('module', 'fullcalendar_legend') . '/js/fullcalendar_legend.js'); + } + return (object) array( 'module' => 'fullcalendar_legend', 'delta' => 'fullcalendar_legend', @@ -54,6 +59,11 @@ function fullcalendar_legend_content_type_edit_form($form, &$form_state) { '#default_value' => isset($conf['legend_type']) ? $conf['legend_type'] : '', '#description' => t('Select the type of legend to display.'), ); + $form['filter'] = array( + '#type' => 'checkbox', + '#title' => t('Allow to filter events by clicking on the legends.'), + '#default_value' => isset($conf['filter']) ? $conf['filter'] : 0, + ); $form['view'] = array( '#type' => 'select', '#title' => t('FullCalendar view'), @@ -68,7 +78,7 @@ function fullcalendar_legend_content_type_edit_form($form, &$form_state) { * Form submission handler for fullcalendar_legend_content_type_edit_form(). */ function fullcalendar_legend_content_type_edit_form_submit($form, &$form_state) { - foreach (array('view', 'legend_type') as $key) { + foreach (array('view', 'legend_type', 'filter') as $key) { $form_state['conf'][$key] = $form_state['values'][$key]; } } -- 1.7.9.4