From 11f1e4dd968c4e37dee4159f0fae3c88811b561f Mon Sep 17 00:00:00 2001 From: okauppinen Date: Wed, 19 Jul 2023 16:17:52 +0300 Subject: [PATCH 1/6] set opacity to layer --- bundles/framework/mapfull/instance.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bundles/framework/mapfull/instance.js b/bundles/framework/mapfull/instance.js index c3e035f829..620143298e 100755 --- a/bundles/framework/mapfull/instance.js +++ b/bundles/framework/mapfull/instance.js @@ -309,8 +309,6 @@ Oskari.clazz.define('Oskari.mapframework.bundle.mapfull.MapFullBundleInstance', const sandbox = this.getSandbox(); const mapModuleName = this.getMapModule().getName(); const rbAdd = Oskari.requestBuilder('AddMapLayerRequest'); - const rbOpacity = Oskari.requestBuilder('ChangeMapLayerOpacityRequest'); - const rbVisible = Oskari.requestBuilder('MapModulePlugin.MapLayerVisibilityRequest'); const isGuest = !Oskari.user().isLoggedIn(); const layersNotAvailable = []; selectedLayers.forEach(layer => { @@ -323,6 +321,9 @@ Oskari.clazz.define('Oskari.mapframework.bundle.mapfull.MapFullBundleInstance', if (layer.style) { oskariLayer.selectStyle(layer.style); } + if (layer.opacity || layer.opacity === 0) { + oskariLayer.setOpacity(layer.opacity); + } const options = {}; if (isGuest) { // for logged in users styles will be populated based on the logged in user @@ -331,10 +332,6 @@ Oskari.clazz.define('Oskari.mapframework.bundle.mapfull.MapFullBundleInstance', options.userStyles = layer.userStyles; } sandbox.request(mapModuleName, rbAdd(layer.id, options)); - sandbox.request(mapModuleName, rbVisible(layer.id, !layer.hidden)); - if (layer.opacity || layer.opacity === 0) { - sandbox.request(mapModuleName, rbOpacity(layer.id, layer.opacity)); - } }); if (!this._initialStateInit || !layersNotAvailable.length) { From cc693d22c926847b331047807a0d61196f8e4b20 Mon Sep 17 00:00:00 2001 From: okauppinen Date: Wed, 26 Jul 2023 14:05:11 +0300 Subject: [PATCH 2/6] rearrange added layer on AddMapLayerRequest by toPosition option --- .../AdminLayerForm/AdminLayerFormHandler.js | 3 +- .../layers/BackgroundLayerSelectionPlugin.js | 66 +++++-------------- .../mapmodule/request/map.layer.handler.js | 6 +- 3 files changed, 22 insertions(+), 53 deletions(-) diff --git a/bundles/admin/admin-layereditor/view/AdminLayerForm/AdminLayerFormHandler.js b/bundles/admin/admin-layereditor/view/AdminLayerForm/AdminLayerFormHandler.js index c6aa1a4fb5..d59c98208d 100644 --- a/bundles/admin/admin-layereditor/view/AdminLayerForm/AdminLayerFormHandler.js +++ b/bundles/admin/admin-layereditor/view/AdminLayerForm/AdminLayerFormHandler.js @@ -746,8 +746,7 @@ class UIHandler extends StateHandler { // if layer was found from the selected layers remove it from map and re-add it // this handles everything that needs to be updated on the map without separate code to update and potentially changed data separately if (originalLayerIndex !== -1) { - this.sandbox.postRequestByName('AddMapLayerRequest', [layerId]); - this.sandbox.postRequestByName('RearrangeSelectedMapLayerRequest', [layerId, originalLayerIndex]); + this.sandbox.postRequestByName('AddMapLayerRequest', [layerId, { toPosition: originalLayerIndex }]); } } diff --git a/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js b/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js index 8b890a0e44..8ee425fa3b 100755 --- a/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js +++ b/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js @@ -175,76 +175,42 @@ Oskari.clazz.define( * @return {Oskari.mapframework.domain.WmsLayer[]/Oskari.mapframework.domain.WfsLayer[]/Oskari.mapframework.domain.VectorLayer[]/Mixed} */ _getBottomLayer: function () { - var bottomIdx = 0, - allSelectedLayers = - this.getSandbox().findAllSelectedMapLayers(); - - if (allSelectedLayers && allSelectedLayers.length) { - return allSelectedLayers[bottomIdx]; - } - return null; + return this.getSandbox().findAllSelectedMapLayers()[0]; }, /** * Does the actual layer selection update - * @param {Number} newSelectionId Id of the new base layer + * @param {String} newId Id of the new base layer * @private */ - _updateSelection: function (newSelectionId) { + _updateSelection: function (newId) { if (this.error) { return; } - var me = this, - conf = me.getConfig(), - currentBottom = me._getBottomLayer(), - currentBottomId = '', - currentSelection = me.getElement().find('div.currentSelection'), - newSelection = - me.getSandbox().findMapLayerFromSelectedMapLayers( - newSelectionId - ); - - if (newSelectionId === currentSelection.attr('data-layerId')) { + const currentId = this.getElement().find('div.currentSelection').attr('data-layerId'); + if (newId === currentId) { // user clicked already selected option, do nothing return; } + const sb = this.getSandbox(); + const { baseLayers = [] } = this.getConfig(); + const currentBottomId = this._getBottomLayer()?.getId(); + // switch bg layer (no need to call update on ui, we should catch the event) // - check if current bottom layer exists & is in our list (if so, remove) - if (currentBottom) { - currentBottomId += currentBottom.getId(); - if (jQuery.inArray(currentBottomId, conf.baseLayers) > -1) { - me.getSandbox().postRequestByName( - 'RemoveMapLayerRequest', - [currentBottomId] - ); - } + if (currentBottomId && baseLayers.includes(currentBottomId.toString())) { + sb.postRequestByName('RemoveMapLayerRequest', [currentBottomId]); } // - check if new selection is already selected, remove if so as rearrange doesn't seem to work - if (newSelection) { - me.getSandbox().postRequestByName( - 'RemoveMapLayerRequest', - [newSelectionId] - ); - } else { - newSelection = - me.getSandbox().findMapLayerFromAllAvailable( - newSelectionId - ); + if (sb.isLayerAlreadySelected(newId)) { + sb.postRequestByName('RemoveMapLayerRequest', [newId]); } + // - add to bottom + sb.postRequestByName('AddMapLayerRequest', [newId, { toPosition: 0 }]); - me.getSandbox().postRequestByName( - 'AddMapLayerRequest', - [newSelectionId] - ); - - // - move new selection to bottom (see layerselection._layerOrderChanged(item)) - me.getSandbox().postRequestByName( - 'RearrangeSelectedMapLayerRequest', - [newSelectionId, 0] - ); // toggle dropdown open/closed. // won't bother with dropdown being on or off here - me._toggleSelection(); + this._toggleSelection(); }, /** diff --git a/bundles/mapping/mapmodule/request/map.layer.handler.js b/bundles/mapping/mapmodule/request/map.layer.handler.js index d87850a063..61380fd3da 100755 --- a/bundles/mapping/mapmodule/request/map.layer.handler.js +++ b/bundles/mapping/mapmodule/request/map.layer.handler.js @@ -95,10 +95,14 @@ Oskari.clazz.define('map.layer.handler', // add layers from front of queue to map this.__processLayerQueue(); // zoom to content or center/supported zoom level + const sandbox = this.layerService.getSandbox(); if (opts.zoomContent) { - const sandbox = this.layerService.getSandbox(); sandbox.postRequestByName('MapModulePlugin.MapMoveByLayerContentRequest', [layerId, opts.zoomContent]); } + if (typeof opts.toPosition === 'number') { + sandbox.postRequestByName('RearrangeSelectedMapLayerRequest', [layerId, opts.toPosition]); + } + }; this._loadLayerInfo(layer, opts, done); }, From 2bfafebf639588a2424b9459f082e06601d8bdc0 Mon Sep 17 00:00:00 2001 From: okauppinen Date: Wed, 26 Jul 2023 17:23:04 +0300 Subject: [PATCH 3/6] use same addMapLayer function on app.start --- bundles/framework/mapfull/instance.js | 41 +++++++++------------------ 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/bundles/framework/mapfull/instance.js b/bundles/framework/mapfull/instance.js index 620143298e..346b411f67 100755 --- a/bundles/framework/mapfull/instance.js +++ b/bundles/framework/mapfull/instance.js @@ -310,19 +310,19 @@ Oskari.clazz.define('Oskari.mapframework.bundle.mapfull.MapFullBundleInstance', const mapModuleName = this.getMapModule().getName(); const rbAdd = Oskari.requestBuilder('AddMapLayerRequest'); const isGuest = !Oskari.user().isLoggedIn(); - const layersNotAvailable = []; - selectedLayers.forEach(layer => { - const oskariLayer = sandbox.findMapLayerFromAllAvailable(layer.id); + + const addMapLayer = layer => { + const { id, style, hidden, opacity } = layer; + const oskariLayer = sandbox.findMapLayerFromAllAvailable(id); if (!oskariLayer) { - layersNotAvailable.push(layer); - return; + return layer; } - oskariLayer.setVisible(!layer.hidden); - if (layer.style) { - oskariLayer.selectStyle(layer.style); + oskariLayer.setVisible(!hidden); + if (style) { + oskariLayer.selectStyle(style); } - if (layer.opacity || layer.opacity === 0) { - oskariLayer.setOpacity(layer.opacity); + if (!isNaN(opacity)) { + oskariLayer.setOpacity(Number.parseInt(opacity)); } const options = {}; if (isGuest) { @@ -332,28 +332,15 @@ Oskari.clazz.define('Oskari.mapframework.bundle.mapfull.MapFullBundleInstance', options.userStyles = layer.userStyles; } sandbox.request(mapModuleName, rbAdd(layer.id, options)); - }); + }; + // add available layers and store unavailable + const layersNotAvailable = selectedLayers.filter(addMapLayer); if (!this._initialStateInit || !layersNotAvailable.length) { return; } // only register this when starting the app to work around timing issues with some dynamically registered layers - Oskari.on('app.start', function () { - layersNotAvailable.forEach(({ id, style, hidden, opacity }) => { - const oskariLayer = sandbox.findMapLayerFromAllAvailable(id); - if (!oskariLayer) { - return; - } - if (style) { - oskariLayer.selectStyle(style); - } - sandbox.postRequestByName('AddMapLayerRequest', [id]); - sandbox.postRequestByName('MapModulePlugin.MapLayerVisibilityRequest', [id, !hidden]); - if (!isNaN(opacity)) { - sandbox.postRequestByName('ChangeMapLayerOpacityRequest', [id, Number.parseInt(opacity)]); - } - }); - }); + Oskari.on('app.start', () => layersNotAvailable.forEach(addMapLayer)); }, // TODO: maybe style.getName() could return '' if name === '!default!', so we get rid of this _getCurrentStyleName: function (layer) { From e27854bed91dddc6b3e41958c747f4f736025b8e Mon Sep 17 00:00:00 2001 From: okauppinen Date: Wed, 26 Jul 2023 17:29:36 +0300 Subject: [PATCH 4/6] select style before add maplayer --- .../userstyle/handler/UserStyleHandler.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/bundles/mapping/userstyle/handler/UserStyleHandler.js b/bundles/mapping/userstyle/handler/UserStyleHandler.js index 9b0edd0e8f..ad2576b921 100644 --- a/bundles/mapping/userstyle/handler/UserStyleHandler.js +++ b/bundles/mapping/userstyle/handler/UserStyleHandler.js @@ -71,10 +71,20 @@ class Handler extends StateHandler { } addLayerToMap (layerId, styleId) { - this.sandbox.postRequestByName('AddMapLayerRequest', [layerId]); - if (styleId) { - this.sandbox.postRequestByName('ChangeMapLayerStyleRequest', [layerId, styleId]); + const styleName = styleId?.toString(); + if (this.sandbox.isLayerAlreadySelected(layerId)) { + if (styleName) { + // use request to trigger event + this.sandbox.postRequestByName('ChangeMapLayerStyleRequest', [layerId, styleName]); + } + return; } + if (styleName) { + // AddMapLayerRequest is asynchoronous so style has to be selected before request + // ChangeMapLayerStyleRequest doesn't work properly as layer isn't selected immediately + this.sandbox.findMapLayerFromAllAvailable(layerId)?.selectStyle(styleName); + } + this.sandbox.postRequestByName('AddMapLayerRequest', [layerId]); } showLayerStyles (layerId) { From 0788f6ff2985969484060493ffe5f09e17ddf475 Mon Sep 17 00:00:00 2001 From: okauppinen Date: Wed, 26 Jul 2023 17:33:32 +0300 Subject: [PATCH 5/6] remove unused boolean --- bundles/framework/publishedstatehandler/instance.js | 3 +-- bundles/framework/publisher2/service/PublisherService.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/bundles/framework/publishedstatehandler/instance.js b/bundles/framework/publishedstatehandler/instance.js index 8d023dbe5a..70e678a59b 100755 --- a/bundles/framework/publishedstatehandler/instance.js +++ b/bundles/framework/publishedstatehandler/instance.js @@ -482,7 +482,6 @@ Oskari.clazz.define( _setMapState: function (mapmodule, state, currentState) { var sandbox = this.getSandbox(), cmpResult = this._compareState(currentState, state, false); - // setting state if (state.selectedLayers && cmpResult.rulesMatched.layers) { this._log.debug('restoring LAYER state'); @@ -497,7 +496,7 @@ Oskari.clazz.define( layer; for (i = 0; i < len; i += 1) { layer = state.selectedLayers[i]; - sandbox.request(mapmodule.getName(), rbAdd(layer.id, true)); + sandbox.request(mapmodule.getName(), rbAdd(layer.id)); if (layer.hidden) { sandbox.request(mapmodule.getName(), visibilityRequestBuilder(layer.id, false)); } else { diff --git a/bundles/framework/publisher2/service/PublisherService.js b/bundles/framework/publisher2/service/PublisherService.js index e08805d0b7..28aced890d 100755 --- a/bundles/framework/publisher2/service/PublisherService.js +++ b/bundles/framework/publisher2/service/PublisherService.js @@ -76,7 +76,7 @@ Oskari.clazz.define('Oskari.mapframework.bundle.publisher2.PublisherService', addLayers: function () { var sandbox = this.__sandbox; this.getNonPublisherLayers().forEach(layer => { - sandbox.postRequestByName('AddMapLayerRequest', [layer.getId(), true]); + sandbox.postRequestByName('AddMapLayerRequest', [layer.getId()]); }); }, From 8440935d7ffdac63a5f8ebc51ecadcaed320df27 Mon Sep 17 00:00:00 2001 From: okauppinen Date: Tue, 8 Aug 2023 12:13:44 +0300 Subject: [PATCH 6/6] eslint --- .../mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js | 2 +- bundles/mapping/mapmodule/request/map.layer.handler.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js b/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js index 8ee425fa3b..61997df0dd 100755 --- a/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js +++ b/bundles/mapping/mapmodule/plugin/layers/BackgroundLayerSelectionPlugin.js @@ -199,7 +199,7 @@ Oskari.clazz.define( // switch bg layer (no need to call update on ui, we should catch the event) // - check if current bottom layer exists & is in our list (if so, remove) if (currentBottomId && baseLayers.includes(currentBottomId.toString())) { - sb.postRequestByName('RemoveMapLayerRequest', [currentBottomId]); + sb.postRequestByName('RemoveMapLayerRequest', [currentBottomId]); } // - check if new selection is already selected, remove if so as rearrange doesn't seem to work if (sb.isLayerAlreadySelected(newId)) { diff --git a/bundles/mapping/mapmodule/request/map.layer.handler.js b/bundles/mapping/mapmodule/request/map.layer.handler.js index 61380fd3da..c1cabe82ea 100755 --- a/bundles/mapping/mapmodule/request/map.layer.handler.js +++ b/bundles/mapping/mapmodule/request/map.layer.handler.js @@ -102,7 +102,6 @@ Oskari.clazz.define('map.layer.handler', if (typeof opts.toPosition === 'number') { sandbox.postRequestByName('RearrangeSelectedMapLayerRequest', [layerId, opts.toPosition]); } - }; this._loadLayerInfo(layer, opts, done); },