All files / src/components/zd-svg-map svg-map.ts

100% Statements 68/68
100% Branches 21/21
100% Functions 13/13
100% Lines 66/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 13193x 93x           93x 9x 9x 9x   9x 9x 9x 9x     9x 9x   9x 9x 9x           9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x       11x       13x 13x       38x       19x 19x       29x 29x 39x 18x   21x           13x 5x 5x 4x 4x 3x           1x       1x       8x 6x 5x   5x 5x 5x 5x   5x   1x           7x 7x 2x   5x   5x 1x     5x 5x 2x   3x   5x     7x      
import { Http } from '@zeedhi/core';
import { ComponentRender } from '../zd-component/component-render';
import { ISvgMap, ISvgMapEvents, TooltipData } from './interfaces';
 
/**
 * Base class for SvgMap component.
 */
export class SvgMap extends ComponentRender implements ISvgMap {
	public areaColor?: string = '#bdbdbd';
	public areaHoverColor?: string = '#999';
	public areaSelectColor?: string = 'primary';
	public declare events: ISvgMapEvents;
	public height: string | number = 'auto';
	public multiple = false;
	public width: string | number = 'auto';
	public svgContent = '';
	public svgContainer!: any;
	public tooltipData?: TooltipData | undefined;
	public loading?: boolean = false;
	public keyColumn = 'id';
 
	private mapAreas: Element[] = [];
	private srcValue = '';
	private selectedAreaValue: string | string[] = '';
 
	protected viewBeforeUpdate?: () => void;
	protected viewUpdate?: () => void;
 
	constructor(props: ISvgMap) {
		super(props);
		this.areaColor = this.getInitValue('areaColor', props.areaColor, this.areaColor);
		this.areaHoverColor = this.getInitValue('areaHoverColor', props.areaHoverColor, this.areaHoverColor);
		this.areaSelectColor = this.getInitValue('areaSelectColor', props.areaSelectColor, this.areaSelectColor);
		this.multiple = this.getInitValue('multiple', props.multiple, this.multiple);
		this.selectedArea = this.getInitValue('selectedArea', props.selectedArea, this.selectedArea);
		this.src = this.getInitValue('src', props.src, this.src);
		this.height = this.getInitValue('height', props.height, this.height);
		this.width = this.getInitValue('width', props.width, this.width);
		this.tooltipData = this.getInitValue('tooltipData', props.tooltipData, this.tooltipData);
		this.keyColumn = this.getInitValue('keyColumn', props.keyColumn, this.keyColumn);
	}
 
	get src() {
		return this.srcValue;
	}
 
	set src(value: string) {
		this.srcValue = value;
		this.getSVG();
	}
 
	get selectedArea() {
		return this.selectedAreaValue;
	}
 
	set selectedArea(value: string | string[]) {
		this.selectedAreaValue = value;
		this.updateSelectedArea();
	}
 
	private updateSelectedArea() {
		const selectedAreas = this.multiple ? this.selectedAreaValue : [this.selectedAreaValue];
		this.mapAreas.forEach((area) => {
			if (selectedAreas.indexOf(area.id) > -1) {
				area.classList.add('active');
			} else {
				area.classList.remove('active');
			}
		});
	}
 
	private getSVG() {
		if (this.srcValue) {
			this.loading = true;
			Http.get(this.srcValue, { baseURL: '' }).then((response) => {
				this.svgContent = response.data;
				this.updateSVGContainer();
				this.loading = false;
			});
		}
	}
 
	public setViewBeforeUpdate(beforeUpdate: () => void) {
		this.viewBeforeUpdate = beforeUpdate;
	}
 
	public setViewUpdate(update: () => void) {
		this.viewUpdate = update;
	}
 
	public updateSVGContainer() {
		if (this.svgContainer && this.svgContent) {
			if (this.svgContent.indexOf('<svg') >= 0 && this.svgContent.indexOf('</svg>') >= 0) {
				if (this.viewBeforeUpdate) this.viewBeforeUpdate();
 
				this.svgContainer.innerHTML = this.svgContent;
				const svgTag = this.svgContainer.getElementsByTagName('svg')[0];
				this.mapAreas = Array.from(svgTag.children);
				this.updateSelectedArea();
 
				if (this.viewUpdate) this.viewUpdate();
			} else {
				throw new Error('Must be a valid svg file');
			}
		}
	}
 
	public mapClick(element: SVGElement) {
		const { id } = element;
		if (!this.multiple) {
			this.selectedArea = id;
		} else {
			this.selectedArea = this.selectedArea || [];
 
			if (!Array.isArray(this.selectedArea)) {
				this.selectedArea = [this.selectedArea];
			}
 
			const idx = this.selectedArea.indexOf(id);
			if (idx > -1) {
				this.selectedArea.splice(idx, 1);
			} else {
				this.selectedArea.push(id);
			}
			this.updateSelectedArea();
		}
 
		this.callEvent('areaClick', { area: element, component: this });
	}
}