Debian Codegraph @

dojox/geo/charting/KeyboardInteractionSupport.js

raw symbols SCIP
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 // summary:
14 // class to handle keyboard interactions on a dojox.geo.charting.Map component.
15 //
16 // The sections on the leading edge should receive the focus in response to a TAB event.
17 // Then use cursor keys to the peer sections. The cursor event should go the adjacent section
18 // in that direction. With the focus, the section zooms in upon SPACE. The map should zoom out
19 // on ESC. Finally, while it has the focus, the map should lose the focus on TAB.
20 // tags:
21 // private
22 _map: null,
23 _zoomEnabled: false,
24
25 constructor: function(/*dojox/geo/charting/Map*/ map, /*Object?*/ options){
26 // summary:
27 // Constructs a new _KeyboardInteractionSupport instance
28 // map: dojox/geo/charting/Map
29 // the Map component this class provides touch navigation for.
30 // options: Object?
31 // An object defining additional configuration properties. Currently,
32 // only the enableZoom property of this object is taken into account to enable/disable
33 // the Map zooming capability.
34 this._map = map;
35 if(options){
36 this._zoomEnabled = options.enableZoom;
37 }
38 },
39 connect: function(){
40 // summary:
41 // connects this keyboard support class to the Map component
42 var container = dom.byId(this._map.container);
43 // tab accessing enable
44 html.attr(container, {
45 tabindex: 0,
46 role: "presentation",
47 "aria-label": "map"
48 });
49 // install listeners
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 // summary:
56 // disconnects any installed listeners
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(/*KeyboardEvent*/e){
65 // summary:
66 // Handles a keydown event.
67 // e: KeyboardEvent
68 // An event.
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 // summary:
99 // Handles the onFocus event.
100 // e: Event
101 // An event.
102
103 // select the leading region at the map center
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 // find the region most closing to the map center
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 // summary:
132 // Handles the onBlur event.
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 });