From 6cede08afbd422d262863ea3b015002399ab6edd Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 12 Jun 2019 11:51:18 +0200 Subject: [PATCH] Add WMS layer support for the disclaimer service --- contribs/gmf/src/disclaimer/component.js | 115 ++++++++++++------ .../gmf/src/layertree/SyncLayertreeMap.js | 12 +- src/message/Disclaimer.js | 26 +++- src/message/Message.js | 2 +- 4 files changed, 109 insertions(+), 46 deletions(-) diff --git a/contribs/gmf/src/disclaimer/component.js b/contribs/gmf/src/disclaimer/component.js index 8ec1402143b2..99f0c896ee64 100644 --- a/contribs/gmf/src/disclaimer/component.js +++ b/contribs/gmf/src/disclaimer/component.js @@ -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: * @@ -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); } } }; @@ -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); } }; @@ -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) { @@ -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 }); @@ -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; @@ -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 }); diff --git a/contribs/gmf/src/layertree/SyncLayertreeMap.js b/contribs/gmf/src/layertree/SyncLayertreeMap.js index 2fe128ef8fe5..93a5f6cb26c5 100644 --- a/contribs/gmf/src/layertree/SyncLayertreeMap.js +++ b/contribs/gmf/src/layertree/SyncLayertreeMap.js @@ -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 */ @@ -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); } }; diff --git a/src/message/Disclaimer.js b/src/message/Disclaimer.js index 96c7004a9555..243cd99027c1 100644 --- a/src/message/Disclaimer.js +++ b/src/message/Disclaimer.js @@ -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 @@ -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.} + * @param {string|Message|Array.} * object A message or list of messages as text or configuration objects. */ alert(object) { @@ -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.} + * @param {string|Message|Array.} * object A message or list of messages as text or configuration objects. */ close(object) { @@ -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 */ @@ -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) { diff --git a/src/message/Message.js b/src/message/Message.js index af44f9bfa31d..fee5cea3ca4b 100644 --- a/src/message/Message.js +++ b/src/message/Message.js @@ -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. */