From e44aa1e14a236bfdc1636c68fa0e49f73ff3df8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 18 Dec 2013 22:06:05 +0100 Subject: [PATCH] Make ol.style.Image a base class for Icon and Circle --- src/ol/style/imagestyle.js | 104 +++++++------------------------------ 1 file changed, 20 insertions(+), 84 deletions(-) diff --git a/src/ol/style/imagestyle.js b/src/ol/style/imagestyle.js index 98087746b9..3c36309c3a 100644 --- a/src/ol/style/imagestyle.js +++ b/src/ol/style/imagestyle.js @@ -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.} - */ - 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;