| 1 |
define([ |
| 2 |
"dojo/_base/lang", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/event", |
| 5 |
"dojo/_base/connect", |
| 6 |
"dojo/_base/html", |
| 7 |
"dojo/dom", |
| 8 |
"dojox/lang/functional", |
| 9 |
"dojo/keys" |
| 10 |
], function(lang, declare, event, connect, html, dom, functional, keys){ |
| 11 |
|
| 12 |
return declare("dojox.geo.charting.KeyboardInteractionSupport", null, { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
_map: null, |
| 23 |
_zoomEnabled: false, |
| 24 |
|
| 25 |
constructor: function( map, options){ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
this._map = map; |
| 35 |
if(options){ |
| 36 |
this._zoomEnabled = options.enableZoom; |
| 37 |
} |
| 38 |
}, |
| 39 |
connect: function(){ |
| 40 |
|
| 41 |
|
| 42 |
var container = dom.byId(this._map.container); |
| 43 |
|
| 44 |
html.attr(container, { |
| 45 |
tabindex: 0, |
| 46 |
role: "presentation", |
| 47 |
"aria-label": "map" |
| 48 |
}); |
| 49 |
|
| 50 |
this._keydownListener = connect.connect(container, "keydown", this, "keydownHandler"); |
| 51 |
this._onFocusListener = connect.connect(container, "focus", this, "onFocus"); |
| 52 |
this._onBlurListener = connect.connect(container, "blur", this, "onBlur"); |
| 53 |
}, |
| 54 |
disconnect: function(){ |
| 55 |
|
| 56 |
|
| 57 |
connect.disconnect(this._keydownListener); |
| 58 |
this._keydownListener = null; |
| 59 |
connect.disconnect(this._onFocusListener); |
| 60 |
this._onFocusListener = null; |
| 61 |
connect.disconnect(this._onBlurListener); |
| 62 |
this._onBlurListener = null |
| 63 |
}, |
| 64 |
keydownHandler: function(e){ |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
switch(e.keyCode){ |
| 70 |
case keys.LEFT_ARROW: |
| 71 |
this._directTo(-1,-1,1,-1); |
| 72 |
break; |
| 73 |
case keys.RIGHT_ARROW: |
| 74 |
this._directTo(-1,-1,-1,1); |
| 75 |
break; |
| 76 |
case keys.UP_ARROW: |
| 77 |
this._directTo(1,-1,-1,-1); |
| 78 |
break; |
| 79 |
case keys.DOWN_ARROW: |
| 80 |
this._directTo(-1,1,-1,-1); |
| 81 |
break; |
| 82 |
case keys.SPACE: |
| 83 |
if(this._map.selectedFeature && !this._map.selectedFeature._isZoomIn && this._zoomEnabled){ |
| 84 |
this._map.selectedFeature._zoomIn(); |
| 85 |
} |
| 86 |
break; |
| 87 |
case keys.ESCAPE: |
| 88 |
if(this._map.selectedFeature && this._map.selectedFeature._isZoomIn && this._zoomEnabled){ |
| 89 |
this._map.selectedFeature._zoomOut(); |
| 90 |
} |
| 91 |
break; |
| 92 |
default: |
| 93 |
return; |
| 94 |
} |
| 95 |
event.stop(e); |
| 96 |
}, |
| 97 |
onFocus: function(e){ |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if(this._map.selectedFeature || this._map.focused){return;} |
| 105 |
this._map.focused = true; |
| 106 |
var leadingRegion, |
| 107 |
needClick = false; |
| 108 |
if(this._map.lastSelectedFeature){ |
| 109 |
leadingRegion = this._map.lastSelectedFeature; |
| 110 |
}else{ |
| 111 |
var mapCenter = this._map.getMapCenter(), |
| 112 |
minDistance = Infinity; |
| 113 |
|
| 114 |
functional.forIn(this._map.mapObj.features, function(feature){ |
| 115 |
var distance = Math.sqrt(Math.pow(feature._center[0] - mapCenter.x, 2) + Math.pow(feature._center[1] - mapCenter.y, 2)); |
| 116 |
if(distance < minDistance){ |
| 117 |
minDistance = distance; |
| 118 |
leadingRegion = feature; |
| 119 |
} |
| 120 |
}); |
| 121 |
needClick = true; |
| 122 |
} |
| 123 |
if(leadingRegion){ |
| 124 |
if(needClick){ |
| 125 |
leadingRegion._onclickHandler(null); |
| 126 |
} |
| 127 |
this._map.mapObj.marker.show(leadingRegion.id); |
| 128 |
} |
| 129 |
}, |
| 130 |
onBlur: function(){ |
| 131 |
|
| 132 |
|
| 133 |
this._map.lastSelectedFeature = this._map.selectedFeature; |
| 134 |
}, |
| 135 |
_directTo: function(up, down, left, right){ |
| 136 |
var currentSelected = this._map.selectedFeature, |
| 137 |
centerX = currentSelected._center[0], |
| 138 |
centerY = currentSelected._center[1], |
| 139 |
minMargin = Infinity, |
| 140 |
nextSelected = null; |
| 141 |
functional.forIn(this._map.mapObj.features, function(feature){ |
| 142 |
var paddingX = Math.abs(centerX - feature._center[0]), |
| 143 |
paddingY = Math.abs(centerY - feature._center[1]), |
| 144 |
paddingSum = paddingX + paddingY; |
| 145 |
if((up - down) * (centerY - feature._center[1]) > 0){ |
| 146 |
if(paddingX < paddingY && minMargin > paddingSum){ |
| 147 |
minMargin = paddingSum; |
| 148 |
nextSelected = feature; |
| 149 |
} |
| 150 |
} |
| 151 |
if((left - right) * (centerX - feature._center[0]) > 0){ |
| 152 |
if(paddingX > paddingY && minMargin > paddingSum){ |
| 153 |
minMargin = paddingSum; |
| 154 |
nextSelected = feature; |
| 155 |
} |
| 156 |
} |
| 157 |
}); |
| 158 |
if(nextSelected){ |
| 159 |
this._map.mapObj.marker.hide(); |
| 160 |
nextSelected._onclickHandler(null); |
| 161 |
this._map.mapObj.marker.show(nextSelected.id); |
| 162 |
} |
| 163 |
} |
| 164 |
}); |
| 165 |
}); |