Replace source ready flag with loading/ready/error enum

This commit is contained in:
Tom Payne
2013-09-24 15:22:23 +02:00
parent da84dd9253
commit 167b309242
12 changed files with 114 additions and 78 deletions
+8 -8
View File
@@ -68,17 +68,17 @@ ol.layer.Layer.prototype.getSource = function() {
};
/**
* @inheritDoc
*/
ol.layer.Layer.prototype.getSourceState = function() {
return this.getSource().getState();
};
/**
* @private
*/
ol.layer.Layer.prototype.handleSourceChange_ = function() {
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
ol.layer.Layer.prototype.isReady = function() {
return this.getSource().isReady();
};
+12 -11
View File
@@ -7,6 +7,7 @@ goog.require('goog.events.EventType');
goog.require('goog.math');
goog.require('goog.object');
goog.require('ol.Object');
goog.require('ol.source.State');
/**
@@ -29,8 +30,8 @@ ol.layer.LayerProperty = {
* contrast: number,
* hue: number,
* opacity: number,
* ready: boolean,
* saturation: number,
* sourceState: ol.source.State,
* visible: boolean,
* maxResolution: number,
* minResolution: number}}
@@ -142,8 +143,8 @@ ol.layer.Base.prototype.getLayerState = function() {
var contrast = this.getContrast();
var hue = this.getHue();
var opacity = this.getOpacity();
var ready = this.isReady();
var saturation = this.getSaturation();
var sourceState = this.getSourceState();
var visible = this.getVisible();
var maxResolution = this.getMaxResolution();
var minResolution = this.getMinResolution();
@@ -152,8 +153,8 @@ ol.layer.Base.prototype.getLayerState = function() {
contrast: goog.isDef(contrast) ? Math.max(contrast, 0) : 1,
hue: goog.isDef(hue) ? hue : 0,
opacity: goog.isDef(opacity) ? goog.math.clamp(opacity, 0, 1) : 1,
ready: ready,
saturation: goog.isDef(saturation) ? Math.max(saturation, 0) : 1,
sourceState: sourceState,
visible: goog.isDef(visible) ? !!visible : true,
maxResolution: goog.isDef(maxResolution) ? maxResolution : Infinity,
minResolution: goog.isDef(minResolution) ? Math.max(minResolution, 0) : 0
@@ -232,6 +233,12 @@ goog.exportProperty(
ol.layer.Base.prototype.getSaturation);
/**
* @return {ol.source.State} Source state.
*/
ol.layer.Base.prototype.getSourceState = goog.abstractMethod;
/**
* @return {boolean} Visible.
*/
@@ -248,7 +255,7 @@ goog.exportProperty(
* @protected
*/
ol.layer.Base.prototype.handleLayerChange = function() {
if (this.getVisible() && this.isReady()) {
if (this.getVisible() && this.getSourceState() == ol.source.State.READY) {
this.dispatchChangeEvent();
}
};
@@ -258,18 +265,12 @@ ol.layer.Base.prototype.handleLayerChange = function() {
* @protected
*/
ol.layer.Base.prototype.handleLayerVisibleChange = function() {
if (this.isReady()) {
if (this.getSourceState() == ol.source.State.READY) {
this.dispatchChangeEvent();
}
};
/**
* @return {boolean} Is ready.
*/
ol.layer.Base.prototype.isReady = goog.abstractMethod;
/**
* Adjust the layer brightness. A value of -1 will render the layer completely
* black. A value of 0 will leave the brightness unchanged. A value of 1 will
+29 -5
View File
@@ -11,6 +11,7 @@ goog.require('ol.CollectionEvent');
goog.require('ol.CollectionEventType');
goog.require('ol.Object');
goog.require('ol.layer.Base');
goog.require('ol.source.State');
/**
@@ -205,6 +206,7 @@ ol.layer.Group.prototype.getLayerStatesArray = function(opt_obj) {
layerState.hue += ownLayerState.hue;
layerState.opacity *= ownLayerState.opacity;
layerState.saturation *= ownLayerState.saturation;
layerState.sourceState = this.getSourceState();
layerState.visible = layerState.visible && ownLayerState.visible;
layerState.maxResolution = Math.min(
layerState.maxResolution, ownLayerState.maxResolution);
@@ -219,9 +221,31 @@ ol.layer.Group.prototype.getLayerStatesArray = function(opt_obj) {
/**
* @inheritDoc
*/
ol.layer.Group.prototype.isReady = function() {
return null === goog.array.find(
this.getLayers().getArray(), function(elt, index, array) {
return !elt.isReady();
});
ol.layer.Group.prototype.getSourceState = function() {
// Return the layer group's source state based on the best source state of its
// children:
// - if any child is READY, return READY
// - otherwise, if any child is LOADING, return LOADING
// - otherwise, all children must be in ERROR, return ERROR
// - otherwise, there are no children, return READY
var layerSourceStates = [0, 0, 0];
var layers = this.getLayers().getArray();
var n = layers.length;
var i;
for (i = 0; i < n; ++i) {
var layerSourceState = layers[i].getSourceState();
goog.asserts.assert(layerSourceState < layerSourceStates.length);
++layerSourceStates[layerSourceState];
}
if (layerSourceStates[ol.source.State.READY]) {
return ol.source.State.READY;
} else if (layerSourceStates[ol.source.State.LOADING]) {
return ol.source.State.LOADING;
} else if (layerSourceStates[ol.source.State.ERROR]) {
goog.asserts.assert(layerSourceStates[ol.source.State.ERROR] == n);
return ol.source.State.ERROR;
} else {
goog.asserts.assert(n === 0);
return ol.source.State.READY;
}
};