Skip to content

Commit

Permalink
refactor: improve consistency & readbility
Browse files Browse the repository at this point in the history
  • Loading branch information
themustafaomar committed Aug 15, 2022
1 parent 5bba8b1 commit 5cf594d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Bringing data to life with interactive maps

This project is a deeply modified version of [jvectormap](https://github.com/bjornd/jvectormap) and was created to help developers who aren't using jQuery anymore in their projects.

Jsvectormap supports all modern brownsers including IE9+
Jsvectormap supports all modern browsers including IE9+

<a href="https://www.npmjs.com/package/jsvectormap"><img src="https://img.shields.io/npm/l/jsvectormap.svg?sanitize=true" alt="License"></a>
<a href="https://npmcharts.com/compare/jsvectormap?minimal=true"><img src="https://img.shields.io/npm/dm/jsvectormap.svg?sanitize=true" alt="Downloads"></a>
Expand Down
8 changes: 5 additions & 3 deletions src/js/components/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ class Tooltip extends BaseComponent {
this._tooltip.classList.remove('active')
}

text(string) {
text(string, html = false) {
const property = html ? 'innerHTML' : 'textContent'

if (!string) {
return this._tooltip.textContent
return this._tooltip[property]
}

this._tooltip.textContent = string
this._tooltip[property] = string
}

css(css) {
Expand Down
68 changes: 35 additions & 33 deletions src/js/core/createMarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ import { merge } from '../util'
import Marker from '../components/marker'

export default function createMarkers(markers = {}, isRecentlyCreated = false) {
let markerConfig, marker, point, uid
let config, marker, point, uid

// Create groups for holding markers and markers labels
// We're checking if `markersGroup` exists or not becuase we may add markers after the map has loaded
// So we will append the futured markers to this group as well.
this.markersGroup = this.markersGroup || this.canvas.createGroup('jvm-markers-group')
this.markerLabelsGroup = this.markerLabelsGroup || this.canvas.createGroup('jvm-markers-labels-group')
this._markersGroup = this._markersGroup || this.canvas.createGroup('jvm-markers-group')
this._markerLabelsGroup = this._markerLabelsGroup || this.canvas.createGroup('jvm-markers-labels-group')

for (let index in markers) {
markerConfig = markers[index]
point = this.getMarkerPosition(markerConfig)
uid = markerConfig.coords.join(':')
config = markers[index]
point = this.getMarkerPosition(config)
uid = config.coords.join(':')

if (!point) {
continue
}

// We're checking if recently created marker does already exist
// If exists we don't need to create it again, so we'll continute
// Becuase we may have more than one marker.
// Becuase we may have more than one marker submitted via `addMarkers` method.
if (isRecentlyCreated) {
if (
Object.keys(this.markers).filter(i => this.markers[i]._uid === uid).length
Expand All @@ -28,33 +32,31 @@ export default function createMarkers(markers = {}, isRecentlyCreated = false) {
index = Object.keys(this.markers).length
}

if (point !== false) {
marker = new Marker({
index,
map: this,
// Merge the `markerStyle` object with the marker config `style` if presented.
style: merge(this.params.markerStyle, { initial: markerConfig.style || {} }, true),
label: this.params.labels && this.params.labels.markers,
labelsGroup: this.markerLabelsGroup,
cx: point.x,
cy: point.y,
group: this.markersGroup,
marker: markerConfig,
isRecentlyCreated,
})

// Check for marker duplication
// this is useful when for example: a user clicks a button for creating marker two times
// so it will remove the old one and the new one will take its place.
if (this.markers[index]) {
this.removeMarkers([index])
}
marker = new Marker({
index,
map: this,
// Merge the `markerStyle` object with the marker config `style` if presented.
style: merge(this.params.markerStyle, { initial: config.style || {} }, true),
label: this.params.labels && this.params.labels.markers,
labelsGroup: this._markerLabelsGroup,
cx: point.x,
cy: point.y,
group: this._markersGroup,
marker: config,
isRecentlyCreated,
})

// Check for marker duplication
// this is useful when for example: a user clicks a button for creating marker two times
// so it will remove the old one and the new one will take its place.
if (this.markers[index]) {
this.removeMarkers([index])
}

this.markers[index] = {
_uid: uid,
config: markerConfig,
element: marker
}
this.markers[index] = {
_uid: uid,
config: config,
element: marker
}
}
}
4 changes: 2 additions & 2 deletions src/js/core/createRegions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Region from '../components/region'
export default function createRegions() {
let code, region

this.regionLabelsGroup = this.regionLabelsGroup || this.canvas.createGroup('jvm-regions-labels-group')
this._regionLabelsGroup = this._regionLabelsGroup || this.canvas.createGroup('jvm-regions-labels-group')

for (code in this.mapData.paths) {
region = new Region({
Expand All @@ -13,7 +13,7 @@ export default function createRegions() {
path: this.mapData.paths[code].path,
style: merge({}, this.params.regionStyle),
labelStyle: this.params.regionLabelStyle,
labelsGroup: this.regionLabelsGroup,
labelsGroup: this._regionLabelsGroup,
label: this.params.labels && this.params.labels.regions,
})

Expand Down
4 changes: 2 additions & 2 deletions src/js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
createElement,
removeElement,
} from './util'
import core from './core'
import Defaults from './defaults/options'
import SVGCanvasElement from './svg/canvasElement'
import MapPrototypes from './core/index'
import Events from './defaults/events'
import EventHandler from './eventHandler'
import Tooltip from './components/tooltip'
Expand Down Expand Up @@ -330,6 +330,6 @@ class Map {
}
}

Object.assign(Map.prototype, MapPrototypes)
Object.assign(Map.prototype, core)

export default Map

0 comments on commit 5cf594d

Please sign in to comment.