From 33008fe81518564326b461d1c621104c4b27e43b Mon Sep 17 00:00:00 2001 From: Juri Strumpflohner Date: Thu, 24 Aug 2017 09:06:51 +0200 Subject: [PATCH] feat(olHelper): auto-detection of attribution (#382) This change tries to infer the attribution given the source configuration of the ol3 object. Apparently each OL3 source type might have different properties for storing the attribution. Thus the code needs to iterate over them and "search" for the presence of it. WARNING: This also slightly changes the default behavior, meaning while currently the attribution had to be enabled explicitly you now have to opt-out by setting the `source.attribution = false`. --- src/services/olHelpers.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/services/olHelpers.js b/src/services/olHelpers.js index 45bdd962..00020d29 100644 --- a/src/services/olHelpers.js +++ b/src/services/olHelpers.js @@ -680,11 +680,41 @@ angular.module('openlayers-directive').factory('olHelpers', function($q, $log, $ var createAttribution = function(source) { var attributions = []; if (isDefined(source.attribution)) { - attributions.unshift(new ol.Attribution({html: source.attribution})); + // opt-out -> default tries to show an attribution + if (!(source.attribution === false)) { // jshint ignore:line + // we got some HTML so display that as the attribution + attributions.unshift(new ol.Attribution({html: source.attribution})); + } + } else { + // try to infer automatically + var attrib = extractAttributionFromSource(source); + if (attrib) { + attributions.unshift(attrib); + } } + return attributions; }; + var extractAttributionFromSource = function(source) { + if (source && source.type) { + var ol3SourceInstance = ol.source[source.type]; + if (ol3SourceInstance) { + // iterate over the object's props and try + // to find the attribution one as it differs + for (var prop in ol3SourceInstance) { + if (ol3SourceInstance.hasOwnProperty(prop)) { + if (prop.toLowerCase().indexOf('attribution') > -1) { + return ol.source[source.type][prop]; + } + } + } + } + } + + return null; + }; + var createGroup = function(name) { var olGroup = new ol.layer.Group(); olGroup.set('name', name);