Files
openlayers/lib/OpenLayers/Icon.js
crschmidt d9a99143af Resolve #609 with Schuyler looking over my shoulder for review. Instead of
always having a calculateOffset (which overwrites offset if it exists), 
always have an offset. If people want calculateOffset, they can add it. 
Reported by David Bitner.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@2962 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2007-04-02 00:39:53 +00:00

137 lines
3.7 KiB
JavaScript

/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */
/**
* @class
*/
OpenLayers.Icon = OpenLayers.Class.create();
OpenLayers.Icon.prototype = {
/** image url
* @type String */
url: null,
/** @type OpenLayers.Size */
size:null,
/** distance in pixels to offset the image when being rendered
* @type OpenLayers.Pixel */
offset: null,
/** Function to calculate the offset (based on the size)
* @type OpenLayers.Pixel */
calculateOffset: null,
/** @type DOMElement */
imageDiv: null,
/** @type OpenLayers.Pixel */
px: null,
/**
* @constructor
*
* @param {String} url
* @param {OpenLayers.Size} size
* @param {Function} calculateOffset
*/
initialize: function(url, size, offset, calculateOffset) {
this.url = url;
this.size = (size) ? size : new OpenLayers.Size(20,20);
this.offset = offset ? offset : new OpenLayers.Pixel(-(size.w/2), -(size.h/2));
this.calculateOffset = calculateOffset;
var id = OpenLayers.Util.createUniqueID("OL_Icon_");
this.imageDiv = OpenLayers.Util.createAlphaImageDiv(id);
},
destroy: function() {
OpenLayers.Event.stopObservingElement(this.imageDiv.firstChild);
this.imageDiv.innerHTML = "";
this.imageDiv = null;
},
/**
* @returns A fresh copy of the icon.
* @type OpenLayers.Icon
*/
clone: function() {
return new OpenLayers.Icon(this.url,
this.size,
this.offset,
this.calculateOffset);
},
/**
* @param {OpenLayers.Size} size
*/
setSize: function(size) {
if (size != null) {
this.size = size;
}
this.draw();
},
/**
* @param {OpenLayers.Pixel} px
*
* @return A new DOM Image of this icon set at the location passed-in
* @type DOMElement
*/
draw: function(px) {
OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv,
null,
null,
this.size,
this.url,
"absolute");
this.moveTo(px);
return this.imageDiv;
},
/** Change the icon's opacity
* @param {float} opacity
*/
setOpacity: function(opacity) {
OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, null, null,
null, null, null, null, opacity);
},
/**
* @param {OpenLayers.Pixel} px
*/
moveTo: function (px) {
//if no px passed in, use stored location
if (px != null) {
this.px = px;
}
if (this.imageDiv != null) {
if (this.px == null) {
this.display(false);
} else {
if (this.calculateOffset) {
this.offset = this.calculateOffset(this.size);
}
var offsetPx = this.px.offset(this.offset);
OpenLayers.Util.modifyAlphaImageDiv(this.imageDiv, null, offsetPx);
}
}
},
/** Hide or show the icon
*
* @param {Boolean} display
*/
display: function(display) {
this.imageDiv.style.display = (display) ? "" : "none";
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Icon"
};