/*!
* jsMap v4.1.1
* Copyright (C) 2018-2019, ZhaoGang
* Released under the MIT license.
*/
!(function ( global, factory ) {
if ( typeof define === "function" && define.amd ) {
define( [ "jsmap" ], factory );
} else if ( typeof module !== "undefined" && typeof exports === "object" ) {
module.exports = factory();
} else {
global.jsMap = factory();
}
})( typeof window !== "undefined" ? window : this, function () {
// 鍙橀噺甯搁噺
var UA = navigator.userAgent.toLowerCase(),
IsMobile = !!( UA.match( /(ios|iphone|ipod|ipad|android)/ ) && "ontouchend" in document ),
CacheJSON = {};
var $tip;
// 鑾峰彇 DOM
function $ ( elem, context ) {
return [].slice.call( typeof elem === "object" ? ( elem.length ? elem : [ elem ] ) : ( context || document ).querySelectorAll( elem ) );
}
// 鏂规硶鍚堥泦
var Methods = {
getCSS: function ( elem, name ) {
return window.getComputedStyle( elem, null ).getPropertyValue( name );
},
setCSS: function ( elem, css ) {
$( elem ).forEach(function ( el ) {
for ( var name in css ) {
el.style[ name ] = css[ name ];
}
})
},
setAttr: function ( elem, props ) {
$( elem ).forEach(function ( el ) {
for ( var name in props ) {
el.setAttribute( name, props[ name ] );
}
})
},
type: function ( obj ) {
return {}.toString.call( obj ).replace( /(\[object |\])/g, "" ).toLowerCase();
},
isEmptyObject: function ( obj ) {
return Object.keys( obj ).length === 0;
}
};
// 鍙戦€佽姹傝幏鍙栧湴鍥炬暟鎹
function Request ( url, done ) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if ( xmlHttp.readyState === 4 ) {
var status = xmlHttp.status;
if ( ( ( status >= 200 && status < 300 ) || status === 304 ) ) {
done( JSON.parse( xmlHttp.responseText.trim() ) );
}
}
}
xmlHttp.open( "GET", url );
xmlHttp.send( null );
}
// 鍦ㄦ敮鎸 classList 鐨 IE 娴忚鍣ㄤ腑, classList 灞炴€ф槸鍦 HTMLElement.prototype 涓婂畾涔夌殑
// 鑻ヨ浣垮叾搴旂敤鍦 svg 鍏冪礌涓? 闇€瑕佸皢 classList 瀹氫箟鍦 Element.prototype 涓
if ( !Object.getOwnPropertyDescriptor( Element.prototype, "classList" ) ){
if ( HTMLElement && Object.getOwnPropertyDescriptor( HTMLElement.prototype, "classList" ) ) {
Object.defineProperty( Element.prototype, "classList", Object.getOwnPropertyDescriptor( HTMLElement.prototype, "classList" ) );
}
}
// 榛樿閰嶇疆
var defaults = {
taiwanJSON: false,
nanhaizhudaoJSON: false,
name: "china",
width: 900,
stroke: {
width: 1,
color: "#d0d0d1"
},
fill: {
basicColor: "#fff",
hoverColor: "#fff",
clickColor: "#fff",
},
areaName: {
show: false,
size: 12,
basicColor: "#000",
hoverColor: "#000",
clickColor: "#cc1f32"
},
disabled: {
color: "#fff",
except: false,
name: []
},
zoom: {
disabled: true,
max: 5,
zoomRange: true,
wheelZoom: true
},
marker: {
disabled: true,
icon: "data:img/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAjVBMVEVMaXEsLCwoKCgrKyssLCwsLCwsLCwgICAqKioqKiorKyspKSkrKysrKyssLCwrKysrKysrKyssLCwrKysqKiosLCwrKyssLCwqKiosLCz///9SUlKVlZW8vLw5OTnX19fx8fF6enpgYGBhYWFTU1NFRUXk5ORtbW3Kysqvr697e3tGRkaIiIg4ODiHh4fw9bOuAAAAGXRSTlMA7yDQv4DwEGAwoFDfsMBw4F9Az5Cvj39PrlPZPAAAAWxJREFUeNrt1tluwyAQBdCxTUzwEmfrMjhu9qXr/39eq0oVkAZfUF7ykPNqezR3QGC6u3Fy8ihEUgnx9Cwp3kSw5WES+3nFZ2ZTCpcKvkAEB1EJX1QpCjJkn6SkACX7JSq+f1clCZAz7iUIGDAwBA24ATaLrlu0boiIBtql/nVqw1uwG1jrPyu7wox6KDbalTYV9myk5Ddn46Qtr4EZRua1vXZ8mic1+bHxpR0b82QUVmChHWtrP5NX6i9wZIO8pmwctWPHRliEl5W2HDi6gJthzUZGfmO7haX5vmMOW4WaL1boXtgyDz/NPg76x/KNHSXBdTR22+07n6E+Y4Zq6tMwgM4DyciY+hUMDNCtxv1ySUCBGkBS1AA0AEsAyZy9MrrydlUUpEYTxCHAHoJKEAArQABIZvzPiGKoHGyh+D+tKUUqwACwDBxDUGqNIQMDQBddrggAgywJAINsCAB/LAUBYEdmdA3ZNHR3874BBc+FYRSdidoAAAAASUVORK5CYII=",
click: function () {}
},
selected: [],
hide: [],
multiple: false,
tip: true,
defaultInteractive: true,
hoverCallback: function () {},
clickCallback: function () {}
};
// 鏁村悎鍙傛暟
function mergeParam ( param ) {
for ( var name in defaults ) {
var v = defaults[ name ];
if ( Methods.type( v ) !== "object" ) {
if ( param[ name ] === undefined ) {
param[ name ] = v;
}
} else {
for ( var _name in v ) {
if ( param[ name ] ) {
if ( param[ name ][ _name ] === undefined ) {
param[ name ][ _name ] = v[ _name ];
}
} else {
param[ name ] = v;
}
}
}
}
return param;
}
// 鏍稿績绋嬪簭
var jsMap = {
version: "4.1.1",
config: function ( selector, jsonData, options ) {
var opt = mergeParam( options || {} );
// 蹇呴』浼犲叆鍦板浘鏁版嵁
if ( !jsonData || Methods.type( jsonData ) !== "object" ) {
console.warn( "鏉ヨ嚜 jsMap 鐨勬彁绀猴細[ 璇蜂紶鍏ュ湴鍥炬暟鎹 ]" );
return;
}
// 鏆傛棤鍙版咕鐪佸拰鍗楁捣璇稿矝鐨勮缁嗗湴鍥炬暟鎹
if ( opt.name === "taiwan" && !opt.taiwanJSON ) {
console.warn( "鏉ヨ嚜 jsMap 鐨勬彁绀猴細[ 寰堟姳姝夛紝鏆傛棤鍙版咕鐪佺殑璇︾粏鍦板浘鏁版嵁銆 ]" );
return;
}
if ( opt.name === "nanhaizhudao" && !opt.nanhaizhudaoJSON ) {
console.warn( "鏉ヨ嚜 jsMap 鐨勬彁绀猴細[ 寰堟姳姝夛紝鏆傛棤鍗楁捣璇稿矝鐨勮缁嗗湴鍥炬暟鎹€ ]" );
return;
}
// 鍙栧嚭瑕佺粯鍒剁殑鍖哄煙鐨 json 鏁版嵁
var mapData = jsonData[ opt.name ];
var path = "",
text = "",
areaBox = [];
for ( var i in mapData ) {
var v = mapData[ i ];
// 瀛樺偍鍦板尯鍚嶇О
areaBox.push( i );
// 缁樺埗 path 璺緞
path += '\
\
\
';
// 璁剧疆鏂囧瓧
text += '\
' + v.name + '\
';
}
path = '
\
\
';
$( selector ).forEach(function ( $el ) {
// 璁板綍褰撳墠鎸囧畾鐨勮鍐呮牱寮
var cssCache = {
width: $el.style.width,
height: $el.style.height,
padding: $el.style.padding,
position: $el.style.position
};
$el[ "__jsmap_csscache__" ] = cssCache;
// 缁欑洰鏍囧厓绱犺缃牱寮忓苟娣诲姞鏁版嵁瀛樺偍
if ( Methods.getCSS( $el, "position" ) === "static" ) {
$el.style.position = "relative";
}
$el.classList.add( "jsmap-container" );
// 瀵瑰楂樼殑澶勭悊
var height;
if ( String( opt.width ).match( /^([1-9][0-9]{0,1}|100)%$/ ) ) {
opt.width = "100%";
height = parseFloat( Methods.getCSS( $el, "width" ) ) / 1.8 + "px";
} else {
opt.width = parseInt( opt.width ) + "px";
height = parseInt( opt.width ) / 1.8 + "px";
}
Methods.setCSS($el, {
width: opt.width,
height: height,
padding: 0
});
$el.innerHTML = path;
$el[ "__jsmap_jsondata__" ] = jsonData;
$el[ "__jsmap_options__" ] = opt;
var $container = $( ".jsmap-svg-container", $el )[ 0 ],
$svg = $( "svg", $container )[ 0 ],
$path = $( "path", $svg ),
$text = $( "text", $svg ),
$pathText = $( "path, text", $svg );
// 绂佺敤榛樿浜や簰 [ 鎮诞鏁堟灉鍜岀偣鍑绘晥鏋 ]
if ( !opt.defaultInteractive ) {
$container.defaultInteractive = true;
}
Methods.setCSS($container, {
width: opt.width,
height: height
});
Methods.setAttr($svg, {
width: opt.width === "100%" ? opt.width : parseFloat( opt.width ),
height: parseFloat( height )
});
Methods.setCSS($svg, {
position: "relative",
overflow: "hidden",
marginLeft: opt.name === "china" ? ( window.innerWidth > 700 ? "-50px" : "-30px" ) : 0
});
$text.length && $( $text ).forEach(function ( text ) {
// 濡傛灉 text 涓嶆槸涓嬪垪鍖归厤鐨勫尯鍩
// 鍒欑鐢ㄤ换浣曢紶鏍囪涓
if ( !text.getAttribute( "data-id" ).match( /(shanghai|xianggang|aomen|nanhaizhudao|tianjin|beijing)/ ) ) {
text.style.pointerEvents = "none";
}
})
// 闅愯棌鎸囧畾鐨勫尯鍩
if ( Array.isArray( opt.hide ) && opt.hide.length ) {
opt.hide.forEach(function ( v ) {
// 鏀寔姹夊瓧鍜屽叏鎷间袱绉嶅舰寮
$( $pathText ).forEach(function ( pathText ) {
if ( pathText.getAttribute( "data-id" ) === v || pathText.getAttribute( "data-name" ) === v ) {
pathText.style.display = "none";
}
})
})
}
var fillBasicColor = opt.fill.basicColor;
// 濉厖鑹 - 缁熶竴璁剧疆
if ( typeof fillBasicColor === "string" ) {
Methods.setAttr($path, {
fill: fillBasicColor,
"data-fill": fillBasicColor
});
}
// 濉厖鑹 - 鍗曠嫭璁剧疆
if ( Methods.type( fillBasicColor ) === "object" && !Methods.isEmptyObject( fillBasicColor ) ) {
Methods.setAttr($path, {
fill: defaults.fill.basicColor,
"data-fill": defaults.fill.basicColor
});
for ( var name in fillBasicColor ) {
var v = fillBasicColor[ name ];
Methods.setAttr($( ".jsmap-" + name, $svg ), {
fill: v,
"data-fill": v
});
}
}
// 缁 path 娣诲姞杩囨浮鏃堕棿
var timer = window.setTimeout(function () {
$( $path ).forEach(function ( path ) {
path.style.transition = ".15s";
})
window.clearTimeout( timer );
}, 0);
// 鎻忚竟
Methods.setAttr($path, {
stroke: opt.stroke.color,
"stroke-width": opt.stroke.width
});
// 鏂囧瓧棰滆壊澶у皬
Methods.setAttr($text, {
fill: opt.areaName.basicColor,
"font-size": opt.areaName.size
});
// 绂佺敤鎸囧畾鐨勫尯鍩
var disabledName = opt.disabled.name;
if ( Array.isArray( disabledName ) && disabledName.length ) {
function setDisabled ( elem ) {
$( elem ).forEach(function ( el ) {
el.classList.add( "jsmap-disabled" );
el.style.cursor = "not-allowed";
if ( el.nodeName.toLowerCase() !== "text" ) {
el.setAttribute( "fill", opt.disabled.color );
}
})
}
// 鍙嶉€
if ( opt.disabled.except ) {
var result = [];
disabledName.forEach(function ( v ) {
// 鏀寔姹夊瓧鍜屽叏鎷间袱绉嶅舰寮
if ( !v.match( /[a-z]/ ) ) {
$( $path ).forEach(function ( path ) {
if ( path.getAttribute( "data-name" ) === v ) {
result.push( path.getAttribute( "data-id" ) );
}
})
} else {
result.push( v );
}
})
// 鍏嬮殕涓€涓惈鏈夊湴鍖哄悕绉扮殑鏁扮粍
var cloneNames = areaBox.map(function ( name ) {
return name;
});
// 杩涜鍙嶉€夌殑绛涢€夋搷浣
result.forEach(function ( v ) {
cloneNames.splice( cloneNames.indexOf( v ), 1 );
})
// 璁剧疆绂佺敤鏁堟灉
cloneNames.forEach(function ( v ) {
setDisabled( $( ".jsmap-" + v, $el ) );
})
} else {
disabledName.forEach(function ( v ) {
// 璁剧疆绂佺敤鏁堟灉
setDisabled( $( ".jsmap-" + v, $el ) );
})
}
}
// 鎮诞鎻愮ず灞
if ( opt.tip ) {
if ( !$( "#jsmap-tip-layer" )[ 0 ] ) {
document.body.insertAdjacentHTML("beforeend", '' );
$tip = $( "#jsmap-tip-layer" )[ 0 ];
}
}
// 浜嬩欢闆嗗悎
areaBox.forEach(function ( v ) {
$( ".jsmap-" + v, $el ).forEach(function ( elem ) {
elem.addEventListener("mouseenter", function () {
if ( IsMobile || !opt.defaultInteractive ) {
return;
}
var _this = this;
// 濡傛灉姝ゅ尯鍩熻绂佺敤
// 鍒欐棤浠讳綍浜嬩欢
if ( _this.classList.contains( "jsmap-disabled" ) ) {
return;
}
// 榧犳爣鎮诞鏃剁殑濉厖鑹 ( 鏈鐐瑰嚮杩囩殑鎯呭喌涓 )
if ( !_this.classList.contains( "jsmap-clicked" ) ) {
// 鍏嬮殕涓€涓惈鏈夊湴鍖哄悕绉扮殑鏁扮粍
var cloneAllName = areaBox.map(function ( name ) {
return name;
});
if ( Methods.type( opt.fill.hoverColor ) === "string" ) {
if ( _this.nodeName.toLowerCase() === "path" ) {
_this.setAttribute( "fill", opt.fill.hoverColor );
}
}
if ( Methods.type( opt.fill.hoverColor ) === "object" && !Methods.isEmptyObject( opt.fill.hoverColor ) ) {
for ( var i in opt.fill.hoverColor ) {
var v = opt.fill.hoverColor[ i ];
if ( _this.getAttribute( "class" ).indexOf( "jsmap-" + i ) > -1 ) {
_this.setAttribute( "fill", v );
}
cloneAllName.splice( cloneAllName.indexOf( i ), 1 );
}
// 鏈壒娈婅缃殑鍦板尯浠嶄繚鎸侀粯璁ら厤缃壊
cloneAllName.forEach(function ( area ) {
if ( _this.getAttribute( "class" ).indexOf( "jsmap-" + area ) > -1 ) {
_this.setAttribute( "fill", defaults.fill.hoverColor );
}
})
}
}
// 鎮诞鍥炶皟浜嬩欢
opt.hoverCallback( _this.getAttribute( "data-id" ), _this.getAttribute( "data-name" ) );
// 鎮诞鎻愮ず妗
if ( opt.tip ) {
// 涓 true 鏃舵樉绀哄湴鍖虹殑姹夊瓧鍚嶇О
if ( opt.tip === true ) {
$tip.innerHTML = '' + _this.getAttribute( "data-name" ) + '
';
}
// 鏄嚱鏁版椂鍙樉绀鸿嚜瀹氫箟鍐呭
// 鍑芥暟鐨勫弬鏁板寘鍚簡鍦板尯鐨勫叏鎷煎拰姹夎鍚嶇О
if ( Methods.type( opt.tip ) === "function" ) {
$tip.innerHTML = opt.tip( _this.getAttribute( "data-id" ), _this.getAttribute( "data-name" ) );
}
// 鎮诞绉诲姩浜嬩欢
document.onmousemove = function ( event ) {
var x = event.pageX + 12 + "px",
y = event.pageY + 12 + "px";
Methods.setCSS($tip, {
transform: "translate3d(" + x + ", " + y + ", 0)",
display: "block"
});
}
}
})
elem.addEventListener("mouseleave", function () {
if ( IsMobile || !opt.defaultInteractive ) {
return;
}
// 濡傛灉姝ゅ尯鍩熻绂佺敤
// 鍒欐棤浠讳綍浜嬩欢
if ( this.classList.contains( "jsmap-disabled" ) ) {
return;
}
// 鎭㈠鍘熷濉厖鑹 ( 鏈鐐瑰嚮杩囩殑鎯呭喌涓 )
if ( !this.classList.contains( "jsmap-clicked" ) ) {
if ( this.nodeName.toLowerCase() === "path" ) {
this.setAttribute( "fill", this.getAttribute( "data-fill" ) );
}
}
// 鎮诞妗嗗唴瀹规竻绌哄苟鎭㈠浣嶇疆
if ( opt.tip ) {
$tip.innerHTML = "";
Methods.setCSS($tip, {
transform: "translate3d(0, 0, 0)",
display: "none"
});
document.onmousemove = null;
}
})
elem.addEventListener("click", function () {
var _this = this;
// 濡傛灉姝ゅ尯鍩熻绂佺敤
// 鍒欐棤浠讳綍浜嬩欢
if ( _this.classList.contains( "jsmap-disabled" ) || !opt.defaultInteractive ) {
return;
}
var id = _this.getAttribute( "data-id" );
// 鐐瑰嚮鍚庣殑濉厖鑹
if ( opt.fill.clickColor === false ) {
return;
} else {
// 鍏嬮殕涓€涓惈鏈夊湴鍖哄悕绉扮殑鏁扮粍
var cloneAllName = areaBox.map(function ( name ) {
return name;
});
if ( typeof opt.fill.clickColor === "string" ) {
if ( _this.nodeName.toLowerCase() === "path" ) {
_this.setAttribute( "fill", opt.fill.clickColor );
}
}
if ( Methods.type( opt.fill.clickColor ) === "object" && !Methods.isEmptyObject( opt.fill.clickColor ) ) {
for ( var i in opt.fill.clickColor ) {
var v = opt.fill.clickColor[ i ];
if ( _this.getAttribute( "class" ).indexOf( "jsmap-" + i ) > -1 ) {
_this.setAttribute( "fill", v );
}
cloneAllName.splice( cloneAllName.indexOf( i ), 1 );
}
// 鏈壒娈婅缃殑鍦板尯浠嶄繚鎸侀粯璁ら厤缃壊
cloneAllName.forEach(function ( area ) {
if ( _this.getAttribute( "class" ).indexOf( "jsmap-" + area ) > -1 ) {
_this.setAttribute( "fill", defaults.fill.clickColor );
}
})
}
}
// 鐐瑰嚮鍚庣殑鏂囧瓧棰滆壊
if ( opt.areaName.clickColor !== false ) {
if ( opt.areaName.show ) {
var $elem = $( "text.jsmap-" + _this.getAttribute( "data-id" ), $el )[ 0 ];
$elem.setAttribute( "fill", opt.areaName.clickColor );
if ( !opt.multiple ) {
$( $( "text", $el ) ).forEach(function ( text ) {
if ( text !== $elem ) {
text.setAttribute( "fill", opt.areaName.basicColor );
}
})
}
}
}
// 鍗曢€
if ( !opt.multiple ) {
_this.classList.add( "jsmap-clicked" );
$( $pathText ).forEach(function ( pathText ) {
if ( !pathText.classList.contains( "jsmap-" + id ) && !pathText.classList.contains( "jsmap-disabled" ) ) {
pathText.classList.remove( "jsmap-clicked" );
if ( pathText.nodeName.toLowerCase() !== "text" ) {
pathText.setAttribute( "fill", pathText.getAttribute( "data-fill" ) );
}
}
})
} else {
// 澶氶€
_this.classList[ $el.outerSelected ? "add" : "toggle" ]( "jsmap-clicked" );
if ( !_this.classList.contains( "jsmap-clicked" ) ) {
_this.setAttribute( "fill", _this.getAttribute( "data-fill" ) );
}
}
// 鐐瑰嚮鍥炶皟浜嬩欢
// 鍑芥暟鐨勫弬鏁板寘鍚簡鍦板尯鐨勫叏鎷煎拰姹夎鍚嶇О
if ( !$el.outerSelected && !$el.autoSelected ) {
opt.clickCallback( _this.getAttribute( "data-id" ), _this.getAttribute( "data-name" ) );
}
})
})
})
// 榛樿閫変腑
if ( Array.isArray( opt.selected ) && opt.selected.length ) {
$el.autoSelected = true;
opt.selected.forEach(function ( v ) {
var $target = $( 'path[data-id="' + v + '"], path[data-name="' + v + '"]', $svg )[ 0 ];
var evt = document.createEvent( "MouseEvents" );
evt.initEvent( "click", true, true );
$target && $target.dispatchEvent( evt );
})
$el.autoSelected = null;
}
// 缂╂斁鎷栨嫿
if ( opt.zoom.disabled === false ) {
if ( opt.zoom.zoomRange ) {
$el.insertAdjacentHTML("beforeend", '\
\
');
}
var $zoomIn = $( ".jsmap-zoom-range div:first-of-type", $el )[ 0 ],
$zoomOut = $( ".jsmap-zoom-range div:last-of-type", $el )[ 0 ];
var zoom = 1;
Methods.setCSS($el, {
overflow: "hidden"
});
if ( $zoomIn ) {
$zoomIn.onclick = function () {
wheelHandle( {}, -1 );
}
}
if ( $zoomOut ) {
$zoomOut.onclick = function () {
wheelHandle( {}, 1 );
}
}
function wheelHandle ( event, upDown ) {
event.preventDefault && event.preventDefault();
var Y = upDown || event.deltaY;
if ( Y < 0 && zoom < parseFloat( opt.zoom.max ) ) {
zoom += .2;
}
if ( Y > 0 && zoom > 1 ) {
zoom -= .2;
}
zoom = parseFloat( zoom.toFixed( 1 ) );
Methods.setCSS($container, {
transform: "scale(" + zoom + ")"
});
if ( zoom > 1 ) {
var left = parseFloat( Methods.getCSS( $container, "margin-left" ) ),
top = parseFloat( Methods.getCSS( $container, "margin-top" ) );
var X = ( _width * zoom - _width ) / 2,
Y = ( _height * zoom - _height ) / 2;
if ( left > X ) {
Methods.setCSS($container, {
marginLeft: X + "px"
});
}
if ( top > Y ) {
Methods.setCSS($container, {
marginTop: Y + "px"
});
}
if ( left < -X ) {
Methods.setCSS($container, {
marginLeft: -X + "px"
});
}
if ( top < -Y ) {
Methods.setCSS($container, {
marginTop: -Y + "px"
});
}
}
if ( Math.ceil( zoom ) === 1 ) {
Methods.setCSS($container, {
marginTop: 0,
marginLeft: 0
});
}
}
if ( opt.zoom.wheelZoom ) {
$el.addEventListener( "wheel", wheelHandle );
}
var _width = parseFloat( opt.width.indexOf( "%" ) > -1 ? Methods.getCSS( $container, "width" ) : opt.width ),
_height = parseFloat( height ),
x,
y,
_x,
_y;
function mousemoveHandle ( event ) {
var _x = event.pageX - x,
_y = event.pageY - y;
if ( zoom > 1 ) {
var X = ( _width * zoom - _width ) / 2,
Y = ( _height * zoom - _height ) / 2;
( _x > X ) && ( _x = X );
( _y > Y ) && ( _y = Y );
( _x < -X ) && ( _x = -X );
( _y < -Y ) && ( _y = -Y );
Methods.setCSS($container, {
marginLeft: _x + "px",
marginTop: _y + "px",
transition: "0s",
cursor: "move"
});
Methods.setCSS($svg, {
pointerEvents: "none"
});
}
}
function mousedownHandle ( event ) {
x = event.pageX - $container.offsetLeft,
y = event.pageY - $container.offsetTop;
document.addEventListener( "mousemove", mousemoveHandle );
}
function mouseupHandle () {
document.removeEventListener( "mousemove", mousemoveHandle );
$container.style.removeProperty( "cursor" );
$svg.style.removeProperty( "pointer-events" );
}
$container.addEventListener( "mousedown", mousedownHandle );
document.addEventListener( "mouseup", mouseupHandle );
}
// 鏍囨敞鐐瑰姛鑳
if ( opt.marker.disabled === false ) {
$text.forEach(function ( text ) {
var box = text.getBBox();
// 蹇呴』浣跨敤姝g‘鐨勫懡鍚嶇┖闂存潵鍒涘缓 SVG $ 鍏冪礌
var image = document.createElementNS( "http://www.w3.org/2000/svg", "image" );
Methods.setAttr(image, {
x: parseInt( text.getAttribute( "x" ) ) + ( parseInt( box.width ) - 24 ) / 2,
y: parseInt( text.getAttribute( "y" ) ) - ( opt.areaName.show ? opt.areaName.size : 0 ) - 22,
width: 24,
height: 24,
style: "cursor:pointer",
"data-id": text.getAttribute( "data-id" ),
"data-name": text.getAttribute( "data-name" )
});
image.href.baseVal = opt.marker.icon;
$svg.appendChild( image );
$( "image", $svg ).forEach(function ( img ) {
var id = img.getAttribute( "data-id" );
// 鍒ゆ柇鏄惁涓鸿绂佺敤鍦板尯
if ( $( 'path[data-id="' + id + '"]', $svg )[ 0 ].classList.contains( "jsmap-disabled" ) ) {
img.setAttribute( "style", "cursor:not-allowed" );
img.classList.add( "jsmap-disabled" );
return;
}
})
})
$svg.addEventListener("click", function ( event ) {
var target = event.target
if ( target.nodeName.match( /(image|text)/ ) ) {
if ( target.classList.contains( "jsmap-disabled" ) ) {
return;
}
if ( Methods.type( opt.marker.click ) === "function" ) {
opt.marker.click( target.getAttribute( "data-id" ), target.getAttribute( "data-name" ) );
}
}
})
}
// 涓嶆樉绀哄尯鍩熷悕绉
if ( !opt.areaName.show ) {
$text.forEach(function ( text ) {
$svg.removeChild( text );
})
}
})
},
refresh: function ( selector ) {
$( selector ).forEach(function ( $el ) {
var cache_1 = $el[ "__jsmap_jsondata__" ],
cache_2 = $el[ "__jsmap_options__" ];
if ( cache_1 && cache_2 ) {
jsMap.config( $el, cache_1, cache_2 );
}
})
},
remove: function ( selector ) {
$( selector ).forEach(function ( $el ) {
var getCssCache = $el[ "__jsmap_csscache__" ];
if ( getCssCache ) {
for ( var i in getCssCache ) {
var v = getCssCache[ i ];
if ( v ) {
$el.style[ i ] = v;
} else {
$el.style.removeProperty( i );
}
}
}
$el.classList.remove( "jsmap-container" );
$el[ "__jsmap_csscache__" ] = null;
$el[ "__jsmap_jsondata__" ] = null;
$el[ "__jsmap_options__" ] = null;
$el.innerHTML = "";
})
},
triggerClick: function ( selector, area ) {
$( selector ).forEach(function ( $el ) {
var target = $( area.match( /[a-z]/ ) ? ".jsmap-" + area : 'path[data-name="' + area + '"]', $el )[ 0 ];
var evt = document.createEvent( "MouseEvents" );
evt.initEvent( "click", true, true );
target && target.dispatchEvent( evt );
})
},
selected: function ( selector, area ) {
var target = [];
var $selector = $( selector );
$selector.forEach(function ( $el ) {
$el.outerSelected = true;
if ( Array.isArray( area ) ) {
area.forEach(function ( a ) {
target.push( $( a.match( /[a-z]/ ) ? ".jsmap-" + a : 'path[data-name="' + a + '"]', $el )[ 0 ] );
})
}
if ( typeof area === "string" ) {
target.push( $( area.match( /[a-z]/ ) ? ".jsmap-" + area : 'path[data-name="' + area + '"]', $el )[ 0 ] );
}
})
target.forEach(function ( el ) {
var evt = document.createEvent( "MouseEvents" );
evt.initEvent( "click", true, true );
el && el.dispatchEvent( evt );
})
$selector.forEach(function ( $el ) {
$el.outerSelected = null;
})
},
getSelected: function ( selector, options ) {
var $target = $( ".jsmap-svg-container", $( selector )[ 0 ] )[ 0 ];
// 琚€変腑鐨勫尯鍩
var $clicked = $( ".jsmap-clicked", $target );
// 榛樿浠ユ暟缁勫舰寮忚繑鍥
if ( !options ) {
options = {
type: "array"
};
}
if ( options ) {
if ( options.type === "array" ) {
var a = [],
b = [];
if ( !$clicked.length ) {
return [ [], [] ];
}
$( $clicked ).forEach(function ( el ) {
a.push( el.getAttribute( "data-id" ) );
b.push( el.getAttribute( "data-name" ) );
})
return [ a, b ];
}
if ( options.type === "object" ) {
var obj = {};
if ( !$clicked.length ) {
return obj;
}
$( $clicked ).forEach(function ( el ) {
obj[ el.getAttribute( "data-id" ) ] = el.getAttribute( "data-name" );
})
return obj;
}
}
},
getJSON: function ( url, callback ) {
if ( !url ) {
return;
}
Request(url, function ( json ) {
if ( Methods.type( callback ) === "function" ) {
callback( json );
}
})
},
preloadJSON: function ( obj, callback ) {
if ( Methods.type( obj ) === "object" && !Methods.isEmptyObject( obj ) ) {
var cache = {};
var totalSize = Object.keys( obj ).length;
var successSize = 0;
for ( var name in obj ) {
var url = obj[ name ];
(function ( name, url ) {
Request(url, function ( json ) {
CacheJSON[ name ] = json;
successSize++;
if ( successSize === totalSize ) {
if ( Methods.type( callback ) === "function" ) {
callback( CacheJSON );
}
}
})
})( name, url );
}
}
},
getPreloadJSON: function ( name ) {
return !name ? CacheJSON : CacheJSON[ name ];
}
};
!(function freezeJsMap ( obj ) {
Object.freeze( obj );
Object.keys( obj ).forEach(function ( v ) {
if ( typeof obj[ v ] === "object" ) {
freezeJsMap( obj[ v ] );
}
})
})( jsMap );
return jsMap;
});