LiveAtlas/src/leaflet/icon/DynmapIcon.ts

69 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-12-12 22:04:56 +00:00
import {DivIconOptions, PointExpression, Icon, DivIcon, DomUtil, point} from 'leaflet';
2020-12-01 23:20:38 +00:00
export interface DynmapIconOptions extends DivIconOptions {
2020-12-01 23:20:38 +00:00
icon: string;
label: string;
showLabel: boolean;
isHtml?: boolean;
}
2020-12-12 22:04:56 +00:00
export class DynmapIcon extends DivIcon {
2020-12-01 23:20:38 +00:00
static defaultOptions: DynmapIconOptions = {
icon: 'default',
label: '',
iconSize: [16, 16],
2020-12-01 23:20:38 +00:00
showLabel: false,
isHtml: false,
className: '',
};
// @ts-ignore
options: DynmapIconOptions;
2020-12-01 23:20:38 +00:00
constructor(options: DynmapIconOptions) {
super(Object.assign(DynmapIcon.defaultOptions, options));
}
createIcon(oldIcon: HTMLElement) {
if (oldIcon) {
2020-12-12 22:04:56 +00:00
DomUtil.remove(oldIcon);
2020-12-01 23:20:38 +00:00
}
const div = document.createElement('div'),
2020-12-01 23:20:38 +00:00
img = document.createElement('img'),
label = document.createElement('span'),
url = `${window.config.url.markers}_markers_/${this.options.icon}.png`,
2020-12-12 22:04:56 +00:00
size = point(this.options.iconSize as PointExpression);
2020-12-01 23:20:38 +00:00
const sizeClass = [size.x, size.y].join('x');
2020-12-01 23:20:38 +00:00
img.className = `marker__icon marker__icon--${sizeClass}`;
2020-12-01 23:20:38 +00:00
img.src = url;
label.className = this.options.showLabel ? 'marker__label marker__label--show' : 'marker__label';
label.classList.add(/*'markerName_' + set.id,*/ `marker__label--${sizeClass}`);
2020-12-01 23:20:38 +00:00
if (this.options.isHtml) {
label.insertAdjacentHTML('afterbegin', this.options.label);
} else {
label.textContent = this.options.label;
}
// @ts-ignore
2020-12-12 22:04:56 +00:00
Icon.prototype._setIconStyles.call(this, div, 'icon');
2020-12-01 23:20:38 +00:00
div.appendChild(img);
div.appendChild(label);
div.classList.add('marker');
if(this.options.className) {
div.classList.add(this.options.className);
}
2020-12-01 23:20:38 +00:00
console.log(div.className);
2020-12-01 23:20:38 +00:00
return div;
2020-12-01 23:20:38 +00:00
}
}