Skip to content

Commit

Permalink
feat(olControl): watch & reinitialize ctrls on map (#345)
Browse files Browse the repository at this point in the history
Adds $watch on the olControl properties and in case updates the
corresponding control on the openlayers map.
  • Loading branch information
juristr authored Jan 12, 2017
1 parent 34ab1fb commit ebebb17
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/directives/control.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
angular.module('openlayers-directive').directive('olControl', function($log, $q, olData, olMapDefaults, olHelpers) {

angular.module('openlayers-directive')
.directive('olControl', function($log, $q, olData, olMapDefaults, olHelpers) {
return {
restrict: 'E',
scope: {
Expand All @@ -12,29 +12,58 @@ angular.module('openlayers-directive').directive('olControl', function($log, $q,
var olScope = controller.getOpenlayersScope();
var olControl;
var olControlOps;
var getControlClasses = olHelpers.getControlClasses;
var controlClasses = getControlClasses();

olScope.getMap().then(function(map) {
var getControlClasses = olHelpers.getControlClasses;
var controlClasses = getControlClasses();

scope.$on('$destroy', function() {
map.removeControl(olControl);
});

if (!isDefined(scope.properties) || !isDefined(scope.properties.control)) {
if (attrs.name) {
if (isDefined(scope.properties)) {
olControlOps = scope.properties;
scope.$watch('properties', function(properties) {
if (!isDefined(properties)) {
return;
}

initCtrls(properties);
});

function initCtrls(properties) {
if (properties && properties.control) {
// the control instance is already defined,
// so simply use it and go ahead

// is there already a control, so destroy and recreate it?
if (olControl) {
map.removeControl(olControl);
}
olControl = new controlClasses[attrs.name](olControlOps);

olControl = properties.control;
map.addControl(olControl);
} else {

// the name is the key to instantiate an ol3 control
if (attrs.name) {
if (isDefined(properties)) {
olControlOps = properties;
}

// is there already a control, so destroy and recreate it?
if (olControl) {
map.removeControl(olControl);
}

olControl = new controlClasses[attrs.name](olControlOps);
map.addControl(olControl);
}
}
return;
}

olControl = scope.properties.control;
map.addControl(olControl);
initCtrls(scope.properties);

});

}
};
});

0 comments on commit ebebb17

Please sign in to comment.