Skip to content

Commit

Permalink
Add WMS layer support for the disclaimer service
Browse files Browse the repository at this point in the history
  • Loading branch information
fredj committed Jun 21, 2019
1 parent 247533e commit 6cede08
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 46 deletions.
115 changes: 79 additions & 36 deletions contribs/gmf/src/disclaimer/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ const module = angular.module('gmfDisclaimer', [
]);


/**
*
* @param {import("ol/layer/Base.js").default} layer Layer
* @param {function(string):void} func Function
*/
function forEachDisclaimer(layer, func) {
const disclaimers = layer.get('disclaimers');
if (disclaimers && Array.isArray(disclaimers)) {
disclaimers.forEach(func);
}
}


/**
* Used metadata:
*
Expand Down Expand Up @@ -219,30 +206,23 @@ DisclaimerController.prototype.registerLayer_ = function(layer) {

if (this.layerVisibility) {
// Show disclaimer messages for this layer
forEachDisclaimer(layer, (disclaimer) => {
if (layer.getVisible()) {
this.showDisclaimerMessage_(disclaimer);
}
});
if (layer.getVisible()) {
this.update_(layer);
} else {
this.closeAll_(layer);
}

const listenerKey = olEvents.listen(layer, 'change:visible', (event) => {
const layer = event.target;
const listenerKey = olEvents.listen(layer, 'change', (event) => {
if (layer.getVisible()) {
forEachDisclaimer(layer, (disclaimer) => {
this.showDisclaimerMessage_(disclaimer);
});
this.update_(layer);
} else {
forEachDisclaimer(layer, (disclaimer) => {
this.closeDisclaimerMessage_(disclaimer);
});
this.closeAll_(layer);
}
});
this.eventHelper_.addListenerKey(layerUid, listenerKey);
} else {
// Show disclaimer messages for this layer
forEachDisclaimer(layer, (disclaimer) => {
this.showDisclaimerMessage_(disclaimer);
});
this.showAll_(layer);
}
}
};
Expand All @@ -265,11 +245,8 @@ DisclaimerController.prototype.unregisterLayer_ = function(layer) {
layer.getLayers().forEach(layer => this.unregisterLayer_(layer));

} else {

// Close disclaimer messages for this layer
forEachDisclaimer(layer, (disclaimer) => {
this.closeDisclaimerMessage_(disclaimer);
});
// Close all disclaimer messages for this layer
this.closeAll_(layer);
}

};
Expand All @@ -281,10 +258,11 @@ DisclaimerController.prototype.$onDestroy = function() {


/**
* @param {string} layerUid Layer identifier.
* @param {string} msg Disclaimer message.
* @private
*/
DisclaimerController.prototype.showDisclaimerMessage_ = function(msg) {
DisclaimerController.prototype.showDisclaimerMessage_ = function(layerUid, msg) {
msg = this.gettextCatalog_.getString(msg);
if (this.external) {
if (this.msgs_.indexOf(msg) < 0) {
Expand All @@ -296,6 +274,7 @@ DisclaimerController.prototype.showDisclaimerMessage_ = function(msg) {
this.disclaimer_.alert({
popup: this.popup,
msg: msg,
layerUid: layerUid,
target: this.element_,
type: MessageType.WARNING
});
Expand All @@ -304,10 +283,73 @@ DisclaimerController.prototype.showDisclaimerMessage_ = function(msg) {


/**
* @param {import("ol/layer/Base.js").default} layer Layer
* @private
*/
DisclaimerController.prototype.closeAll_ = function(layer) {
const disclaimers = layer.get('disclaimers');
if (disclaimers) {
const layerUid = olUtilGetUid(layer);
for (const key in disclaimers) {
const uid = `${layerUid}-${key}`;
this.closeDisclaimerMessage_(uid, disclaimers[key]);
}
}
};


/**
* @param {import("ol/layer/Base.js").default} layer Layer
* @private
*/
DisclaimerController.prototype.showAll_ = function(layer) {
const disclaimers = layer.get('disclaimers');
if (disclaimers) {
const layerUid = olUtilGetUid(layer);
for (const key in disclaimers) {
const uid = `${layerUid}-${key}`;
this.showDisclaimerMessage_(uid, disclaimers[key]);
}
}
};


/**
* @param {import("ol/layer/Base.js").default} layer Layer
* @private
*/
DisclaimerController.prototype.update_ = function(layer) {
const disclaimers = layer.get('disclaimers');
if (disclaimers) {
if ('all' in disclaimers) {
// the disclaimer is for all the layers, WMS or WMTS.
console.assert(Object.keys(disclaimers).length === 1);
this.showAll_(layer);
} else {
const layerWMS = /** @type {import("ol/layer/Layer.js").default} */ (layer);
const sourceWMS = /** @type {import("ol/source/ImageWMS.js").default} */ (layerWMS.getSource());
if (sourceWMS.getParams) {
const layers = sourceWMS.getParams()['LAYERS'];
const layerUid = olUtilGetUid(layer);
for (const key in disclaimers) {
const uid = `${layerUid}-${key}`;
if (layers.indexOf(key) !== -1) {
this.showDisclaimerMessage_(uid, disclaimers[key]);
} else {
this.closeDisclaimerMessage_(uid, disclaimers[key]);
}
}
}
}
}
};

/**
* @param {string} layerUid Layer identifier.
* @param {string} msg Disclaimer message.
* @private
*/
DisclaimerController.prototype.closeDisclaimerMessage_ = function(msg) {
DisclaimerController.prototype.closeDisclaimerMessage_ = function(layerUid, msg) {
msg = this.gettextCatalog_.getString(msg);
if (this.external) {
this.visibility = false;
Expand All @@ -317,6 +359,7 @@ DisclaimerController.prototype.closeDisclaimerMessage_ = function(msg) {
this.disclaimer_.close({
popup: this.popup,
msg: msg,
layerUid: layerUid,
target: this.element_,
type: MessageType.WARNING
});
Expand Down
12 changes: 9 additions & 3 deletions contribs/gmf/src/layertree/SyncLayertreeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ SyncLayertreeMap.prototype.createWMTSLayer_ = function(gmfLayerWMTS) {

/**
* Update properties of a layer with the node of a given leafNode.
* @param {import('gmf/themes.js').GmfLayer} leafNode a leaf node.
* @param {import('gmf/themes.js').GmfLayer|import('gmf/themes.js').GmfLayerWMS} leafNode a leaf node.
* @param {import("ol/layer/Base.js").default} layer A layer.
* @private
*/
Expand All @@ -356,8 +356,14 @@ SyncLayertreeMap.prototype.updateLayerReferences_ = function(leafNode, layer) {

const disclaimer = leafNode.metadata.disclaimer;
if (disclaimer) {
const disclaimers = layer.get('disclaimers') || [];
disclaimers.push(leafNode.metadata.disclaimer);
const disclaimers = layer.get('disclaimers') || {};

// 'all' means that the disclaimer is for all the layer.
let layers = 'all';
if ('layers' in leafNode) {
layers = /** @type {import('gmf/themes.js').GmfLayerWMS} */ (leafNode).layers;
}
disclaimers[layers] = leafNode.metadata.disclaimer;
layer.set('disclaimers', disclaimers);
}
};
Expand Down
26 changes: 20 additions & 6 deletions src/message/Disclaimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import ngeoMessageMessage, {MessageType} from 'ngeo/message/Message.js';
import 'ngeo/sass/font.scss';


/**
* A message to display by the disclaimer service.
*
* @typedef {Object} Message
* @property {number} [delay=7000] The delay in milliseconds the message is shown
* @property {boolean} [popup=false] Whether the message should be displayed inside a popup window or not.
* @property {string} msg The message text to display.
* @property {string} layerUid The OpenLayers layer identifier.
* @property {JQuery|Element|string} [target] The target element (or selector to get the element) in which
* to display the message. If not defined, then the default target of the notification service is used.
* @property {string} [type='info'] The type of message.
*/


/**
* Provides methods to display any sort of messages, disclaimers, errors,
* etc. Requires Bootstrap library (both CSS and JS) to display the alerts
Expand Down Expand Up @@ -60,7 +74,7 @@ export class MessageDisclaimerService extends ngeoMessageMessage {
/**
* Show disclaimer message string or object or list of disclaimer message
* strings or objects.
* @param {string|import('ngeo/message/Message.js').Message|Array.<string|import('ngeo/message/Message.js').Message>}
* @param {string|Message|Array.<string|Message>}
* object A message or list of messages as text or configuration objects.
*/
alert(object) {
Expand All @@ -70,7 +84,7 @@ export class MessageDisclaimerService extends ngeoMessageMessage {
/**
* Close disclaimer message string or object or list of disclaimer message
* strings or objects.
* @param {string|import('ngeo/message/Message.js').Message|Array.<string|import('ngeo/message/Message.js').Message>}
* @param {string|Message|Array.<string|Message>}
* object A message or list of messages as text or configuration objects.
*/
close(object) {
Expand All @@ -80,7 +94,7 @@ export class MessageDisclaimerService extends ngeoMessageMessage {

/**
* Show the message.
* @param {import('ngeo/message/Message.js').Message} message Message.
* @param {Message} message Message.
* @protected
* @override
*/
Expand Down Expand Up @@ -166,17 +180,17 @@ export class MessageDisclaimerService extends ngeoMessageMessage {
}

/**
* @param {import('ngeo/message/Message.js').Message} message Message.
* @param {Message} message Message.
* @return {string} The uid.
* @private
*/
getMessageUid_(message) {
return `${message.msg}-${message.type}`;
return `${message.msg}-${message.type}-${message.layerUid}`;
}

/**
* Close the message.
* @param {import('ngeo/message/Message.js').Message} message Message.
* @param {Message} message Message.
* @protected
*/
closeMessage_(message) {
Expand Down
2 changes: 1 addition & 1 deletion src/message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @property {string} msg The message text to display.
* @property {JQuery|Element|string} [target] The target element (or selector to get the element) in which
* to display the message. If not defined, then the default target of the notification service is used.
* @property {string} [type] The type of message. Defaults to `info`.
* @property {string} [type='info'] The type of message.
*/


Expand Down

0 comments on commit 6cede08

Please sign in to comment.