Move toolbar-toggle map resize event listener JS to farm_ui_theme.

This commit is contained in:
Michael Stenta 2021-09-06 09:12:37 -04:00
parent 99eb69434a
commit 434b473e14
3 changed files with 31 additions and 22 deletions

View File

@ -1,7 +1,6 @@
(function (Drupal) {
Drupal.behaviors.farm_map = {
attach: function (context, settings) {
context.querySelectorAll('[data-map-instantiator="farm_map"]').forEach(function (element) {
// Only create a map once per element.
@ -14,27 +13,6 @@
const mapInstanceOptions = {};
Drupal.behaviors.farm_map.createMapInstance(context, element, drupalSettingsKey, mapInstanceOptions);
});
// Add an event listener to update the map size when the Gin toolbar is toggled.
if (context === document) {
document.addEventListener('toolbar-toggle', function(e) {
// Only continue if map instances are provided.
if (typeof farmOS !== 'undefined' && farmOS.map.instances !== 'undefined') {
// Set a timeout so the computed CSS properties are applied
// before updating the map size.
setTimeout(function () {
// Update the map size of all map instances.
farmOS.map.instances.forEach(function (instance) {
instance.map.updateSize();
});
}, 200);
}
});
}
},
createMapInstance: function(context, element, drupalSettingsKey, mapInstanceOptions) {

View File

@ -14,6 +14,11 @@ map:
css:
theme:
css/map.css: { }
js:
theme:
js/map.js: { }
dependencies:
- farm_map/farm_map
toolbar:
css:
theme:

View File

@ -0,0 +1,26 @@
(function (Drupal) {
Drupal.behaviors.farm_ui_theme_map = {
attach: function (context, settings) {
// Add an event listener to update the map size when the Gin toolbar is toggled.
if (context === document) {
document.addEventListener('toolbar-toggle', function(e) {
// Only continue if map instances are provided.
if (typeof farmOS !== 'undefined' && farmOS.map.instances !== 'undefined') {
// Set a timeout so the computed CSS properties are applied
// before updating the map size.
setTimeout(function () {
// Update the map size of all map instances.
farmOS.map.instances.forEach(function (instance) {
instance.map.updateSize();
});
}, 200);
}
});
}
}
};
}(Drupal));