]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/history-changesets-layer.js
Move creating changeset rectangles to bbox layers
[rails.git] / app / assets / javascripts / index / history-changesets-layer.js
1 OSM.HistoryChangesetBboxLayer = L.FeatureGroup.extend({
2   getLayerId: function (layer) {
3     return layer.id;
4   },
5
6   addChangesetLayer: function (changeset) {
7     const style = this._getChangesetStyle(changeset);
8     const rectangle = L.rectangle(changeset.bounds, style);
9     rectangle.id = changeset.id;
10     return this.addLayer(rectangle);
11   },
12
13   updateChangesetLayerBounds: function (changeset) {
14     this.getLayer(changeset.id)?.setBounds(changeset.bounds);
15   },
16
17   _getSidebarRelativeClassName: function ({ sidebarRelativePosition }) {
18     if (sidebarRelativePosition > 0) {
19       return "changeset-above-sidebar-viewport";
20     } else if (sidebarRelativePosition < 0) {
21       return "changeset-below-sidebar-viewport";
22     } else {
23       return "changeset-in-sidebar-viewport";
24     }
25   }
26 });
27
28 OSM.HistoryChangesetBboxAreaLayer = OSM.HistoryChangesetBboxLayer.extend({
29   _getChangesetStyle: function (changeset) {
30     return {
31       weight: 0,
32       fillOpacity: 0,
33       className: this._getSidebarRelativeClassName(changeset)
34     };
35   }
36 });
37
38 OSM.HistoryChangesetBboxBorderLayer = OSM.HistoryChangesetBboxLayer.extend({
39   _getChangesetStyle: function (changeset) {
40     return {
41       weight: 2,
42       color: "var(--changeset-border-color)",
43       fill: false,
44       className: this._getSidebarRelativeClassName(changeset)
45     };
46   }
47 });
48
49 OSM.HistoryChangesetBboxHighlightLayer = OSM.HistoryChangesetBboxLayer.extend({
50   _getChangesetStyle: function (changeset) {
51     return {
52       interactive: false,
53       weight: 4,
54       color: "var(--changeset-border-color)",
55       fillColor: "var(--changeset-fill-color)",
56       fillOpacity: 0.3,
57       className: this._getSidebarRelativeClassName(changeset) + " changeset-highlighted"
58     };
59   }
60 });
61
62 OSM.HistoryChangesetsLayer = L.FeatureGroup.extend({
63   updateChangesets: function (map, changesets) {
64     this._changesets = new Map(changesets.map(changeset => [changeset.id, changeset]));
65     this.updateChangesetShapes(map);
66   },
67
68   updateChangesetShapes: function (map) {
69     for (const changeset of this._changesets.values()) {
70       const bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)),
71             topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)),
72             width = topRight.x - bottomLeft.x,
73             height = bottomLeft.y - topRight.y,
74             minSize = 20; // Min width/height of changeset in pixels
75
76       if (width < minSize) {
77         bottomLeft.x -= ((minSize - width) / 2);
78         topRight.x += ((minSize - width) / 2);
79       }
80
81       if (height < minSize) {
82         bottomLeft.y += ((minSize - height) / 2);
83         topRight.y -= ((minSize - height) / 2);
84       }
85
86       changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
87                                         map.unproject(topRight));
88     }
89
90     this.updateChangesetLocations(map);
91     this.reorderChangesets();
92   },
93
94   updateChangesetLocations: function (map) {
95     const mapCenterLng = map.getCenter().lng;
96
97     for (const changeset of this._changesets.values()) {
98       const changesetSouthWest = changeset.bounds.getSouthWest();
99       const changesetNorthEast = changeset.bounds.getNorthEast();
100       const changesetCenterLng = (changesetSouthWest.lng + changesetNorthEast.lng) / 2;
101       const shiftInWorldCircumferences = Math.round((changesetCenterLng - mapCenterLng) / 360);
102
103       if (shiftInWorldCircumferences) {
104         changesetSouthWest.lng -= shiftInWorldCircumferences * 360;
105         changesetNorthEast.lng -= shiftInWorldCircumferences * 360;
106
107         for (const layer of this._bboxLayers) {
108           layer.updateChangesetLayerBounds(changeset);
109         }
110       }
111     }
112   },
113
114   reorderChangesets: function () {
115     const changesetEntries = [...this._changesets];
116     changesetEntries.sort(([, a], [, b]) => {
117       const aInViewport = !a.sidebarRelativePosition;
118       const bInViewport = !b.sidebarRelativePosition;
119       if (aInViewport !== bInViewport) return aInViewport - bInViewport;
120       return b.bounds.getSize() - a.bounds.getSize();
121     });
122     this._changesets = new Map(changesetEntries);
123
124     for (const layer of this._bboxLayers) {
125       layer.clearLayers();
126     }
127
128     for (const changeset of this._changesets.values()) {
129       this._areaLayer.addChangesetLayer(changeset);
130     }
131
132     for (const changeset of this._changesets.values()) {
133       this._borderLayer.addChangesetLayer(changeset);
134     }
135   },
136
137   toggleChangesetHighlight: function (id, state) {
138     const changeset = this._changesets.get(id);
139     if (!changeset) return;
140
141     if (state) {
142       this._highlightLayer.addChangesetLayer(changeset);
143     } else {
144       this._highlightLayer.removeLayer(id);
145     }
146   },
147
148   setChangesetSidebarRelativePosition: function (id, state) {
149     const changeset = this._changesets.get(id);
150     if (!changeset) return;
151     changeset.sidebarRelativePosition = state;
152   }
153 });
154
155 OSM.HistoryChangesetsLayer.addInitHook(function () {
156   this._changesets = new Map;
157
158   this._bboxLayers = [
159     this._areaLayer = new OSM.HistoryChangesetBboxAreaLayer().addTo(this),
160     this._borderLayer = new OSM.HistoryChangesetBboxBorderLayer().addTo(this),
161     this._highlightLayer = new OSM.HistoryChangesetBboxHighlightLayer().addTo(this)
162   ];
163 });