-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathjquery.mapael.js
More file actions
2898 lines (2559 loc) · 125 KB
/
Copy pathjquery.mapael.js
File metadata and controls
2898 lines (2559 loc) · 125 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
*
* Jquery Mapael - Dynamic maps jQuery plugin (based on raphael.js)
* Requires jQuery, raphael.js and jquery.mousewheel
*
* Version: 2.1.0
*
* Copyright (c) 2017 Vincent Brouté (https://www.vincentbroute.fr/mapael)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
*
* Thanks to Indigo744
*
*/
(function (factory) {
if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('jquery'), require('raphael'), require('jquery-mousewheel'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'raphael', 'mousewheel'], factory);
} else {
// Browser globals
factory(jQuery, Raphael, jQuery.fn.mousewheel);
}
}(function ($, Raphael, mousewheel, undefined) {
"use strict";
// The plugin name (used on several places)
var pluginName = "mapael";
// Version number of jQuery Mapael. See http://semver.org/ for more information.
var version = "2.1.0";
/*
* Mapael constructor
* Init instance vars and call init()
* @param container the DOM element on which to apply the plugin
* @param options the complete options to use
*/
var Mapael = function (container, options) {
var self = this;
// the global container (DOM element object)
self.container = container;
// the global container (jQuery object)
self.$container = $(container);
// the global options
self.options = self.extendDefaultOptions(options);
// zoom TimeOut handler (used to set and clear)
self.zoomTO = 0;
// zoom center coordinate (set at touchstart)
self.zoomCenterX = 0;
self.zoomCenterY = 0;
// Zoom pinch (set at touchstart and touchmove)
self.previousPinchDist = 0;
// Zoom data
self.zoomData = {
zoomLevel: 0,
zoomX: 0,
zoomY: 0,
panX: 0,
panY: 0,
scaling: false
};
self.currentViewBox = {
x: 0, y: 0, w: 0, h: 0
};
// Panning: tell if panning action is in progress
self.panning = false;
// Animate view box
self.zoomAnimID = null; // Interval handler (used to set and clear)
self.zoomAnimStartTime = null; // Animation start time
self.zoomAnimCVBTarget = null; // Current ViewBox target
// Map subcontainer jQuery object
self.$map = $("." + self.options.map.cssClass, self.container);
// Save initial HTML content (used by destroy method)
self.initialMapHTMLContent = self.$map.html();
// The tooltip jQuery object
self.$tooltip = {};
// The paper Raphael object
self.paper = {};
// The areas object list
self.areas = {};
// The plots object list
self.plots = {};
// The links object list
self.links = {};
// The legends list
self.legends = {};
// The map configuration object (taken from map file)
self.mapConf = {};
// Holds all custom event handlers
self.customEventHandlers = {};
// Let's start the initialization
self.init();
};
/*
* Mapael Prototype
* Defines all methods and properties needed by Mapael
* Each mapael object inherits their properties and methods from this prototype
*/
Mapael.prototype = {
/* Filtering TimeOut value in ms
* Used for mouseover trigger over elements */
MouseOverFilteringTO: 120,
/* Filtering TimeOut value in ms
* Used for afterPanning trigger when panning */
panningFilteringTO: 150,
/* Filtering TimeOut value in ms
* Used for mouseup/touchend trigger when panning */
panningEndFilteringTO: 50,
/* Filtering TimeOut value in ms
* Used for afterZoom trigger when zooming */
zoomFilteringTO: 150,
/* Filtering TimeOut value in ms
* Used for when resizing window */
resizeFilteringTO: 150,
/*
* Initialize the plugin
* Called by the constructor
*/
init: function () {
var self = this;
// Init check for class existence
if (self.options.map.cssClass === "" || $("." + self.options.map.cssClass, self.container).length === 0) {
throw new Error("The map class `" + self.options.map.cssClass + "` doesn't exists");
}
// Create the tooltip container
self.$tooltip = $("<div>").addClass(self.options.map.tooltip.cssClass).css("display", "none");
// Get the map container, empty it then append tooltip
self.$map.empty().append(self.$tooltip);
// Get the map from $.mapael or $.fn.mapael (backward compatibility)
if ($[pluginName] && $[pluginName].maps && $[pluginName].maps[self.options.map.name]) {
// Mapael version >= 2.x
self.mapConf = $[pluginName].maps[self.options.map.name];
} else if ($.fn[pluginName] && $.fn[pluginName].maps && $.fn[pluginName].maps[self.options.map.name]) {
// Mapael version <= 1.x - DEPRECATED
self.mapConf = $.fn[pluginName].maps[self.options.map.name];
if (window.console && window.console.warn) {
window.console.warn("Extending $.fn.mapael is deprecated (map '" + self.options.map.name + "')");
}
} else {
throw new Error("Unknown map '" + self.options.map.name + "'");
}
// Create Raphael paper
self.paper = new Raphael(self.$map[0], self.mapConf.width, self.mapConf.height);
// issue #135: Check for Raphael bug on text element boundaries
if (self.isRaphaelBBoxBugPresent() === true) {
self.destroy();
throw new Error("Can't get boundary box for text (is your container hidden? See #135)");
}
// add plugin class name on element
self.$container.addClass(pluginName);
if (self.options.map.tooltip.css) self.$tooltip.css(self.options.map.tooltip.css);
self.setViewBox(0, 0, self.mapConf.width, self.mapConf.height);
// Handle map size
if (self.options.map.width) {
// NOT responsive: map has a fixed width
self.paper.setSize(self.options.map.width, self.mapConf.height * (self.options.map.width / self.mapConf.width));
// Create the legends for plots taking into account the scale of the map
self.createLegends("plot", self.plots, (self.options.map.width / self.mapConf.width));
} else {
// Responsive: handle resizing of the map
self.initResponsiveSize();
}
// Draw map areas
$.each(self.mapConf.elems, function (id) {
// Init area object
self.areas[id] = {};
// Set area options
self.areas[id].options = self.getElemOptions(
self.options.map.defaultArea,
(self.options.areas[id] ? self.options.areas[id] : {}),
self.options.legend.area
);
// draw area
self.areas[id].mapElem = self.paper.path(self.mapConf.elems[id]);
});
// Hook that allows to add custom processing on the map
if (self.options.map.beforeInit) self.options.map.beforeInit(self.$container, self.paper, self.options);
// Init map areas in a second loop
// Allows text to be added after ALL areas and prevent them from being hidden
$.each(self.mapConf.elems, function (id) {
self.initElem(id, 'area', self.areas[id]);
});
// Draw links
self.links = self.drawLinksCollection(self.options.links);
// Draw plots
$.each(self.options.plots, function (id) {
self.plots[id] = self.drawPlot(id);
});
// Attach zoom event
self.$container.on("zoom." + pluginName, function (e, zoomOptions) {
self.onZoomEvent(e, zoomOptions);
});
if (self.options.map.zoom.enabled) {
// Enable zoom
self.initZoom(self.mapConf.width, self.mapConf.height, self.options.map.zoom);
}
// Set initial zoom
if (self.options.map.zoom.init !== undefined) {
if (self.options.map.zoom.init.animDuration === undefined) {
self.options.map.zoom.init.animDuration = 0;
}
self.$container.trigger("zoom", self.options.map.zoom.init);
}
// Create the legends for areas
self.createLegends("area", self.areas, 1);
// Attach update event
self.$container.on("update." + pluginName, function (e, opt) {
self.onUpdateEvent(e, opt);
});
// Attach showElementsInRange event
self.$container.on("showElementsInRange." + pluginName, function (e, opt) {
self.onShowElementsInRange(e, opt);
});
// Attach delegated events
self.initDelegatedMapEvents();
// Attach delegated custom events
self.initDelegatedCustomEvents();
// Hook that allows to add custom processing on the map
if (self.options.map.afterInit) self.options.map.afterInit(self.$container, self.paper, self.areas, self.plots, self.options);
$(self.paper.desc).append(" and Mapael " + self.version + " (https://www.vincentbroute.fr/mapael/)");
},
/*
* Destroy mapael
* This function effectively detach mapael from the container
* - Set the container back to the way it was before mapael instanciation
* - Remove all data associated to it (memory can then be free'ed by browser)
*
* This method can be call directly by user:
* $(".mapcontainer").data("mapael").destroy();
*
* This method is also automatically called if the user try to call mapael
* on a container already containing a mapael instance
*/
destroy: function () {
var self = this;
// Detach all event listeners attached to the container
self.$container.off("." + pluginName);
self.$map.off("." + pluginName);
// Detach the global resize event handler
if (self.onResizeEvent) $(window).off("resize." + pluginName, self.onResizeEvent);
// Empty the container (this will also detach all event listeners)
self.$map.empty();
// Replace initial HTML content
self.$map.html(self.initialMapHTMLContent);
// Empty legend containers and replace initial HTML content
$.each(self.legends, function(legendType) {
$.each(self.legends[legendType], function(legendIndex) {
var legend = self.legends[legendType][legendIndex];
legend.container.empty();
legend.container.html(legend.initialHTMLContent);
});
});
// Remove mapael class
self.$container.removeClass(pluginName);
// Remove the data
self.$container.removeData(pluginName);
// Remove all internal reference
self.container = undefined;
self.$container = undefined;
self.options = undefined;
self.paper = undefined;
self.$map = undefined;
self.$tooltip = undefined;
self.mapConf = undefined;
self.areas = undefined;
self.plots = undefined;
self.links = undefined;
self.customEventHandlers = undefined;
},
initResponsiveSize: function () {
var self = this;
var resizeTO = null;
// Function that actually handle the resizing
var handleResize = function(isInit) {
var containerWidth = self.$map.width();
if (self.paper.width !== containerWidth) {
var newScale = containerWidth / self.mapConf.width;
// Set new size
self.paper.setSize(containerWidth, self.mapConf.height * newScale);
// Create plots legend again to take into account the new scale
if (isInit || self.options.legend.redrawOnResize) {
self.createLegends("plot", self.plots, newScale);
}
}
};
self.onResizeEvent = function() {
// Clear any previous setTimeout (avoid too much triggering)
clearTimeout(resizeTO);
// setTimeout to wait for the user to finish its resizing
resizeTO = setTimeout(function () {
handleResize();
}, self.resizeFilteringTO);
};
// Attach resize handler
$(window).on("resize." + pluginName, self.onResizeEvent);
// Call once
handleResize(true);
},
/*
* Extend the user option with the default one
* @param options the user options
* @return new options object
*/
extendDefaultOptions: function (options) {
// Extend default options with user options
options = $.extend(true, {}, Mapael.prototype.defaultOptions, options);
// Extend legend default options
$.each(['area', 'plot'], function (key, type) {
if ($.isArray(options.legend[type])) {
for (var i = 0; i < options.legend[type].length; ++i)
options.legend[type][i] = $.extend(true, {}, Mapael.prototype.legendDefaultOptions[type], options.legend[type][i]);
} else {
options.legend[type] = $.extend(true, {}, Mapael.prototype.legendDefaultOptions[type], options.legend[type]);
}
});
return options;
},
/*
* Init all delegated events for the whole map:
* mouseover
* mousemove
* mouseout
*/
initDelegatedMapEvents: function() {
var self = this;
// Mapping between data-type value and the corresponding elements array
// Note: legend-elem and legend-label are not in this table because
// they need a special processing
var dataTypeToElementMapping = {
'area' : self.areas,
'area-text' : self.areas,
'plot' : self.plots,
'plot-text' : self.plots,
'link' : self.links,
'link-text' : self.links
};
/* Attach mouseover event delegation
* Note: we filter the event with a timeout to reduce the firing when the mouse moves quickly
*/
var mapMouseOverTimeoutID;
self.$container.on("mouseover." + pluginName, "[data-id]", function () {
var elem = this;
clearTimeout(mapMouseOverTimeoutID);
mapMouseOverTimeoutID = setTimeout(function() {
var $elem = $(elem);
var id = $elem.attr('data-id');
var type = $elem.attr('data-type');
if (dataTypeToElementMapping[type] !== undefined) {
self.elemEnter(dataTypeToElementMapping[type][id]);
} else if (type === 'legend-elem' || type === 'legend-label') {
var legendIndex = $elem.attr('data-legend-id');
var legendType = $elem.attr('data-legend-type');
self.elemEnter(self.legends[legendType][legendIndex].elems[id]);
}
}, self.MouseOverFilteringTO);
});
/* Attach mousemove event delegation
* Note: timeout filtering is small to update the Tooltip position fast
*/
var mapMouseMoveTimeoutID;
self.$container.on("mousemove." + pluginName, "[data-id]", function (event) {
var elem = this;
clearTimeout(mapMouseMoveTimeoutID);
mapMouseMoveTimeoutID = setTimeout(function() {
var $elem = $(elem);
var id = $elem.attr('data-id');
var type = $elem.attr('data-type');
if (dataTypeToElementMapping[type] !== undefined) {
self.elemHover(dataTypeToElementMapping[type][id], event);
} else if (type === 'legend-elem' || type === 'legend-label') {
/* Nothing to do */
}
}, 0);
});
/* Attach mouseout event delegation
* Note: we don't perform any timeout filtering to clear & reset elem ASAP
* Otherwise an element may be stuck in 'hover' state (which is NOT good)
*/
self.$container.on("mouseout." + pluginName, "[data-id]", function () {
var elem = this;
// Clear any
clearTimeout(mapMouseOverTimeoutID);
clearTimeout(mapMouseMoveTimeoutID);
var $elem = $(elem);
var id = $elem.attr('data-id');
var type = $elem.attr('data-type');
if (dataTypeToElementMapping[type] !== undefined) {
self.elemOut(dataTypeToElementMapping[type][id]);
} else if (type === 'legend-elem' || type === 'legend-label') {
var legendIndex = $elem.attr('data-legend-id');
var legendType = $elem.attr('data-legend-type');
self.elemOut(self.legends[legendType][legendIndex].elems[id]);
}
});
/* Attach click event delegation
* Note: we filter the event with a timeout to avoid double click
*/
self.$container.on("click." + pluginName, "[data-id]", function (evt, opts) {
var $elem = $(this);
var id = $elem.attr('data-id');
var type = $elem.attr('data-type');
if (dataTypeToElementMapping[type] !== undefined) {
self.elemClick(dataTypeToElementMapping[type][id]);
} else if (type === 'legend-elem' || type === 'legend-label') {
var legendIndex = $elem.attr('data-legend-id');
var legendType = $elem.attr('data-legend-type');
self.handleClickOnLegendElem(self.legends[legendType][legendIndex].elems[id], id, legendIndex, legendType, opts);
}
});
},
/*
* Init all delegated custom events
*/
initDelegatedCustomEvents: function() {
var self = this;
$.each(self.customEventHandlers, function(eventName) {
// Namespace the custom event
// This allow to easily unbound only custom events and not regular ones
var fullEventName = eventName + '.' + pluginName + ".custom";
self.$container.off(fullEventName).on(fullEventName, "[data-id]", function (e) {
var $elem = $(this);
var id = $elem.attr('data-id');
var type = $elem.attr('data-type').replace('-text', '');
if (!self.panning &&
self.customEventHandlers[eventName][type] !== undefined &&
self.customEventHandlers[eventName][type][id] !== undefined)
{
// Get back related elem
var elem = self.customEventHandlers[eventName][type][id];
// Run callback provided by user
elem.options.eventHandlers[eventName](e, id, elem.mapElem, elem.textElem, elem.options);
}
});
});
},
/*
* Init the element "elem" on the map (drawing text, setting attributes, events, tooltip, ...)
*
* @param id the id of the element
* @param type the type of the element (area, plot, link)
* @param elem object the element object (with mapElem), it will be updated
*/
initElem: function (id, type, elem) {
var self = this;
var $mapElem = $(elem.mapElem.node);
// Store element id
elem.id = id;
// If an HTML link exists for this element, add cursor attributes
if (elem.options.href) {
elem.options.attrs.cursor = "pointer";
if (elem.options.text) elem.options.text.attrs.cursor = "pointer";
}
// Set SVG attributes to map element
elem.mapElem.attr(elem.options.attrs);
// Set DOM attributes to map element
$mapElem.attr({
"data-id": id,
"data-type": type
});
if (elem.options.cssClass !== undefined) {
$mapElem.addClass(elem.options.cssClass);
}
// Init the label related to the element
if (elem.options.text && elem.options.text.content !== undefined) {
// Set a text label in the area
var textPosition = self.getTextPosition(elem.mapElem.getBBox(), elem.options.text.position, elem.options.text.margin);
elem.options.text.attrs.text = elem.options.text.content;
elem.options.text.attrs.x = textPosition.x;
elem.options.text.attrs.y = textPosition.y;
elem.options.text.attrs['text-anchor'] = textPosition.textAnchor;
// Draw text
elem.textElem = self.paper.text(textPosition.x, textPosition.y, elem.options.text.content);
// Apply SVG attributes to text element
elem.textElem.attr(elem.options.text.attrs);
// Apply DOM attributes
$(elem.textElem.node).attr({
"data-id": id,
"data-type": type + '-text'
});
}
// Set user event handlers
if (elem.options.eventHandlers) self.setEventHandlers(id, type, elem);
// Set hover option for mapElem
self.setHoverOptions(elem.mapElem, elem.options.attrs, elem.options.attrsHover);
// Set hover option for textElem
if (elem.textElem) self.setHoverOptions(elem.textElem, elem.options.text.attrs, elem.options.text.attrsHover);
},
/*
* Init zoom and panning for the map
* @param mapWidth
* @param mapHeight
* @param zoomOptions
*/
initZoom: function (mapWidth, mapHeight, zoomOptions) {
var self = this;
var mousedown = false;
var previousX = 0;
var previousY = 0;
var fnZoomButtons = {
"reset": function () {
self.$container.trigger("zoom", {"level": 0});
},
"in": function () {
self.$container.trigger("zoom", {"level": "+1"});
},
"out": function () {
self.$container.trigger("zoom", {"level": -1});
}
};
// init Zoom data
$.extend(self.zoomData, {
zoomLevel: 0,
panX: 0,
panY: 0,
scaling: false
});
// init zoom buttons
$.each(zoomOptions.buttons, function(type, opt) {
if (fnZoomButtons[type] === undefined) throw new Error("Unknown zoom button '" + type + "'");
// Create div with classes, contents and title (for tooltip)
var $button = $("<div>").addClass(opt.cssClass)
.html(opt.content)
.attr("title", opt.title);
// Assign click event
$button.on("click." + pluginName, fnZoomButtons[type]);
// Append to map
self.$map.append($button);
});
// Update the zoom level of the map on mousewheel
if (self.options.map.zoom.mousewheel) {
self.$map.on("mousewheel." + pluginName, function (e) {
var zoomLevel = (e.deltaY > 0) ? 1 : -1;
var coord = self.mapPagePositionToXY(e.pageX, e.pageY);
self.$container.trigger("zoom", {
"fixedCenter": true,
"level": self.zoomData.zoomLevel + zoomLevel,
"x": coord.x,
"y": coord.y
});
e.preventDefault();
});
}
// Update the zoom level of the map on touch pinch
if (self.options.map.zoom.touch) {
self.$map.on("touchstart." + pluginName, function (e) {
if (e.originalEvent.touches.length === 2) {
self.zoomCenterX = (e.originalEvent.touches[0].pageX + e.originalEvent.touches[1].pageX) / 2;
self.zoomCenterY = (e.originalEvent.touches[0].pageY + e.originalEvent.touches[1].pageY) / 2;
self.previousPinchDist = Math.sqrt(Math.pow((e.originalEvent.touches[1].pageX - e.originalEvent.touches[0].pageX), 2) + Math.pow((e.originalEvent.touches[1].pageY - e.originalEvent.touches[0].pageY), 2));
}
});
self.$map.on("touchmove." + pluginName, function (e) {
var pinchDist = 0;
var zoomLevel = 0;
if (e.originalEvent.touches.length === 2) {
pinchDist = Math.sqrt(Math.pow((e.originalEvent.touches[1].pageX - e.originalEvent.touches[0].pageX), 2) + Math.pow((e.originalEvent.touches[1].pageY - e.originalEvent.touches[0].pageY), 2));
if (Math.abs(pinchDist - self.previousPinchDist) > 15) {
var coord = self.mapPagePositionToXY(self.zoomCenterX, self.zoomCenterY);
zoomLevel = (pinchDist - self.previousPinchDist) / Math.abs(pinchDist - self.previousPinchDist);
self.$container.trigger("zoom", {
"fixedCenter": true,
"level": self.zoomData.zoomLevel + zoomLevel,
"x": coord.x,
"y": coord.y
});
self.previousPinchDist = pinchDist;
}
return false;
}
});
}
// When the user drag the map, prevent to move the clicked element instead of dragging the map (behaviour seen with Firefox)
self.$map.on("dragstart", function() {
return false;
});
// Panning
var panningMouseUpTO = null;
var panningMouseMoveTO = null;
$("body").on("mouseup." + pluginName + (zoomOptions.touch ? " touchend." + pluginName : ""), function () {
mousedown = false;
clearTimeout(panningMouseUpTO);
clearTimeout(panningMouseMoveTO);
panningMouseUpTO = setTimeout(function () {
self.panning = false;
}, self.panningEndFilteringTO);
});
self.$map.on("mousedown." + pluginName + (zoomOptions.touch ? " touchstart." + pluginName : ""), function (e) {
clearTimeout(panningMouseUpTO);
clearTimeout(panningMouseMoveTO);
if (e.pageX !== undefined) {
mousedown = true;
previousX = e.pageX;
previousY = e.pageY;
} else {
if (e.originalEvent.touches.length === 1) {
mousedown = true;
previousX = e.originalEvent.touches[0].pageX;
previousY = e.originalEvent.touches[0].pageY;
}
}
}).on("mousemove." + pluginName + (zoomOptions.touch ? " touchmove." + pluginName : ""), function (e) {
var currentLevel = self.zoomData.zoomLevel;
var pageX = 0;
var pageY = 0;
clearTimeout(panningMouseUpTO);
clearTimeout(panningMouseMoveTO);
if (e.pageX !== undefined) {
pageX = e.pageX;
pageY = e.pageY;
} else {
if (e.originalEvent.touches.length === 1) {
pageX = e.originalEvent.touches[0].pageX;
pageY = e.originalEvent.touches[0].pageY;
} else {
mousedown = false;
}
}
if (mousedown && currentLevel !== 0) {
var offsetX = (previousX - pageX) / (1 + (currentLevel * zoomOptions.step)) * (mapWidth / self.paper.width);
var offsetY = (previousY - pageY) / (1 + (currentLevel * zoomOptions.step)) * (mapHeight / self.paper.height);
var panX = Math.min(Math.max(0, self.currentViewBox.x + offsetX), (mapWidth - self.currentViewBox.w));
var panY = Math.min(Math.max(0, self.currentViewBox.y + offsetY), (mapHeight - self.currentViewBox.h));
if (Math.abs(offsetX) > 5 || Math.abs(offsetY) > 5) {
$.extend(self.zoomData, {
panX: panX,
panY: panY,
zoomX: panX + self.currentViewBox.w / 2,
zoomY: panY + self.currentViewBox.h / 2
});
self.setViewBox(panX, panY, self.currentViewBox.w, self.currentViewBox.h);
panningMouseMoveTO = setTimeout(function () {
self.$map.trigger("afterPanning", {
x1: panX,
y1: panY,
x2: (panX + self.currentViewBox.w),
y2: (panY + self.currentViewBox.h)
});
}, self.panningFilteringTO);
previousX = pageX;
previousY = pageY;
self.panning = true;
}
return false;
}
});
},
/*
* Map a mouse position to a map position
* Transformation principle:
* ** start with (pageX, pageY) absolute mouse coordinate
* - Apply translation: take into accounts the map offset in the page
* ** from this point, we have relative mouse coordinate
* - Apply homothetic transformation: take into accounts initial factor of map sizing (fullWidth / actualWidth)
* - Apply homothetic transformation: take into accounts the zoom factor
* ** from this point, we have relative map coordinate
* - Apply translation: take into accounts the current panning of the map
* ** from this point, we have absolute map coordinate
* @param pageX: mouse client coordinate on X
* @param pageY: mouse client coordinate on Y
* @return map coordinate {x, y}
*/
mapPagePositionToXY: function(pageX, pageY) {
var self = this;
var offset = self.$map.offset();
var initFactor = (self.options.map.width) ? (self.mapConf.width / self.options.map.width) : (self.mapConf.width / self.$map.width());
var zoomFactor = 1 / (1 + (self.zoomData.zoomLevel * self.options.map.zoom.step));
return {
x: (zoomFactor * initFactor * (pageX - offset.left)) + self.zoomData.panX,
y: (zoomFactor * initFactor * (pageY - offset.top)) + self.zoomData.panY
};
},
/*
* Zoom on the map
*
* zoomOptions.animDuration zoom duration
*
* zoomOptions.level level of the zoom between minLevel and maxLevel (absolute number, or relative string +1 or -1)
* zoomOptions.fixedCenter set to true in order to preserve the position of x,y in the canvas when zoomed
*
* zoomOptions.x x coordinate of the point to focus on
* zoomOptions.y y coordinate of the point to focus on
* - OR -
* zoomOptions.latitude latitude of the point to focus on
* zoomOptions.longitude longitude of the point to focus on
* - OR -
* zoomOptions.plot plot ID to focus on
* - OR -
* zoomOptions.area area ID to focus on
* zoomOptions.areaMargin margin (in pixels) around the area
*
* If an area ID is specified, the algorithm will override the zoom level to focus on the area
* but it may be limited by the min/max zoom level limits set at initialization.
*
* If no coordinates are specified, the zoom will be focused on the center of the current view box
*
*/
onZoomEvent: function (e, zoomOptions) {
var self = this;
// new Top/Left corner coordinates
var panX;
var panY;
// new Width/Height viewbox size
var panWidth;
var panHeight;
// Zoom level in absolute scale (from 0 to max, by step of 1)
var zoomLevel = self.zoomData.zoomLevel;
// Relative zoom level (from 1 to max, by step of 0.25 (default))
var previousRelativeZoomLevel = 1 + self.zoomData.zoomLevel * self.options.map.zoom.step;
var relativeZoomLevel;
var animDuration = (zoomOptions.animDuration !== undefined) ? zoomOptions.animDuration : self.options.map.zoom.animDuration;
if (zoomOptions.area !== undefined) {
/* An area is given
* We will define x/y coordinate AND a new zoom level to fill the area
*/
if (self.areas[zoomOptions.area] === undefined) throw new Error("Unknown area '" + zoomOptions.area + "'");
var areaMargin = (zoomOptions.areaMargin !== undefined) ? zoomOptions.areaMargin : 10;
var areaBBox = self.areas[zoomOptions.area].mapElem.getBBox();
var areaFullWidth = areaBBox.width + 2 * areaMargin;
var areaFullHeight = areaBBox.height + 2 * areaMargin;
// Compute new x/y focus point (center of area)
zoomOptions.x = areaBBox.cx;
zoomOptions.y = areaBBox.cy;
// Compute a new absolute zoomLevel value (inverse of relative -> absolute)
// Take the min between zoomLevel on width vs. height to be able to see the whole area
zoomLevel = Math.min(Math.floor((self.mapConf.width / areaFullWidth - 1) / self.options.map.zoom.step),
Math.floor((self.mapConf.height / areaFullHeight - 1) / self.options.map.zoom.step));
} else {
// Get user defined zoom level
if (zoomOptions.level !== undefined) {
if (typeof zoomOptions.level === "string") {
// level is a string, either "n", "+n" or "-n"
if ((zoomOptions.level.slice(0, 1) === '+') || (zoomOptions.level.slice(0, 1) === '-')) {
// zoomLevel is relative
zoomLevel = self.zoomData.zoomLevel + parseInt(zoomOptions.level, 10);
} else {
// zoomLevel is absolute
zoomLevel = parseInt(zoomOptions.level, 10);
}
} else {
// level is integer
if (zoomOptions.level < 0) {
// zoomLevel is relative
zoomLevel = self.zoomData.zoomLevel + zoomOptions.level;
} else {
// zoomLevel is absolute
zoomLevel = zoomOptions.level;
}
}
}
if (zoomOptions.plot !== undefined) {
if (self.plots[zoomOptions.plot] === undefined) throw new Error("Unknown plot '" + zoomOptions.plot + "'");
zoomOptions.x = self.plots[zoomOptions.plot].coords.x;
zoomOptions.y = self.plots[zoomOptions.plot].coords.y;
} else {
if (zoomOptions.latitude !== undefined && zoomOptions.longitude !== undefined) {
var coords = self.mapConf.getCoords(zoomOptions.latitude, zoomOptions.longitude);
zoomOptions.x = coords.x;
zoomOptions.y = coords.y;
}
if (zoomOptions.x === undefined) {
zoomOptions.x = self.currentViewBox.x + self.currentViewBox.w / 2;
}
if (zoomOptions.y === undefined) {
zoomOptions.y = self.currentViewBox.y + self.currentViewBox.h / 2;
}
}
}
// Make sure we stay in the zoom level boundaries
zoomLevel = Math.min(Math.max(zoomLevel, self.options.map.zoom.minLevel), self.options.map.zoom.maxLevel);
// Compute relative zoom level
relativeZoomLevel = 1 + zoomLevel * self.options.map.zoom.step;
// Compute panWidth / panHeight
panWidth = self.mapConf.width / relativeZoomLevel;
panHeight = self.mapConf.height / relativeZoomLevel;
if (zoomLevel === 0) {
panX = 0;
panY = 0;
} else {
if (zoomOptions.fixedCenter !== undefined && zoomOptions.fixedCenter === true) {
panX = self.zoomData.panX + ((zoomOptions.x - self.zoomData.panX) * (relativeZoomLevel - previousRelativeZoomLevel)) / relativeZoomLevel;
panY = self.zoomData.panY + ((zoomOptions.y - self.zoomData.panY) * (relativeZoomLevel - previousRelativeZoomLevel)) / relativeZoomLevel;
} else {
panX = zoomOptions.x - panWidth / 2;
panY = zoomOptions.y - panHeight / 2;
}
// Make sure we stay in the map boundaries
panX = Math.min(Math.max(0, panX), self.mapConf.width - panWidth);
panY = Math.min(Math.max(0, panY), self.mapConf.height - panHeight);
}
// Update zoom level of the map
if (relativeZoomLevel === previousRelativeZoomLevel && panX === self.zoomData.panX && panY === self.zoomData.panY) return;
if (animDuration > 0) {
self.animateViewBox(panX, panY, panWidth, panHeight, animDuration, self.options.map.zoom.animEasing);
} else {
self.setViewBox(panX, panY, panWidth, panHeight);
clearTimeout(self.zoomTO);
self.zoomTO = setTimeout(function () {
self.$map.trigger("afterZoom", {
x1: panX,
y1: panY,
x2: panX + panWidth,
y2: panY + panHeight
});
}, self.zoomFilteringTO);
}
$.extend(self.zoomData, {
zoomLevel: zoomLevel,
panX: panX,
panY: panY,
zoomX: panX + panWidth / 2,
zoomY: panY + panHeight / 2,
scaling: false
});
// Check if we should scale plots
if (self.options.map.zoom.scalePlots === true) {
// Scaling: inverse fn of zoom value since we need to reduce size when increasing zoom
self.zoomData.scaling = 1 / relativeZoomLevel;
self.handlePlotsScaling(animDuration);
}
},
/*
* Get the plot scaling transform string
*
* @param id int plot id
* @return string
*/
getPlotScalingTransform: function(id) {
var self = this;
// Assign scale string
var scaleStr = 's' + self.zoomData.scaling;
// If text is defined, we need to udpdate the transform string to add center x,y
if (self.plots[id].textElem) {
// Get BBox of malelem to get the center coordinates (cx, cy)
var mapElemBBox = self.plots[id].mapElem.getBBox();
// Transform strinf format is
// s[Scale on x],[Scale on y],[Scale center on X],[Scale center on Y]
scaleStr += "," + self.zoomData.scaling + "," + mapElemBBox.cx + "," + mapElemBBox.cy;
}
return scaleStr;
},
/*
* Handle scaling of plots
* Apply a scaling transformation on plots depending on zoom level
*
* @param animDuration int animation duration in ms
*/
handlePlotsScaling: function(animDuration) {
var self = this;
$.each(self.plots, function(id) {
// Assign scale string
var scaleStr = self.getPlotScalingTransform(id);
// Save once transform if defined
if (self.plots[id].mapElem.transformWithoutZoomScaling === undefined) {
if (self.plots[id].options.attrs.transform) {
self.plots[id].mapElem.transformWithoutZoomScaling = self.plots[id].options.attrs.transform;
} else {