svgmap 2.20.1 → 2.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/index.cjs +186 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +186 -4
- package/dist/index.js.map +1 -1
- package/dist/svg-map.css +9 -0
- package/dist/svg-map.min.css +1 -1
- package/dist/svg-map.umd.js +186 -4
- package/dist/svg-map.umd.js.map +1 -1
- package/dist/svg-map.umd.min.js +1 -1
- package/dist/svgMap.css +9 -0
- package/dist/svgMap.js +186 -4
- package/dist/svgMap.js.map +1 -1
- package/dist/svgMap.min.css +1 -1
- package/dist/svgMap.min.js +1 -1
- package/package.json +1 -1
- package/src/js/core/svg-map.js +186 -4
- package/src/scss/map.scss +11 -0
package/dist/svgMap.css
CHANGED
|
@@ -204,6 +204,15 @@
|
|
|
204
204
|
cursor: pointer;
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
.svgMap-pin-group {
|
|
208
|
+
pointer-events: none;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
.svgMap-pin {
|
|
212
|
+
vector-effect: non-scaling-stroke;
|
|
213
|
+
transition: r 250ms;
|
|
214
|
+
}
|
|
215
|
+
|
|
207
216
|
.svgMap-tooltip,
|
|
208
217
|
.svgMap-persistent-tooltip {
|
|
209
218
|
box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
|
package/dist/svgMap.js
CHANGED
|
@@ -2379,7 +2379,34 @@
|
|
|
2379
2379
|
showContinentSelector: false,
|
|
2380
2380
|
|
|
2381
2381
|
// Reset zoom on resize
|
|
2382
|
-
resetZoomOnResize: false
|
|
2382
|
+
resetZoomOnResize: false,
|
|
2383
|
+
|
|
2384
|
+
// Static pins: false | string[] | function(countryID, countryValues) => boolean
|
|
2385
|
+
staticPins: false,
|
|
2386
|
+
|
|
2387
|
+
// Default pin fill color
|
|
2388
|
+
pinColor: '#000000',
|
|
2389
|
+
|
|
2390
|
+
// Default pin stroke color and width (circle pins; width is in screen pixels with non-scaling stroke)
|
|
2391
|
+
pinStrokeColor: '#ffffff',
|
|
2392
|
+
pinStrokeWidth: 1.5,
|
|
2393
|
+
|
|
2394
|
+
// Default pin radius in SVG units (viewBox is 2000 × 1001)
|
|
2395
|
+
pinSize: 8,
|
|
2396
|
+
|
|
2397
|
+
// Custom pin element: function(countryID, countryValues) => SVGElement | null
|
|
2398
|
+
onGetPin: null,
|
|
2399
|
+
|
|
2400
|
+
// Image URL to use as a pin instead of the default circle (can also be set per-country via values[id].pinImage)
|
|
2401
|
+
pinImage: null,
|
|
2402
|
+
|
|
2403
|
+
// Width and height of the pin image in SVG units (viewBox is 2000 × 1001)
|
|
2404
|
+
pinImageWidth: 20,
|
|
2405
|
+
pinImageHeight: 20,
|
|
2406
|
+
|
|
2407
|
+
// Offset from computed pin position, in SVG units (added after auto center or pinX/pinY)
|
|
2408
|
+
pinOffsetX: 0,
|
|
2409
|
+
pinOffsetY: 0
|
|
2383
2410
|
};
|
|
2384
2411
|
|
|
2385
2412
|
this.options = Object.assign({}, defaultOptions, options);
|
|
@@ -3253,7 +3280,7 @@
|
|
|
3253
3280
|
}
|
|
3254
3281
|
countryElement.parentNode.insertBefore(
|
|
3255
3282
|
countryElement,
|
|
3256
|
-
this.persistentTooltipGroup || null
|
|
3283
|
+
this.persistentTooltipGroup || this.pinGroup || null
|
|
3257
3284
|
);
|
|
3258
3285
|
if (setActive) {
|
|
3259
3286
|
countryElement.classList.add('svgMap-active');
|
|
@@ -3373,6 +3400,10 @@
|
|
|
3373
3400
|
this.createPersistentTooltips(countryElements);
|
|
3374
3401
|
}
|
|
3375
3402
|
|
|
3403
|
+
if (this.options.staticPins) {
|
|
3404
|
+
this.createStaticPins(countryElements);
|
|
3405
|
+
}
|
|
3406
|
+
|
|
3376
3407
|
let pointerStart = null;
|
|
3377
3408
|
let activeCountry = null;
|
|
3378
3409
|
|
|
@@ -3435,7 +3466,7 @@
|
|
|
3435
3466
|
clearActive();
|
|
3436
3467
|
countryElement.parentNode.insertBefore(
|
|
3437
3468
|
countryElement,
|
|
3438
|
-
this.persistentTooltipGroup || null
|
|
3469
|
+
this.persistentTooltipGroup || this.pinGroup || null
|
|
3439
3470
|
);
|
|
3440
3471
|
countryElement.classList.add('svgMap-active');
|
|
3441
3472
|
this.setTooltipContent(this.getTooltipContent(countryID));
|
|
@@ -3449,7 +3480,7 @@
|
|
|
3449
3480
|
clearActive();
|
|
3450
3481
|
countryElement.parentNode.insertBefore(
|
|
3451
3482
|
countryElement,
|
|
3452
|
-
this.persistentTooltipGroup || null
|
|
3483
|
+
this.persistentTooltipGroup || this.pinGroup || null
|
|
3453
3484
|
);
|
|
3454
3485
|
countryElement.classList.add('svgMap-active');
|
|
3455
3486
|
this.setTooltipContent(this.getTooltipContent(countryID));
|
|
@@ -3637,6 +3668,157 @@
|
|
|
3637
3668
|
);
|
|
3638
3669
|
}
|
|
3639
3670
|
|
|
3671
|
+
// Create static pins on the map
|
|
3672
|
+
|
|
3673
|
+
createStaticPins(countryElements) {
|
|
3674
|
+
if (this.pinGroup) {
|
|
3675
|
+
this.pinGroup.remove();
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3678
|
+
this.pinGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
|
|
3679
|
+
this.pinGroup.classList.add('svgMap-pin-group');
|
|
3680
|
+
this.mapImage.appendChild(this.pinGroup);
|
|
3681
|
+
|
|
3682
|
+
countryElements.forEach(
|
|
3683
|
+
function (countryElement) {
|
|
3684
|
+
var countryID = countryElement.getAttribute('data-id');
|
|
3685
|
+
if (!this.shouldShowPin(countryID)) {
|
|
3686
|
+
return;
|
|
3687
|
+
}
|
|
3688
|
+
|
|
3689
|
+
var countryValues = this.options.data.values[countryID];
|
|
3690
|
+
var cx, cy;
|
|
3691
|
+
|
|
3692
|
+
if (
|
|
3693
|
+
countryValues &&
|
|
3694
|
+
countryValues.pinX != null &&
|
|
3695
|
+
countryValues.pinY != null
|
|
3696
|
+
) {
|
|
3697
|
+
cx = countryValues.pinX;
|
|
3698
|
+
cy = countryValues.pinY;
|
|
3699
|
+
} else {
|
|
3700
|
+
// Split the path at absolute M commands and use the largest sub-path
|
|
3701
|
+
// to avoid overseas territories (islands, colonies) skewing the center.
|
|
3702
|
+
var d = countryElement.getAttribute('d');
|
|
3703
|
+
var subPaths = d.split(/(?=M)/).filter((s) => s.trim().length > 0);
|
|
3704
|
+
var largestBB = null;
|
|
3705
|
+
var largestArea = -1;
|
|
3706
|
+
|
|
3707
|
+
subPaths.forEach(
|
|
3708
|
+
function (subPath) {
|
|
3709
|
+
var tmp = document.createElementNS(
|
|
3710
|
+
'http://www.w3.org/2000/svg',
|
|
3711
|
+
'path'
|
|
3712
|
+
);
|
|
3713
|
+
tmp.setAttribute('d', subPath);
|
|
3714
|
+
this.mapImage.appendChild(tmp);
|
|
3715
|
+
var bb = tmp.getBBox();
|
|
3716
|
+
var area = bb.width * bb.height;
|
|
3717
|
+
if (area > largestArea) {
|
|
3718
|
+
largestArea = area;
|
|
3719
|
+
largestBB = bb;
|
|
3720
|
+
}
|
|
3721
|
+
this.mapImage.removeChild(tmp);
|
|
3722
|
+
}.bind(this)
|
|
3723
|
+
);
|
|
3724
|
+
|
|
3725
|
+
cx = largestBB.x + largestBB.width / 2;
|
|
3726
|
+
cy = largestBB.y + largestBB.height / 2;
|
|
3727
|
+
}
|
|
3728
|
+
|
|
3729
|
+
var offsetX =
|
|
3730
|
+
countryValues && countryValues.pinOffsetX != null
|
|
3731
|
+
? countryValues.pinOffsetX
|
|
3732
|
+
: this.options.pinOffsetX;
|
|
3733
|
+
var offsetY =
|
|
3734
|
+
countryValues && countryValues.pinOffsetY != null
|
|
3735
|
+
? countryValues.pinOffsetY
|
|
3736
|
+
: this.options.pinOffsetY;
|
|
3737
|
+
cx += offsetX;
|
|
3738
|
+
cy += offsetY;
|
|
3739
|
+
|
|
3740
|
+
var color =
|
|
3741
|
+
(countryValues && countryValues.pinColor) || this.options.pinColor;
|
|
3742
|
+
var size =
|
|
3743
|
+
(countryValues && countryValues.pinSize) || this.options.pinSize;
|
|
3744
|
+
var strokeColor =
|
|
3745
|
+
(countryValues && countryValues.pinStrokeColor) ||
|
|
3746
|
+
this.options.pinStrokeColor;
|
|
3747
|
+
var strokeWidth =
|
|
3748
|
+
(countryValues && countryValues.pinStrokeWidth) ||
|
|
3749
|
+
this.options.pinStrokeWidth;
|
|
3750
|
+
|
|
3751
|
+
if (typeof this.options.onGetPin === 'function') {
|
|
3752
|
+
var custom = this.options.onGetPin(countryID, countryValues);
|
|
3753
|
+
if (custom) {
|
|
3754
|
+
custom.setAttribute(
|
|
3755
|
+
'transform',
|
|
3756
|
+
'translate(' + cx + ',' + cy + ')'
|
|
3757
|
+
);
|
|
3758
|
+
this.pinGroup.appendChild(custom);
|
|
3759
|
+
return;
|
|
3760
|
+
}
|
|
3761
|
+
}
|
|
3762
|
+
|
|
3763
|
+
var pinImage =
|
|
3764
|
+
(countryValues && countryValues.pinImage) || this.options.pinImage;
|
|
3765
|
+
|
|
3766
|
+
if (pinImage) {
|
|
3767
|
+
var pinW =
|
|
3768
|
+
(countryValues && countryValues.pinImageWidth) ||
|
|
3769
|
+
this.options.pinImageWidth;
|
|
3770
|
+
var pinH =
|
|
3771
|
+
(countryValues && countryValues.pinImageHeight) ||
|
|
3772
|
+
this.options.pinImageHeight;
|
|
3773
|
+
var img = document.createElementNS(
|
|
3774
|
+
'http://www.w3.org/2000/svg',
|
|
3775
|
+
'image'
|
|
3776
|
+
);
|
|
3777
|
+
img.setAttribute('href', pinImage);
|
|
3778
|
+
img.setAttribute('x', cx - pinW / 2);
|
|
3779
|
+
img.setAttribute('y', cy - pinH / 2);
|
|
3780
|
+
img.setAttribute('width', pinW);
|
|
3781
|
+
img.setAttribute('height', pinH);
|
|
3782
|
+
img.setAttribute('data-id', countryID);
|
|
3783
|
+
img.classList.add('svgMap-pin');
|
|
3784
|
+
this.pinGroup.appendChild(img);
|
|
3785
|
+
return;
|
|
3786
|
+
}
|
|
3787
|
+
|
|
3788
|
+
var circle = document.createElementNS(
|
|
3789
|
+
'http://www.w3.org/2000/svg',
|
|
3790
|
+
'circle'
|
|
3791
|
+
);
|
|
3792
|
+
circle.setAttribute('cx', cx);
|
|
3793
|
+
circle.setAttribute('cy', cy);
|
|
3794
|
+
circle.setAttribute('r', size);
|
|
3795
|
+
circle.setAttribute('fill', color);
|
|
3796
|
+
if (strokeWidth > 0) {
|
|
3797
|
+
circle.setAttribute('stroke', strokeColor);
|
|
3798
|
+
circle.setAttribute('stroke-width', strokeWidth);
|
|
3799
|
+
circle.setAttribute('vector-effect', 'non-scaling-stroke');
|
|
3800
|
+
}
|
|
3801
|
+
circle.setAttribute('data-id', countryID);
|
|
3802
|
+
circle.classList.add('svgMap-pin');
|
|
3803
|
+
this.pinGroup.appendChild(circle);
|
|
3804
|
+
}.bind(this)
|
|
3805
|
+
);
|
|
3806
|
+
}
|
|
3807
|
+
|
|
3808
|
+
// Check if a static pin should be shown for a country
|
|
3809
|
+
|
|
3810
|
+
shouldShowPin(countryID) {
|
|
3811
|
+
var pins = this.options.staticPins;
|
|
3812
|
+
var countryValues = this.options.data.values[countryID];
|
|
3813
|
+
if (Array.isArray(pins)) {
|
|
3814
|
+
return pins.indexOf(countryID) !== -1;
|
|
3815
|
+
}
|
|
3816
|
+
if (typeof pins === 'function') {
|
|
3817
|
+
return pins(countryID, countryValues);
|
|
3818
|
+
}
|
|
3819
|
+
return false;
|
|
3820
|
+
}
|
|
3821
|
+
|
|
3640
3822
|
// Check if a persistent tooltip should be shown on load
|
|
3641
3823
|
|
|
3642
3824
|
shouldShowTooltipOnLoad(countryID) {
|