Skip to content

Commit

Permalink
WIP: Example integrating ol-side-panel
Browse files Browse the repository at this point in the history
  • Loading branch information
symbioquine committed Jun 27, 2021
1 parent 3ea171d commit 1c3af74
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 16 deletions.
82 changes: 67 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
"eslint-plugin-react": "^7.23.1",
"mini-css-extract-plugin": "^1.6.0",
"style-loader": "^0.23.1",
"svg-inline-loader": "^0.8.2",
"webpack": "^5.36.0",
"webpack-cli": "^4.6.0",
"webpack-dev-server": "^4.0.0-beta.2"
},
"dependencies": {
"bootstrap-icons": "^1.5.0",
"ol": "^6.5.0",
"ol-geocoder": "^4.1.2",
"ol-grid": "^1.1.4",
"ol-layerswitcher": "^3.7.0",
"ol-popup": "^4.0.0"
"ol-popup": "^4.0.0",
"ol-side-panel": "^1.0.4"
}
}
1 change: 1 addition & 0 deletions src/MapInstanceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MapInstanceManager {
// instances.
this.behaviors = {
rememberLayer: namedBehaviors.rememberLayer,
layerSwitcherInSidePanel: namedBehaviors.layerSwitcherInSidePanel,
};

}
Expand Down
1 change: 1 addition & 0 deletions src/behavior/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function lazyLoadedBehavior(name) {

export default {
edit: lazyLoadedBehavior('edit'),
layerSwitcherInSidePanel: lazyLoadedBehavior('layerSwitcherInSidePanel'),
measure: lazyLoadedBehavior('measure'),
rememberLayer: lazyLoadedBehavior('rememberLayer'),
snappingGrid: lazyLoadedBehavior('snappingGrid'),
Expand Down
46 changes: 46 additions & 0 deletions src/behavior/layerSwitcherInSidePanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import LayerSwitcher from 'ol-layerswitcher';

import layersHalfIcon from 'bootstrap-icons/icons/layers-half.svg';

// layer switcher in side panel behavior.
export default {
attach(instance) {
const existingLayerSwitcherControl = instance.map.getControls().getArray()
.find(control => typeof control.renderPanel === 'function');

const sidePanel = instance.map.getControls().getArray()
.find(control => typeof control.definePane === 'function');

if (!sidePanel) {
return;
}

const layersPane = sidePanel.definePane({
paneId: 'layers',
name: 'Layers',
icon: layersHalfIcon,
});

const layersDiv = document.createElement('div');
layersDiv.classList = 'layer-switcher';

layersPane.addWidgetElement(layersDiv);

const renderLayerSwitcher = () => LayerSwitcher.renderPanel(
instance.map,
layersDiv,
{ reverse: true },
);

renderLayerSwitcher();

// When new layers are added, refresh the layer layer switcher
instance.map.on('farmOS-map.layer', () => {
renderLayerSwitcher();
});

instance.map.removeControl(existingLayerSwitcherControl);

},

};
5 changes: 5 additions & 0 deletions src/instance/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import {
PinchRotate,
} from 'ol/interaction';

// Import ol-side-panel control.
import 'ol-side-panel/dist/ol-side-panel.css';
import { SidePanel } from 'ol-side-panel';

// Import Geolocate control.
import Geolocate from '../control/Geolocate/Geolocate';

Expand Down Expand Up @@ -68,6 +72,7 @@ const defaults = {
limit: 5,
autoComplete: true,
}),
new SidePanel(),
];

// If controls were set to 'false', don't attach any controls.
Expand Down
18 changes: 18 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,21 @@
.layer-switcher.shown {
max-height: 300px;
}

.ol-side-panel-pane .layer-switcher {
left: 0px;
}

.ol-side-panel-pane .layer-switcher > ul {
margin-top: 0px;
}

.ol-side-panel-tabs button svg {
margin: 5px;
width: 28px;
}

.ol-side-panel-tabs button.active {
background-color: #ffcc33;
background-color: rgba(255,204,51,0.7);
}
4 changes: 4 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module.exports = {
{ loader: 'css-loader' },
],
},
{
test: /\.svg$/,
loader: 'svg-inline-loader'
},
],
},
plugins: [
Expand Down

0 comments on commit 1c3af74

Please sign in to comment.