Make ol.style.Image a base class for Icon and Circle

This commit is contained in:
Éric Lemoine
2013-12-18 22:06:05 +01:00
parent c786b91f3c
commit e44aa1e14a

View File

@@ -1,10 +1,7 @@
// FIXME decide default value for snapToPixel
goog.provide('ol.style.Image');
goog.provide('ol.style.ImageState');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType');
@@ -21,15 +18,24 @@ ol.style.ImageState = {
};
/**
* @typedef {{anchor: ol.Pixel,
* imageState: ol.style.ImageState,
* rotation: number,
* size: ol.Size,
* snapToPixel: (boolean|undefined),
* subtractViewRotation: boolean}}
*/
ol.style.ImageOptions;
/**
* @constructor
* @param {olx.style.ImageOptions=} opt_options Options.
* @param {ol.style.ImageOptions} options Options.
* @extends {goog.events.EventTarget}
*/
ol.style.Image = function(opt_options) {
var options = goog.isDef(opt_options) ? opt_options : {};
ol.style.Image = function(options) {
goog.base(this);
@@ -38,20 +44,10 @@ ol.style.Image = function(opt_options) {
*/
this.anchor = options.anchor;
/**
* @type {HTMLCanvasElement|HTMLVideoElement|Image}
*/
this.image = goog.isDefAndNotNull(options.image) ?
options.image : new Image();
if (!goog.isNull(options.crossOrigin)) {
this.image.crossOrigin = options.crossOrigin;
}
/**
* @type {ol.style.ImageState}
*/
this.imageState = goog.isDef(options.imageState) ?
options.imageState : ol.style.ImageState.IDLE;
this.imageState = options.imageState;
/**
* @type {number|undefined}
@@ -73,86 +69,26 @@ ol.style.Image = function(opt_options) {
*/
this.subtractViewRotation = options.subtractViewRotation;
/**
* @private
* @type {Array.<number>}
*/
this.imageListenerKeys_ = null;
/**
* @private
* @type {string|undefined}
*/
this.src_ = options.src;
};
goog.inherits(ol.style.Image, goog.events.EventTarget);
/**
* @private
* @protected
*/
ol.style.Image.prototype.dispatchChangeEvent_ = function() {
ol.style.Image.prototype.dispatchChangeEvent = function() {
this.dispatchEvent(goog.events.EventType.CHANGE);
};
/**
* Tracks loading or read errors.
*
* @private
* @param {number} pixelRatio Pixel ratio.
* @return {HTMLCanvasElement|HTMLVideoElement|Image} Image element.
*/
ol.style.Image.prototype.handleImageError_ = function() {
this.imageState = ol.style.ImageState.ERROR;
this.unlistenImage_();
this.dispatchChangeEvent_();
};
/**
* Tracks successful image load.
*
* @private
*/
ol.style.Image.prototype.handleImageLoad_ = function() {
this.imageState = ol.style.ImageState.LOADED;
if (goog.isNull(this.size)) {
this.size = [this.image.width, this.image.height];
}
if (goog.isNull(this.anchor)) {
this.anchor = [this.size[0] / 2, this.size[1] / 2];
}
this.unlistenImage_();
this.dispatchChangeEvent_();
};
ol.style.Image.prototype.getImage = goog.abstractMethod;
/**
* Load not yet loaded URI.
*/
ol.style.Image.prototype.load = function() {
if (this.imageState == ol.style.ImageState.IDLE) {
goog.asserts.assert(goog.isDef(this.src_));
goog.asserts.assert(goog.isNull(this.imageListenerKeys_));
this.imageState = ol.style.ImageState.LOADING;
this.imageListenerKeys_ = [
goog.events.listenOnce(this.image, goog.events.EventType.ERROR,
this.handleImageError_, false, this),
goog.events.listenOnce(this.image, goog.events.EventType.LOAD,
this.handleImageLoad_, false, this)
];
this.image.src = this.src_;
}
};
/**
* Discards event handlers which listen for load completion or errors.
*
* @private
*/
ol.style.Image.prototype.unlistenImage_ = function() {
goog.asserts.assert(!goog.isNull(this.imageListenerKeys_));
goog.array.forEach(this.imageListenerKeys_, goog.events.unlistenByKey);
this.imageListenerKeys_ = null;
};
ol.style.Image.prototype.load = goog.abstractMethod;