Add JS to toggle expand/collapse map header and remember state in localstorage.

This commit is contained in:
Michael Stenta 2020-02-07 11:34:44 -05:00
parent 59abbb1304
commit 8bb7fe4bf6
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,51 @@
(function ($) {
Drupal.behaviors.farm_theme_map = {
attach: function(context, settings) {
// Specify the ID of the map wrapper.
var id = 'block-farm-map-farm-map';
// Load the collapsed state from localStorage (default to expanded).
var collapsed = localStorage.getItem('farmThemeMapCollapsed') || false;
// Activate the Bootstrap collapse component on the wrapper element.
$('#' + id, context).addClass('collapse');
$('#' + id, context).collapse({
toggle: !collapsed,
});
// Generate a link for expanding/collapsing the map.
var link = '<a href="#' + id + '" data-toggle="collapse" aria-expanded="' + (collapsed ? 'false' : 'true') + '" aria-controls="' + id + '" style="float: right; margin: 0.5em 1em 0 0;"><span class="glyphicon glyphicon-globe"></span> Toggle map</a>';
$('#' + id, context).after(link);
// When the map is expanded/collapsed, toggle the link text and update
// the OpenLayers map.
$('#' + id).on('shown.bs.collapse', function () {
Drupal.behaviors.farm_theme_map.toggleCollapsed(id, false);
});
$('#' + id).on('hidden.bs.collapse', function () {
Drupal.behaviors.farm_theme_map.toggleCollapsed(id, true);
});
},
// Toggle the map collapsed state.
toggleCollapsed: function(id, collapsed) {
// If the map is being expanded, update the OpenLayers map size.
if (!collapsed) {
var mapId = $('#' + id + ' .farm-map').attr('id');
var index = farmOS.map.targetIndex(mapId);
if (index !== -1) {
farmOS.map.instances[index].map.updateSize();
}
}
// Save the state to localStorage.
if (collapsed) {
localStorage.setItem('farmThemeMapCollapsed', '1');
} else {
localStorage.removeItem('farmThemeMapCollapsed');
}
}
};
})(jQuery);

View File

@ -439,6 +439,9 @@ function farm_theme_block_view_alter(&$data, $block) {
'css' => array(
drupal_get_path('theme', 'farm_theme') . '/css/map_header.css',
),
'js' => array(
drupal_get_path('theme', 'farm_theme') . '/js/map_header.js',
),
);
// If the block is being displayed on the homepage, show the farm_areas map.