-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
206 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ | |
}, | ||
"dependencies": { | ||
"@limistah/here-map-js": "^3.1.0", | ||
"dot-prop": "^8.0.2", | ||
"lodash.merge": "^4.6.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,65 @@ | ||
import React from 'react'; | ||
import React, { useContext, useEffect, useRef, useState } from 'react'; | ||
import { defaultOptions } from '../libs/defaults'; | ||
import { PlatformContext } from '../../contexts/platform'; | ||
import { MapContext } from '../../contexts/map'; | ||
import merge from 'lodash.merge'; | ||
|
||
export interface IHMapProps {} | ||
export interface IHMapProps { | ||
loadingEl: React.ReactNode; | ||
style: React.CSSProperties; | ||
container: React.RefObject<HTMLDivElement>; | ||
children: React.ReactNode | React.ReactNode[]; | ||
options: {}; | ||
} | ||
|
||
export const HMap = () => { | ||
export interface IHMapState {} | ||
|
||
export const HMap = (props: IHMapProps) => { | ||
// const Platform = useHPlatform() | ||
return <div>HMap</div>; | ||
const platformState = useContext(PlatformContext); | ||
|
||
const containerRef = props.container || useRef<HTMLDivElement>(); | ||
|
||
const [mapState, setMapState] = useState<IHMapState>({}); | ||
|
||
useEffect(() => { | ||
const mergedOptions = merge( | ||
{ | ||
container, | ||
build: true, | ||
}, | ||
props.options | ||
); | ||
const buildResult = buildMap(platformState.platform, mergedOptions); | ||
|
||
setMapState(buildResult); | ||
}, [props]); | ||
|
||
const { style, loadingEl, container, children } = props; | ||
// const { options } = this.state.builder; | ||
|
||
const options = {}; | ||
|
||
const LoadingComponent = () => { | ||
return <div>Loading</div>; | ||
}; | ||
|
||
const loading = loadingEl || <LoadingComponent />; | ||
|
||
// @ts-ignore | ||
const h = window.H; | ||
|
||
return ( | ||
<MapContext.Provider value={mapState}> | ||
<div | ||
id={defaultOptions.containerId} | ||
className={defaultOptions.defaultClassName} | ||
style={style} | ||
ref={containerRef} | ||
> | ||
{typeof h === 'undefined' && !options && loading} | ||
{typeof h === 'object' && options && children} | ||
</div> | ||
</MapContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import initMap from './initMap'; | ||
import initInteraction from './initInteraction'; | ||
import initDefaultUI from './initDefaultUI'; | ||
import initInteractionStyles from './initInteractionStyles'; | ||
|
||
import dotProp from 'dot-prop'; | ||
import { validateMapType } from './validateMapType'; | ||
import { MAP_TYPES } from './defaults'; | ||
|
||
export default (platform, container, mapOptions, mapType: MAP_TYPES) => { | ||
validateMapType(mapType); | ||
// Get all the default layers so we can set which to use based on the map type | ||
const defaultLayers = platform.createDefaultLayers(); | ||
|
||
// Instantiate (and display) a map object: | ||
return new H.Map(container, dotProp.get(defaultLayers, mapType), mapOptions); | ||
}; | ||
|
||
/** | ||
* The whole library is bootstrapped after the initialization is done using the options | ||
* @property {string} options.version Version of the api to load. Defaults to v3 | ||
* @property {string} options.VERSION Default version Defaults to v3 | ||
* @property {object} options.mapEvents Map events implementation | ||
* @property {object} options.mapOptions Options needed to initialize the map | ||
* @property {object} options.platformOptions Options needed to initialize the map | ||
* @property {boolean} options.interactive Adds interactivity to the MAP | ||
* @property {boolean} options.useEvents Adds event handling to the map | ||
* @property {boolean} options.includeUI Add UI script to the MAP | ||
* @property {boolean} options.build Determines if the map should be built | ||
* @property {Node} options.container DOM Element to hold the MAP | ||
* @property {string} options.uiLang Language of the UI | ||
* @property {build} options.build Flag to tell if the MAP should be build immediately | ||
* @property {string} options.appId Here Map APP ID | ||
* @property {string} options.appCode Here Map APP code | ||
* @property {string} options.mapType The type of the map to load e.g // "normal.map" | ||
* @property {string} options.MAP_TYPE Default map type to load "normal.map" | ||
* @param {object} options Items necessary to run the library | ||
* @returns {object} | ||
*/ | ||
export const buildMap = (platform, options) => { | ||
// Get values from the options | ||
const { | ||
useEvents, | ||
mapEvents, | ||
interactive, | ||
includeUI, | ||
mapType, | ||
MAP_TYPE, | ||
mapTypes, | ||
mapOptions, | ||
uiLang, | ||
container, | ||
build, | ||
} = options; | ||
|
||
const _mapType = mapType || MAP_TYPE; | ||
|
||
let ret = { options: { ...options, MAP_TYPE: _mapType }, platform }; | ||
|
||
if (container && build) { | ||
// Create a Map | ||
ret.map = initMap(platform, container, mapOptions, mapTypes, _mapType); | ||
ret.interaction = initInteraction( | ||
ret.map, | ||
interactive, | ||
useEvents, | ||
mapEvents | ||
); | ||
if (includeUI) { | ||
ret.ui = initDefaultUI(platform, ret.map, includeUI, uiLang); | ||
} | ||
// Adds the grabbing to the document | ||
initInteractionStyles(); | ||
} else { | ||
ret.createMap = initMap; | ||
ret.createPlatform = initPlatform; | ||
ret.createInteraction = initInteraction; | ||
ret.createDefaultUI = initDefaultUI; | ||
ret.createInteractionStyles = initInteractionStyles; | ||
} | ||
return ret; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { MAP_TYPES, mapTypes } from './defaults'; | ||
|
||
export const validateMapType = (mapType: MAP_TYPES) => { | ||
if (!mapTypes.includes(mapType)) { | ||
throw new Error( | ||
'mapType Should be one from https://developer.here.com/documentation/maps/topics/map-types.html in dot notation' | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// provider a provider | ||
// provider a consumer | ||
|
||
import { createContext } from 'react'; | ||
import { IHMapState } from '../components/Map'; | ||
|
||
// create a hook for the platform context | ||
export const MapContext = createContext<IHMapState>({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters