Skip to content

Commit 1087512

Browse files
authored
Merge pull request #258 from gtt-project/fix/map-blank-bug-257
fix map blank bug 257
2 parents 11e1f17 + 8ee369e commit 1087512

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

app/helpers/gtt_map_helper.rb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ module GttMapHelper
55
def map_form_field(form, map, field: :geojson, bounds: nil, edit_mode: nil, upload: true, rotation: 0)
66
safe_join [
77
form.hidden_field(field, id: 'geom'),
8-
map_tag(map: map, bounds: bounds, edit: edit_mode, upload: upload, rotation: rotation)
8+
map_tag(map: map, bounds: bounds, edit: edit_mode, upload: upload, rotation: rotation, show: false)
99
]
1010
end
1111

1212
def map_tag(map: nil, layers: map&.layers,
1313
geom: map.json, bounds: map.bounds,
1414
edit: nil, popup: nil, upload: true,
15-
collapsed: false, rotation: map&.rotation)
15+
collapsed: false, rotation: map&.rotation,
16+
show: true)
1617

1718
data = {
1819
geom: geom.is_a?(String) ? geom : geom.to_json,
@@ -39,9 +40,39 @@ def map_tag(map: nil, layers: map&.layers,
3940
content_tag(:div, "", data: data, id: uid, class: 'ol-map',
4041
style: (collapsed ? "display: none" : "display: block")),
4142
javascript_tag("
43+
var contentObserver = () => {
44+
const target = document.getElementById('#{uid}');
45+
const observerCallback = function(mutations) {
46+
mutations.forEach(function(mutation) {
47+
if (mutation.removedNodes.length) {
48+
mutation.removedNodes.forEach(function(node) {
49+
if (node === target) {
50+
observer.disconnect();
51+
let event = new Event('contentUpdated');
52+
document.dispatchEvent(event);
53+
}
54+
});
55+
}
56+
});
57+
};
58+
const observer = new MutationObserver(observerCallback);
59+
const config = {
60+
childList: true,
61+
subtree: true
62+
};
63+
observer.observe(document.body, config);
64+
}
65+
if (!#{show}) {
66+
document.addEventListener('contentUpdated', function(){
67+
var target = document.getElementById('#{uid}');
68+
window.createGttClient(target);
69+
contentObserver();
70+
}, { once: true });
71+
}
4272
document.addEventListener('DOMContentLoaded', function(){
4373
var target = document.getElementById('#{uid}');
4474
window.createGttClient(target);
75+
contentObserver();
4576
});
4677
")
4778
]

0 commit comments

Comments
 (0)