-
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
10 changed files
with
734 additions
and
678 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
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
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,82 +1,128 @@ | ||
import initMap from './initMap'; | ||
import initInteraction from './initInteraction'; | ||
import initDefaultUI from './initDefaultUI'; | ||
import initInteractionStyles from './initInteractionStyles'; | ||
// import initInteraction from './initInteraction'; | ||
// import initDefaultUI from './initDefaultUI'; | ||
// import initInteractionStyles from './initInteractionStyles'; | ||
|
||
import dotProp from 'dot-prop'; | ||
import * as dotProp from 'dot-prop'; | ||
import { | ||
DefaultOptionsType, | ||
MAP_TYPES, | ||
mapEventTypes, | ||
mapEvents, | ||
} from './defaults'; | ||
import { IHMapOptions, IHMapOptionsMerged } from '../Map'; | ||
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(); | ||
const initMap = ( | ||
container: React.RefObject<HTMLDivElement>, | ||
mapLayer: any, | ||
mapOptions: IHMapOptions | ||
) => { | ||
// @ts-ignore | ||
const h = window.H; | ||
|
||
// Instantiate (and display) a map object: | ||
return new H.Map(container, dotProp.get(defaultLayers, mapType), mapOptions); | ||
return ( | ||
container.current && new h.Map(container.current, mapLayer, 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) => { | ||
export const initInteraction = ( | ||
map: any, | ||
interactive: boolean, | ||
useEvents: boolean, | ||
events: typeof mapEvents | ||
) => { | ||
console.log(interactive, useEvents, map); | ||
// @ts-ignore | ||
const h = window.H; | ||
const behavior = interactive | ||
? new h.mapevents.Behavior(new h.mapevents.MapEvents(map)) | ||
: null; | ||
if (useEvents && interactive) { | ||
for (const type in events) { | ||
if (events.hasOwnProperty(type)) { | ||
const callback = events[type as mapEventTypes]; | ||
if (callback && typeof callback === 'function') { | ||
map.addEventListener(type, callback); | ||
} | ||
} | ||
} | ||
} | ||
return behavior; | ||
}; | ||
|
||
export const initDefaultUI = ( | ||
platform: any, | ||
map: any, | ||
includeUI: boolean, | ||
uiLang?: string | ||
) => { | ||
if (!includeUI) { | ||
throw new Error('includeUI must be set to true to initialize default UI'); | ||
} | ||
|
||
// @ts-ignore | ||
const h = window.H; | ||
// Create the default UI components | ||
return h.ui.UI.createDefault(map, platform.createDefaultLayers(), uiLang); | ||
}; | ||
|
||
export const buildMap = ( | ||
platform: any, | ||
options: IHMapOptionsMerged & DefaultOptionsType | ||
) => { | ||
// Get values from the options | ||
const { | ||
useEvents, | ||
mapEvents, | ||
interactive, | ||
includeUI, | ||
// includeUI, | ||
mapType, | ||
MAP_TYPE, | ||
mapTypes, | ||
mapOptions, | ||
uiLang, | ||
// uiLang, | ||
container, | ||
build, | ||
} = options; | ||
|
||
const _mapType = mapType || MAP_TYPE; | ||
|
||
let ret = { options: { ...options, MAP_TYPE: _mapType }, platform }; | ||
const retObject: { | ||
map?: any; | ||
interaction?: any; | ||
ui?: any; | ||
options: typeof options & { mapType: MAP_TYPES }; | ||
} = { | ||
options: { ...options, mapType: mapType || 'vector.normal.map' }, | ||
}; | ||
|
||
if (container && build) { | ||
// Create a Map | ||
ret.map = initMap(platform, container, mapOptions, mapTypes, _mapType); | ||
ret.interaction = initInteraction( | ||
ret.map, | ||
interactive, | ||
useEvents, | ||
mapEvents | ||
validateMapType(retObject.options.mapType); | ||
// Get all the default layers so we can set which to use based on the map type | ||
const defaultLayers = platform.createDefaultLayers(); | ||
|
||
const mapLayer = dotProp.getProperty( | ||
defaultLayers, | ||
retObject.options.mapType | ||
); | ||
if (includeUI) { | ||
ret.ui = initDefaultUI(platform, ret.map, includeUI, uiLang); | ||
// Create a Map | ||
retObject.map = mapLayer && initMap(container, mapLayer, mapOptions); | ||
while (interactive && !retObject.interaction) { | ||
retObject.interaction = initInteraction( | ||
retObject.map, | ||
interactive, | ||
useEvents, | ||
mapEvents | ||
); | ||
// if (includeUI) { | ||
// retObject.ui = initDefaultUI(platform, retObject.map, includeUI, uiLang); | ||
// } | ||
// Adds the grabbing to the document | ||
// initInteractionStyles(); | ||
} | ||
// Adds the grabbing to the document | ||
initInteractionStyles(); | ||
} else { | ||
ret.createMap = initMap; | ||
ret.createPlatform = initPlatform; | ||
ret.createInteraction = initInteraction; | ||
ret.createDefaultUI = initDefaultUI; | ||
ret.createInteractionStyles = initInteractionStyles; | ||
// ret.createMap = initMap; | ||
// ret.createPlatform = initPlatform; | ||
// ret.createInteraction = initInteraction; | ||
// ret.createDefaultUI = initDefaultUI; | ||
// ret.createInteractionStyles = initInteractionStyles; | ||
} | ||
return ret; | ||
return retObject; | ||
}; |
Oops, something went wrong.