Kenneth Bruen
3 years ago
5 changed files with 67 additions and 4 deletions
@ -0,0 +1,29 @@
|
||||
import L from "leaflet"; |
||||
import { DOMCreateElement } from "./jsxDomCreateElement"; |
||||
|
||||
type ButtonControlConstructorOptions = { |
||||
text?: string, |
||||
position?: L.ControlPosition, |
||||
onClick?: () => void, |
||||
} |
||||
|
||||
export class ButtonControl extends L.Control { |
||||
text = ''; |
||||
onClick: (() => void) | undefined; |
||||
|
||||
constructor({ text, position, onClick }: ButtonControlConstructorOptions = {}) { |
||||
super({ position: position ?? 'bottomright' }); |
||||
this.onClick = onClick; |
||||
if (text) { |
||||
this.text = text; |
||||
} |
||||
} |
||||
|
||||
getContainer(): HTMLElement { |
||||
const button: HTMLButtonElement = ( |
||||
<button>{this.text}</button> |
||||
); |
||||
button.addEventListener('click', this.onClick); |
||||
return button; |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
function DOMParseElement( |
||||
element: string, |
||||
properties: { [k: string]: any } | null, |
||||
children: Array<Node | string>): HTMLElement { |
||||
const el = document.createElement(element); |
||||
if (properties) { |
||||
Object.keys(properties).forEach(k => { |
||||
el[k] = properties[k]; |
||||
}); |
||||
} |
||||
children.map(c => typeof c === 'string' ? document.createTextNode(c) : c).forEach(c => { |
||||
el.appendChild(c); |
||||
}); |
||||
return el; |
||||
} |
||||
|
||||
export function DOMCreateElement( |
||||
element: ((...args: unknown[]) => HTMLElement) | string, |
||||
properties: { [k: string]: any } | null, |
||||
...children: Array<Node | string>): HTMLElement { |
||||
if (typeof element === 'function') { |
||||
return element({ |
||||
...(properties ? properties : {}), |
||||
children |
||||
}); |
||||
} |
||||
else { |
||||
return DOMParseElement(element, properties, children); |
||||
} |
||||
} |
Loading…
Reference in new issue