Skip to content

Commit

Permalink
style: add comments for folding
Browse files Browse the repository at this point in the history
  • Loading branch information
Hsieh Chin Fan committed Nov 13, 2024
1 parent 6f032bd commit 1697f8c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 145 deletions.
18 changes: 7 additions & 11 deletions src/BaseRenderer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class MapOption {
* Class: base class for Map Renderer
*/
export default class {
// properties {{{
/** fields */
width = '300px'
height = '300px'
center = [121, 24]
Expand Down Expand Up @@ -87,7 +87,6 @@ export default class {
]
}

// }}}
validateOption (option, value) {
const isValid = this.constructor.validOptions.find(
opt => opt.valueOf() === option,
Expand All @@ -97,7 +96,7 @@ export default class {
return isValid(value)
}

// Valid Options {{{
/** options *
static validOptions = Object.freeze([
new MapOption({
name: 'id',
Expand Down Expand Up @@ -202,8 +201,8 @@ export default class {
isValid: () => true,
}),
])
// }}}
/** step: options */
setOptionAliases (config) {
if (config.XYZ) {
const xyzArray =
Expand Down Expand Up @@ -266,6 +265,7 @@ export default class {
})
}

/** step: HTMLElement */
createView ({ target, width, height }) {
target.style.width = width
target.style.height = height
Expand All @@ -274,6 +274,7 @@ export default class {
}
}

/** step: draw */
setDraw = ({ target, terraDrawAdapter }) => {
const idPrefix = target?.id ? target.id + '-' : ''
const options = {
Expand All @@ -296,6 +297,7 @@ export default class {
return this.terraDraw
}

/** options: not-implemented */
getTerraDrawAdapter () {
return { state: 'skip' }
}
Expand Down Expand Up @@ -336,13 +338,7 @@ export default class {
this.updateCamera({ center: this.center, zoom: this.zoom })
}

showLayerSwitcher (data) {
const wmtsRecords = data.filter(record => record.type === 'wmts')
const tileRecords = data.filter(record => record.type === 'tile')

return wmtsRecords.length > 0 || tileRecords.length > 1
}

/** utils: eval */
propsForEval () {
let currentPrototype = this
let props = []
Expand Down
16 changes: 15 additions & 1 deletion src/BasicDrawComponent.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** Import */
import {
TerraDraw,
TerraDrawSelectMode,
Expand All @@ -9,7 +10,13 @@ import {
TerraDrawRenderMode,
} from 'terra-draw'

// ref: https://github.com/JamesLMilner/terra-draw/blob/main/guides/4.MODES.md#selection-mode
/**
* BasicDrawComponent.
* ref: https://github.com/JamesLMilner/terra-draw/blob/main/guides/4.MODES.md#selection-mode
* @param {TerraDrawBaseAdapter} adapter
* @param {Object} options
* @return TerraDratxw
*/
export const BasicDrawComponent = (adapter, options = {}) =>
new TerraDraw({
adapter,
Expand Down Expand Up @@ -53,6 +60,13 @@ export const BasicDrawComponent = (adapter, options = {}) =>
...options,
})

/**
* addSimpleSelector.
*
* @param {HTMLElement} target
* @param {TerraDraw} draw
* @param {Object} options
*/
export const addSimpleSelector = (target, draw, options = {}) => {
const selector = document.createElement('select')
target.appendChild(selector)
Expand Down
96 changes: 34 additions & 62 deletions src/BasicLeafletRenderer.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import defaultExport, { loadCSS } from './BaseRenderer'
import {
renderWith,
renderByYamlWith,
renderByScriptTargetWith,
} from './mapclay.mjs'
import * as L from 'leaflet/dist/leaflet-src.esm'
import { TerraDrawLeafletAdapter } from 'terra-draw'
loadCSS('https://unpkg.com/[email protected]/dist/leaflet.css')

/** class: Leaflet */
const Renderer = class extends defaultExport {
/** fields */
id = 'leaflet'
version = '1.9.4'
L = L

/** options: center, zoom */
addMap ({ target, center, zoom }) {
const [x, y] = center
this.map = L.map(target).setView([y, x], zoom)
Expand All @@ -26,13 +24,15 @@ const Renderer = class extends defaultExport {
return this.map
}

/** options: draw */
getTerraDrawAdapter ({ draw, L, map }) {
if (!draw) return { state: 'skip' }

this.terraDrawAdapter = new TerraDrawLeafletAdapter({ lib: L, map })
return this.getTerraDrawAdapter
}

/** options: control */
setControl ({ map, control }) {
if (!control || Object.values(control).filter(v => v).length === 0) { return { state: 'skip' } }

Expand Down Expand Up @@ -71,7 +71,7 @@ const Renderer = class extends defaultExport {
return new L.GridLayer.GridDebug()
}

// Configure extra stuff
/** options: debug, eval */
setExtra (config) {
const { map, debug } = config
if (!debug && !config.eval) return { state: 'skip' }
Expand All @@ -87,32 +87,7 @@ const Renderer = class extends defaultExport {
}
}

addMarker (config) {
const options = config.element
? {
html: config.element.innerHTML,
iconSize: config.size,
iconAnchor: config.anchor,
}
: {
html: this.svgPin.html,
iconSize: this.svgPin.size,
iconAnchor: this.svgPin.anchor,
}
const markerIcon = L.divIcon({
...options,
className: 'marker',
})
const xy = Array.from(config.xy).reverse()
const marker = L.marker(xy, { icon: markerIcon })
.addTo(this.map)
const element = marker.getElement()
element.classList.add('marker')

element.remove = () => this.map.removeLayer(marker)
return element
}

/** options: data */
addTileData ({ map, data }) {
const tileData = data.filter(d => d.type === 'tile')

Expand All @@ -136,39 +111,34 @@ const Renderer = class extends defaultExport {
}
}

addGPXFile ({ map, data }) {
const gpxUrl = data.find(record => record.type === 'gpx')
if (!gpxUrl) return { state: 'skip' }

const script = document.createElement('script')
script.src =
'https://cdnjs.cloudflare.com/ajax/libs/leaflet-gpx/1.7.0/gpx.min.js'
document.body.append(script)
/** actions: marker */
addMarker (config) {
const options = config.element
? {
html: config.element.innerHTML,
iconSize: config.size,
iconAnchor: config.anchor,
}
: {
html: this.svgPin.html,
iconSize: this.svgPin.size,
iconAnchor: this.svgPin.anchor,
}
const markerIcon = L.divIcon({
...options,
className: 'marker',
})
const xy = Array.from(config.xy).reverse()
const marker = L.marker(xy, { icon: markerIcon })
.addTo(this.map)
const element = marker.getElement()
element.classList.add('marker')

const options = {
gpx_options: {
joinTrackSegments: false,
},
polyline_options: {
color: 'red',
weight: 3,
lineCap: 'round',
},
marker_options: {
startIconUrl: null,
endIconUrl: null,
shadowUrl: '',
wptIconUrls: {
'': null,
},
},
async: true,
}
script.onload = () => {
new L.GPX(gpxUrl, options).addTo(map)
}
element.remove = () => this.map.removeLayer(marker)
return element
}

/** actions: camera */
async updateCamera ({ center, zoom, bounds, animation, padding, duration }) {
const latLon = center ? L.latLng(center[1], center[0]) : this.map.getCenter()
const options = {
Expand Down Expand Up @@ -196,6 +166,7 @@ const Renderer = class extends defaultExport {
})
}

/** utils: projection */
project ([lng, lat]) {
return this.map.latLngToContainerPoint([lat, lng])
}
Expand All @@ -206,4 +177,5 @@ const Renderer = class extends defaultExport {
}
}

/** export */
export default Renderer
Loading

0 comments on commit 1697f8c

Please sign in to comment.