Debian Codegraph @

js/jquery.geo.geomap.js

raw symbols SCIP
1 (function ($, window, undefined) {
2 var _widgetIdSeed = 0,
3 _ieVersion = ( function() {
4 var v = 5, div = document.createElement("div"), a = div.all || [];
5 do {
6 div.innerHTML = "<!--[if gt IE " + (++v) + "]><br/><![endif]-->";
7 } while ( a[0] );
8 return v > 6 ? v : !v;
9 }() ),
10
11 _gecko = ( function() {
12 return ( navigator.userAgent.match( /gecko\/*\d/i ) || [] ).length > 0;
13 }() ),
14
15 _defaultOptions = {
16 bbox: [-180, -85, 180, 85],
17 bboxMax: [-180, -85, 180, 85],
18 center: [0, 0],
19 cursors: {
20 "static": "default",
21 pan: "url(data:image/vnd.microsoft.icon;base64,AAACAAEAICACAAgACAAwAQAAFgAAACgAAAAgAAAAQAAAAAEAAQAAAAAAAAEAAAAAAAAAAAAAAgAAAAAAAAAAAAAA////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD8AAAA/AAAAfwAAAP+AAAH/gAAB/8AAA//AAAd/wAAGf+AAAH9gAADbYAAA2yAAAZsAAAGbAAAAGAAAAAAAAA//////////////////////////////////////////////////////////////////////////////////////gH///4B///8Af//+AD///AA///wAH//4AB//8AAf//AAD//5AA///gAP//4AD//8AF///AB///5A////5///8=), move",
22 click: "crosshair",
23 zoom: "crosshair",
24 dragBox: "crosshair",
25 dragCircle: "crosshair",
26 drawPoint: "crosshair",
27 drawLineString: "crosshair",
28 drawPolygon: "crosshair",
29 measureLength: "crosshair",
30 measureArea: "crosshair"
31 },
32 measureLabels: {
33 length: "{{:length.toFixed( 2 )}} m",
34 area: "{{:area.toFixed( 2 )}} sq m"
35 },
36 drawStyle: {},
37 shapeStyle: {},
38 mode: "pan",
39 pannable: true,
40 scroll: "default",
41 shift: "default",
42 services: [
43 {
44 "class": "osm",
45 type: "tiled",
46 src: function (view) {
47 return "//" + String.fromCharCode(97 + (view.index % 3)) + ".tile.openstreetmap.org/" + view.zoom + "/" + view.tile.column + "/" + view.tile.row + ".png";
48 },
49 attr: '&copy; <a href="http://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
50 }
51 ],
52 tilingScheme: {
53 tileWidth: 256,
54 tileHeight: 256,
55 levels: 18,
56 basePixelSize: 156543.03392799936,
57 origin: [-20037508.342787, 20037508.342787]
58 },
59 axisLayout: "map",
60 zoom: 0,
61 zoomMin: 0,
62 zoomMax: Number.POSITIVE_INFINITY,
63 zoomFactor: 2, //< determines what a zoom level means
64 pixelSize: 0
65 };
66
67 $.widget("geo.geomap", {
68 // private widget members
69 _$elem: undefined, //< map div for maps, service div for services
70 _map: undefined, //< only defined in services
71 _created: false,
72 _createdGraphics: false,
73 _widgetId: 0,
74 _tmplLengthId: "",
75 _tmplAreaId: "",
76
77 _contentBounds: {},
78
79 _$resizeContainer: undefined, //< all elements that should match _contentBounds' size
80
81 _$eventTarget: undefined,
82 _$contentFrame: undefined,
83 _$existingChildren: undefined,
84 _$attrList: undefined,
85 _$servicesContainer: undefined,
86 _$shapesContainers: undefined, //< all shapesContainer divs (map only)
87
88 _$panContainer: undefined, //< all non-service elements that move while panning
89 _$shapesContainer: undefined, //< just "our" shapesContainer div (map & service)
90 _$drawContainer: undefined,
91 _$measureContainer: undefined,
92 _$measureLabel: undefined,
93
94 _dpi: 96,
95
96 _currentServices: [], //< internal copy
97
98 _center: undefined,
99 _pixelSize: undefined,
100 _centerMax: undefined,
101 _pixelSizeMax: undefined,
102 _pixelSizeMin: undefined,
103
104 _userGeodetic: true,
105
106 _centerInteractive: undefined,
107 _pixelSizeInteractive: undefined,
108 _timeoutInteractive: null,
109 _timeoutWheel: null,
110 _triggerInteractive: false,
111
112 _timeoutRefreshShapes: null,
113
114 _loadCount: 0,
115
116 _wheelLevel: 0,
117
118 _fullZoomFactor: 2, //< interactiveScale factor needed to zoom a whole level
119 _partialZoomFactor: 1.18920711500273, //< interactiveScale factor needed to zoom a fraction of a level (the fourth root of 2)
120
121 _mouseDown: undefined,
122 _inOp: undefined,
123 _toolPan: undefined,
124 _shiftDown: undefined,
125 _anchor: undefined,
126 _current: undefined,
127 _downDate: undefined,
128 _moveDate: undefined,
129 _clickDate: undefined,
130 _lastMove: undefined,
131 _lastDrag: undefined,
132
133 _windowHandler: null,
134 _resizeTimeout: null,
135
136 _panning: undefined,
137 _velocity: undefined,
138 _friction: undefined,
139
140 _pointerEvents: undefined,
141 _supportTouch: undefined,
142 _softDblClick: undefined,
143 _isTap: undefined,
144 _isDbltap: undefined,
145
146 _isMultiTouch: undefined,
147 _multiTouchAnchor: [], //< TouchList
148 _multiTouchAnchorBbox: undefined, //< bbox
149 _multiTouchCurrentBbox: undefined, //< bbox
150
151 _drawTimeout: null, //< used in drawPoint mode so we don't send two shape events on dbltap
152 _drawPixels: [], //< an array of coordinate arrays for drawing lines & polygons, in pixel coordinates
153 _drawCoords: [],
154
155 _graphicShapes: [], //< an array of objects containing style object refs & GeoJSON object refs
156
157 _initOptions: {},
158
159 _options: {},
160
161 options: $.extend({}, _defaultOptions),
162
163 _createWidget: function (options, element) {
164 this._$elem = $(element);
165
166 if (this._$elem.is(".geo-service")) {
167 this._graphicShapes = [];
168 $.Widget.prototype._createWidget.apply(this, arguments);
169 return;
170 }
171
172 this._widgetId = _widgetIdSeed++;
173 this._serviceIdSeed = 0;
174 this._tmplLengthId = "geoMeasureLength" + this._widgetId;
175 this._tmplAreaId = "geoMeasureArea" + this._widgetId;
176
177 this._$elem.addClass("geo-map").css( {
178 webkitTransform: "translateZ(0)",
179 touchAction: 'none'
180 } );
181
182 this._initOptions = options || {};
183
184 this._forcePosition(this._$elem);
185
186 this._$elem.css("text-align", "left");
187
188 var size = this._findMapSize();
189 this._contentBounds = {
190 x: parseInt(this._$elem.css("padding-left"), 10),
191 y: parseInt(this._$elem.css("padding-top"), 10),
192 width: size["width"],
193 height: size["height"]
194 };
195
196 this._createChildren();
197
198 this._center = [ 0, 0 ];
199 this._centerMax = [ 0, 0 ];
200 this._centerInteractive = [ 0, 0 ];
201
202 this.options["pixelSize"] = this._pixelSize = this._pixelSizeMax = 156543.03392799936;
203 this._pixelSizeMin = this._pixelSizeMax / Math.pow( 2, 17 );
204
205 this._mouseDown =
206 this._inOp =
207 this._toolPan =
208 this._shiftDown =
209 this._panning =
210 this._isTap =
211 this._isDbltap =
212 this._isMultiTouch = false;
213
214 this._anchor = [ 0, 0 ]; /* mouse down */
215 this._current = [ 0, 0 ]; /* mouse move no matter what */
216 this._lastMove = [ 0, 0 ];
217 this._lastDrag = [ 0, 0 ];
218 this._velocity = [ 0, 0 ];
219
220 this._friction = [0.8, 0.8];
221
222 this._downDate =
223 this._moveDate =
224 this._clickDate = 0;
225
226 this._drawPixels = [];
227 this._drawCoords = [];
228 this._graphicShapes = [];
229
230
231 $.Widget.prototype._createWidget.apply(this, arguments);
232 },
233
234 _create: function () {
235 this._options = this.options;
236
237 if (this._$elem.is(".geo-service")) {
238 this._map = this._$elem.data( "geoMap" );
239 this._$elem.data( "geoService", this );
240 return;
241 }
242
243 this._map = this;
244
245 this._pointerEvents = window.PointerEvent;
246 this._supportTouch = ("ontouchend" in document);
247 this._softDblClick = this._pointerEvents || this._supportTouch || _ieVersion === 7;
248
249 var geomap = this,
250 touchStartEvent = this._pointerEvents ? 'pointerdown' : ( this._supportTouch ? "touchstart mousedown" : "mousedown" ),
251 touchStopEvent = this._pointerEvents ? 'pointerup pointercancel' : ( this._supportTouch ? "touchend touchcancel mouseup" : "mouseup" ),
252 touchMoveEvent = this._pointerEvents ? 'pointermove' : ( this._supportTouch ? "touchmove mousemove" : "mousemove" );
253
254 $(document).keydown($.proxy(this._document_keydown, this));
255
256 if ( this._softDblClick ) {
257 this._$eventTarget.on( 'softdblclick', $.proxy(this._eventTarget_dblclick, this));
258 } else {
259 this._$eventTarget.dblclick($.proxy(this._eventTarget_dblclick, this));
260 }
261
262 this._$eventTarget.bind(touchStartEvent, $.proxy(this._eventTarget_touchstart, this));
263
264 var dragTarget = (this._$eventTarget[0].setCapture || this._pointerEvents) ? this._$eventTarget : $(document);
265 dragTarget.bind(touchMoveEvent, $.proxy(this._dragTarget_touchmove, this));
266 dragTarget.bind(touchStopEvent, $.proxy(this._dragTarget_touchstop, this));
267
268 this._$eventTarget.mousewheel($.proxy(this._eventTarget_mousewheel, this));
269
270 this._windowHandler = function () {
271 if (geomap._resizeTimeout) {
272 clearTimeout(geomap._resizeTimeout);
273 }
274 geomap._resizeTimeout = setTimeout(function () {
275 if (geomap._created) {
276 geomap._$elem.geomap( "resize", true );
277 }
278 }, 500);
279 };
280
281 $(window).resize(this._windowHandler);
282
283 this._$drawContainer.geographics({ style: this._initOptions.drawStyle || {}, doubleBuffer: false });
284 this._options["drawStyle"] = this._$drawContainer.geographics("option", "style");
285
286 this._$shapesContainer.geographics( { style: this._initOptions.shapeStyle || { }, doubleBuffer: !_gecko } );
287 this._createdGraphics = true;
288
289 this._options["shapeStyle"] = this._$shapesContainer.geographics("option", "style");
290
291 if (this._initOptions) {
292 // always init tilingScheme right away, even if it's null
293 if ( this._initOptions.tilingScheme !== undefined ) {
294 this._setOption("tilingScheme", this._initOptions.tilingScheme || null, false);
295 }
296
297 if ( this._initOptions.services ) {
298 // jQuery UI Widget Factory merges user services with our default, we want to clobber the default
299 this._options[ "services" ] = $.merge( [ ], this._initOptions.services );
300 }
301 if (this._initOptions.bboxMax) {
302 this._setOption("bboxMax", this._initOptions.bboxMax, false);
303 this._setOption("bbox", this._initOptions.bboxMax, false);
304 }
305 if (this._initOptions.zoomMin !== undefined) {
306 this._setOption("zoomMin", this._initOptions.zoomMin, false);
307 }
308 if (this._initOptions.zoomMax !== undefined) {
309 this._setOption("zoomMax", this._initOptions.zoomMax, false);
310 }
311 if (this._initOptions.zoomFactor !== undefined) {
312 this._setOption("zoomFactor", this._initOptions.zoomFactor, false);
313 this._fullZoomFactor = this._initOptions.zoomFactor;
314 this._partialZoomFactor = Math.pow(4, 1 / this._fullZoomFactor); // 4th root of full
315 }
316 if (this._initOptions.bbox) {
317 this._setOption("bbox", this._initOptions.bbox, false);
318 }
319 if (this._initOptions.center) {
320 this._setOption("center", this._initOptions.center, false);
321 }
322 if (this._initOptions.zoom !== undefined) {
323 this._setOption("zoom", this._initOptions.zoom, false);
324 }
325 }
326
327 $.templates( this._tmplLengthId, this._options[ "measureLabels" ].length );
328 $.templates( this._tmplAreaId, this._options[ "measureLabels" ].area );
329
330 this._$eventTarget.css("cursor", this._options["cursors"][this._options["mode"]]);
331
332 this._createServices();
333 this._refresh();
334
335 this._created = true;
336 },
337
338 _setOption: function (key, value, refresh) {
339 if ( key === "pixelSize" ) {
340 return;
341 }
342
343 refresh = (refresh === undefined || refresh);
344
345 if ( this._$elem.is( ".geo-map" ) ) {
346 this._panFinalize();
347 }
348
349 var center, pixelSize, bbox, zoom;
350
351 switch (key) {
352 case "bbox":
353 if ( this._created ) {
354 this._clearInteractiveTimeout( );
355 }
356
357 this._userGeodetic = this._options["axisLayout"] === "map" && $.geo.proj && $.geo._isGeodetic( value );
358 if ( this._userGeodetic ) {
359 value = $.geo.proj.fromGeodetic( value );
360 }
361
362 center = [value[0] + (value[2] - value[0]) / 2, value[1] + (value[3] - value[1]) / 2];
363 pixelSize = Math.max($.geo.width(value, true) / this._contentBounds.width, $.geo.height(value, true) / this._contentBounds.height);
364
365 // clamp to zoom
366 zoom = this._getZoom( center, pixelSize );
367
368 if ( this._options[ "tilingScheme" ] ) {
369 pixelSize = this._getPixelSize( Math.min( Math.max( zoom, this._options[ "zoomMin" ] ), this._options[ "zoomMax" ] ) );
370 } else {
371 if ( zoom < this._options[ "zoomMin" ] ) {
372 pixelSize = this._getPixelSize( this._options[ "zoomMin" ] );
373 } else if ( zoom > this._options[ "zoomMax" ] ) {
374 pixelSize = this._getPixelSize( this._options[ "zoomMax" ] );
375 }
376 }
377
378 if ( this._created ) {
379 this._setInteractiveCenterAndSize( center, pixelSize );
380 this._setInteractiveTimeout( false );
381 } else {
382 this._setCenterAndSize( center, pixelSize, false, refresh );
383 }
384
385 value = this._getBbox( center, pixelSize );
386 break;
387
388 case "bboxMax":
389 this._userGeodetic = this._options["axisLayout"] === "map" && $.geo.proj && $.geo._isGeodetic( value );
390 break;
391
392 case "center":
393 if ( this._created ) {
394 this._clearInteractiveTimeout( );
395 }
396
397 this._userGeodetic = this._options["axisLayout"] === "map" && $.geo.proj && $.geo._isGeodetic( value );
398 if ( this._userGeodetic ) {
399 value = $.geo.proj.fromGeodetic( value );
400 }
401
402 if ( this._created ) {
403 this._setInteractiveCenterAndSize( value, this._pixelSizeInteractive );
404 this._interactiveTransform( );
405 this._setInteractiveTimeout( false );
406 } else {
407 this._setCenterAndSize( value, this._pixelSize, false, refresh );
408 }
409 break;
410
411 case "measureLabels":
412 value = $.extend( this._options[ "measureLabels" ], value );
413
414
415 $.templates( this._tmplLengthId, this._options[ "measureLabels" ].length );
416 $.templates( this._tmplAreaId, this._options[ "measureLabels" ].area );
417
418 break;
419
420 case "drawStyle":
421 if (this._$drawContainer) {
422 this._$drawContainer.geographics("option", "style", value);
423 value = this._$drawContainer.geographics("option", "style");
424 }
425 break;
426
427 case "shapeStyle":
428 if ( this._$elem.is( ".geo-service" ) && !this._createdGraphics ) {
429 this._createServiceGraphics( );
430 }
431
432 if ( this._createdGraphics ) {
433 this._$shapesContainer.geographics("option", "style", value);
434 value = this._$shapesContainer.geographics("option", "style");
435 }
436 break;
437
438 case "mode":
439 this._resetDrawing( );
440 this._$eventTarget.css("cursor", this._options["cursors"][value]);
441 break;
442
443 case "zoom":
444 this._userGeodetic = this._options["axisLayout"] === "map" && $.geo.proj;
445 if ( this._created ) {
446 this._setZoom(value, false, refresh);
447 } else {
448 value = Math.max( value, 0 );
449 this._setCenterAndSize( this._center, this._getPixelSize( value ), false, refresh );
450 }
451 break;
452 }
453
454 $.Widget.prototype._setOption.apply(this, arguments);
455
456 switch ( key ) {
457 case "bbox":
458 case "center":
459 if ( this._userGeodetic ) {
460 this._options[ "bbox" ] = $.geo.proj.toGeodetic( this._options[ "bbox" ] );
461 this._options[ "center" ] = $.geo.proj.toGeodetic( this._center );
462 }
463 break;
464
465 case "tilingScheme":
466 if ( value !== null ) {
467 this._pixelSizeMax = this._getPixelSize( 0 );
468 this._pixelSizeMin = this._getPixelSize( value.pixelSizes ? value.pixelSizes.length - 1 : value.levels - 1 );
469 this._centerMax = [
470 value.origin[ 0 ] + this._pixelSizeMax * value.tileWidth / 2,
471 value.origin[ 1 ] + this._pixelSizeMax * value.tileHeight / 2
472 ];
473 }
474 break;
475
476 case "bboxMax":
477 if ( this._userGeodetic ) {
478 bbox = $.geo.proj.fromGeodetic( value );
479 } else {
480 bbox = value;
481 }
482
483 this._centerMax = $.geo.center( bbox );
484 this._pixelSizeMax = Math.max( $.geo.width( bbox, true ) / this._contentBounds.width, $.geo.height( bbox, true ) / this._contentBounds.height );
485 break;
486
487 case "services":
488 this._createServices();
489 if (refresh) {
490 this._refresh();
491 this._refreshAllShapes();
492 }
493 break;
494
495 case "shapeStyle":
496 if ( refresh && this._createdGraphics ) {
497 this._$shapesContainer.geographics("clear");
498 this._refreshShapes( this._$shapesContainer, this._graphicShapes, this._graphicShapes, this._graphicShapes );
499 }
500 break;
501 }
502 },
503
504 destroy: function () {
505 if ( this._$elem.is(".geo-service") ) {
506 if ( this._createdGraphics ) {
507 this._$shapesContainer.geographics("destroy");
508 this._$shapesContainer = undefined;
509 this._createdGraphics = false;
510 }
511 } else {
512 clearTimeout( this._timeoutInteractive );
513 this._timeoutInteractive = null;
514
515 this._created = false;
516
517 $(window).unbind("resize", this._windowHandler);
518
519 for ( var i = 0; i < this._currentServices.length; i++ ) {
520 this._currentServices[ i ].serviceContainer.geomap("destroy");
521 $.geo["_serviceTypes"][this._currentServices[i].type].destroy(this, this._$servicesContainer, this._currentServices[i]);
522 }
523
524 this._$shapesContainer.geographics("destroy");
525 this._$shapesContainer = undefined;
526 this._createdGraphics = false;
527
528 this._$drawContainer.geographics("destroy");
529 this._$drawContainer = undefined;
530
531 this._$existingChildren.detach();
532 this._$elem.html("");
533 this._$elem.append(this._$existingChildren);
534 this._$elem.removeClass("geo-map");
535 }
536
537 $.Widget.prototype.destroy.apply(this, arguments);
538 },
539
540 toMap: function (p) {
541 p = this._toMap(p);
542 return this._userGeodetic ? $.geo.proj.toGeodetic(p) : p;
543 },
544
545 toPixel: function ( p, _center /* Internal Use Only */, _pixelSize /* Internal Use Only */ ) {
546 return this._toPixel( $.geo.proj ? $.geo.proj.fromGeodetic( p ) : p, _center, _pixelSize );
547 },
548
549 opacity: function ( value, _serviceContainer ) {
550 if ( this._$elem.is( ".geo-service" ) ) {
551 this._$elem.closest( ".geo-map" ).geomap( "opacity", value, this._$elem );
552 } else {
553 if ( value >= 0 || value <= 1 ) {
554 for ( var i = 0; i < this._currentServices.length; i++ ) {
555 var service = this._currentServices[ i ];
556 if ( !_serviceContainer || service.serviceContainer[ 0 ] === _serviceContainer[ 0 ] ) {
557 service.style.opacity = value;
558
559 // update the original service object's style property
560 service.serviceObject.style = $.extend( { }, service.serviceObject.style, service.style );
561
562 $.geo[ "_serviceTypes" ][ service.type ].opacity( this, service );
563 }
564 }
565 }
566 }
567 },
568
569 toggle: function ( value, _serviceContainer ) {
570 if ( this._$elem.is( ".geo-service" ) ) {
571 this._$elem.closest( ".geo-map" ).geomap( "toggle", value, this._$elem );
572 } else {
573
574 for ( var i = 0; i < this._currentServices.length; i++ ) {
575 var service = this._currentServices[ i ];
576
577 if ( !_serviceContainer || service.serviceContainer[ 0 ] === _serviceContainer[ 0 ] ) {
578 if ( value === undefined ) {
579 // toggle visibility
580 value = ( service.style.visibility !== "visible" );
581 }
582
583 service.style.visibility = ( value ? "visible" : "hidden" );
584
585 // update the original service object's style property
586 service.serviceObject.style = $.extend( { }, service.serviceObject.style, service.style );
587
588 service.serviceContainer.toggle( value );
589
590 if ( value ) {
591 $.geo[ "_serviceTypes" ][ service.type ].refresh( this, service );
592 }
593 }
594 }
595 }
596 },
597
598 zoom: function (numberOfLevels) {
599 if (numberOfLevels !== null) {
600 this._setZoom(this._options["zoom"] + numberOfLevels, false, true);
601 }
602 },
603
604 refresh: function ( force, _serviceContainer ) {
605 if ( !this._panning ) {
606 if ( this._$elem.is( ".geo-service" ) ) {
607 this._$elem.closest( ".geo-map" ).geomap( "refresh", force, this._$elem );
608 } else {
609 this._refresh( force, _serviceContainer );
610 this._refreshAllShapes( _serviceContainer );
611 }
612 }
613 },
614
615 resize: function ( _trigger /* Internal Use Only */ ) {
616 var size = this._findMapSize(),
617 dx = size["width"]/2 - this._contentBounds.width/2,
618 dy = size["height"]/2 - this._contentBounds.height/2,
619 i;
620
621 this._contentBounds = {
622 x: parseInt(this._$elem.css("padding-left"), 10),
623 y: parseInt(this._$elem.css("padding-top"), 10),
624 width: size["width"],
625 height: size["height"]
626 };
627
628 this._$resizeContainer.css( {
629 width: size["width"],
630 height: size["height"]
631 } );
632
633 for (i = 0; i < this._currentServices.length; i++) {
634 $.geo["_serviceTypes"][this._currentServices[i].type].resize(this, this._currentServices[i], dx, dy);
635 }
636
637 this._$elem.find( ".geo-graphics" ).css( {
638 width: size["width"],
639 height: size["height"]
640 }).geographics( "resize", dx, dy );
641
642 for (i = 0; i < this._drawPixels.length; i++) {
643 this._drawPixels[i][0] += dx;
644 this._drawPixels[i][1] += dy;
645 }
646
647 this._setCenterAndSize(this._center, this._pixelSize, _trigger, true);
648 },
649
650 append: function ( shape /* , style, label, refresh */ ) {
651 if ( shape && ( $.isPlainObject( shape ) || ( $.isArray( shape ) && shape.length > 0 ) ) ) {
652 if ( !this._createdGraphics ) {
653 this._createServiceGraphics( );
654 }
655
656 var shapes, arg, i, realStyle, realLabel, realRefresh;
657
658 if ( $.isArray( shape ) ) {
659 shapes = shape;
660 } else if ( shape.type === "FeatureCollection" ) {
661 shapes = shape.features;
662 } else {
663 shapes = [ shape ];
664 }
665
666 for ( i = 1; i < arguments.length; i++ ) {
667 arg = arguments[ i ];
668
669 if ( arg !== null ) {
670 if ( typeof arg === "object" ) {
671 realStyle = arg;
672 } else if ( typeof arg === "number" || typeof arg === "string" ) {
673 realLabel = arg;
674 } else if ( typeof arg === "boolean" ) {
675 realRefresh = arg;
676 }
677 }
678 }
679
680 for ( i = 0; i < shapes.length; i++ ) {
681 if ( !$.data( shapes[ i ], "geoBbox" ) ) {
682 var bbox = $.geo.bbox( shapes[ i ] );
683 if ( $.geo.proj && $.geo._isGeodetic( bbox ) ) {
684 bbox = $.geo.proj.fromGeodetic( bbox );
685 }
686 $.data( shapes[ i ], "geoBbox", bbox );
687 }
688
689 this._graphicShapes.push( {
690 shape: shapes[ i ],
691 style: realStyle,
692 label: realLabel
693 } );
694 }
695
696 if ( realRefresh === undefined || realRefresh ) {
697 if ( this._$elem.is( ".geo-service" ) ) {
698 this._refresh( false, this._$elem );
699 } else {
700 this._refresh( );
701 }
702 this._refreshAllShapes( );
703 }
704 }
705 },
706
707 empty: function ( refresh ) {
708 this._graphicShapes = [];
709
710 if ( refresh === undefined || refresh ) {
711 if ( this._$elem.is( ".geo-service" ) ) {
712 this._refresh( false, this._$elem );
713 } else {
714 this._refresh( );
715 }
716 this._refreshAllShapes( );
717 }
718 },
719
720 find: function ( selector, pixelTolerance ) {
721 if ( this._timeoutInteractive ) {
722 return [];
723 }
724
725 var isPoint = $.isPlainObject( selector ),
726 //searchPixel = isPoint ? this._map.toPixel( selector.coordinates ) : undefined,
727 mapTol = this._map._pixelSize * pixelTolerance,
728 result = [],
729 graphicShape,
730 geometries,
731 curGeom,
732 i;
733
734 if ( this._$elem.is( ".geo-map" ) ) {
735 $( this._$elem.find( ".geo-service" ).get().reverse() ).each( function( ) {
736 result = $.merge( result, $( this ).geomap( "find", selector, pixelTolerance ) );
737 } );
738 }
739
740 for ( i = this._graphicShapes.length - 1; i >= 0; i-- ) {
741 graphicShape = this._graphicShapes[ i ];
742
743 if ( isPoint ) {
744 if ( graphicShape.shape.type === "Point" ) {
745 if ( $.geo.distance( graphicShape.shape, selector ) <= mapTol ) {
746 result.push( graphicShape.shape );
747 }
748 } else {
749 var bbox = $.data( graphicShape.shape, "geoBbox" ),
750 bboxPolygon = {
751 type: "Polygon",
752 coordinates: [ [
753 [bbox[0], bbox[1]],
754 [bbox[0], bbox[3]],
755 [bbox[2], bbox[3]],
756 [bbox[2], bbox[1]],
757 [bbox[0], bbox[1]]
758 ] ]
759 },
760 projectedPoint = {
761 type: "Point",
762 coordinates: $.geo.proj && $.geo._isGeodetic( selector.coordinates ) ? $.geo.proj.fromGeodetic( selector.coordinates ) : selector.coordinates
763 };
764
765 if ( $.geo.distance( bboxPolygon, projectedPoint, true ) <= mapTol ) {
766 geometries = $.geo._basic( graphicShape.shape );
767 for ( curGeom = 0; curGeom < geometries.length; curGeom++ ) {
768 if ( $.geo.distance( geometries[ curGeom ], selector ) <= mapTol ) {
769 result.push( graphicShape.shape );
770 break;
771 }
772 }
773 }
774 }
775 } else {
776 result.push( graphicShape.shape );
777 }
778 }
779
780 return result;
781 },
782
783 remove: function ( shape, refresh ) {
784 if ( shape && ( $.isPlainObject( shape ) || ( $.isArray( shape ) && shape.length > 0 ) ) ) {
785 var shapes = $.isArray( shape ) ? shape : [ shape ],
786 rest;
787
788 for ( var i = 0; i < this._graphicShapes.length; i++ ) {
789 if ( $.inArray( this._graphicShapes[ i ].shape, shapes ) >= 0 ) {
790 rest = this._graphicShapes.slice( i + 1 );
791 this._graphicShapes.length = i;
792 this._graphicShapes.push.apply( this._graphicShapes, rest );
793 i--;
794 }
795 }
796
797 if ( refresh === undefined || refresh ) {
798 if ( this._$elem.is( ".geo-service" ) ) {
799 this._refresh( false, this._$elem );
800 } else {
801 this._refresh( );
802 }
803 this._refreshAllShapes( );
804 }
805 }
806 },
807
808 _getBbox: function (center, pixelSize) {
809 center = center || this._center;
810 pixelSize = pixelSize || this._pixelSize;
811
812 // calculate the internal bbox
813 var halfWidth = this._contentBounds[ "width" ] / 2 * pixelSize,
814 halfHeight = this._contentBounds[ "height" ] / 2 * pixelSize;
815 return [ center[ 0 ] - halfWidth, center[ 1 ] - halfHeight, center[ 0 ] + halfWidth, center[ 1 ] + halfHeight ];
816 },
817
818 _setBbox: function (value /* , trigger, refresh */ ) {
819 var center = [value[0] + (value[2] - value[0]) / 2, value[1] + (value[3] - value[1]) / 2],
820 pixelSize = Math.max($.geo.width(value, true) / this._contentBounds.width, $.geo.height(value, true) / this._contentBounds.height),
821 zoom = this._getZoom( center, pixelSize );
822
823 // clamp to zoom
824 if ( this._options[ "tilingScheme" ] ) {
825 pixelSize = this._getPixelSize( Math.min( Math.max( zoom, this._options[ "zoomMin" ] ), this._options[ "zoomMax" ] ) );
826 } else {
827 if ( zoom < this._options[ "zoomMin" ] ) {
828 pixelSize = this._getPixelSize( this._options[ "zoomMin" ] );
829 } else if ( zoom > this._options[ "zoomMax" ] ) {
830 pixelSize = this._getPixelSize( this._options[ "zoomMax" ] );
831 }
832 }
833
834 this._setInteractiveCenterAndSize( center, pixelSize );
835 this._interactiveTransform( );
836 },
837
838 _getBboxMax: function () {
839 // calculate the internal bboxMax
840 var halfWidth = this._contentBounds["width"] / 2 * this._pixelSizeMax,
841 halfHeight = this._contentBounds["height"] / 2 * this._pixelSizeMax;
842 return [this._centerMax[0] - halfWidth, this._centerMax[1] - halfHeight, this._centerMax[0] + halfWidth, this._centerMax[1] + halfHeight];
843 },
844
845 _getCenter: function () {
846 return this._center;
847 },
848
849 _getContentBounds: function () {
850 return this._contentBounds;
851 },
852
853 _getServicesContainer: function () {
854 return this._$servicesContainer;
855 },
856
857 _getZoom: function ( center, pixelSize ) {
858 // calculate the internal zoom level, vs. public zoom property
859 // this does not take zoomMin or zoomMax into account
860 center = center || this._center;
861 pixelSize = pixelSize || this._pixelSize;
862
863 var tilingScheme = this._options["tilingScheme"];
864 if ( tilingScheme ) {
865 if ( tilingScheme.pixelSizes ) {
866 var roundedPixelSize = Math.floor(pixelSize * 1000),
867 levels = tilingScheme.pixelSizes.length,
868 i = levels - 1;
869
870 for ( ; i >= 0; i-- ) {
871 if ( Math.floor( tilingScheme.pixelSizes[ i ] * 1000 ) >= roundedPixelSize ) {
872 return i;
873 }
874 }
875
876 return 0;
877 } else {
878 return Math.floor( Math.log( tilingScheme.basePixelSize / pixelSize) / Math.log( 2 ) );
879 }
880 } else {
881 var ratio = this._contentBounds["width"] / this._contentBounds["height"],
882 bbox = $.geo.reaspect( this._getBbox( center, pixelSize ), ratio, true ),
883 bboxMax = $.geo.reaspect(this._getBboxMax(), ratio, true);
884
885 return Math.round( Math.log($.geo.width(bboxMax, true) / $.geo.width(bbox, true)) / Math.log(this._fullZoomFactor) );
886 }
887 },
888
889 _setZoom: function ( value, trigger /* , refresh */ ) {
890 // set the map widget's zoom, taking zoomMin and zoomMax into account
891 this._clearInteractiveTimeout( );
892
893 value = Math.min( Math.max( value, this._options[ "zoomMin" ] ), this._options[ "zoomMax" ] );
894
895 this._setInteractiveCenterAndSize( this._centerInteractive, this._getPixelSize( value ) );
896 this._interactiveTransform( );
897 this._setInteractiveTimeout( trigger );
898 },
899
900 _createChildren: function () {
901 this._$existingChildren = this._$elem.children();
902
903 this._forcePosition(this._$existingChildren);
904
905 this._$existingChildren.detach().css( {
906 mozUserSelect: "none"
907 } );
908
909
910 var contentSizeCss = "width:" + this._contentBounds["width"] + "px; height:" + this._contentBounds["height"] + "px; margin:0; padding:0;",
911 contentPosCss = "position:absolute; left:0; top:0;";
912
913 this._$elem.prepend( window.toStaticHTML( '<div class="geo-event-target geo-content-frame" style="position:absolute; left:' + this._contentBounds.x + 'px; top:' + this._contentBounds.y + 'px;' + contentSizeCss + 'overflow:hidden; -khtml-user-select:none; -moz-user-select:none; -webkit-user-select:none; user-select:none;" unselectable="on"></div>' ) );
914 this._$eventTarget = this._$contentFrame = this._$elem.children(':first');
915
916 this._$contentFrame.append('<div class="geo-services-container" style="' + contentPosCss + contentSizeCss + '"></div>');
917 this._$servicesContainer = this._$contentFrame.children(':last');
918
919 this._$contentFrame.append( '<ul style="position: absolute; bottom: 8px; left: 8px; list-style-type: none; max-width: 50%; padding: 0; margin: 0;"></ul>' );
920 this._$attrList = this._$contentFrame.children( ":last" );
921
922 this._$contentFrame.append('<div class="geo-draw-container" style="' + contentPosCss + contentSizeCss + '"></div>');
923 this._$drawContainer = this._$contentFrame.children(':last');
924
925 this._$contentFrame.append('<div class="geo-measure-container" style="' + contentPosCss + contentSizeCss + '"><span class="geo-measure-label" style="' + contentPosCss + '; display: none;"></span></div>');
926 this._$measureContainer = this._$contentFrame.children(':last');
927 this._$measureLabel = this._$measureContainer.children();
928
929 this._$contentFrame.append('<div class="geo-shapes-container" style="' + contentPosCss + contentSizeCss + '"></div>');
930 this._$shapesContainer = this._$contentFrame.children(':last');
931
932 this._$panContainer = $( [ this._$shapesContainer[ 0 ], this._$drawContainer[ 0 ], this._$measureContainer[ 0 ] ] );
933
934 this._$resizeContainer = $( [ this._$contentFrame[ 0 ], this._$servicesContainer[ 0 ], this._$eventTarget[ 0 ], this._$measureContainer[ 0 ] ] );
935
936 this._$contentFrame.append(this._$existingChildren);
937
938 if ( ! $("#geo-measure-style").length ) {
939 $("head").prepend( window.toStaticHTML( '<style type="text/css" id="geo-measure-style">.geo-measure-label { margin: 4px 0 0 6px; font-family: sans-serif;' + ( _ieVersion ? 'letter-spacing: 2px; color: #444; filter:progid:DXImageTransform.Microsoft.DropShadow(Color=white, OffX=1, OffY=2, Positive=true);' : 'color: #000; text-shadow: #fff 1px 2px; font-weight: bold;' ) + ' }</style>' ) );
940 }
941 },
942
943 _createServices: function () {
944 var service, i;
945
946 for ( i = 0; i < this._currentServices.length; i++ ) {
947 this._currentServices[ i ].serviceContainer.geomap( "destroy" );
948 $.geo[ "_serviceTypes" ][ this._currentServices[ i ].type ].destroy( this, this._$servicesContainer, this._currentServices[ i ] );
949 }
950
951 this._currentServices = [ ];
952 this._$servicesContainer.html( "" );
953 this._$attrList.html( "" );
954
955 for ( i = 0; i < this._options[ "services" ].length; i++ ) {
956 service = this._currentServices[ i ] = $.extend( { }, this._options[ "services" ][ i ] );
957
958 // keep a reference to the original
959 service.serviceObject = this._options[ "services" ][ i ];
960
961 // default the service style property on our copy
962 service.style = $.extend( {
963 visibility: "visible",
964 opacity: 1
965 }, service.style );
966
967 var idString = service.id ? ' id="' + service.id + '"' : "",
968 classString = 'class="geo-service ' + ( service["class"] ? service["class"] : '' ) + '"',
969 scHtml = '<div ' + idString + classString + ' style="-webkit-transform:translateZ(0);position:absolute; left:0; top:0; width:32px; height:32px; margin:0; padding:0; display:' + ( service.style.visibility === "visible" ? "block" : "none" ) + ';"></div>',
970 serviceContainer;
971
972 this._$servicesContainer.append( window.toStaticHTML( scHtml ) );
973 serviceContainer = this._$servicesContainer.children( ":last" );
974 service.serviceContainer = serviceContainer;
975
976 $.geo[ "_serviceTypes" ][ service.type ].create( this, serviceContainer, service, i );
977
978 serviceContainer.data( "geoMap", this ).geomap();
979
980 if ( service.attr ) {
981 this._$attrList.append( '<li>' + service.attr + '</li>' );
982 }
983 }
984
985 // start with our map-level shapesContainer
986 this._$shapesContainers = this._$shapesContainer;
987
988 this._$attrList.find( "a" ).css( {
989 position: "relative",
990 zIndex: 1,
991 display: "inline-block",
992 webkitTransform: "translateZ(0)"
993 } );
994 },
995
996 _createServiceGraphics: function( ) {
997 // only called in the context of a service-level geomap
998 var $contentFrame = this._$elem.closest( ".geo-content-frame" );
999 this._$elem.append('<div class="geo-shapes-container" style="position:absolute; left:0; top:0; width:' + $contentFrame.css( "width" ) + '; height:' + $contentFrame.css( "height" ) + '; margin:0; padding:0;"></div>');
1000 this._$shapesContainer = this._$elem.children(':last');
1001
1002 this._map._$shapesContainers = this._map._$shapesContainers.add( this._$shapesContainer );
1003
1004 this._$shapesContainer.geographics( { doubleBuffer: !_gecko });
1005 this._createdGraphics = true;
1006
1007 this._options["shapeStyle"] = this._$shapesContainer.geographics("option", "style");
1008 },
1009
1010 _refreshDrawing: function ( ) {
1011 this._$drawContainer.geographics("clear");
1012
1013 if ( this._drawPixels.length > 0 ) {
1014 var mode = this._options[ "mode" ],
1015 pixels = this._drawPixels,
1016 coords = this._drawCoords,
1017 label,
1018 labelShape,
1019 labelPixel,
1020 widthOver,
1021 heightOver;
1022
1023 switch ( mode ) {
1024 case "measureLength":
1025 mode = "drawLineString";
1026 labelShape = {
1027 type: "LineString",
1028 coordinates: coords
1029 };
1030 label = $.render[ this._tmplLengthId ]( { length: $.geo.length( labelShape, true ) } );
1031 labelPixel = $.merge( [], pixels[ pixels.length - 1 ] );
1032 break;
1033
1034 case "measureArea":
1035 mode = "drawPolygon";
1036
1037 labelShape = {
1038 type: "Polygon",
1039 coordinates: [ $.merge( [ ], coords ) ]
1040 };
1041 labelShape.coordinates[ 0 ].push( coords[ 0 ] );
1042
1043 label = $.render[ this._tmplAreaId ]( { area: $.geo.area( labelShape, true ) } );
1044 labelPixel = this._toPixel( $.geo.centroid( labelShape ).coordinates );
1045 pixels = [ pixels ];
1046 break;
1047
1048 case "drawPolygon":
1049 pixels = [ pixels ];
1050 break;
1051 }
1052
1053 this._$drawContainer.geographics( mode, pixels );
1054
1055 if ( label ) {
1056 this._$measureLabel.html( label );
1057
1058 widthOver = this._contentBounds.width - ( this._$measureLabel.outerWidth( true ) + labelPixel[ 0 ] );
1059 heightOver = this._contentBounds.height - ( this._$measureLabel.outerHeight( true ) + labelPixel[ 1 ] );
1060
1061 if ( widthOver < 0 ) {
1062 labelPixel[ 0 ] += widthOver;
1063 }
1064
1065 if ( heightOver < 0 ) {
1066 labelPixel[ 1 ] += heightOver;
1067 }
1068
1069 this._$measureLabel.css( {
1070 left: Math.max( labelPixel[ 0 ], 0 ),
1071 top: Math.max( labelPixel[ 1 ], 0 )
1072 } ).show();
1073 }
1074 }
1075 },
1076
1077 _resetDrawing: function () {
1078 this._drawPixels = [];
1079 this._drawCoords = [];
1080 this._$drawContainer.geographics("clear");
1081 this._$measureLabel.hide();
1082 },
1083
1084 _refreshAllShapes: function ( _serviceContainer ) {
1085 this._timeoutRefreshShapes = null;
1086
1087 var service,
1088 geoService,
1089 i = 0;
1090
1091 for ( ; i < this._currentServices.length; i++ ) {
1092 service = this._currentServices[ i ];
1093
1094 if ( !_serviceContainer || service.serviceContainer[ 0 ] === _serviceContainer[ 0 ] ) {
1095 geoService = service.serviceContainer.data( "geoService" );
1096
1097 if ( geoService._createdGraphics ) {
1098 geoService._$shapesContainer.geographics( "clear" );
1099 if ( geoService._graphicShapes.length > 0 ) {
1100 geoService._refreshShapes( geoService._$shapesContainer, geoService._graphicShapes, geoService._graphicShapes, geoService._graphicShapes );
1101 }
1102 }
1103 }
1104 }
1105
1106 if ( this._createdGraphics && !_serviceContainer ) {
1107 this._$shapesContainer.geographics( "clear" );
1108 if ( this._graphicShapes.length > 0 ) {
1109 this._refreshShapes( this._$shapesContainer, this._graphicShapes, this._graphicShapes, this._graphicShapes );
1110 }
1111 }
1112 },
1113
1114
1115 _refreshShapes: function (geographics, shapes, styles, labels, center, pixelSize) {
1116 var i, mgi,
1117 shape,
1118 shapeBbox,
1119 style,
1120 label,
1121 hasLabel,
1122 labelPixel,
1123 centroid,
1124 bbox = this._map._getBbox(center, pixelSize);
1125
1126 for (i = 0; i < shapes.length; i++) {
1127 shape = shapes[i].shape || shapes[i];
1128 shape = shape.geometry || shape;
1129 shapeBbox = $.data(shape, "geoBbox");
1130
1131 if ( shapeBbox && $.geo._bboxDisjoint( bbox, shapeBbox ) ) {
1132 continue;
1133 }
1134
1135 style = $.isArray(styles) ? styles[i].style : styles;
1136 label = $.isArray(labels) ? labels[i].label : labels;
1137 hasLabel = ( label !== undefined );
1138 labelPixel = undefined;
1139 centroid = $.geo.centroid( shape );
1140
1141 switch (shape.type) {
1142 case "Point":
1143 labelPixel = this._map.toPixel( shape.coordinates, center, pixelSize );
1144 this._$shapesContainer.geographics("drawPoint", labelPixel, style);
1145 break;
1146 case "LineString":
1147 this._$shapesContainer.geographics("drawLineString", this._map.toPixel(shape.coordinates, center, pixelSize), style);
1148 if ( hasLabel ) {
1149 labelPixel = this._map.toPixel( $.geo.pointAlong( shape, 0.5 ).coordinates, center, pixelSize );
1150 }
1151 break;
1152 case "Polygon":
1153 this._$shapesContainer.geographics("drawPolygon", this._map.toPixel(shape.coordinates, center, pixelSize), style);
1154 if ( hasLabel && centroid ) {
1155 labelPixel = this._map.toPixel( centroid.coordinates, center, pixelSize );
1156 }
1157 break;
1158 case "MultiPoint":
1159 for (mgi = 0; mgi < shape.coordinates.length; mgi++) {
1160 this._$shapesContainer.geographics("drawPoint", this._map.toPixel(shape.coordinates[mgi], center, pixelSize), style);
1161 }
1162 if ( hasLabel && centroid ) {
1163 labelPixel = this._map.toPixel( centroid.coordinates, center, pixelSize );
1164 }
1165 break;
1166 case "MultiLineString":
1167 for (mgi = 0; mgi < shape.coordinates.length; mgi++) {
1168 this._$shapesContainer.geographics("drawLineString", this._map.toPixel(shape.coordinates[mgi], center, pixelSize), style);
1169 }
1170 if ( hasLabel && centroid ) {
1171 labelPixel = this._map.toPixel( centroid.coordinates, center, pixelSize );
1172 }
1173 break;
1174 case "MultiPolygon":
1175 for (mgi = 0; mgi < shape.coordinates.length; mgi++) {
1176 this._$shapesContainer.geographics("drawPolygon", this._map.toPixel(shape.coordinates[mgi], center, pixelSize), style);
1177 }
1178 if ( hasLabel && centroid ) {
1179 labelPixel = this._map.toPixel( centroid.coordinates, center, pixelSize );
1180 }
1181 break;
1182
1183 case "GeometryCollection":
1184 this._refreshShapes(geographics, shape.geometries, style, label, center, pixelSize);
1185 break;
1186 }
1187
1188 if ( hasLabel && labelPixel ) {
1189 this._$shapesContainer.geographics( "drawLabel", labelPixel, label );
1190 }
1191 }
1192 },
1193
1194 _findMapSize: function () {
1195 // really, really attempt to find a size for this thing
1196 // even if it's hidden (look at parents)
1197 var size = { width: 0, height: 0 },
1198 sizeContainer = this._$elem;
1199
1200 while (sizeContainer.length && !(size["width"] > 0 && size["height"] > 0)) {
1201 size = { width: sizeContainer.width(), height: sizeContainer.height() };
1202 if (size["width"] <= 0 || size["height"] <= 0) {
1203 size = { width: parseInt(sizeContainer.css("width"), 10), height: parseInt(sizeContainer.css("height"), 10) };
1204 }
1205 sizeContainer = sizeContainer.parent();
1206 }
1207 return size;
1208 },
1209
1210 _forcePosition: function (elem) {
1211 var cssPosition = elem.css("position");
1212 if (cssPosition !== "relative" && cssPosition !== "absolute" && cssPosition !== "fixed") {
1213 elem.css("position", "relative");
1214 }
1215 },
1216
1217 _getPixelSize: function ( zoom ) {
1218 var tilingScheme = this._options["tilingScheme"];
1219 if (tilingScheme !== null) {
1220 if (zoom === 0) {
1221 return tilingScheme.pixelSizes ? tilingScheme.pixelSizes[0] : tilingScheme.basePixelSize;
1222 }
1223
1224 zoom = Math.round(zoom);
1225 zoom = Math.max(zoom, 0);
1226 var levels = tilingScheme.pixelSizes ? tilingScheme.pixelSizes.length : tilingScheme.levels;
1227 zoom = Math.min(zoom, levels - 1);
1228
1229 if ( tilingScheme.pixelSizes ) {
1230 return tilingScheme.pixelSizes[zoom];
1231 } else {
1232 return tilingScheme.basePixelSize / Math.pow(2, zoom);
1233 }
1234 } else {
1235 var bbox = $.geo.scaleBy( this._getBboxMax(), 1 / Math.pow( this._fullZoomFactor, zoom ), true );
1236 return Math.max( $.geo.width( bbox, true ) / this._contentBounds.width, $.geo.height( bbox, true ) / this._contentBounds.height );
1237 }
1238 },
1239
1240 _getZoomCenterAndSize: function ( anchor, zoomDelta, full ) {
1241 var zoomFactor = ( full ? this._fullZoomFactor : this._partialZoomFactor ),
1242 scale = Math.pow( zoomFactor, -zoomDelta ),
1243 pixelSize = this._pixelSizeInteractive * scale;
1244
1245 if ( this._options[ "tilingScheme" ] ) {
1246 // clamp to min/max pixelSize
1247 pixelSize = Math.min( Math.max( pixelSize, this._pixelSizeMin ), this._pixelSizeMax );
1248 }
1249
1250 var zoom = this._getZoom(this._centerInteractive, pixelSize);
1251
1252 // clamp to zoom
1253 if ( full && this._options[ "tilingScheme" ] ) {
1254 pixelSize = this._getPixelSize( Math.min( Math.max( zoom, this._options[ "zoomMin" ] ), this._options[ "zoomMax" ] ) );
1255 } else {
1256 if ( zoomDelta < 0 && zoom < this._options[ "zoomMin" ] ) {
1257 pixelSize = this._pixelSizeInteractive;
1258 } else if ( zoomDelta > 0 && zoom > this._options[ "zoomMax" ] ) {
1259 pixelSize = this._pixelSizeInteractive;
1260 }
1261 }
1262
1263 var ratio = pixelSize / this._pixelSizeInteractive,
1264 anchorMapCoord = this._toMap( anchor, this._centerInteractive, this._pixelSizeInteractive ),
1265 centerDelta = [(this._centerInteractive[0] - anchorMapCoord[0]) * ratio, (this._centerInteractive[1] - anchorMapCoord[1]) * ratio],
1266 scaleCenter = [anchorMapCoord[0] + centerDelta[0], anchorMapCoord[1] + centerDelta[1]];
1267
1268 return { pixelSize: pixelSize, center: scaleCenter };
1269 },
1270
1271 _panFinalize: function () {
1272 if (this._panning) {
1273 this._velocity = [0, 0];
1274
1275 this._$eventTarget.css("cursor", this._options["cursors"][this._options["mode"]]);
1276
1277 this._inOp = false;
1278 this._anchor = this._current;
1279 this._mouseDown = this._toolPan = this._panning = false;
1280 }
1281 },
1282
1283 _panMove: function () {
1284 if ( ! this._options[ "pannable" ] ) {
1285 return;
1286 }
1287
1288 var dx = this._current[0] - this._lastDrag[0],
1289 dy = this._current[1] - this._lastDrag[1];
1290
1291 if (this._toolPan || dx > 3 || dx < -3 || dy > 3 || dy < -3) {
1292 if (!this._toolPan) {
1293 this._toolPan = true;
1294 this._$eventTarget.css("cursor", this._options["cursors"]["pan"]);
1295 }
1296
1297 if (this._mouseDown) {
1298 this._velocity = [dx, dy];
1299 }
1300
1301 if (dx !== 0 || dy !== 0) {
1302 this._panning = true;
1303 this._lastDrag = this._current;
1304
1305 this._centerInteractive[ 0 ] -= ( dx * this._pixelSizeInteractive );
1306 this._centerInteractive[ 1 ] += ( ( this._options[ "axisLayout" ] === "image" ? -1 : 1 ) * dy * this._pixelSizeInteractive );
1307 this._setInteractiveCenterAndSize( this._centerInteractive, this._pixelSizeInteractive );
1308 this._interactiveTransform( );
1309 }
1310 }
1311 },
1312
1313 _clearInteractiveTimeout: function() {
1314 if ( this._timeoutRefreshShapes ) {
1315 clearTimeout( this._timeoutRefreshShapes );
1316 this._timeoutRefreshShapes = null;
1317 }
1318
1319 if ( this._timeoutInteractive ) {
1320 clearTimeout( this._timeoutInteractive );
1321 this._timeoutInteractive = null;
1322 return true;
1323 } else {
1324 this._centerInteractive[ 0 ] = this._center[ 0 ];
1325 this._centerInteractive[ 1 ] = this._center[ 1 ];
1326 this._pixelSizeInteractive = this._pixelSize;
1327 return false;
1328 }
1329 },
1330
1331 _interactiveTransform: function( ) {
1332 var service,
1333
1334 scalePixelSize = this._pixelSize,
1335 scaleRatio = scalePixelSize / this._pixelSizeInteractive;
1336
1337 if ( scalePixelSize > 0 ) {
1338 scaleRatio = Math.round(scaleRatio * 1000) / 1000;
1339
1340 var oldMapOrigin = this._toMap( [ 0, 0 ] ),
1341 newPixelPoint = this._toPixel( oldMapOrigin, this._centerInteractive, this._pixelSizeInteractive );
1342
1343
1344 this._$shapesContainers.geographics("interactiveTransform", newPixelPoint, scaleRatio);
1345 }
1346
1347 for ( var i = 0; i < this._currentServices.length; i++ ) {
1348 service = this._currentServices[ i ];
1349 $.geo[ "_serviceTypes" ][ service.type ].interactiveTransform( this, service, this._centerInteractive, this._pixelSizeInteractive );
1350 }
1351
1352 if (this._drawCoords.length > 0) {
1353 this._drawPixels = this._toPixel( this._drawCoords, this._centerInteractive, this._pixelSizeInteractive );
1354 this._refreshDrawing();
1355 }
1356 },
1357
1358 _interactiveTimeout: function( ) {
1359 if ( this._isMultiTouch ) {
1360 this._timeoutInteractive = setTimeout( $.proxy( this._interactiveTimeout, this ), 128 );
1361 } else if ( this._created && this._timeoutInteractive ) {
1362 this._setCenterAndSize( this._centerInteractive, this._pixelSizeInteractive, this._triggerInteractive, true );
1363 this._timeoutInteractive = null;
1364 this._triggerInteractive = false;
1365
1366 this._timeoutRefreshShapes = setTimeout( $.proxy( this._refreshAllShapes, this ), 128 );
1367 }
1368 },
1369
1370 _setInteractiveTimeout: function( trigger ) {
1371 this._timeoutInteractive = setTimeout( $.proxy( this._interactiveTimeout, this ), 128 );
1372 this._triggerInteractive |= trigger;
1373 },
1374
1375 _refresh: function ( force, _serviceContainer ) {
1376 var service,
1377 i = 0;
1378
1379 for ( ; i < this._currentServices.length; i++ ) {
1380 service = this._currentServices[ i ];
1381 if ( !_serviceContainer || service.serviceContainer[ 0 ] === _serviceContainer[ 0 ] ) {
1382 $.geo[ "_serviceTypes" ][ service.type ].refresh( this, service, force );
1383 }
1384 }
1385 },
1386
1387 _setInteractiveCenterAndSize: function ( center, pixelSize ) {
1388 // set the temporary (interactive) center & size
1389 // also, update the public-facing options
1390 // this does not take zoomMin or zoomMax into account
1391 this._centerInteractive[ 0 ] = center[ 0 ];
1392 this._centerInteractive[ 1 ] = center[ 1 ];
1393 this._pixelSizeInteractive = pixelSize;
1394
1395 if ( this._userGeodetic ) {
1396 this._options["bbox"] = $.geo.proj.toGeodetic( this._getBbox( center, pixelSize ) );
1397 this._options["center"] = $.geo.proj.toGeodetic( center );
1398 } else {
1399 this._options["bbox"] = this._getBbox( center, pixelSize );
1400 this._options["center"][ 0 ] = center[ 0 ];
1401 this._options["center"][ 1 ] = center[ 1 ];
1402 }
1403
1404 this._options["pixelSize"] = pixelSize;
1405 this._options["zoom"] = this._getZoom( center, pixelSize );
1406 },
1407
1408 _setCenterAndSize: function (center, pixelSize, trigger, refresh) {
1409 if ( ! $.isArray( center ) || center.length !== 2 || typeof center[ 0 ] !== "number" || typeof center[ 1 ] !== "number" ) {
1410 return;
1411 }
1412
1413 // the final call during any extent change
1414 // only called by timeoutInteractive & resize
1415 // clamp to zoom
1416 var zoom = this._getZoom( center, pixelSize );
1417
1418 if ( this._options[ "tilingScheme" ] ) {
1419 this._pixelSizeInteractive = pixelSize = this._getPixelSize( Math.min( Math.max( zoom, this._options[ "zoomMin" ] ), this._options[ "zoomMax" ] ) );
1420 } else {
1421 if ( zoom < this._options[ "zoomMin" ] ) {
1422 this._pixelSizeInteractive = pixelSize = this._getPixelSize( this._options[ "zoomMin" ] );
1423 } else if ( zoom > this._options[ "zoomMax" ] ) {
1424 this._pixelSizeInteractive = pixelSize = this._getPixelSize( this._options[ "zoomMax" ] );
1425 }
1426 }
1427
1428 this._centerInteractive[ 0 ] = center[ 0 ];
1429 this._centerInteractive[ 1 ] = center[ 1 ];
1430
1431 if ( this._created ) {
1432 // one last interactiveTransform to put pinched map into place
1433 this._interactiveTransform( );
1434 }
1435
1436 this._options["pixelSize"] = this._pixelSize = pixelSize;
1437
1438 this._center[ 0 ] = center[ 0 ];
1439 this._center[ 1 ] = center[ 1 ];
1440
1441
1442 if ( this._userGeodetic ) {
1443 this._options["bbox"] = $.geo.proj.toGeodetic( this._getBbox() );
1444 this._options["center"] = $.geo.proj.toGeodetic( this._center );
1445 } else {
1446 this._options["bbox"] = this._getBbox();
1447 this._options["center"] = $.merge( [ ], center );
1448 }
1449
1450 this._options["zoom"] = zoom;
1451
1452 if (trigger) {
1453 this._trigger("bboxchange", window.event, { bbox: $.merge( [ ], this._options["bbox"] ) });
1454 }
1455
1456 if (refresh) {
1457 this._refresh();
1458 this._refreshAllShapes( );
1459 this._refreshDrawing();
1460 }
1461 },
1462
1463 _requestQueued: function ( ) {
1464 if ( this._loadCount === 0 ) {
1465 this._trigger( "loadstart", window.event );
1466 }
1467 this._loadCount++;
1468 },
1469
1470 _requestComplete: function ( ) {
1471 this._loadCount--;
1472 if ( this._loadCount <= 0 ) {
1473 this._loadCount = 0;
1474 this._trigger( "loadend", window.event );
1475 }
1476 },
1477
1478 _toMap: function (p, center, pixelSize) {
1479 // ignores $.geo.proj
1480
1481 center = center || this._center;
1482 pixelSize = pixelSize || this._pixelSize;
1483
1484 var isMultiPointOrLineString = $.isArray( p[ 0 ] ),
1485 isMultiLineStringOrPolygon = isMultiPointOrLineString && $.isArray( p[ 0 ][ 0 ] ),
1486 isMultiPolygon = isMultiLineStringOrPolygon && $.isArray( p[ 0 ][ 0 ][ 0 ] ),
1487 width = this._contentBounds["width"],
1488 height = this._contentBounds["height"],
1489 halfWidth = width / 2 * pixelSize,
1490 halfHeight = height / 2 * pixelSize,
1491 bbox = [center[0] - halfWidth, center[1] - halfHeight, center[0] + halfWidth, center[1] + halfHeight],
1492 xRatio = $.geo.width(bbox, true) / width,
1493 yRatio = $.geo.height(bbox, true) / height,
1494 yOffset,
1495 image = this._options[ "axisLayout" ] === "image",
1496 result = [],
1497 i, j, k;
1498
1499 if ( !isMultiPolygon ) {
1500 if ( !isMultiLineStringOrPolygon ) {
1501 if ( !isMultiPointOrLineString ) {
1502 p = [ p ];
1503 }
1504 p = [ p ];
1505 }
1506 p = [ p ];
1507 }
1508
1509 for ( i = 0; i < p.length; i++ ) {
1510 result[ i ] = [ ];
1511 for ( j = 0; j < p[ i ].length; j++ ) {
1512 result[ i ][ j ] = [ ];
1513 for ( k = 0; k < p[ i ][ j ].length; k++ ) {
1514 yOffset = (p[ i ][ j ][ k ][1] * yRatio);
1515 result[ i ][ j ][ k ] = [
1516 bbox[ 0 ] + ( p[ i ][ j ][ k ][ 0 ] * xRatio ),
1517 image ? bbox[ 1 ] + yOffset : bbox[ 3 ] - yOffset
1518 ];
1519 }
1520 }
1521 }
1522
1523 return isMultiPolygon ? result : isMultiLineStringOrPolygon ? result[ 0 ] : isMultiPointOrLineString ? result[ 0 ][ 0 ] : result[ 0 ][ 0 ][ 0 ];
1524 },
1525
1526 _toPixel: function (p, center, pixelSize) {
1527 // ignores $.geo.proj
1528
1529 center = center || this._center;
1530 pixelSize = pixelSize || this._pixelSize;
1531
1532 var isMultiPointOrLineString = $.isArray( p[ 0 ] ),
1533 isMultiLineStringOrPolygon = isMultiPointOrLineString && $.isArray( p[ 0 ][ 0 ] ),
1534 isMultiPolygon = isMultiLineStringOrPolygon && $.isArray( p[ 0 ][ 0 ][ 0 ] ),
1535 width = this._contentBounds["width"],
1536 height = this._contentBounds["height"],
1537 halfWidth = width / 2 * pixelSize,
1538 halfHeight = height / 2 * pixelSize,
1539 bbox = [center[0] - halfWidth, center[1] - halfHeight, center[0] + halfWidth, center[1] + halfHeight],
1540 bboxWidth = $.geo.width(bbox, true),
1541 bboxHeight = $.geo.height(bbox, true),
1542 image = this._options[ "axisLayout" ] === "image",
1543 xRatio = width / bboxWidth,
1544 yRatio = height / bboxHeight,
1545 result = [ ],
1546 i, j, k;
1547
1548 if ( !isMultiPolygon ) {
1549 if ( !isMultiLineStringOrPolygon ) {
1550 if ( !isMultiPointOrLineString ) {
1551 p = [ p ];
1552 }
1553 p = [ p ];
1554 }
1555 p = [ p ];
1556 }
1557
1558 for ( i = 0; i < p.length; i++ ) {
1559 result[ i ] = [ ];
1560 for ( j = 0; j < p[ i ].length; j++ ) {
1561 result[ i ][ j ] = [ ];
1562 for ( k = 0; k < p[ i ][ j ].length; k++ ) {
1563 result[ i ][ j ][ k ] = [
1564 Math.round( ( p[ i ][ j ][ k ][ 0 ] - bbox[ 0 ] ) * xRatio ),
1565 Math.round( ( image ? p[ i ][ j ][ k ][ 1 ] - bbox[ 1 ] : bbox[ 3 ] - p[ i ][ j ][ k ][ 1 ] ) * yRatio )
1566 ];
1567 }
1568 }
1569 }
1570
1571 return isMultiPolygon ? result : isMultiLineStringOrPolygon ? result[ 0 ] : isMultiPointOrLineString ? result[ 0 ][ 0 ] : result[ 0 ][ 0 ][ 0 ];
1572 },
1573
1574 _document_keydown: function (e) {
1575 var len = this._drawCoords.length;
1576 if (len > 0 && e.which === 27) {
1577 if (len <= 2) {
1578 this._resetDrawing();
1579 this._inOp = false;
1580 } else {
1581 this._drawCoords[len - 2] = $.merge( [], this._drawCoords[ len - 1 ] );
1582 this._drawPixels[len - 2] = $.merge( [], this._drawPixels[ len - 1 ] );
1583
1584 this._drawCoords.length--;
1585 this._drawPixels.length--;
1586
1587 this._refreshDrawing();
1588 }
1589 }
1590 },
1591
1592 _eventTarget_dblclick_zoom: function(e) {
1593 var doInteractiveTimeout = this._clearInteractiveTimeout( );
1594
1595 this._trigger("dblclick", e, { type: "Point", coordinates: this._toMap(this._current, this._centerInteractive, this._pixelSizeInteractive ) });
1596
1597 if (!e.isDefaultPrevented()) {
1598 var centerAndSize = this._getZoomCenterAndSize(this._current, 1, true );
1599
1600 this._setInteractiveCenterAndSize( centerAndSize.center, centerAndSize.pixelSize );
1601 this._interactiveTransform( );
1602
1603 doInteractiveTimeout = true;
1604 }
1605
1606 if ( doInteractiveTimeout ) {
1607 this._setInteractiveTimeout( true );
1608 }
1609 },
1610
1611 _eventTarget_dblclick: function (e) {
1612 if ( this._options[ "mode" ] === "static" ) {
1613 return;
1614 }
1615
1616 if (this._drawTimeout) {
1617 window.clearTimeout(this._drawTimeout);
1618 this._drawTimeout = null;
1619 }
1620
1621 switch (this._options["mode"]) {
1622 case "drawLineString":
1623 case "measureLength":
1624 if ( this._drawCoords.length > 1 && ! ( this._drawCoords[0][0] === this._drawCoords[1][0] &&
1625 this._drawCoords[0][1] === this._drawCoords[1][1] ) ) {
1626 this._drawCoords.length--;
1627 this._trigger( "shape", e, {
1628 type: "LineString",
1629 coordinates: this._userGeodetic ? $.geo.proj.toGeodetic(this._drawCoords) : this._drawCoords
1630 } );
1631 } else {
1632 this._eventTarget_dblclick_zoom(e);
1633 }
1634 this._resetDrawing();
1635 break;
1636
1637 case "drawPolygon":
1638 case "measureArea":
1639 if ( this._drawCoords.length > 1 && ! ( this._drawCoords[0][0] === this._drawCoords[1][0] &&
1640 this._drawCoords[0][1] === this._drawCoords[1][1] ) ) {
1641 var endIndex = this._drawCoords.length - 1;
1642 if (endIndex > 2) {
1643 this._drawCoords[endIndex] = $.merge( [], this._drawCoords[0] );
1644 this._trigger( "shape", e, {
1645 type: "Polygon",
1646 coordinates: [ this._userGeodetic ? $.geo.proj.toGeodetic(this._drawCoords) : this._drawCoords ]
1647 } );
1648 }
1649 } else {
1650 this._eventTarget_dblclick_zoom(e);
1651 }
1652 this._resetDrawing();
1653 break;
1654
1655 default:
1656 this._eventTarget_dblclick_zoom(e);
1657 break;
1658 }
1659
1660 this._inOp = false;
1661 },
1662
1663 _eventTarget_touchstart: function (e) {
1664 if ( typeof( document.elementFromPoint ) !== "undefined" ) {
1665 var elFromPt = document.elementFromPoint( e.pageX, e.pageY );
1666 if ( elFromPt && elFromPt.nodeName === "A" ) {
1667 return;
1668 }
1669 }
1670
1671 var mode = this._options[ "mode" ],
1672 shift = this._options[ "shift" ],
1673 defaultShift = ( mode === "dragBox" ? "dragBox" : "zoom" );
1674
1675 if ( mode === "static" ) {
1676 return;
1677 }
1678
1679 if ( !this._pointerEvents && !this._supportTouch && e.which !== 1 ) {
1680 return;
1681 }
1682
1683 var doInteractiveTimeout = this._clearInteractiveTimeout( );
1684
1685 var offset = $(e.currentTarget).offset(),
1686 touches = e.originalEvent.changedTouches;
1687
1688 if ( this._pointerEvents ) {
1689 if ( !this._isMultiTouch && this._mouseDown && this._multiTouchAnchor.length > 0 ) {
1690 // switch to multitouch
1691 e.currentTarget.setPointerCapture( e.originalEvent.pointerId );
1692
1693 this._isMultiTouch = true;
1694 this._wheelLevel = 0;
1695
1696 this._multiTouchAnchor.push( e.originalEvent );
1697
1698 this._multiTouchCurrentBbox = [
1699 this._multiTouchCurrentBbox[ 0 ],
1700 this._multiTouchCurrentBbox[ 1 ],
1701 this._multiTouchAnchor[1].pageX - offset.left,
1702 this._multiTouchAnchor[1].pageY - offset.top
1703 ];
1704
1705 this._multiTouchAnchorBbox = $.merge( [ ], this._multiTouchCurrentBbox );
1706
1707 this._anchor = $.geo.center( this._multiTouchCurrentBbox, true );
1708 this._current = $.merge( [], this._anchor );
1709
1710 if ( doInteractiveTimeout ) {
1711 this._setInteractiveTimeout( true );
1712 }
1713
1714 return false;
1715 } else if ( this._multiTouchAnchor.length < 2 ) {
1716 e.currentTarget.setPointerCapture( e.originalEvent.pointerId );
1717
1718 this._multiTouchAnchor.push( e.originalEvent );
1719
1720 this._multiTouchCurrentBbox = [
1721 this._multiTouchAnchor[0].pageX - offset.left,
1722 this._multiTouchAnchor[0].pageY - offset.top,
1723 NaN,
1724 NaN
1725 ];
1726
1727 this._current = [ this._multiTouchAnchor[0].pageX - offset.left, this._multiTouchAnchor[0].pageY - offset.top ];
1728 }
1729 } else if ( this._supportTouch && touches ) {
1730 this._multiTouchAnchor = $.merge( [ ], touches );
1731
1732 this._isMultiTouch = this._multiTouchAnchor.length > 1;
1733
1734 if ( this._isMultiTouch ) {
1735 this._multiTouchCurrentBbox = [
1736 touches[0].pageX - offset.left,
1737 touches[0].pageY - offset.top,
1738 touches[1].pageX - offset.left,
1739 touches[1].pageY - offset.top
1740 ];
1741
1742 this._multiTouchAnchorBbox = $.merge( [ ], this._multiTouchCurrentBbox );
1743
1744 this._current = $.geo.center( this._multiTouchCurrentBbox, true );
1745 } else {
1746 this._multiTouchCurrentBbox = [
1747 touches[0].pageX - offset.left,
1748 touches[0].pageY - offset.top,
1749 NaN,
1750 NaN
1751 ];
1752
1753 this._current = [ touches[0].pageX - offset.left, touches[0].pageY - offset.top ];
1754 }
1755 } else {
1756 this._current = [e.pageX - offset.left, e.pageY - offset.top];
1757 }
1758
1759 if (this._softDblClick) {
1760 var downDate = $.now();
1761 if (downDate - this._downDate < 750) {
1762 if (this._isTap) {
1763 var dx = this._current[0] - this._anchor[0],
1764 dy = this._current[1] - this._anchor[1],
1765 distance = Math.sqrt((dx * dx) + (dy * dy));
1766 if (distance > 8) {
1767 this._isTap = false;
1768 } else {
1769 this._current = $.merge( [ ], this._anchor );
1770 }
1771 }
1772
1773 if (this._isDbltap) {
1774 this._isDbltap = false;
1775 } else {
1776 this._isDbltap = this._isTap;
1777 }
1778 } else {
1779 this._isDbltap = false;
1780 }
1781 this._isTap = true;
1782 this._downDate = downDate;
1783 }
1784
1785 this._mouseDown = true;
1786 this._anchor = $.merge( [ ], this._current );
1787
1788 if (!this._inOp && e.shiftKey && shift !== "off") {
1789 this._shiftDown = true;
1790 this._$eventTarget.css( "cursor", this._options[ "cursors" ][ shift === "default" ? defaultShift : shift ] );
1791 } else if ( !this._isMultiTouch && ( this._options[ "pannable" ] || mode === "dragBox" || mode === "dragCircle" ) ) {
1792 this._inOp = true;
1793
1794 if ( mode !== "zoom" && mode !== "dragBox" && mode !== "dragCircle" ) {
1795 this._lastDrag = this._current;
1796
1797 if (e.currentTarget.setCapture) {
1798 e.currentTarget.setCapture();
1799 }
1800 }
1801 }
1802
1803 e.preventDefault();
1804
1805 if ( doInteractiveTimeout ) {
1806 this._setInteractiveTimeout( true );
1807 }
1808
1809 return false;
1810 },
1811
1812 _dragTarget_touchmove: function (e) {
1813 if ( this._options[ "mode" ] === "static" ) {
1814 return;
1815 }
1816
1817 var doInteractiveTimeout = false;
1818 if ( this._mouseDown ) {
1819 doInteractiveTimeout = this._clearInteractiveTimeout( );
1820 }
1821
1822 var offset = this._$eventTarget.offset(),
1823 drawCoordsLen = this._drawCoords.length,
1824 touches = e.originalEvent.changedTouches,
1825 current = null,
1826 anchorDistance,
1827 currentDistance,
1828 wheelLevel,
1829 delta,
1830 pinchCenterAndSize,
1831 i;
1832
1833 if ( this._pointerEvents ) {
1834 if ( this._isMultiTouch ) {
1835 if ( e.originalEvent.pointerId === this._multiTouchAnchor[ 0 ].pointerId ) {
1836 this._multiTouchCurrentBbox[ 0 ] = e.originalEvent.pageX - offset.left;
1837 this._multiTouchCurrentBbox[ 1 ] = e.originalEvent.pageY - offset.top;
1838 } else if ( e.originalEvent.pointerId === this._multiTouchAnchor[ 1 ].pointerId ) {
1839 this._multiTouchCurrentBbox[ 2 ] = e.originalEvent.pageX - offset.left;
1840 this._multiTouchCurrentBbox[ 3 ] = e.originalEvent.pageY - offset.top;
1841 } else {
1842 // untracked pointer
1843 e.preventDefault();
1844 if ( doInteractiveTimeout ) {
1845 this._setInteractiveTimeout( true );
1846 }
1847 return false;
1848 }
1849
1850 anchorDistance = $.geo._distancePointPoint( [ this._multiTouchAnchorBbox[ 0 ], this._multiTouchAnchorBbox[ 1 ] ], [ this._multiTouchAnchorBbox[ 2 ], this._multiTouchAnchorBbox[ 3 ] ] );
1851 currentDistance = $.geo._distancePointPoint( [ this._multiTouchCurrentBbox[ 0 ], this._multiTouchCurrentBbox[ 1 ] ], [ this._multiTouchCurrentBbox[ 2 ], this._multiTouchCurrentBbox[ 3 ] ] );
1852
1853 wheelLevel = ( ( currentDistance - anchorDistance ) / anchorDistance );
1854
1855 if ( wheelLevel > 0 ) {
1856 wheelLevel *= 5;
1857 } else {
1858 wheelLevel *= 10;
1859 }
1860
1861 delta = wheelLevel - this._wheelLevel;
1862
1863 this._wheelLevel = wheelLevel;
1864
1865 pinchCenterAndSize = this._getZoomCenterAndSize( this._anchor, delta, false );
1866
1867 this._setInteractiveCenterAndSize( pinchCenterAndSize.center, pinchCenterAndSize.pixelSize );
1868 this._interactiveTransform( );
1869
1870 doInteractiveTimeout = true;
1871
1872 current = $.geo.center( this._multiTouchCurrentBbox, true );
1873
1874 } else {
1875 this._multiTouchAnchor[ 0 ] = e.originalEvent;
1876
1877 this._multiTouchCurrentBbox = [
1878 this._multiTouchAnchor[0].pageX - offset.left,
1879 this._multiTouchAnchor[0].pageY - offset.top,
1880 NaN,
1881 NaN
1882 ];
1883
1884 current = [e.originalEvent.pageX - offset.left, e.originalEvent.pageY - offset.top];
1885 }
1886 } else if ( this._supportTouch && touches ) {
1887 if ( !this._isMultiTouch && this._mouseDown && this._multiTouchAnchor.length > 0 && touches[ 0 ].identifier !== this._multiTouchAnchor[ 0 ].identifier ) {
1888 // switch to multitouch
1889 this._isMultiTouch = true;
1890 this._wheelLevel = 0;
1891
1892 this._multiTouchAnchor.push( touches[ 0 ] );
1893
1894 this._multiTouchCurrentBbox = [
1895 this._multiTouchCurrentBbox[ 0 ],
1896 this._multiTouchCurrentBbox[ 1 ],
1897 this._multiTouchAnchor[1].pageX - offset.left,
1898 this._multiTouchAnchor[1].pageY - offset.top
1899 ];
1900
1901 this._multiTouchAnchorBbox = $.merge( [ ], this._multiTouchCurrentBbox );
1902
1903 this._anchor = this._current = $.geo.center( this._multiTouchCurrentBbox, true );
1904
1905
1906 if ( doInteractiveTimeout ) {
1907 this._setInteractiveTimeout( true );
1908 }
1909 return false;
1910 }
1911
1912 if ( this._isMultiTouch ) {
1913
1914 for ( i = 0; i < touches.length; i++ ) {
1915 if ( touches[ i ].identifier === this._multiTouchAnchor[ 0 ].identifier ) {
1916 this._multiTouchCurrentBbox[ 0 ] = touches[ i ].pageX - offset.left;
1917 this._multiTouchCurrentBbox[ 1 ] = touches[ i ].pageY - offset.top;
1918 } else if ( touches[ i ].identifier === this._multiTouchAnchor[ 1 ].identifier ) {
1919 this._multiTouchCurrentBbox[ 2 ] = touches[ i ].pageX - offset.left;
1920 this._multiTouchCurrentBbox[ 3 ] = touches[ i ].pageY - offset.top;
1921 }
1922 }
1923
1924 anchorDistance = $.geo._distancePointPoint( [ this._multiTouchAnchorBbox[ 0 ], this._multiTouchAnchorBbox[ 1 ] ], [ this._multiTouchAnchorBbox[ 2 ], this._multiTouchAnchorBbox[ 3 ] ] );
1925 currentDistance = $.geo._distancePointPoint( [ this._multiTouchCurrentBbox[ 0 ], this._multiTouchCurrentBbox[ 1 ] ], [ this._multiTouchCurrentBbox[ 2 ], this._multiTouchCurrentBbox[ 3 ] ] );
1926
1927 wheelLevel = ( ( currentDistance - anchorDistance ) / anchorDistance );
1928
1929 if ( wheelLevel > 0 ) {
1930 wheelLevel *= 5;
1931 } else {
1932 wheelLevel *= 10;
1933 }
1934
1935 delta = wheelLevel - this._wheelLevel;
1936
1937 this._wheelLevel = wheelLevel;
1938
1939 pinchCenterAndSize = this._getZoomCenterAndSize( this._anchor, delta, false );
1940
1941 this._setInteractiveCenterAndSize( pinchCenterAndSize.center, pinchCenterAndSize.pixelSize );
1942 this._interactiveTransform( );
1943
1944 doInteractiveTimeout = true;
1945
1946 current = $.geo.center( this._multiTouchCurrentBbox, true );
1947 } else {
1948 current = [e.originalEvent.changedTouches[0].pageX - offset.left, e.originalEvent.changedTouches[0].pageY - offset.top];
1949 }
1950 } else {
1951 current = [e.pageX - offset.left, e.pageY - offset.top];
1952 }
1953
1954 if ( current === null ) {
1955 e.preventDefault();
1956 if ( doInteractiveTimeout ) {
1957 this._setInteractiveTimeout( true );
1958 }
1959 return false;
1960 }
1961
1962 if (current[0] === this._lastMove[0] && current[1] === this._lastMove[1]) {
1963 if ( this._inOp ) {
1964 e.preventDefault();
1965 if ( doInteractiveTimeout ) {
1966 this._setInteractiveTimeout( true );
1967 }
1968 return false;
1969 } else {
1970 // fixes: [bug] highlight pop
1971 return false;
1972 }
1973 }
1974
1975 if ( _ieVersion === 7 ) {
1976 this._isDbltap = this._isTap = false;
1977 }
1978
1979 if (this._mouseDown) {
1980 this._current = current;
1981 this._moveDate = $.now();
1982 }
1983
1984 if ( this._isMultiTouch ) {
1985 e.preventDefault( );
1986 this._isDbltap = this._isTap = false;
1987 if ( doInteractiveTimeout ) {
1988 this._setInteractiveTimeout( true );
1989 }
1990 return false;
1991 }
1992
1993 var mode = this._options["mode"],
1994 shift = this._options[ "shift" ],
1995 defaultShift = ( mode === "dragBox" ? "dragBox" : "zoom" ),
1996 dx, dy, circleSize;
1997
1998 if ( this._shiftDown ) {
1999 mode = ( shift === "default" ? defaultShift : shift );
2000 }
2001
2002 switch (mode) {
2003 case "zoom":
2004 case "dragBox":
2005 if ( this._mouseDown ) {
2006 this._$drawContainer.geographics( "clear" );
2007 this._$drawContainer.geographics( "drawBbox", [
2008 this._anchor[ 0 ],
2009 this._anchor[ 1 ],
2010 current[ 0 ],
2011 current[ 1 ]
2012 ] );
2013 } else {
2014 this._trigger("move", e, { type: "Point", coordinates: this.toMap(current) });
2015 }
2016 break;
2017
2018 case "dragCircle":
2019 if ( this._mouseDown ) {
2020 dx = current[ 0 ] - this._anchor[ 0 ];
2021 dy = current[ 1 ] - this._anchor[ 1 ];
2022 circleSize = Math.sqrt( ( dx * dx) + ( dy * dy ) ) * 2;
2023 //circleSize = Math.max( Math.abs( current[ 0 ] - this._anchor[ 0 ] ), Math.abs( current[ 1 ] - this._anchor[ 1 ] ) ) * 2;
2024
2025 // not part of _refreshDrawing
2026 this._$drawContainer.geographics( "clear" );
2027 this._$drawContainer.geographics( "drawArc", this._anchor, 0, 360, {
2028 width: circleSize,
2029 height: circleSize
2030 } );
2031 } else {
2032 this._trigger("move", e, { type: "Point", coordinates: this.toMap(current) });
2033 }
2034 break;
2035
2036 case "drawLineString":
2037 case "drawPolygon":
2038 case "measureLength":
2039 case "measureArea":
2040 if (this._mouseDown || this._toolPan) {
2041 this._panMove();
2042 doInteractiveTimeout = true;
2043 } else {
2044 if (drawCoordsLen > 0) {
2045 this._drawCoords[drawCoordsLen - 1] = this._toMap( current, this._centerInteractive, this._pixelSizeInteractive );
2046 this._drawPixels[drawCoordsLen - 1] = current;
2047
2048 this._refreshDrawing();
2049 }
2050
2051 this._trigger("move", e, { type: "Point", coordinates: this.toMap(current) });
2052 }
2053 break;
2054
2055 default:
2056 if (this._mouseDown || this._toolPan) {
2057 this._panMove();
2058 doInteractiveTimeout = true;
2059 } else {
2060 this._trigger("move", e, { type: "Point", coordinates: this.toMap(current) });
2061 }
2062 break;
2063 }
2064
2065 this._lastMove = current;
2066
2067 if ( doInteractiveTimeout ) {
2068 this._setInteractiveTimeout( true );
2069 }
2070
2071 if ( this._inOp ) {
2072 e.preventDefault();
2073 return false;
2074 }
2075 },
2076
2077 _dragTarget_touchstop: function (e) {
2078 if ( this._options[ "mode" ] === "static" ) {
2079 return;
2080 }
2081
2082 if ( !this._mouseDown ) {
2083 if ( _ieVersion === 7 ) {
2084 // ie7 doesn't appear to trigger dblclick on this._$eventTarget,
2085 // we fake regular click here to cause soft dblclick
2086 this._eventTarget_touchstart(e);
2087 } else {
2088 // Chrome & Firefox trigger a rogue mouseup event when doing a dblclick maximize in Windows(/Linux?)
2089 // ignore it
2090 return;
2091 }
2092 }
2093
2094 var doInteractiveTimeout = this._clearInteractiveTimeout( );
2095
2096 var mouseWasDown = this._mouseDown,
2097 wasToolPan = this._toolPan,
2098 offset = this._$eventTarget.offset(),
2099 mode = this._options[ "mode" ],
2100 shift = this._options[ "shift" ],
2101 defaultShift = ( mode === "dragBox" ? "dragBox" : "zoom" ),
2102 current = null, i, clickDate,
2103 dx, dy,
2104 coordBuffer,
2105 triggerShape;
2106
2107 if ( this._shiftDown ) {
2108 mode = ( shift === "default" ? defaultShift : shift );
2109 }
2110
2111 if ( this._pointerEvents ) {
2112 if ( e.originalEvent.pointerId === this._multiTouchAnchor[ 0 ].pointerId || e.originalEvent.pointerId === this._multiTouchAnchor[ 1 ].pointerId ) {
2113 e.currentTarget.releasePointerCapture( e.originalEvent.pointerId );
2114
2115 current = [e.originalEvent.pageX - offset.left, e.originalEvent.pageY - offset.top];
2116
2117 this._multiTouchAnchor = [];
2118 this._inOp = false;
2119 } else {
2120 // untracked pointer
2121 e.preventDefault();
2122 if ( doInteractiveTimeout ) {
2123 this._setInteractiveTimeout( true );
2124 }
2125 return;
2126 }
2127
2128 } else if (this._supportTouch && e.originalEvent.changedTouches) {
2129 current = [e.originalEvent.changedTouches[0].pageX - offset.left, e.originalEvent.changedTouches[0].pageY - offset.top];
2130 this._multiTouchAnchor = [];
2131 this._inOp = false;
2132 } else {
2133 current = [e.pageX - offset.left, e.pageY - offset.top];
2134 }
2135
2136 if (this._softDblClick) {
2137 if (this._isTap) {
2138 dx = current[0] - this._anchor[0];
2139 dy = current[1] - this._anchor[1];
2140 if (Math.sqrt((dx * dx) + (dy * dy)) <= 8) {
2141 current = $.merge( [ ], this._anchor );
2142 }
2143 }
2144 }
2145
2146 dx = current[0] - this._anchor[0];
2147 dy = current[1] - this._anchor[1];
2148
2149 this._$eventTarget.css("cursor", this._options["cursors"][this._options["mode"]]);
2150
2151 this._shiftDown = this._mouseDown = this._toolPan = false;
2152
2153 if ( this._isMultiTouch ) {
2154 e.preventDefault( );
2155 this._isMultiTouch = false;
2156
2157 this._wheelLevel = 0;
2158
2159 if ( doInteractiveTimeout ) {
2160 this._setInteractiveTimeout( true );
2161 }
2162
2163 return;
2164 }
2165
2166 if (document.releaseCapture) {
2167 document.releaseCapture();
2168 }
2169
2170 if (mouseWasDown) {
2171 clickDate = $.now();
2172 this._current = current;
2173
2174 switch ( mode ) {
2175 case "zoom":
2176 case "dragBox":
2177 if ( dx !== 0 || dy !== 0 ) {
2178 var minSize = this._pixelSize * 6,
2179 bboxCoords = this._toMap( [ this._anchor, current ] ),
2180 bbox = [
2181 Math.min( bboxCoords[0][0], bboxCoords[1][0] ),
2182 Math.min( bboxCoords[0][1], bboxCoords[1][1] ),
2183 Math.max( bboxCoords[0][0], bboxCoords[1][0] ),
2184 Math.max( bboxCoords[0][1], bboxCoords[1][1] )
2185 ];
2186
2187 if ( mode === "zoom" ) {
2188 if ( ( bbox[2] - bbox[0] ) < minSize && ( bbox[3] - bbox[1] ) < minSize ) {
2189 bbox = $.geo.scaleBy( this._getBbox( $.geo.center( bbox, true ) ), 0.5, true );
2190 }
2191
2192 this._setBbox(bbox, true, true);
2193 doInteractiveTimeout = true;
2194 } else {
2195 triggerShape = $.geo.polygonize( bbox, true );
2196 triggerShape.bbox = bbox;
2197
2198 if ( this._userGeodetic ) {
2199 triggerShape.coordinates = $.geo.proj.toGeodetic( triggerShape.coordinates );
2200 triggerShape.bbox = $.geo.proj.toGeodetic( triggerShape.bbox );
2201 }
2202 this._trigger( "shape", e, triggerShape );
2203 }
2204 } else {
2205 if ( mode === "dragBox" ) {
2206 coordBuffer = this._toMap( current );
2207
2208 triggerShape = {
2209 type: "Point",
2210 coordinates: [ coordBuffer[ 0 ], coordBuffer[ 1 ] ],
2211 bbox: [ coordBuffer[ 0 ], coordBuffer[ 1 ], coordBuffer[ 0 ], coordBuffer[ 1 ] ]
2212 };
2213
2214 if ( this._userGeodetic ) {
2215 triggerShape.coordinates = $.geo.proj.toGeodetic( triggerShape.coordinates );
2216 triggerShape.bbox = $.geo.proj.toGeodetic( triggerShape.bbox );
2217 }
2218
2219 this._trigger( "shape", e, triggerShape );
2220 }
2221 }
2222
2223 this._resetDrawing();
2224 break;
2225
2226 case "dragCircle":
2227 if ( dx !== 0 || dy !== 0 ) {
2228 var image = this._options[ "axisLayout" ] === "image",
2229 d = Math.sqrt( ( dx * dx) + ( dy * dy ) ),
2230 n = 180,
2231 a;
2232
2233 this._drawPixels.length = n + 1;
2234
2235 for ( i = 0; i < n; i++ ) {
2236 a = ( i * 360 / n ) * ( Math.PI / 180 );
2237 this._drawPixels[ i ] = [
2238 this._anchor[ 0 ] + Math.cos( a ) * d,
2239 this._anchor[ 1 ] + Math.sin( a ) * d
2240 ];
2241 }
2242
2243 this._drawPixels[ n ] = [
2244 this._drawPixels[ 0 ][ 0 ],
2245 this._drawPixels[ 0 ][ 1 ]
2246 ];
2247
2248 // using coordBuffer for bbox coords
2249 coordBuffer = this._toMap( [
2250 [ this._anchor[ 0 ] - d, this._anchor[ 1 ] + ( image ? -d : d ) ],
2251 [ this._anchor[ 0 ] + d, this._anchor[ 1 ] + ( image ? d : -d ) ]
2252 ] );
2253
2254 triggerShape = {
2255 type: "Polygon",
2256 coordinates: [ this._toMap( this._drawPixels ) ],
2257 bbox: [ coordBuffer[ 0 ][ 0 ], coordBuffer[ 0 ][ 1 ], coordBuffer[ 1 ][ 0 ], coordBuffer[ 1 ][ 1 ] ]
2258 };
2259
2260 if ( this._userGeodetic ) {
2261 triggerShape.coordinates = $.geo.proj.toGeodetic( triggerShape.coordinates );
2262 triggerShape.bbox = $.geo.proj.toGeodetic( triggerShape.bbox );
2263 }
2264
2265 this._trigger( "shape", e, triggerShape );
2266
2267 this._resetDrawing();
2268 } else {
2269 coordBuffer = this._toMap( current );
2270
2271 triggerShape = {
2272 type: "Point",
2273 coordinates: [ coordBuffer[ 0 ], coordBuffer[ 1 ] ],
2274 bbox: [ coordBuffer[ 0 ], coordBuffer[ 1 ], coordBuffer[ 0 ], coordBuffer[ 1 ] ]
2275 };
2276
2277 if ( this._userGeodetic ) {
2278 triggerShape.coordinates = $.geo.proj.toGeodetic( triggerShape.coordinates );
2279 triggerShape.bbox = $.geo.proj.toGeodetic( triggerShape.bbox );
2280 }
2281
2282 this._trigger( "shape", e, triggerShape );
2283 }
2284 break;
2285
2286 case "drawPoint":
2287 if (this._drawTimeout) {
2288 window.clearTimeout(this._drawTimeout);
2289 this._drawTimeout = null;
2290 }
2291
2292 if (wasToolPan) {
2293 this._panFinalize();
2294 } else {
2295 if (clickDate - this._clickDate > 100) {
2296 var geomap = this;
2297 this._drawTimeout = setTimeout(function () {
2298 if (geomap._drawTimeout) {
2299 geomap._trigger("shape", e, { type: "Point", coordinates: geomap.toMap(current) });
2300 geomap._inOp = false;
2301 geomap._drawTimeout = null;
2302 }
2303 }, 250);
2304 }
2305 }
2306 break;
2307
2308 case "drawLineString":
2309 case "drawPolygon":
2310 case "measureLength":
2311 case "measureArea":
2312 if (wasToolPan) {
2313 this._panFinalize();
2314 } else {
2315 this._trigger("click", e, { type: "Point", coordinates: this.toMap(current) });
2316
2317 if (!e.isDefaultPrevented()) {
2318 i = (this._drawCoords.length === 0 ? 0 : this._drawCoords.length - 1);
2319
2320 this._drawCoords[i] = this._toMap(current);
2321 this._drawPixels[i] = current;
2322
2323 if (i < 2 || !(this._drawCoords[i][0] === this._drawCoords[i-1][0] &&
2324 this._drawCoords[i][1] === this._drawCoords[i-1][1])) {
2325 this._drawCoords[i + 1] = this._toMap( current, this._centerInteractive, this._pixelSizeInteractive );
2326 this._drawPixels[i + 1] = current;
2327 }
2328
2329 this._refreshDrawing();
2330 }
2331 }
2332 break;
2333
2334 default:
2335 if (wasToolPan) {
2336 this._panFinalize();
2337 } else {
2338 if (clickDate - this._clickDate > 100) {
2339 this._trigger("click", e, { type: "Point", coordinates: this.toMap(current) });
2340 this._inOp = false;
2341 }
2342 }
2343 break;
2344 }
2345
2346 this._clickDate = clickDate;
2347
2348 if (this._softDblClick && this._isDbltap) {
2349 this._isDbltap = this._isTap = false;
2350 if ( doInteractiveTimeout ) {
2351 this._setInteractiveTimeout( true );
2352 }
2353 this._$eventTarget.trigger("softdblclick", e);
2354 return false;
2355 }
2356 }
2357
2358 if ( doInteractiveTimeout ) {
2359 this._setInteractiveTimeout( true );
2360 }
2361
2362 if ( this._inOp ) {
2363 e.preventDefault();
2364 return false;
2365 }
2366 },
2367
2368 _eventTarget_mousewheel: function ( e ) {
2369 if ( this._options[ "mode" ] === "static" || this._options[ "scroll" ] === "off" ) {
2370 return;
2371 }
2372
2373 e.preventDefault();
2374
2375 if ( this._mouseDown ) {
2376 return false;
2377 }
2378
2379 if ( e.deltaY !== 0 ) {
2380 if ( this._timeoutWheel ) {
2381 clearTimeout( this._timeoutWheel );
2382 this._timeoutWheel = null;
2383 }
2384
2385 var geomap = this;
2386 var currentTarget = e.currentTarget;
2387 var deltaY = e.deltaY;
2388 var pageX = e.pageX;
2389 var pageY = e.pageY;
2390
2391 this._timeoutWheel = setTimeout( function () {
2392 geomap._clearInteractiveTimeout( );
2393
2394 var delta = deltaY > 0 ? 1 : -1;
2395
2396 var offset = $(currentTarget).offset();
2397 geomap._anchor = [pageX - offset.left, pageY - offset.top];
2398
2399 var wheelCenterAndSize = geomap._getZoomCenterAndSize( geomap._anchor, delta, geomap._options[ "tilingScheme" ] !== null );
2400
2401 geomap._setInteractiveCenterAndSize( wheelCenterAndSize.center, wheelCenterAndSize.pixelSize );
2402 geomap._interactiveTransform( );
2403
2404 geomap._setInteractiveTimeout( true );
2405 }, 30 );
2406
2407 }
2408
2409 return false;
2410 }
2411 }
2412 );
2413 }(jQuery, window));
2414