@@ -5,14 +5,15 @@ module GttMapHelper
5
5
def map_form_field ( form , map , field : :geojson , bounds : nil , edit_mode : nil , upload : true , rotation : 0 )
6
6
safe_join [
7
7
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 )
9
9
]
10
10
end
11
11
12
12
def map_tag ( map : nil , layers : map &.layers ,
13
13
geom : map . json , bounds : map . bounds ,
14
14
edit : nil , popup : nil , upload : true ,
15
- collapsed : false , rotation : map &.rotation )
15
+ collapsed : false , rotation : map &.rotation ,
16
+ show : true )
16
17
17
18
data = {
18
19
geom : geom . is_a? ( String ) ? geom : geom . to_json ,
@@ -39,9 +40,39 @@ def map_tag(map: nil, layers: map&.layers,
39
40
content_tag ( :div , "" , data : data , id : uid , class : 'ol-map' ,
40
41
style : ( collapsed ? "display: none" : "display: block" ) ) ,
41
42
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
+ }
42
72
document.addEventListener('DOMContentLoaded', function(){
43
73
var target = document.getElementById('#{ uid } ');
44
74
window.createGttClient(target);
75
+ contentObserver();
45
76
});
46
77
" )
47
78
]
0 commit comments