Ticket #1484: cursors_revisited.patch
| File cursors_revisited.patch, 21.5 kB (added by bartvde, 2 months ago) |
|---|
-
tests/Control/DragFeature.html
old new 173 173 function test_Control_DragFeature_up(t) { 174 174 t.plan(7); 175 175 var map = new OpenLayers.Map("map"); 176 var cls = map.div.className; 176 177 var layer = new OpenLayers.Layer.Vector(); 177 178 map.addLayer(layer); 178 var control = new OpenLayers.Control.DragFeature(layer );179 var control = new OpenLayers.Control.DragFeature(layer, {handlerOptions: { cursorClasses: {'in': 'overFeature', 'out': 'outFeature'} } }); 179 180 map.addControl(control); 180 181 181 182 control.activate(); … … 189 190 map.events.triggerEvent("mousemove", {type: "mousemove"}); 190 191 t.eq(control.over, true, 191 192 "mouseover on a feature sets the over property to true"); 192 t.eq(control.map.div. style.cursor, "move",193 "mouseover on a feature sets the cursor to move");193 t.eq(control.map.div.className, cls+" overFeature", 194 "mouseover on a feature sets the cursorClass to overFeature"); 194 195 t.eq(control.handlers.drag.active, true, 195 196 "mouseover on a feature activates drag handler"); 196 197 … … 198 199 // over the dragged feature 199 200 control.handlers.drag.started = true; 200 201 map.events.triggerEvent("mouseup", {type: "mouseup"}); 201 t.eq(control.map.div.style.cursor, "move",202 "mouseup while still over dragged feature does not reset cursor to default");203 202 t.eq(control.handlers.drag.active, true, 204 203 "mouseup while still over dragged feature does not deactivate drag handler"); 205 204 … … 208 207 control.handlers.drag.started = true; 209 208 control.over = false; 210 209 map.events.triggerEvent("mouseup", {type: "mouseup"}); 211 t.eq(control.map.div.style.cursor, "default",212 "mouseup resets cursor to default");213 210 t.eq(control.handlers.drag.active, false, 214 211 "mouseup deactivates drag handler"); 215 212 213 control.handlers.feature.triggerCallback("mousemove", 'in', [null]); 214 t.eq(control.map.div.className, cls + " overFeature", 215 "when getting in of the feature the cursorClass is set to overFeature"); 216 217 control.handlers.feature.triggerCallback("mousemove", 'out', [null]); 218 t.eq(control.map.div.className, cls + " outFeature", 219 "when getting out of the feature the cursorClass is set to outFeature"); 220 216 221 control.deactivate(); 217 222 } 218 223 -
lib/OpenLayers/Handler.js
old new 44 44 control: null, 45 45 46 46 /** 47 * APIProperty: cursorClasses 48 * {Array(String)} An array specifying the mouse cursors through css classes 49 * to use on the map div. The key of the array is the function name. 50 */ 51 cursorClasses: null, 52 53 /** 54 * Property: previousFunctionName 55 * {String} The previous function name that was used to add a cursor class 56 * to the map div. It needs to be removed prior to adding a new cursor 57 * class. 58 */ 59 previousFunctionName: null, 60 61 /** 47 62 * Property: map 48 63 * {<OpenLayers.Map>} 49 64 */ … … 176 191 if(!this.active) { 177 192 return false; 178 193 } 194 if (this.previousFunctionName) { 195 OpenLayers.Element.removeClass(this.map.div, 196 this.cursorClasses[this.previousFunctionName]); 197 } 179 198 // unregister event handlers defined on this class. 180 199 var events = OpenLayers.Events.prototype.BROWSER_EVENTS; 181 200 for (var i=0, len=events.length; i<len; i++) { … … 245 264 }, 246 265 247 266 /** 267 * Method: setCursor 268 * Set the mouse cursor on the map div 269 * 270 * Parameters: 271 * functionName - {String} the name of the function (e.g. mousedown) 272 */ 273 setCursor: function(functionName) { 274 if (this.previousFunctionName) { 275 OpenLayers.Element.removeClass(this.map.div, 276 this.cursorClasses[this.previousFunctionName]); 277 } 278 if (this.cursorClasses && this.cursorClasses[functionName]) { 279 OpenLayers.Element.addClass(this.map.div, this.cursorClasses[functionName]); 280 this.previousFunctionName = functionName; 281 } 282 }, 283 284 /** 248 285 * Method: destroy 249 286 * Deconstruct the handler. 250 287 */ -
lib/OpenLayers/Control.js
old new 128 128 * events. 129 129 */ 130 130 events: null, 131 132 /** 133 * APIProperty: cursorClass 134 * {String} a css class which can contain a cursor and will be set on the 135 * map div upon activation. 136 cursorClass: null, 131 137 132 138 /** 133 139 * Constant: EVENT_TYPES … … 299 305 if (this.handler) { 300 306 this.handler.activate(); 301 307 } 308 if (this.cursorClass) { 309 OpenLayers.Element.addClass(this.map.div, this.cursorClass); 310 } 302 311 this.active = true; 303 312 this.events.triggerEvent("activate"); 304 313 return true; … … 318 327 if (this.handler) { 319 328 this.handler.deactivate(); 320 329 } 330 if (this.cursorClass) { 331 OpenLayers.Element.removeClass(this.map.div, this.cursorClass); 332 } 321 333 this.active = false; 322 334 this.events.triggerEvent("deactivate"); 323 335 return true; -
lib/OpenLayers/Control/DragPan.js
old new 35 35 * Defaults to 25 milliseconds. 36 36 */ 37 37 interval: 25, 38 39 /** 40 * APIProperty: handlerOptions 41 * {Object} Used to set non-default properties on the control's handler 42 */ 43 handlerOptions: null, 38 44 39 45 /** 40 46 * Method: draw … … 45 51 this.handler = new OpenLayers.Handler.Drag(this, { 46 52 "move": this.panMap, 47 53 "done": this.panMapDone 48 }, { 49 interval: this.interval 50 } 54 }, OpenLayers.Util.extend({interval: this.interval}, 55 this.handlerOptions) 51 56 ); 52 57 }, 53 58 -
lib/OpenLayers/Control/DragFeature.js
old new 95 95 lastPixel: null, 96 96 97 97 /** 98 * APIProperty: handlerOptions 99 * {Object} Used to set non-default properties on the control's handler 100 */ 101 handlerOptions: null, 102 103 /** 98 104 * Constructor: OpenLayers.Control.DragFeature 99 105 * Create a new control to drag features. 100 106 * … … 115 121 up: this.upFeature, 116 122 out: this.cancel, 117 123 done: this.doneDragging 118 }, this.dragCallbacks) 124 }, this.dragCallbacks), this.handlerOptions 119 125 ), 120 126 feature: new OpenLayers.Handler.Feature( 121 127 this, this.layer, OpenLayers.Util.extend({ 122 128 over: this.overFeature, 123 129 out: this.outFeature 124 130 }, this.featureCallbacks), 125 {geometryTypes: this.geometryTypes} 131 OpenLayers.Util.extend({geometryTypes: this.geometryTypes}, 132 this.handlerOptions) 126 133 ) 127 134 }; 128 135 }, 129 136 130 137 /** 131 138 * APIMethod: destroy 132 139 * Take care of things that are not handled in superclass … … 178 185 this.feature = feature; 179 186 this.handlers.drag.activate(); 180 187 this.over = true; 181 // TBD replace with CSS classes182 this.map.div.style.cursor = "move";183 188 } else { 184 189 if(this.feature.id == feature.id) { 185 190 this.over = true; … … 230 235 if(!this.over) { 231 236 this.handlers.drag.deactivate(); 232 237 this.feature = null; 233 // TBD replace with CSS classes234 this.map.div.style.cursor = "default";235 } else {236 // the drag handler itself resetted the cursor, so237 // set it back to "move" here238 this.map.div.style.cursor = "move";239 238 } 240 239 }, 241 240 … … 262 261 if(!this.handlers.drag.dragging) { 263 262 this.over = false; 264 263 this.handlers.drag.deactivate(); 265 // TBD replace with CSS classes266 this.map.div.style.cursor = "default";267 264 this.feature = null; 268 265 } else { 269 266 if(this.feature.id == feature.id) { -
lib/OpenLayers/Control/ZoomBox.js
old new 25 25 * {Boolean} Should the control be used for zooming out? 26 26 */ 27 27 out: false, 28 29 /** 30 * APIProperty: handlerOptions 31 * {Object} Used to set non-default properties on the control's handler 32 */ 33 handlerOptions: null, 28 34 29 35 /** 30 36 * Method: draw 31 37 */ 32 38 draw: function() { 33 39 this.handler = new OpenLayers.Handler.Box( this, 34 {done: this.zoomBox}, {keyMask: this.keyMask} ); 40 {done: this.zoomBox}, 41 OpenLayers.Util.extend({keyMask: this.keyMask}, 42 this.handlerOptions) ); 35 43 }, 36 44 37 45 /** -
lib/OpenLayers/Handler/Box.js
old new 83 83 this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1; 84 84 this.map.viewPortDiv.appendChild(this.zoomBox); 85 85 86 // TBD: use CSS classes instead 87 this.map.div.style.cursor = "crosshair"; 86 this.setCursor('startBox'); 88 87 }, 89 88 90 89 /** … … 133 132 } 134 133 this.removeBox(); 135 134 136 // TBD: use CSS classes instead 137 this.map.div.style.cursor = ""; 135 this.setCursor('endBox'); 138 136 139 137 this.callback("done", [result]); 140 138 }, -
lib/OpenLayers/Handler/Feature.js
old new 268 268 * type - {String} 269 269 */ 270 270 triggerCallback: function(type, mode, args) { 271 this.setCursor(mode); 271 272 var key = this.EVENTMAP[type][mode]; 272 273 if(key) { 273 274 if(type == 'click' && mode == 'out' && this.up && this.down) { -
lib/OpenLayers/Handler/Drag.js
old new 177 177 this.started = true; 178 178 this.start = evt.xy; 179 179 this.last = evt.xy; 180 // TBD replace with CSS classes 181 this.map.div.style.cursor = "move"; 180 this.setCursor(evt.type); 182 181 this.down(evt); 183 182 this.callback("down", [evt.xy]); 184 183 OpenLayers.Event.stop(evt); … … 247 246 var dragged = (this.start != this.last); 248 247 this.started = false; 249 248 this.dragging = false; 250 // TBD replace with CSS classes 251 this.map.div.style.cursor = ""; 249 this.setCursor(evt.type); 252 250 this.up(evt); 253 251 this.callback("up", [evt.xy]); 254 252 if(dragged) { … … 274 272 var dragged = (this.start != this.last); 275 273 this.started = false; 276 274 this.dragging = false; 277 // TBD replace with CSS classes 278 this.map.div.style.cursor = ""; 275 this.setCursor(evt.type); 279 276 this.out(evt); 280 277 this.callback("out", []); 281 278 if(dragged) { -
examples/cursor.html
old new 1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> 4 <style type="text/css"> 5 #map { 6 width: 800px; 7 height: 475px; 8 border: 1px solid black; 9 } 10 11 .olControlPanel { 12 cursor: pointer; 13 } 14 15 .olControlPanel div { 16 display:block; 17 width: 30px; 18 height: 30px; 19 margin: 5px; 20 } 21 22 .olControlZoomOutBoxCursor { 23 cursor: url(../theme/default/img/zoomout.cur) 9 9, url(../theme/default/img/zoomout.cur), auto; 24 } 25 26 .olControlDragPanCursor { 27 cursor: url(../theme/default/img/pan.cur) 16 16, url(../theme/default/img/pan.cur), auto; 28 } 29 30 .olControlZoomInBoxCursor { 31 cursor: url(../theme/default/img/zoomin.cur) 9 9, url(../theme/default/img/zoomin.cur), auto; 32 } 33 34 .dragMouseDown { 35 cursor: url(../theme/default/img/pandown.cur) 16 16, url(../theme/default/img/pandown.cur), auto; 36 } 37 38 .dragMouseUp { 39 cursor: url(../theme/default/img/pan.cur) 16 16, url(../theme/default/img/pan.cur), auto; 40 } 41 42 .crossHair { 43 cursor: crosshair; 44 } 45 46 .overFeature { 47 cursor: move; 48 } 49 50 .outFeature { 51 cursor: default; 52 } 53 54 .olControlPanel .olControlDrawPolygonItemActive { 55 border: 1px red solid; 56 background-color: #EEEEEE; 57 background-image: url("../theme/default/img/draw_polygon_on.png"); 58 background-repeat: no-repeat; 59 background-position: center; 60 } 61 62 .olControlPanel .olControlDrawPolygonItemInactive { 63 background-color: #EEEEEE; 64 background-image: url("../theme/default/img/draw_polygon_on.png"); 65 background-repeat: no-repeat; 66 background-position: center; 67 } 68 69 .olControlPanel .olControlDragFeatureItemActive { 70 border: 1px red solid; 71 background-color: #EEEEEE; 72 background-image: url("../theme/default/img/move_feature_on.png"); 73 background-repeat: no-repeat; 74 background-position: center; 75 } 76 77 .olControlPanel .olControlDragFeatureItemInactive { 78 background-color: #EEEEEE; 79 background-image: url("../theme/default/img/move_feature_on.png"); 80 background-repeat: no-repeat; 81 background-position: center; 82 } 83 84 .olControlPanel .olControlDragPanItemActive { 85 border: 1px red solid; 86 background-color: #EEEEEE; 87 background-image: url("../theme/default/img/pan_on.png"); 88 background-repeat: no-repeat; 89 background-position: center; 90 } 91 92 .olControlPanel .olControlDragPanItemInactive { 93 background-color: #EEEEEE; 94 background-image: url("../theme/default/img/pan_on.png"); 95 background-repeat: no-repeat; 96 background-position: center; 97 } 98 99 .olControlPanel .olControlZoomBoxItemInactive { 100 background-color: #EEEEEE; 101 background-image: url("../theme/default/img/icn-zoomin.png"); 102 background-repeat: no-repeat; 103 background-position: center; 104 } 105 106 .olControlPanel .olControlZoomBoxItemActive { 107 border: 1px red solid; 108 background-color: #EEEEEE; 109 background-image: url("../theme/default/img/icn-zoomin.png"); 110 background-repeat: no-repeat; 111 background-position: center; 112 } 113 114 .olControlPanel .olControlZoomOutBoxItemActive { 115 border: 1px red solid; 116 background-color: #EEEEEE; 117 background-image: url("../theme/default/img/icn-zoomout.png"); 118 background-repeat: no-repeat; 119 background-position: center; 120 } 121 122 .olControlPanel .olControlZoomOutBoxItemInactive { 123 background-color: #EEEEEE; 124 background-image: url("../theme/default/img/icn-zoomout.png"); 125 background-repeat: no-repeat; 126 background-position: center; 127 } 128 129 </style> 130 <script src="../lib/Firebug/firebug.js"></script> 131 <script src="../lib/OpenLayers.js"></script> 132 <script type="text/javascript"> 133 var lon = 5; 134 var lat = 40; 135 var zoom = 5; 136 var map, layer; 137 138 function init(){ 139 map = new OpenLayers.Map( 'map', { controls: [] } ); 140 layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", 141 "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); 142 map.addLayer(layer); 143 144 vlayer = new OpenLayers.Layer.Vector( "Editable" ); 145 map.addLayer(vlayer); 146 147 var zb = new OpenLayers.Control.ZoomBox( 148 {title:"Zoom in box", cursorClass: 'olControlZoomInBoxCursor'}); 149 var panel = new OpenLayers.Control.Panel({defaultControl: zb}); 150 panel.addControls([ 151 zb, 152 new OpenLayers.Control.ZoomBox({title:"Zoom out box", displayClass: 153 'olControlZoomOutBox', out: true, cursorClass: 'olControlZoomOutBoxCursor', 154 handlerOptions: {cursorClasses: {'startBox': 'crossHair', 'endBox': 'olControlZoomOutBoxCursor' } } }), 155 new OpenLayers.Control.DragPan({title:'Drag map', 156 cursorClass: 'olControlDragPanCursor', 157 handlerOptions: {cursorClasses: {'mousedown': 'dragMouseDown', 'mouseup': 'dragMouseUp' } } }), 158 new OpenLayers.Control.DrawFeature(vlayer, OpenLayers.Handler.Polygon, {title: "Draw polygon", cursorClass: 'crossHair', displayClass: 'olControlDrawPolygon'}), 159 new OpenLayers.Control.DragFeature(vlayer, {title: "Drag polygon", displayClass: 'olControlDragFeature', 160 handlerOptions: {cursorClasses: {'in': 'overFeature', 'out': 'outFeature'} } }) 161 ]); 162 map.addControl(panel); 163 164 map.setCenter(new OpenLayers.LonLat(lon, lat), zoom); 165 } 166 </script> 167 </head> 168 <body onload="init()"> 169 <h3 id="title">Custom cursors for controls</h3> 170 <p id="shortdesc"> 171 Use custom cursors for your controls. 172 </p> 173 <div id="map"></div> 174 </body> 175 </html>
