-
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
14 changed files
with
1,873 additions
and
1,579 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
node_modules | ||
.cache | ||
dist | ||
storybook-static | ||
storybook-static | ||
.rough/ |
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,8 @@ | ||
import React from 'react'; | ||
|
||
export interface IHMapProps {} | ||
|
||
export const HMap = () => { | ||
// const Platform = useHPlatform() | ||
return <div>HMap</div>; | ||
}; |
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 @@ | ||
export * from './Map'; |
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,45 @@ | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import { ILoadHMapOptions, loadHMap } from '../libs/loadHMap'; | ||
import { initHPlatform } from '../libs/initPlatform'; | ||
import { DefaultOptionsType } from '../libs/defaults'; | ||
import { PlatformContext } from '../../contexts/platform'; | ||
|
||
export interface IHPlatform extends ILoadHMapOptions { | ||
children: React.ReactNode | React.ReactNode[]; | ||
} | ||
|
||
export interface IHPlatformState { | ||
options?: DefaultOptionsType; | ||
platform?: any; | ||
reInitMap?: () => void; | ||
} | ||
|
||
export const HPlatform = (props: IHPlatform) => { | ||
const loadMapCB = useCallback(() => { | ||
loadHMap(props).then((options: DefaultOptionsType) => { | ||
const platform = initHPlatform(options); | ||
setPlatformState((prevState: IHPlatformState) => ({ | ||
...prevState, | ||
platform, | ||
options, | ||
})); | ||
}); | ||
}, [props]); | ||
const [platformState, setPlatformState] = useState<IHPlatformState>({ | ||
reInitMap: loadMapCB, | ||
platform: {}, | ||
}); | ||
|
||
useEffect(() => { | ||
loadMapCB(); | ||
}, [platformState.platform.A]); | ||
const { platform, options } = platformState; | ||
|
||
return ( | ||
<PlatformContext.Provider value={platformState}> | ||
{platform?.A == 'api.here.com' && | ||
(options?.app_code || options?.apikey) && | ||
props.children} | ||
</PlatformContext.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 @@ | ||
export * from './Map'; |
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,98 @@ | ||
/** | ||
* @type {string} Default version for the API | ||
*/ | ||
const VERSION = 'v3/3.0'; | ||
export type MAP_TYPES = | ||
| 'normal.map' | ||
| 'normal.xbase' | ||
| 'normal.xbasenight' | ||
| 'normal.basen' | ||
| 'normal.basenight' | ||
| 'normal.mapnight' | ||
| 'normal.traffic' | ||
| 'normal.trafficnight' | ||
| 'normal.transit' | ||
| 'normal.panoram' | ||
| 'normal.panoramnight' | ||
| 'normal.labels' | ||
| 'normal.metaInfo' | ||
| 'satellite.xbase' | ||
| 'satellite.base' | ||
| 'satellite.map' | ||
| 'satellite.traffic' | ||
| 'satellite.panorama' | ||
| 'satellite.labels' | ||
| 'terrain.xbase' | ||
| 'terrain.base' | ||
| 'terrain.map' | ||
| 'terrain.traffic' | ||
| 'terrain.panorama' | ||
| 'terrain.labels' | ||
| 'incidents' | ||
| 'venues'; | ||
|
||
const MAP_TYPE: MAP_TYPES = 'normal.map'; | ||
|
||
const mapOptions = { | ||
zoom: 8, | ||
center: { | ||
lat: 6.5243793, | ||
lng: 3.3792057, | ||
}, | ||
}; | ||
const useEvents = false; | ||
const interactive = false; | ||
const includeUI = false; | ||
const containerId = 'HERE_MAP_CONTAINER'; | ||
|
||
type mapEventTypes = | ||
| 'pointerdown' | ||
| 'pointerup' | ||
| 'pointermove' | ||
| 'pointerenter' | ||
| 'pointerleave' | ||
| 'pointercancel' | ||
| 'dragstart' | ||
| 'drag' | ||
| 'dragend' | ||
| 'tab' | ||
| 'dbltap'; | ||
|
||
const defaultClassName = 'here-map-container'; | ||
|
||
const includePlaces = false; | ||
|
||
// Function that does really nothing, still it is a function, and has its right! | ||
const noop = () => {}; | ||
|
||
const mapEvents: Record<mapEventTypes, typeof noop> = { | ||
pointercancel: noop, | ||
drag: noop, | ||
dragend: noop, | ||
tab: noop, | ||
dbltap: noop, | ||
pointerdown: noop, | ||
pointerenter: noop, | ||
pointerleave: noop, | ||
pointermove: noop, | ||
pointerup: noop, | ||
dragstart: noop, | ||
}; | ||
|
||
export type DefaultOptionsType = typeof defaultOptions; | ||
|
||
export const defaultOptions = { | ||
VERSION, | ||
mapEvents, | ||
MAP_TYPE, | ||
mapOptions, | ||
interactive, | ||
includeUI, | ||
includePlaces, | ||
useEvents, | ||
containerId, | ||
defaultClassName, | ||
app_id: '', | ||
app_code: '', | ||
apikey: '', | ||
}; |
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 @@ | ||
declare module '@limistah/here-map-js'; |
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,14 @@ | ||
import { DefaultOptionsType } from './defaults'; | ||
|
||
export const initHPlatform = (options: DefaultOptionsType) => { | ||
const { app_id, app_code, apikey } = options; | ||
if ((!app_id || !app_code) && !apikey) { | ||
throw new Error('Options must include appId and appCode OR an apiKey'); | ||
} | ||
// @ts-ignore | ||
const h = window.H; | ||
if (typeof h === 'undefined' || !h.service) { | ||
throw new Error('Here Map JavaScript files are not loaded.'); | ||
} | ||
return new h.service.Platform(options); | ||
}; |
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,40 @@ | ||
import hereMapJS from '@limistah/here-map-js'; | ||
import { DefaultOptionsType, defaultOptions } from './defaults'; | ||
import merge from 'lodash.merge'; | ||
|
||
// Merges the option with the defaults to create a unison and make required values available | ||
const optionMerger = (options: ILoadHMapOptions) => | ||
merge(defaultOptions, options); | ||
|
||
export interface ILoadHMapOptions { | ||
version?: string; // Version of the api to load. Defaults to v3 | ||
interactive?: boolean; // Adds interactive scripts | ||
includeUI?: boolean; // Should add the UI scripts | ||
includePlaces?: boolean; // Include the places script | ||
} | ||
|
||
export const loadHMap = async ( | ||
options: ILoadHMapOptions = { | ||
includePlaces: false, | ||
includeUI: false, | ||
interactive: false, | ||
version: 'v3/3.0', | ||
} | ||
): Promise<DefaultOptionsType> => { | ||
const mergedOptions = optionMerger(options); | ||
const { | ||
VERSION, | ||
version, | ||
interactive, | ||
includeUI, | ||
includePlaces, | ||
} = mergedOptions; | ||
// Returns async loading of the component | ||
// First load the core, to save us reference error if all of the libraries are loaded asynchronously due to race conditions | ||
return hereMapJS({ | ||
includeUI, | ||
includePlaces, | ||
interactive, | ||
version: version || VERSION, | ||
}).then(() => mergedOptions); | ||
}; |
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 { IHPlatformState } from '../components/Platform'; | ||
|
||
// create a hook for the platform context | ||
export const PlatformContext = createContext<IHPlatformState>({}); |
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,20 @@ | ||
import React from 'react'; | ||
import { Meta, Story } from '@storybook/react'; | ||
import { HMap, IHMapProps } from '../src'; | ||
|
||
const meta: Meta = { | ||
title: 'HMap', | ||
component: HMap, | ||
argTypes: {}, | ||
parameters: {}, | ||
}; | ||
|
||
export default meta; | ||
|
||
const Template: Story<IHMapProps> = args => <HMap {...args} />; | ||
|
||
// By passing using the Args format for exported stories, you can control the props for a component for reuse in a test | ||
// https://storybook.js.org/docs/react/workflows/unit-testing | ||
export const Default = Template.bind({}); | ||
|
||
Default.args = {}; |
Oops, something went wrong.