LaravelTest
1289 строк · 33.5 Кб
1/*!
2* JQVMap: jQuery Vector Map Library
3* @author JQVMap <me@peterschmalfeldt.com>
4* @version 1.5.1
5* @link http://jqvmap.com
6* @license https://github.com/manifestinteractive/jqvmap/blob/master/LICENSE
7* @builddate 2016/06/02
8*/
9
10var VectorCanvas = function (width, height, params) {11this.mode = window.SVGAngle ? 'svg' : 'vml';12this.params = params;13
14if (this.mode === 'svg') {15this.createSvgNode = function (nodeName) {16return document.createElementNS(this.svgns, nodeName);17};18} else {19try {20if (!document.namespaces.rvml) {21document.namespaces.add('rvml', 'urn:schemas-microsoft-com:vml');22}23this.createVmlNode = function (tagName) {24return document.createElement('<rvml:' + tagName + ' class="rvml">');25};26} catch (e) {27this.createVmlNode = function (tagName) {28return document.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');29};30}31
32document.createStyleSheet().addRule('.rvml', 'behavior:url(#default#VML)');33}34
35if (this.mode === 'svg') {36this.canvas = this.createSvgNode('svg');37} else {38this.canvas = this.createVmlNode('group');39this.canvas.style.position = 'absolute';40}41
42this.setSize(width, height);43};44
45VectorCanvas.prototype = {46svgns: 'http://www.w3.org/2000/svg',47mode: 'svg',48width: 0,49height: 0,50canvas: null51};52
53var ColorScale = function (colors, normalizeFunction, minValue, maxValue) {54if (colors) {55this.setColors(colors);56}57if (normalizeFunction) {58this.setNormalizeFunction(normalizeFunction);59}60if (minValue) {61this.setMin(minValue);62}63if (minValue) {64this.setMax(maxValue);65}66};67
68ColorScale.prototype = {69colors: []70};71
72var JQVMap = function (params) {73params = params || {};74var map = this;75var mapData = JQVMap.maps[params.map];76var mapPins;77
78if( !mapData){79throw new Error('Invalid "' + params.map + '" map parameter. Please make sure you have loaded this map file in your HTML.');80}81
82this.selectedRegions = [];83this.multiSelectRegion = params.multiSelectRegion;84
85this.container = params.container;86
87this.defaultWidth = mapData.width;88this.defaultHeight = mapData.height;89
90this.color = params.color;91this.selectedColor = params.selectedColor;92this.hoverColor = params.hoverColor;93this.hoverColors = params.hoverColors;94this.hoverOpacity = params.hoverOpacity;95this.setBackgroundColor(params.backgroundColor);96
97this.width = params.container.width();98this.height = params.container.height();99
100this.resize();101
102jQuery(window).resize(function () {103var newWidth = params.container.width();104var newHeight = params.container.height();105
106if(newWidth && newHeight){107map.width = newWidth;108map.height = newHeight;109map.resize();110map.canvas.setSize(map.width, map.height);111map.applyTransform();112
113var resizeEvent = jQuery.Event('resize.jqvmap');114jQuery(params.container).trigger(resizeEvent, [newWidth, newHeight]);115
116if(mapPins){117jQuery('.jqvmap-pin').remove();118map.pinHandlers = false;119map.placePins(mapPins.pins, mapPins.mode);120}121}122});123
124this.canvas = new VectorCanvas(this.width, this.height, params);125params.container.append(this.canvas.canvas);126
127this.makeDraggable();128
129this.rootGroup = this.canvas.createGroup(true);130
131this.index = JQVMap.mapIndex;132this.label = jQuery('<div/>').addClass('jqvmap-label').appendTo(jQuery('body')).hide();133
134if (params.enableZoom) {135jQuery('<div/>').addClass('jqvmap-zoomin').text('+').appendTo(params.container);136jQuery('<div/>').addClass('jqvmap-zoomout').html('−').appendTo(params.container);137}138
139map.countries = [];140
141for (var key in mapData.paths) {142var path = this.canvas.createPath({143path: mapData.paths[key].path144});145
146path.setFill(this.color);147path.id = map.getCountryId(key);148map.countries[key] = path;149
150if (this.canvas.mode === 'svg') {151path.setAttribute('class', 'jqvmap-region');152} else {153jQuery(path).addClass('jqvmap-region');154}155
156jQuery(this.rootGroup).append(path);157}158
159jQuery(params.container).delegate(this.canvas.mode === 'svg' ? 'path' : 'shape', 'mouseover mouseout', function (e) {160var containerPath = e.target,161code = e.target.id.split('_').pop(),162labelShowEvent = jQuery.Event('labelShow.jqvmap'),163regionMouseOverEvent = jQuery.Event('regionMouseOver.jqvmap');164
165code = code.toLowerCase();166
167if (e.type === 'mouseover') {168jQuery(params.container).trigger(regionMouseOverEvent, [code, mapData.paths[code].name]);169if (!regionMouseOverEvent.isDefaultPrevented()) {170map.highlight(code, containerPath);171}172if (params.showTooltip) {173map.label.text(mapData.paths[code].name);174jQuery(params.container).trigger(labelShowEvent, [map.label, code]);175
176if (!labelShowEvent.isDefaultPrevented()) {177map.label.show();178map.labelWidth = map.label.width();179map.labelHeight = map.label.height();180}181}182} else {183map.unhighlight(code, containerPath);184
185map.label.hide();186jQuery(params.container).trigger('regionMouseOut.jqvmap', [code, mapData.paths[code].name]);187}188});189
190jQuery(params.container).delegate(this.canvas.mode === 'svg' ? 'path' : 'shape', 'click', function (regionClickEvent) {191
192var targetPath = regionClickEvent.target;193var code = regionClickEvent.target.id.split('_').pop();194var mapClickEvent = jQuery.Event('regionClick.jqvmap');195
196code = code.toLowerCase();197
198jQuery(params.container).trigger(mapClickEvent, [code, mapData.paths[code].name]);199
200if ( !params.multiSelectRegion && !mapClickEvent.isDefaultPrevented()) {201for (var keyPath in mapData.paths) {202map.countries[keyPath].currentFillColor = map.countries[keyPath].getOriginalFill();203map.countries[keyPath].setFill(map.countries[keyPath].getOriginalFill());204}205}206
207if ( !mapClickEvent.isDefaultPrevented()) {208if (map.isSelected(code)) {209map.deselect(code, targetPath);210} else {211map.select(code, targetPath);212}213}214});215
216if (params.showTooltip) {217params.container.mousemove(function (e) {218if (map.label.is(':visible')) {219var left = e.pageX - 15 - map.labelWidth;220var top = e.pageY - 15 - map.labelHeight;221
222if(left < 0) {223left = e.pageX + 15;224}225if(top < 0) {226top = e.pageY + 15;227}228
229map.label.css({230left: left,231top: top232});233}234});235}236
237this.setColors(params.colors);238
239this.canvas.canvas.appendChild(this.rootGroup);240
241this.applyTransform();242
243this.colorScale = new ColorScale(params.scaleColors, params.normalizeFunction, params.valueMin, params.valueMax);244
245if (params.values) {246this.values = params.values;247this.setValues(params.values);248}249
250if (params.selectedRegions) {251if (params.selectedRegions instanceof Array) {252for(var k in params.selectedRegions) {253this.select(params.selectedRegions[k].toLowerCase());254}255} else {256this.select(params.selectedRegions.toLowerCase());257}258}259
260this.bindZoomButtons();261
262if(params.pins) {263mapPins = {264pins: params.pins,265mode: params.pinMode266};267
268this.pinHandlers = false;269this.placePins(params.pins, params.pinMode);270}271
272if(params.showLabels){273this.pinHandlers = false;274
275var pins = {};276for (key in map.countries){277if (typeof map.countries[key] !== 'function') {278if( !params.pins || !params.pins[key] ){279pins[key] = key.toUpperCase();280}281}282}283
284mapPins = {285pins: pins,286mode: 'content'287};288
289this.placePins(pins, 'content');290}291
292JQVMap.mapIndex++;293};294
295JQVMap.prototype = {296transX: 0,297transY: 0,298scale: 1,299baseTransX: 0,300baseTransY: 0,301baseScale: 1,302width: 0,303height: 0,304countries: {},305countriesColors: {},306countriesData: {},307zoomStep: 1.4,308zoomMaxStep: 4,309zoomCurStep: 1310};311
312JQVMap.xlink = 'http://www.w3.org/1999/xlink';313JQVMap.mapIndex = 1;314JQVMap.maps = {};315
316(function(){317
318var apiParams = {319colors: 1,320values: 1,321backgroundColor: 1,322scaleColors: 1,323normalizeFunction: 1,324enableZoom: 1,325showTooltip: 1,326borderColor: 1,327borderWidth: 1,328borderOpacity: 1,329selectedRegions: 1,330multiSelectRegion: 1331};332
333var apiEvents = {334onLabelShow: 'labelShow',335onLoad: 'load',336onRegionOver: 'regionMouseOver',337onRegionOut: 'regionMouseOut',338onRegionClick: 'regionClick',339onRegionSelect: 'regionSelect',340onRegionDeselect: 'regionDeselect',341onResize: 'resize'342};343
344jQuery.fn.vectorMap = function (options) {345
346var defaultParams = {347map: 'world_en',348backgroundColor: '#a5bfdd',349color: '#f4f3f0',350hoverColor: '#c9dfaf',351hoverColors: {},352selectedColor: '#c9dfaf',353scaleColors: ['#b6d6ff', '#005ace'],354normalizeFunction: 'linear',355enableZoom: true,356showTooltip: true,357borderColor: '#818181',358borderWidth: 1,359borderOpacity: 0.25,360selectedRegions: null,361multiSelectRegion: false362}, map = this.data('mapObject');363
364if (options === 'addMap') {365JQVMap.maps[arguments[1]] = arguments[2];366} else if (options === 'set' && apiParams[arguments[1]]) {367map['set' + arguments[1].charAt(0).toUpperCase() + arguments[1].substr(1)].apply(map, Array.prototype.slice.call(arguments, 2));368} else if (typeof options === 'string' &&369typeof map[options] === 'function') {370return map[options].apply(map, Array.prototype.slice.call(arguments, 1));371} else {372jQuery.extend(defaultParams, options);373defaultParams.container = this;374this.css({ position: 'relative', overflow: 'hidden' });375
376map = new JQVMap(defaultParams);377
378this.data('mapObject', map);379
380this.unbind('.jqvmap');381
382for (var e in apiEvents) {383if (defaultParams[e]) {384this.bind(apiEvents[e] + '.jqvmap', defaultParams[e]);385}386}387
388var loadEvent = jQuery.Event('load.jqvmap');389jQuery(defaultParams.container).trigger(loadEvent, map);390
391return map;392}393};394
395})(jQuery);396
397ColorScale.arrayToRgb = function (ar) {398var rgb = '#';399var d;400for (var i = 0; i < ar.length; i++) {401d = ar[i].toString(16);402rgb += d.length === 1 ? '0' + d : d;403}404return rgb;405};406
407ColorScale.prototype.getColor = function (value) {408if (typeof this.normalize === 'function') {409value = this.normalize(value);410}411
412var lengthes = [];413var fullLength = 0;414var l;415
416for (var i = 0; i < this.colors.length - 1; i++) {417l = this.vectorLength(this.vectorSubtract(this.colors[i + 1], this.colors[i]));418lengthes.push(l);419fullLength += l;420}421
422var c = (this.maxValue - this.minValue) / fullLength;423
424for (i = 0; i < lengthes.length; i++) {425lengthes[i] *= c;426}427
428i = 0;429value -= this.minValue;430
431while (value - lengthes[i] >= 0) {432value -= lengthes[i];433i++;434}435
436var color;437if (i === this.colors.length - 1) {438color = this.vectorToNum(this.colors[i]).toString(16);439} else {440color = (this.vectorToNum(this.vectorAdd(this.colors[i], this.vectorMult(this.vectorSubtract(this.colors[i + 1], this.colors[i]), (value) / (lengthes[i]))))).toString(16);441}442
443while (color.length < 6) {444color = '0' + color;445}446return '#' + color;447};448
449ColorScale.rgbToArray = function (rgb) {450rgb = rgb.substr(1);451return [parseInt(rgb.substr(0, 2), 16), parseInt(rgb.substr(2, 2), 16), parseInt(rgb.substr(4, 2), 16)];452};453
454ColorScale.prototype.setColors = function (colors) {455for (var i = 0; i < colors.length; i++) {456colors[i] = ColorScale.rgbToArray(colors[i]);457}458this.colors = colors;459};460
461ColorScale.prototype.setMax = function (max) {462this.clearMaxValue = max;463if (typeof this.normalize === 'function') {464this.maxValue = this.normalize(max);465} else {466this.maxValue = max;467}468};469
470ColorScale.prototype.setMin = function (min) {471this.clearMinValue = min;472
473if (typeof this.normalize === 'function') {474this.minValue = this.normalize(min);475} else {476this.minValue = min;477}478};479
480ColorScale.prototype.setNormalizeFunction = function (f) {481if (f === 'polynomial') {482this.normalize = function (value) {483return Math.pow(value, 0.2);484};485} else if (f === 'linear') {486delete this.normalize;487} else {488this.normalize = f;489}490this.setMin(this.clearMinValue);491this.setMax(this.clearMaxValue);492};493
494ColorScale.prototype.vectorAdd = function (vector1, vector2) {495var vector = [];496for (var i = 0; i < vector1.length; i++) {497vector[i] = vector1[i] + vector2[i];498}499return vector;500};501
502ColorScale.prototype.vectorLength = function (vector) {503var result = 0;504for (var i = 0; i < vector.length; i++) {505result += vector[i] * vector[i];506}507return Math.sqrt(result);508};509
510ColorScale.prototype.vectorMult = function (vector, num) {511var result = [];512for (var i = 0; i < vector.length; i++) {513result[i] = vector[i] * num;514}515return result;516};517
518ColorScale.prototype.vectorSubtract = function (vector1, vector2) {519var vector = [];520for (var i = 0; i < vector1.length; i++) {521vector[i] = vector1[i] - vector2[i];522}523return vector;524};525
526ColorScale.prototype.vectorToNum = function (vector) {527var num = 0;528for (var i = 0; i < vector.length; i++) {529num += Math.round(vector[i]) * Math.pow(256, vector.length - i - 1);530}531return num;532};533
534JQVMap.prototype.applyTransform = function () {535var maxTransX, maxTransY, minTransX, minTransY;536if (this.defaultWidth * this.scale <= this.width) {537maxTransX = (this.width - this.defaultWidth * this.scale) / (2 * this.scale);538minTransX = (this.width - this.defaultWidth * this.scale) / (2 * this.scale);539} else {540maxTransX = 0;541minTransX = (this.width - this.defaultWidth * this.scale) / this.scale;542}543
544if (this.defaultHeight * this.scale <= this.height) {545maxTransY = (this.height - this.defaultHeight * this.scale) / (2 * this.scale);546minTransY = (this.height - this.defaultHeight * this.scale) / (2 * this.scale);547} else {548maxTransY = 0;549minTransY = (this.height - this.defaultHeight * this.scale) / this.scale;550}551
552if (this.transY > maxTransY) {553this.transY = maxTransY;554} else if (this.transY < minTransY) {555this.transY = minTransY;556}557if (this.transX > maxTransX) {558this.transX = maxTransX;559} else if (this.transX < minTransX) {560this.transX = minTransX;561}562
563this.canvas.applyTransformParams(this.scale, this.transX, this.transY);564};565
566JQVMap.prototype.bindZoomButtons = function () {567var map = this;568this.container.find('.jqvmap-zoomin').click(function(){569map.zoomIn();570});571this.container.find('.jqvmap-zoomout').click(function(){572map.zoomOut();573});574};575
576JQVMap.prototype.deselect = function (cc, path) {577cc = cc.toLowerCase();578path = path || jQuery('#' + this.getCountryId(cc))[0];579
580if (this.isSelected(cc)) {581this.selectedRegions.splice(this.selectIndex(cc), 1);582
583jQuery(this.container).trigger('regionDeselect.jqvmap', [cc]);584path.currentFillColor = path.getOriginalFill();585path.setFill(path.getOriginalFill());586} else {587for (var key in this.countries) {588this.selectedRegions.splice(this.selectedRegions.indexOf(key), 1);589this.countries[key].currentFillColor = this.color;590this.countries[key].setFill(this.color);591}592}593};594
595JQVMap.prototype.getCountryId = function (cc) {596return 'jqvmap' + this.index + '_' + cc;597};598
599JQVMap.prototype.getPin = function(cc){600var pinObj = jQuery('#' + this.getPinId(cc));601return pinObj.html();602};603
604JQVMap.prototype.getPinId = function (cc) {605return this.getCountryId(cc) + '_pin';606};607
608JQVMap.prototype.getPins = function(){609var pins = this.container.find('.jqvmap-pin');610var ret = {};611jQuery.each(pins, function(index, pinObj){612pinObj = jQuery(pinObj);613var cc = pinObj.attr('for').toLowerCase();614var pinContent = pinObj.html();615ret[cc] = pinContent;616});617return JSON.stringify(ret);618};619
620JQVMap.prototype.highlight = function (cc, path) {621path = path || jQuery('#' + this.getCountryId(cc))[0];622if (this.hoverOpacity) {623path.setOpacity(this.hoverOpacity);624} else if (this.hoverColors && (cc in this.hoverColors)) {625path.currentFillColor = path.getFill() + '';626path.setFill(this.hoverColors[cc]);627} else if (this.hoverColor) {628path.currentFillColor = path.getFill() + '';629path.setFill(this.hoverColor);630}631};632
633JQVMap.prototype.isSelected = function(cc) {634return this.selectIndex(cc) >= 0;635};636
637JQVMap.prototype.makeDraggable = function () {638var mouseDown = false;639var oldPageX, oldPageY;640var self = this;641
642self.isMoving = false;643self.isMovingTimeout = false;644
645var lastTouchCount;646var touchCenterX;647var touchCenterY;648var touchStartDistance;649var touchStartScale;650var touchX;651var touchY;652
653this.container.mousemove(function (e) {654
655if (mouseDown) {656self.transX -= (oldPageX - e.pageX) / self.scale;657self.transY -= (oldPageY - e.pageY) / self.scale;658
659self.applyTransform();660
661oldPageX = e.pageX;662oldPageY = e.pageY;663
664self.isMoving = true;665if (self.isMovingTimeout) {666clearTimeout(self.isMovingTimeout);667}668
669self.container.trigger('drag');670}671
672return false;673
674}).mousedown(function (e) {675
676mouseDown = true;677oldPageX = e.pageX;678oldPageY = e.pageY;679
680return false;681
682}).mouseup(function () {683
684mouseDown = false;685
686clearTimeout(self.isMovingTimeout);687self.isMovingTimeout = setTimeout(function () {688self.isMoving = false;689}, 100);690
691return false;692
693}).mouseout(function () {694
695if(mouseDown && self.isMoving){696
697clearTimeout(self.isMovingTimeout);698self.isMovingTimeout = setTimeout(function () {699mouseDown = false;700self.isMoving = false;701}, 100);702
703return false;704}705});706
707jQuery(this.container).bind('touchmove', function (e) {708
709var offset;710var scale;711var touches = e.originalEvent.touches;712var transformXOld;713var transformYOld;714
715if (touches.length === 1) {716if (lastTouchCount === 1) {717
718if(touchX === touches[0].pageX && touchY === touches[0].pageY){719return;720}721
722transformXOld = self.transX;723transformYOld = self.transY;724
725self.transX -= (touchX - touches[0].pageX) / self.scale;726self.transY -= (touchY - touches[0].pageY) / self.scale;727
728self.applyTransform();729
730if (transformXOld !== self.transX || transformYOld !== self.transY) {731e.preventDefault();732}733
734self.isMoving = true;735if (self.isMovingTimeout) {736clearTimeout(self.isMovingTimeout);737}738}739
740touchX = touches[0].pageX;741touchY = touches[0].pageY;742
743} else if (touches.length === 2) {744
745if (lastTouchCount === 2) {746scale = Math.sqrt(747Math.pow(touches[0].pageX - touches[1].pageX, 2) +748Math.pow(touches[0].pageY - touches[1].pageY, 2)749) / touchStartDistance;750
751self.setScale(752touchStartScale * scale,753touchCenterX,754touchCenterY
755);756
757e.preventDefault();758
759} else {760
761offset = jQuery(self.container).offset();762if (touches[0].pageX > touches[1].pageX) {763touchCenterX = touches[1].pageX + (touches[0].pageX - touches[1].pageX) / 2;764} else {765touchCenterX = touches[0].pageX + (touches[1].pageX - touches[0].pageX) / 2;766}767
768if (touches[0].pageY > touches[1].pageY) {769touchCenterY = touches[1].pageY + (touches[0].pageY - touches[1].pageY) / 2;770} else {771touchCenterY = touches[0].pageY + (touches[1].pageY - touches[0].pageY) / 2;772}773
774touchCenterX -= offset.left;775touchCenterY -= offset.top;776touchStartScale = self.scale;777
778touchStartDistance = Math.sqrt(779Math.pow(touches[0].pageX - touches[1].pageX, 2) +780Math.pow(touches[0].pageY - touches[1].pageY, 2)781);782}783}784
785lastTouchCount = touches.length;786});787
788jQuery(this.container).bind('touchstart', function () {789lastTouchCount = 0;790});791
792jQuery(this.container).bind('touchend', function () {793lastTouchCount = 0;794});795};796
797JQVMap.prototype.placePins = function(pins, pinMode){798var map = this;799
800if(!pinMode || (pinMode !== 'content' && pinMode !== 'id')) {801pinMode = 'content';802}803
804if(pinMode === 'content') {//treat pin as content805jQuery.each(pins, function(index, pin){806if(jQuery('#' + map.getCountryId(index)).length === 0){807return;808}809
810var pinIndex = map.getPinId(index);811var $pin = jQuery('#' + pinIndex);812if($pin.length > 0){813$pin.remove();814}815map.container.append('<div id="' + pinIndex + '" for="' + index + '" class="jqvmap-pin" style="position:absolute">' + pin + '</div>');816});817} else { //treat pin as id of an html content818jQuery.each(pins, function(index, pin){819if(jQuery('#' + map.getCountryId(index)).length === 0){820return;821}822var pinIndex = map.getPinId(index);823var $pin = jQuery('#' + pinIndex);824if($pin.length > 0){825$pin.remove();826}827map.container.append('<div id="' + pinIndex + '" for="' + index + '" class="jqvmap-pin" style="position:absolute"></div>');828$pin.append(jQuery('#' + pin));829});830}831
832this.positionPins();833if(!this.pinHandlers){834this.pinHandlers = true;835var positionFix = function(){836map.positionPins();837};838this.container.bind('zoomIn', positionFix)839.bind('zoomOut', positionFix)840.bind('drag', positionFix);841}842};843
844JQVMap.prototype.positionPins = function(){845var map = this;846var pins = this.container.find('.jqvmap-pin');847jQuery.each(pins, function(index, pinObj){848pinObj = jQuery(pinObj);849var countryId = map.getCountryId(pinObj.attr('for').toLowerCase());850var countryObj = jQuery('#' + countryId);851var bbox = countryObj[0].getBBox();852
853var scale = map.scale;854var rootCoords = map.canvas.rootGroup.getBoundingClientRect();855var mapCoords = map.container[0].getBoundingClientRect();856var coords = {857left: rootCoords.left - mapCoords.left,858top: rootCoords.top - mapCoords.top859};860
861var middleX = (bbox.x * scale) + ((bbox.width * scale) / 2);862var middleY = (bbox.y * scale) + ((bbox.height * scale) / 2);863
864pinObj.css({865left: coords.left + middleX - (pinObj.width() / 2),866top: coords.top + middleY - (pinObj.height() / 2)867});868});869};870
871JQVMap.prototype.removePin = function(cc) {872cc = cc.toLowerCase();873jQuery('#' + this.getPinId(cc)).remove();874};875
876JQVMap.prototype.removePins = function(){877this.container.find('.jqvmap-pin').remove();878};879
880JQVMap.prototype.reset = function () {881for (var key in this.countries) {882this.countries[key].setFill(this.color);883}884this.scale = this.baseScale;885this.transX = this.baseTransX;886this.transY = this.baseTransY;887this.applyTransform();888this.zoomCurStep = 1;889};890
891JQVMap.prototype.resize = function () {892var curBaseScale = this.baseScale;893if (this.width / this.height > this.defaultWidth / this.defaultHeight) {894this.baseScale = this.height / this.defaultHeight;895this.baseTransX = Math.abs(this.width - this.defaultWidth * this.baseScale) / (2 * this.baseScale);896} else {897this.baseScale = this.width / this.defaultWidth;898this.baseTransY = Math.abs(this.height - this.defaultHeight * this.baseScale) / (2 * this.baseScale);899}900this.scale *= this.baseScale / curBaseScale;901this.transX *= this.baseScale / curBaseScale;902this.transY *= this.baseScale / curBaseScale;903};904
905JQVMap.prototype.select = function (cc, path) {906cc = cc.toLowerCase();907path = path || jQuery('#' + this.getCountryId(cc))[0];908
909if (!this.isSelected(cc)) {910if (this.multiSelectRegion) {911this.selectedRegions.push(cc);912} else {913this.selectedRegions = [cc];914}915
916jQuery(this.container).trigger('regionSelect.jqvmap', [cc]);917if (this.selectedColor && path) {918path.currentFillColor = this.selectedColor;919path.setFill(this.selectedColor);920}921}922};923
924JQVMap.prototype.selectIndex = function (cc) {925cc = cc.toLowerCase();926for (var i = 0; i < this.selectedRegions.length; i++) {927if (cc === this.selectedRegions[i]) {928return i;929}930}931return -1;932};933
934JQVMap.prototype.setBackgroundColor = function (backgroundColor) {935this.container.css('background-color', backgroundColor);936};937
938JQVMap.prototype.setColors = function (key, color) {939if (typeof key === 'string') {940this.countries[key].setFill(color);941this.countries[key].setAttribute('original', color);942} else {943var colors = key;944
945for (var code in colors) {946if (this.countries[code]) {947this.countries[code].setFill(colors[code]);948this.countries[code].setAttribute('original', colors[code]);949}950}951}952};953
954JQVMap.prototype.setNormalizeFunction = function (f) {955this.colorScale.setNormalizeFunction(f);956
957if (this.values) {958this.setValues(this.values);959}960};961
962JQVMap.prototype.setScale = function (scale) {963this.scale = scale;964this.applyTransform();965};966
967JQVMap.prototype.setScaleColors = function (colors) {968this.colorScale.setColors(colors);969
970if (this.values) {971this.setValues(this.values);972}973};974
975JQVMap.prototype.setValues = function (values) {976var max = 0,977min = Number.MAX_VALUE,978val;979
980for (var cc in values) {981cc = cc.toLowerCase();982val = parseFloat(values[cc]);983
984if (isNaN(val)) {985continue;986}987if (val > max) {988max = values[cc];989}990if (val < min) {991min = val;992}993}994
995if (min === max) {996max++;997}998
999this.colorScale.setMin(min);1000this.colorScale.setMax(max);1001
1002var colors = {};1003for (cc in values) {1004cc = cc.toLowerCase();1005val = parseFloat(values[cc]);1006colors[cc] = isNaN(val) ? this.color : this.colorScale.getColor(val);1007}1008this.setColors(colors);1009this.values = values;1010};1011
1012JQVMap.prototype.unhighlight = function (cc, path) {1013cc = cc.toLowerCase();1014path = path || jQuery('#' + this.getCountryId(cc))[0];1015path.setOpacity(1);1016if (path.currentFillColor) {1017path.setFill(path.currentFillColor);1018}1019};1020
1021JQVMap.prototype.zoomIn = function () {1022var map = this;1023var sliderDelta = (jQuery('#zoom').innerHeight() - 6 * 2 - 15 * 2 - 3 * 2 - 7 - 6) / (this.zoomMaxStep - this.zoomCurStep);1024
1025if (map.zoomCurStep < map.zoomMaxStep) {1026map.transX -= (map.width / map.scale - map.width / (map.scale * map.zoomStep)) / 2;1027map.transY -= (map.height / map.scale - map.height / (map.scale * map.zoomStep)) / 2;1028map.setScale(map.scale * map.zoomStep);1029map.zoomCurStep++;1030
1031var $slider = jQuery('#zoomSlider');1032
1033$slider.css('top', parseInt($slider.css('top'), 10) - sliderDelta);1034
1035map.container.trigger('zoomIn');1036}1037};1038
1039JQVMap.prototype.zoomOut = function () {1040var map = this;1041var sliderDelta = (jQuery('#zoom').innerHeight() - 6 * 2 - 15 * 2 - 3 * 2 - 7 - 6) / (this.zoomMaxStep - this.zoomCurStep);1042
1043if (map.zoomCurStep > 1) {1044map.transX += (map.width / (map.scale / map.zoomStep) - map.width / map.scale) / 2;1045map.transY += (map.height / (map.scale / map.zoomStep) - map.height / map.scale) / 2;1046map.setScale(map.scale / map.zoomStep);1047map.zoomCurStep--;1048
1049var $slider = jQuery('#zoomSlider');1050
1051$slider.css('top', parseInt($slider.css('top'), 10) + sliderDelta);1052
1053map.container.trigger('zoomOut');1054}1055};1056
1057VectorCanvas.prototype.applyTransformParams = function (scale, transX, transY) {1058if (this.mode === 'svg') {1059this.rootGroup.setAttribute('transform', 'scale(' + scale + ') translate(' + transX + ', ' + transY + ')');1060} else {1061this.rootGroup.coordorigin = (this.width - transX) + ',' + (this.height - transY);1062this.rootGroup.coordsize = this.width / scale + ',' + this.height / scale;1063}1064};1065
1066VectorCanvas.prototype.createGroup = function (isRoot) {1067var node;1068if (this.mode === 'svg') {1069node = this.createSvgNode('g');1070} else {1071node = this.createVmlNode('group');1072node.style.width = this.width + 'px';1073node.style.height = this.height + 'px';1074node.style.left = '0px';1075node.style.top = '0px';1076node.coordorigin = '0 0';1077node.coordsize = this.width + ' ' + this.height;1078}1079
1080if (isRoot) {1081this.rootGroup = node;1082}1083return node;1084};1085
1086VectorCanvas.prototype.createPath = function (config) {1087var node;1088if (this.mode === 'svg') {1089node = this.createSvgNode('path');1090node.setAttribute('d', config.path);1091
1092if (this.params.borderColor !== null) {1093node.setAttribute('stroke', this.params.borderColor);1094}1095if (this.params.borderWidth > 0) {1096node.setAttribute('stroke-width', this.params.borderWidth);1097node.setAttribute('stroke-linecap', 'round');1098node.setAttribute('stroke-linejoin', 'round');1099}1100if (this.params.borderOpacity > 0) {1101node.setAttribute('stroke-opacity', this.params.borderOpacity);1102}1103
1104node.setFill = function (color) {1105this.setAttribute('fill', color);1106if (this.getAttribute('original') === null) {1107this.setAttribute('original', color);1108}1109};1110
1111node.getFill = function () {1112return this.getAttribute('fill');1113};1114
1115node.getOriginalFill = function () {1116return this.getAttribute('original');1117};1118
1119node.setOpacity = function (opacity) {1120this.setAttribute('fill-opacity', opacity);1121};1122} else {1123node = this.createVmlNode('shape');1124node.coordorigin = '0 0';1125node.coordsize = this.width + ' ' + this.height;1126node.style.width = this.width + 'px';1127node.style.height = this.height + 'px';1128node.fillcolor = JQVMap.defaultFillColor;1129node.stroked = false;1130node.path = VectorCanvas.pathSvgToVml(config.path);1131
1132var scale = this.createVmlNode('skew');1133scale.on = true;1134scale.matrix = '0.01,0,0,0.01,0,0';1135scale.offset = '0,0';1136
1137node.appendChild(scale);1138
1139var fill = this.createVmlNode('fill');1140node.appendChild(fill);1141
1142node.setFill = function (color) {1143this.getElementsByTagName('fill')[0].color = color;1144if (this.getAttribute('original') === null) {1145this.setAttribute('original', color);1146}1147};1148
1149node.getFill = function () {1150return this.getElementsByTagName('fill')[0].color;1151};1152node.getOriginalFill = function () {1153return this.getAttribute('original');1154};1155node.setOpacity = function (opacity) {1156this.getElementsByTagName('fill')[0].opacity = parseInt(opacity * 100, 10) + '%';1157};1158}1159return node;1160};1161
1162VectorCanvas.prototype.pathSvgToVml = function (path) {1163var result = '';1164var cx = 0, cy = 0, ctrlx, ctrly;1165
1166return path.replace(/([MmLlHhVvCcSs])((?:-?(?:\d+)?(?:\.\d+)?,?\s?)+)/g, function (segment, letter, coords) {1167coords = coords.replace(/(\d)-/g, '$1,-').replace(/\s+/g, ',').split(',');1168if (!coords[0]) {1169coords.shift();1170}1171
1172for (var i = 0, l = coords.length; i < l; i++) {1173coords[i] = Math.round(100 * coords[i]);1174}1175
1176switch (letter) {1177case 'm':1178cx += coords[0];1179cy += coords[1];1180result = 't' + coords.join(',');1181break;1182
1183case 'M':1184cx = coords[0];1185cy = coords[1];1186result = 'm' + coords.join(',');1187break;1188
1189case 'l':1190cx += coords[0];1191cy += coords[1];1192result = 'r' + coords.join(',');1193break;1194
1195case 'L':1196cx = coords[0];1197cy = coords[1];1198result = 'l' + coords.join(',');1199break;1200
1201case 'h':1202cx += coords[0];1203result = 'r' + coords[0] + ',0';1204break;1205
1206case 'H':1207cx = coords[0];1208result = 'l' + cx + ',' + cy;1209break;1210
1211case 'v':1212cy += coords[0];1213result = 'r0,' + coords[0];1214break;1215
1216case 'V':1217cy = coords[0];1218result = 'l' + cx + ',' + cy;1219break;1220
1221case 'c':1222ctrlx = cx + coords[coords.length - 4];1223ctrly = cy + coords[coords.length - 3];1224cx += coords[coords.length - 2];1225cy += coords[coords.length - 1];1226result = 'v' + coords.join(',');1227break;1228
1229case 'C':1230ctrlx = coords[coords.length - 4];1231ctrly = coords[coords.length - 3];1232cx = coords[coords.length - 2];1233cy = coords[coords.length - 1];1234result = 'c' + coords.join(',');1235break;1236
1237case 's':1238coords.unshift(cy - ctrly);1239coords.unshift(cx - ctrlx);1240ctrlx = cx + coords[coords.length - 4];1241ctrly = cy + coords[coords.length - 3];1242cx += coords[coords.length - 2];1243cy += coords[coords.length - 1];1244result = 'v' + coords.join(',');1245break;1246
1247case 'S':1248coords.unshift(cy + cy - ctrly);1249coords.unshift(cx + cx - ctrlx);1250ctrlx = coords[coords.length - 4];1251ctrly = coords[coords.length - 3];1252cx = coords[coords.length - 2];1253cy = coords[coords.length - 1];1254result = 'c' + coords.join(',');1255break;1256
1257default:1258break;1259}1260
1261return result;1262
1263}).replace(/z/g, '');1264};1265
1266VectorCanvas.prototype.setSize = function (width, height) {1267if (this.mode === 'svg') {1268this.canvas.setAttribute('width', width);1269this.canvas.setAttribute('height', height);1270} else {1271this.canvas.style.width = width + 'px';1272this.canvas.style.height = height + 'px';1273this.canvas.coordsize = width + ' ' + height;1274this.canvas.coordorigin = '0 0';1275if (this.rootGroup) {1276var paths = this.rootGroup.getElementsByTagName('shape');1277for (var i = 0, l = paths.length; i < l; i++) {1278paths[i].coordsize = width + ' ' + height;1279paths[i].style.width = width + 'px';1280paths[i].style.height = height + 'px';1281}1282this.rootGroup.coordsize = width + ' ' + height;1283this.rootGroup.style.width = width + 'px';1284this.rootGroup.style.height = height + 'px';1285}1286}1287this.width = width;1288this.height = height;1289};1290