Skip to content

Commit

Permalink
Map object should be accessed using promise
Browse files Browse the repository at this point in the history
fixes #253
  • Loading branch information
raitisbe committed Aug 20, 2019
1 parent b459579 commit 0d5aac0
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 102 deletions.
12 changes: 4 additions & 8 deletions components/add-layers/add-layers-vector.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,10 @@ angular.module('hs.addLayersVector', ['hs.styles'])
]
});

if (OlMap.map) {
OlMap.map.addInteraction(dragAndDrop);
} else {
$rootScope.$on('map.loaded', function () {
OlMap.map.addInteraction(dragAndDrop);
me.checkUrlParamsAndAdd()
});
}
OlMap.loaded().then(map => {
map.addInteraction(dragAndDrop);
me.checkUrlParamsAndAdd()
})

dragAndDrop.on('addfeatures', function (event) {
if (event.features.length > 0) {
Expand Down
19 changes: 8 additions & 11 deletions components/compositions/compositions.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default ['$rootScope', '$location', '$http', 'hs.map.service',
composition.feature.set('highlighted', state)
}

function init() {
function init(map) {
extentLayer = new VectorLayer({
title: "Composition extents",
show_in_manager: false,
Expand All @@ -105,7 +105,7 @@ export default ['$rootScope', '$location', '$http', 'hs.map.service',
}
});

OlMap.map.on('pointermove', function (evt) {
map.on('pointermove', function (evt) {
var features = extentLayer.getSource().getFeaturesAtCoordinate(evt.coordinate);
var somethingDone = false;
angular.forEach(extentLayer.getSource().getFeatures(), function (feature) {
Expand Down Expand Up @@ -134,18 +134,21 @@ export default ['$rootScope', '$location', '$http', 'hs.map.service',
$cookies.remove('hs_layers');
}

OlMap.map.addLayer(extentLayer);
map.addLayer(extentLayer);

if (permalink.getParamValue('composition')) {
var id = permalink.getParamValue('composition');
if (id.indexOf('http') == -1 && id.indexOf(config.status_manager_url) == -1)
id = statusManagerService.endpointUrl() + '?request=load&id=' + id;
compositionParser.loadUrl(id);
}

if (permalink.getParamValue('permalink')) {
permalink.parsePermalinkLayers();
}
}

if (angular.isDefined(OlMap.map)) init()
else $rootScope.$on('map.loaded', function () { init(); });
OlMap.loaded().then(init);

$rootScope.$on('compositions.composition_edited', function (event) {
compositionParser.composition_edited = true;
Expand Down Expand Up @@ -256,12 +259,6 @@ export default ['$rootScope', '$location', '$http', 'hs.map.service',
});
};

$rootScope.$on('map.loaded', function () {
if (permalink.getParamValue('permalink')) {
permalink.parsePermalinkLayers();
}
})

me.loadComposition = function (url, overwrite) {
return compositionParser.loadUrl(url, overwrite);
}
Expand Down
13 changes: 4 additions & 9 deletions components/datasource-selector/datasource-selector.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ export default ['$rootScope', '$timeout', 'hs.map.service', 'Core', 'config',
return Core.panelVisible('datasource_selector') || Core.panelVisible('datasourceBrowser')
}

function init() {
OlMap.map.on('pointermove', function (evt) {
function init(map) {
map.on('pointermove', function (evt) {
var features = extentLayer.getSource().getFeaturesAtCoordinate(evt.coordinate);
var something_done = false;
angular.forEach(extentLayer.getSource().getFeatures(), function (feature) {
Expand All @@ -273,7 +273,7 @@ export default ['$rootScope', '$timeout', 'hs.map.service', 'Core', 'config',
if (!panelVisible()) return;
if (mickaFilterService.filterByExtent) me.queryCatalogs(me.data.datasources);
});
OlMap.map.addLayer(extentLayer);
map.addLayer(extentLayer);
if (dataSourceExistsAndEmpty() && panelVisible()) {
me.queryCatalogs(me.data.datasources);
mickaFilterService.fillCodesets(me.data.datasources);
Expand All @@ -287,12 +287,7 @@ export default ['$rootScope', '$timeout', 'hs.map.service', 'Core', 'config',
});
}

if (angular.isDefined(OlMap.map))
init()
else
$rootScope.$on('map.loaded', function () {
init();
});
OlMap.loaded().then(init)

return me;
}]
6 changes: 2 additions & 4 deletions components/geolocation/geolocation.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export default ['hs.map.service', '$rootScope', '$log', 'Core',
var accuracyFeature = new Feature({ known: false, geometry: new CircleGeom([0, 0], 1) });
var positionFeature = new Feature({ known: false, geometry: new Point([0, 0]) });

$rootScope.$on('map.loaded', function () {
init();
});

function init() {
if (Core.isMobile()) {
/**
Expand Down Expand Up @@ -216,6 +212,8 @@ export default ['hs.map.service', '$rootScope', '$log', 'Core',
}
}

OlMap.loaded().then(init);

me.style = new Style({
image: new Circle({
fill: new Fill({
Expand Down
7 changes: 1 addition & 6 deletions components/layermanager/layermanager.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,7 @@ export default {
map = OlMap.map;
}

if (angular.isDefined(OlMap.map)) init();
else {
$rootScope.$on('map.loaded', function () {
init();
});
}
OlMap.loaded().then(init);

$scope.$emit('scope_loaded', "LayerManager");
}
Expand Down
11 changes: 2 additions & 9 deletions components/layermanager/layermanager.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con

boxLayersInit();

OlMap.map.getView().on('change:resolution', function (e) {
map.getView().on('change:resolution', function (e) {
if (timer != null) clearTimeout(timer);
timer = setTimeout(function () {
for (var i = 0; i < me.data.layers.length; i++) {
Expand All @@ -613,14 +613,7 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con
map.getLayers().on("remove", layerRemoved);
}

if (angular.isDefined(OlMap.map)) {
init();
}
else {
$rootScope.$on('map.loaded', function () {
init();
});
}
OlMap.loaded().then(init);

return me;
}
Expand Down
7 changes: 1 addition & 6 deletions components/legend/legend.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ export default {
}
}

if (OlMap.map)
init();
else
$rootScope.$on('map.loaded', function () {
init()
});
OlMap.loaded().then(init);

$scope.$emit('scope_loaded', "Legend");
}]
Expand Down
23 changes: 15 additions & 8 deletions components/map/map.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ export default ['config', '$rootScope', 'hs.utils.service', '$timeout', function
$rootScope.$broadcast('map.loaded');
}

this.loaded = function () {
return new Promise((resolve, reject) => {
if (me.map) {
resolve(me.map);
return;
} else {
$timeout(() => {
if (me.map) resolve(me.map); else reject();
}, 1000);
}
})
}

//clone View to not overwrite deafult
function cloneView(template) {
var view = new View({
Expand Down Expand Up @@ -343,12 +356,6 @@ export default ['config', '$rootScope', 'hs.utils.service', '$timeout', function
})
}

/**
* @function isLayerVisibleInPermalink
* @memberOf hs.map.controller.init
* @param {ol.Layer} lyr - Layer for which to determine visibility
* @description Finds out if layer is set as visible in URL (permalink)
*/
/**
* @ngdoc method
* @name hs.map.service#isLayerVisible
Expand Down Expand Up @@ -433,15 +440,15 @@ export default ['config', '$rootScope', 'hs.utils.service', '$timeout', function
view.setZoom(zoom);
}

this.getMapExtent = function(){
this.getMapExtent = function () {
var mapSize = me.map.getSize();
var mapExtent = angular.isDefined(mapSize) ?
me.map.getView().calculateExtent(mapSize) :
[0, 0, 100, 100];
return mapExtent;
};

this.getMapExtentInEpsg4326 = function(){
this.getMapExtentInEpsg4326 = function () {
var bbox = transformExtent(me.getMapExtent(),
me.map.getView().getProjection(),
'EPSG:4326');
Expand Down
5 changes: 1 addition & 4 deletions components/measure/measure.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ export default ['$rootScope', 'hs.map.service', 'hs.utils.service',

var map;

if (angular.isDefined(OlMap.map)) map = OlMap.map;
else $rootScope.$on('map.loaded', function () {
map = OlMap.map;
});
OlMap.loaded().then(m => { map = m });

this.draw;

Expand Down
14 changes: 5 additions & 9 deletions components/permalink/permalink-url.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,17 @@ export default ['$rootScope', '$http', '$location', '$window', 'hs.map.service',
* @private
* Function for service initialization when map object is ready
*/
function init() {
function init(map) {
if (url_generation) {
var timer = null;
$rootScope.$on('map.extent_changed', function (event, data, b) {
me.update()
$rootScope.$broadcast('browserurl.updated');
});
OlMap.map.getLayers().on("add", function (e) {
map.getLayers().on("add", function (e) {
var layer = e.element;
if (layer.get('show_in_manager') != null && layer.get('show_in_manager') == false) return;
if (layer.get('show_in_manager') != null
&& layer.get('show_in_manager') == false) return;
layer.on('change:visible', function (e) {
if (timer != null) clearTimeout(timer);
timer = setTimeout(function () {
Expand All @@ -221,12 +222,7 @@ export default ['$rootScope', '$http', '$location', '$window', 'hs.map.service',
}
}

if (angular.isDefined(OlMap.map))
init()
else
$rootScope.$on('map.loaded', function () {
init();
});
OlMap.loaded().then(init);
return me;
}
]
4 changes: 1 addition & 3 deletions components/query/query-base.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export default ['$rootScope', 'hs.map.service', 'Core', '$sce', 'config',
});
}

if (angular.isDefined(OlMap.map)) init();
if (me.deregisterOnMapLoaded) me.deregisterOnMapLoaded();
me.deregisterOnMapLoaded = $rootScope.$on('map.loaded', init);
OlMap.loaded().then(init);

this.setData = function (data, type, overwrite) {
if (angular.isDefined(type)) {
Expand Down
22 changes: 9 additions & 13 deletions components/query/query.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ export default ['$scope', '$rootScope', '$timeout', 'hs.map.service', 'hs.query.
function ($scope, $rootScope, $timeout, OlMap, Base, WMS, Vector, Core, config) {
var popup = new Popup();

if (OlMap.map)
OlMap.map.addOverlay(popup);
else
$rootScope.$on('map.loaded', function () {
OlMap.map.addOverlay(popup);
});
OlMap.loaded().then(map => {
map.addOverlay(popup);
if (Core.current_panel_queryable) {
if (!Base.queryActive) Base.activateQueries();
}
else {
if (Base.queryActive) Base.deactivateQueries();
}
});

try {
var $mdDialog = $injector.get('$mdDialog');
Expand Down Expand Up @@ -84,13 +87,6 @@ export default ['$scope', '$rootScope', '$timeout', 'hs.map.service', 'hs.query.
if (deregisterQueryStatusChanged) deregisterQueryStatusChanged();
});

if (Core.current_panel_queryable) {
if (!Base.queryActive) Base.activateQueries();
}
else {
if (Base.queryActive) Base.deactivateQueries();
}

$scope.$on('queryVectorResult', function () {
if (!$scope.$$phase) $scope.$digest();
});
Expand Down
9 changes: 1 addition & 8 deletions components/trip_planner/trip_planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,7 @@ angular.module('hs.trip_planner', ['hs.map', 'hs.core', 'focus-if'])
features: movable_features
});

if (angular.isUndefined(OlMap.map))
$scope.$on('map.loaded', function () {
OlMap.map.addLayer(vector);
});
else {
if (console) console.log('add trip layer');
OlMap.map.addLayer(vector);
}
OlMap.loaded().then(map => { map.addLayer(vector); });

if (angular.isUndefined(config.default_layers))
config.default_layers = [];
Expand Down
8 changes: 4 additions & 4 deletions materialComponents/matCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ define(['angular', 'core', 'angular-material'],
}
})

$scope.$on('map.loaded', function () {
angular.forEach(OlMap.map.getControls(), function (control) {
OlMap.map.removeControl(control);
OlMap.loaded().then(map => {
angular.forEach(map.getControls(), control => {
map.removeControl(control);
})
})
});
}])
})

0 comments on commit 0d5aac0

Please sign in to comment.