Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature change layer color2 #14

Merged
merged 8 commits into from
Jun 23, 2017
86 changes: 64 additions & 22 deletions src/ol3/OL3.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ define([
visible : "visibility",
opacity : "opacity",
zIndex : "position",
grayScaled : "grayScaled",
minResolution : "maxZoom",
maxResolution : "minZoom"
} ;
Expand Down Expand Up @@ -2487,28 +2488,53 @@ define([
map.logger.trace("[OL3] listen : abonnement layerProperty : " + obsProperty) ;
this.libMap.getLayers().forEach(function (olLayer,i,array) {
var layerOpts = this._getLayerOpts(olLayer) ;
olEventKey = olLayer.on(
"change:" + obsProperty,
function (ol3evt) {
// la fonction _getCommonLayerParams permet de faire la conversion
// propriete ol3 => propriete commune
var oldOl3obj = {} ;
oldOl3obj[ol3evt.key] = ol3evt.oldValue ;
var oldCommonProp = map._getCommonLayerParams(oldOl3obj) ;
var newOl3obj = {} ;
newOl3obj[ol3evt.key] = this.get(ol3evt.key) ;
var newCommonProp = map._getCommonLayerParams(newOl3obj) ;
if ( obsProperty === "grayScaled" ) {

/** layerChangeGrayScaled callback */
var callBack = function (evt) {
action.call(context,{
property : OL3.LAYERPROPERTIES[ol3evt.key],
oldValue : oldCommonProp[OL3.LAYERPROPERTIES[ol3evt.key]],
newValue : newCommonProp[OL3.LAYERPROPERTIES[ol3evt.key]],
property : "grayScaled",
oldValue : evt.detail.oldValue,
newValue : evt.detail.newValue,
layerChanged : layerOpts
}) ;
},
olLayer
) ;
this._registerEvent(olEventKey,eventId,action,context) ;
olEventKey = null ;
};

var gpEventKey = {
type : "change:grayScaled",
handler : callBack,
target : olLayer
};

gpEventKey.target.addEventListener(gpEventKey.type, gpEventKey.handler);

this._registerEvent(gpEventKey, eventId, action, context) ;

} else {
olEventKey = olLayer.on(
"change:" + obsProperty,
function (ol3evt) {
// la fonction _getCommonLayerParams permet de faire la conversion
// propriete ol3 => propriete commune
var oldOl3obj = {} ;
oldOl3obj[ol3evt.key] = ol3evt.oldValue ;
var oldCommonProp = map._getCommonLayerParams(oldOl3obj) ;
var newOl3obj = {} ;
newOl3obj[ol3evt.key] = this.get(ol3evt.key) ;
var newCommonProp = map._getCommonLayerParams(newOl3obj) ;
action.call(context,{
property : OL3.LAYERPROPERTIES[ol3evt.key],
oldValue : oldCommonProp[OL3.LAYERPROPERTIES[ol3evt.key]],
newValue : newCommonProp[OL3.LAYERPROPERTIES[ol3evt.key]],
layerChanged : layerOpts
}) ;
},
olLayer
) ;

this._registerEvent(olEventKey,eventId,action,context) ;
olEventKey = null ;
}
},
map) ;
}
Expand Down Expand Up @@ -2563,10 +2589,16 @@ define([
var evKey = null ;
for (var i = 0 ; i < rEvents.length ; i ++) {
if (rEvents[i].action == action) {
evKey = rEvents[i].key ;
evKey = rEvents[i].key;
rEvents.splice(i,1) ;
this.logger.trace("[OL3] : forgetting : " + eventId + " (" + evKey + ")") ;
ol.Observable.unByKey(evKey) ;

if ( evKey.type === "change:grayScaled" ) {
evKey.target.removeEventListener(evKey.type, evKey.handler);
} else {
ol.Observable.unByKey(evKey) ;
}

// on decale i d'un cran en arriere pour ne pas sauter d'elements
i -= 1 ;
}
Expand Down Expand Up @@ -2598,6 +2630,17 @@ define([
}

this._colorGrayscaleLayerSwitch(gpLayer,toGrayScale);

var event = new CustomEvent(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utiliser le polyfill IMap.CustomEvent() pour construire l'événement pour assurer la compatibilité IE)

"change:grayScaled",
{
detail : {
oldValue : !toGrayScale,
newValue : toGrayScale
}
}
);
gpLayer.obj.dispatchEvent(event);
};

/**
Expand Down Expand Up @@ -2697,7 +2740,6 @@ define([
source.loadstartListenerKey = null;
source.loadendListenerKey = null;
}
gpLayer.options.grayScaled = toGrayScale;

// maj du cache
source.refresh();
Expand Down