1 OSM.HistoryChangesetBboxLayer = L.FeatureGroup.extend({
2 getLayerId: function (layer) {
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);
13 updateChangesetLayerBounds: function (changeset) {
14 this.getLayer(changeset.id)?.setBounds(changeset.bounds);
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";
23 return "changeset-in-sidebar-viewport";
28 OSM.HistoryChangesetBboxAreaLayer = OSM.HistoryChangesetBboxLayer.extend({
29 _getChangesetStyle: function (changeset) {
33 className: this._getSidebarRelativeClassName(changeset)
38 OSM.HistoryChangesetBboxBorderLayer = OSM.HistoryChangesetBboxLayer.extend({
39 _getChangesetStyle: function (changeset) {
42 color: "var(--changeset-border-color)",
44 className: this._getSidebarRelativeClassName(changeset)
49 OSM.HistoryChangesetBboxHighlightLayer = OSM.HistoryChangesetBboxLayer.extend({
50 _getChangesetStyle: function (changeset) {
54 color: "var(--changeset-border-color)",
55 fillColor: "var(--changeset-fill-color)",
57 className: this._getSidebarRelativeClassName(changeset) + " changeset-highlighted"
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);
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
76 if (width < minSize) {
77 bottomLeft.x -= ((minSize - width) / 2);
78 topRight.x += ((minSize - width) / 2);
81 if (height < minSize) {
82 bottomLeft.y += ((minSize - height) / 2);
83 topRight.y -= ((minSize - height) / 2);
86 changeset.bounds = L.latLngBounds(map.unproject(bottomLeft),
87 map.unproject(topRight));
90 this.updateChangesetLocations(map);
91 this.reorderChangesets();
94 updateChangesetLocations: function (map) {
95 const mapCenterLng = map.getCenter().lng;
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);
103 if (shiftInWorldCircumferences) {
104 changesetSouthWest.lng -= shiftInWorldCircumferences * 360;
105 changesetNorthEast.lng -= shiftInWorldCircumferences * 360;
107 for (const layer of this._bboxLayers) {
108 layer.updateChangesetLayerBounds(changeset);
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();
122 this._changesets = new Map(changesetEntries);
124 for (const layer of this._bboxLayers) {
128 for (const changeset of this._changesets.values()) {
129 this._areaLayer.addChangesetLayer(changeset);
132 for (const changeset of this._changesets.values()) {
133 this._borderLayer.addChangesetLayer(changeset);
137 toggleChangesetHighlight: function (id, state) {
138 const changeset = this._changesets.get(id);
139 if (!changeset) return;
142 this._highlightLayer.addChangesetLayer(changeset);
144 this._highlightLayer.removeLayer(id);
148 setChangesetSidebarRelativePosition: function (id, state) {
149 const changeset = this._changesets.get(id);
150 if (!changeset) return;
151 changeset.sidebarRelativePosition = state;
155 OSM.HistoryChangesetsLayer.addInitHook(function () {
156 this._changesets = new Map;
159 this._areaLayer = new OSM.HistoryChangesetBboxAreaLayer().addTo(this),
160 this._borderLayer = new OSM.HistoryChangesetBboxBorderLayer().addTo(this),
161 this._highlightLayer = new OSM.HistoryChangesetBboxHighlightLayer().addTo(this)