/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the * full text of the license. */ /** * @requires OpenLayers/Control.js * @requires OpenLayers/Feature/Vector.js */ /** * Class: OpenLayers.Control.SelectFeature * Selects vector features from a given layer on click or hover. * * Inherits from: * - */ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, { /** * Property: multipleKey * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets * the property to true. Default is null. */ multipleKey: null, /** * Property: toggleKey * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets * the property to true. Default is null. */ toggleKey: null, /** * APIProperty: multiple * {Boolean} Allow selection of multiple geometries. Default is false. */ multiple: false, /** * APIProperty: clickout * {Boolean} Unselect features when clicking outside any feature. * Default is true. */ clickout: true, /** * APIProperty: toggle * {Boolean} Unselect a selected feature on click. Default is false. Only * has meaning if hover is false. */ toggle: false, /** * APIProperty: hover * {Boolean} Select on mouse over and deselect on mouse out. If true, this * ignores clicks and only listens to mouse moves. */ hover: false, /** * APIProperty: onSelect * {Function} Optional function to be called when a feature is selected. * The function should expect to be called with a feature. */ onSelect: function() {}, /** * APIProperty: onUnselect * {Function} Optional function to be called when a feature is unselected. * The function should expect to be called with a feature. */ onUnselect: function() {}, /** * APIProperty: geometryTypes * {Array(String)} To restrict selecting to a limited set of geometry types, * send a list of strings corresponding to the geometry class names. */ geometryTypes: null, /** * Property: layer * {} */ layer: null, /** * APIProperty: callbacks * {Object} The functions that are sent to the handler for callback */ callbacks: null, /** * APIProperty: selectStyle * {Object} Hash of styles */ selectStyle: OpenLayers.Feature.Vector.style['select'], /** * Property: handler * {} */ handler: null, /** * Constructor: * * Parameters: * layer - {} * options - {Object} */ initialize: function(layer, options) { OpenLayers.Control.prototype.initialize.apply(this, [options]); this.layer = layer; this.callbacks = OpenLayers.Util.extend({ click: this.clickFeature, clickout: this.clickoutFeature, over: this.overFeature, out: this.outFeature }, this.callbacks); var handlerOptions = { geometryTypes: this.geometryTypes}; this.handler = new OpenLayers.Handler.Feature(this, layer, this.callbacks, handlerOptions); }, /** * Method: unselectAll * Unselect all selected features. */ unselectAll: function() { // we'll want an option to supress notification here while (this.layer.selectedFeatures.length > 0) { this.unselect(this.layer.selectedFeatures[0]); } }, /** * Method: clickFeature * Called on click in a feature * Only responds if this.hover is false. * * Parameters: * feature - {} */ clickFeature: function(feature) { if(this.hover) { return; } var selected = (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) > -1); if(!this.multiple && !this.handler.evt[this.multipleKey]) { // perhaps an "except" argument this.unselectAll(); } if(selected) { if(this.toggle || this.handler.evt[this.toggleKey]) { // notify here this.unselect(feature); } else { // don't notify here - could be removed if unselectAll is modified this.select(feature); } } else { this.select(feature); } }, /** * Method: clickoutFeature * Called on click outside a previously clicked (selected) feature. * Only responds if this.hover is false. * * Parameters: * feature - {} */ clickoutFeature: function(feature) { if(!this.hover && this.clickout) { this.unselectAll(); } }, /** * Method: overFeature * Called on over a feature. * Only responds if this.hover is true. * * Parameters: * feature - {} */ overFeature: function(feature) { if(this.hover && (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) == -1)) { this.select(feature); } }, /** * Method: outFeature * Called on out of a selected feature. * Only responds if this.hover is true. * * Parameters: * feature - {} */ outFeature: function(feature) { if(this.hover) { this.unselect(feature); } }, /** * Method: select * Add feature to the layer's selectedFeature array, render the feature as * selected, and call the onSelect function. * * Parameters: * feature - {} */ select: function(feature) { // Store feature style for restoration later if(feature.originalStyle == null) { feature.originalStyle = feature.style; } this.layer.selectedFeatures.push(feature); feature.style = this.selectStyle; this.layer.drawFeature(feature); this.onSelect(feature); }, /** * Method: unselect * Remove feature from the layer's selectedFeature array, render the feature as * normal, and call the onUnselect function. * * Parameters: * feature - {} */ unselect: function(feature) { // Store feature style for restoration later if(feature.originalStyle != null) { feature.style = feature.originalStyle; } this.layer.drawFeature(feature); OpenLayers.Util.removeItem(this.layer.selectedFeatures, feature); this.onUnselect(feature); }, /** * Method: setMap * Set the map property for the control. * * Parameters: * map - {} */ setMap: function(map) { this.handler.setMap(map); OpenLayers.Control.prototype.setMap.apply(this, arguments); }, CLASS_NAME: "OpenLayers.Control.SelectFeature" });