Skip to content

Commit 75101a6

Browse files
committed
fix: added scaleToBottom feature
1 parent e442566 commit 75101a6

File tree

4 files changed

+80
-36
lines changed

4 files changed

+80
-36
lines changed

src/GEOComp.tsx

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,12 @@ var GEOComp = (function () {
164164
buttons: withDefault(JSONObjectControl, "{menu:false,north:false,save:false}"),
165165
features: withDefault(
166166
JSONObjectControl,
167-
"{draw:true,swipe:false,tracker:false,timeline:false,gpsCentered:true,largeButtons:false}"
167+
"{draw:true,swipe:false,tracker:false,timeline:false,gpsCentered:true,largeButtons:false,scaleToBottom:false}"
168168
),
169169
onEvent: eventHandlerControl(eventDefintions),
170170
};
171171

172+
172173
//The Builder function creating the real component
173174
return new UICompBuilder(childrenMap, (props: {
174175
onEvent: any;
@@ -192,11 +193,35 @@ var GEOComp = (function () {
192193
event: any;
193194
feature: any
194195
}) => {
195-
const doDebug = function () { return props.defaults && props.defaults.debug === true }
196-
//Cache for all events
197-
var _events = {}
196+
const doDebug = function () {
197+
return props.defaults && props.defaults.debug === true
198+
}
198199
//Default size of component
199200
const [dimensions, setDimensions] = useState({ width: 650, height: 400 });
201+
//Catch the resizing of component
202+
const { width, height, ref: conRef } = useResizeDetector({
203+
onResize: () => {
204+
const container = conRef.current;
205+
if (!container || !width || !height) return;
206+
207+
if (props.autoHeight) {
208+
setDimensions({
209+
width,
210+
height: dimensions.height,
211+
})
212+
213+
return;
214+
}
215+
216+
setDimensions({
217+
width,
218+
height: height,
219+
})
220+
}
221+
});
222+
223+
//Cache for all events
224+
var _events = {}
200225
//The event handler will also sent the event value to use
201226
const handleEvent = function (name: string, eventObj: any) {
202227
return new Promise((resolve) => {
@@ -218,6 +243,12 @@ var GEOComp = (function () {
218243
case 'click:feature':
219244
props.feature.onChange(eventObj)
220245
break;
246+
case 'window:resize':
247+
if (props.features && props.features.scaleToBottom == true) {
248+
var div = eventObj.window.height - eventObj.element.bottom - 1
249+
setDimensions({ width: dimensions.width, height: dimensions.height + div })
250+
}
251+
break
221252
default:
222253
switch (eventName) {
223254
case 'bbox':
@@ -234,28 +265,6 @@ var GEOComp = (function () {
234265
})
235266
}
236267

237-
//Catch the resizing of component
238-
const { width, height, ref: conRef } = useResizeDetector({
239-
onResize: () => {
240-
const container = conRef.current;
241-
if (!container || !width || !height) return;
242-
243-
if (props.autoHeight) {
244-
setDimensions({
245-
width,
246-
height: dimensions.height,
247-
})
248-
249-
return;
250-
}
251-
252-
setDimensions({
253-
width,
254-
height: height,
255-
})
256-
}
257-
});
258-
259268
//Create the container for the component
260269
return (
261270
<div className={styles.wrapper}

src/vendors/Geo.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ import { RingLoader } from 'react-spinners'
1212
//The real GEO OpenLayers packages
1313
import { Map, View } from 'ol/index';
1414
import { Vector as VectorLayer } from 'ol/layer';
15-
import { TileWMS, Vector as VectorSource } from 'ol/source';
16-
import TileLayer from 'ol/layer/WebGLTile.js';
15+
import { Vector as VectorSource } from 'ol/source';
1716
import { LineString, Polygon } from 'ol/geom';
1817
import { fromLonLat, transformExtent } from 'ol/proj';
1918
import { FullScreen, Zoom } from 'ol/control';
20-
import { GeoJSON } from 'ol/format';
2119
import { Draw, Snap, Select } from 'ol/interaction'
2220

2321
//Openlayer Extend imports
2422
import GeolocationBar from 'ol-ext/control/GeolocationBar'
25-
import GeolocationButton from 'ol-ext/control/GeolocationButton'
2623
import CanvasScaleLine from 'ol-ext/control/CanvasScaleLine'
2724
import Notification from 'ol-ext/control/Notification'
2825
import Bar from 'ol-ext/control/Bar'
@@ -38,13 +35,14 @@ import LayerSwitcher from 'ol-ext/control/LayerSwitcher'
3835
///Local import
3936
import RotateNorthControl from './RotateNorthControl'
4037
import { createLayer } from './helpers/Layers'
41-
import { animate, geoJsonStyleFunction } from './helpers'
38+
import { animate, geoJsonStyleFunction, useScreenSize } from './helpers'
4239

4340
function Geo(props) {
4441
const [geoRef, setGeoRef] = useState();
4542
const [geoLoc, setGeoLoc] = useState();
4643
const [map, setMap] = useState();
4744
const [geoId] = useState(Math.random().toString(16).slice(2));
45+
4846
//Global notification item
4947
const [notification] = useState(new Notification({}))
5048
// Vector layer for drawing
@@ -59,16 +57,12 @@ function Geo(props) {
5957
source: new VectorSource(),
6058
}))
6159

62-
//Function to check if updating of a variable is allowed
63-
const allowUpdate = function (name) {
64-
return !(props.ignoreUpdate && props.ignoreUpdate(name))
65-
}
66-
6760
//Function to check if updating of a variable is allowed
6861
const featureEnabled = function (name) {
6962
return !((props.features && props.features[name] === false))
7063
}
7164

65+
//Fire and event to controling ReactComponent
7266
const fireEvent = function (name, eventObject) {
7367
if (props.onEvent) {
7468
props.onEvent(name, eventObject || {})
@@ -665,6 +659,14 @@ function Geo(props) {
665659
}
666660
}, [elementRef]);
667661

662+
663+
var windowSize = useScreenSize()
664+
useEffect(() => {
665+
var el = document.getElementById('GEO_' + geoId)
666+
fireEvent('window:resize', { window: windowSize, element: el ? el.getBoundingClientRect() : null })
667+
}, [windowSize])
668+
669+
668670
return (
669671
<div
670672
ref={elementRef}

src/vendors/helpers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./Layers";
33
export * from "./Popup";
44
export * from "./Styles";
55
export * from "./Animate";
6+
export * from "./useScreenSize";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// useScreenSize.js
2+
import { useState, useEffect } from 'react';
3+
4+
export function useScreenSize() {
5+
const [screenSize, setScreenSize] = useState({
6+
width: window.innerWidth,
7+
height: window.innerHeight,
8+
});
9+
10+
useEffect(() => {
11+
const handleResize = () => {
12+
const rec = {
13+
width: window.innerWidth,
14+
height: window.innerHeight,
15+
}
16+
clearTimeout(window.resizedFinished);
17+
window.resizedFinished = setTimeout(function () {
18+
setScreenSize(rec);
19+
}, 250);
20+
};
21+
22+
window.addEventListener('resize', handleResize);
23+
24+
25+
// Clean up the event listener when the component unmounts
26+
return () => {
27+
window.removeEventListener('resize', handleResize);
28+
};
29+
}, []);
30+
31+
return screenSize;
32+
};

0 commit comments

Comments
 (0)