2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('ffffng')
|
|
|
|
.directive('fNodeForm', function () {
|
2014-08-10 11:37:05 +02:00
|
|
|
var ctrl = function (
|
|
|
|
$scope,
|
|
|
|
$timeout,
|
|
|
|
Constraints,
|
|
|
|
$element,
|
|
|
|
_,
|
|
|
|
config,
|
|
|
|
$window,
|
|
|
|
geolib,
|
|
|
|
OutsideOfCommunityDialog
|
|
|
|
) {
|
2016-05-18 19:32:19 +02:00
|
|
|
var initialEmail = $scope.node.email;
|
|
|
|
var initialMonitoring = $scope.node.monitoring;
|
|
|
|
var monitoringConfirmed = $scope.node.monitoringConfirmed;
|
|
|
|
|
2014-05-31 19:19:36 +02:00
|
|
|
$scope.config = config;
|
2016-05-24 10:35:53 +02:00
|
|
|
$scope.map = {
|
2014-05-12 20:08:19 +02:00
|
|
|
center: {
|
2014-05-31 19:19:36 +02:00
|
|
|
lat: config.coordsSelector.lat,
|
|
|
|
lng: config.coordsSelector.lng,
|
|
|
|
zoom: config.coordsSelector.defaultZoom
|
2014-05-12 20:08:19 +02:00
|
|
|
},
|
|
|
|
markers: {},
|
|
|
|
layers: {
|
|
|
|
baselayers: {
|
|
|
|
osm: {
|
|
|
|
name: '',
|
|
|
|
url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg',
|
|
|
|
type: 'xyz',
|
|
|
|
layerOptions: {
|
|
|
|
subdomains: '1234',
|
|
|
|
attribution:
|
|
|
|
'Map data Tiles © <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> ' +
|
2016-01-11 20:10:24 +01:00
|
|
|
'<img src="https://developer.mapquest.com/content/osm/mq_logo.png" />, ' +
|
2014-05-12 20:08:19 +02:00
|
|
|
'Map data © OpenStreetMap contributors, CC-BY-SA'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 10:35:53 +02:00
|
|
|
};
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2014-08-10 11:37:05 +02:00
|
|
|
if (config.otherCommunityInfo.showBorderForDebugging) {
|
2016-05-24 10:35:53 +02:00
|
|
|
$scope.map.paths = {
|
2014-08-10 11:37:05 +02:00
|
|
|
border: {
|
|
|
|
color: '#ff0000',
|
|
|
|
weight: 3,
|
|
|
|
latlngs: config.otherCommunityInfo.localCommunityPolygon.concat(
|
|
|
|
[config.otherCommunityInfo.localCommunityPolygon[0]]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var geolibPolygon = _.map(config.otherCommunityInfo.localCommunityPolygon, function (point) {
|
|
|
|
return {
|
|
|
|
latitude: point.lat,
|
|
|
|
longitude: point.lng
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
var inCommunityArea = function (lat, lng) {
|
|
|
|
return geolib.isPointInside({latitude: lat, longitude: lng}, geolibPolygon);
|
|
|
|
};
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
var updateNodePosition = function (lat, lng) {
|
2016-05-24 10:35:53 +02:00
|
|
|
$scope.map.markers.node = {
|
2014-05-12 20:08:19 +02:00
|
|
|
lat: lat,
|
|
|
|
lng: lng,
|
|
|
|
focus: true,
|
|
|
|
draggable: false
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.$on('leafletDirectiveMap.click', function (event, leaflet) {
|
|
|
|
var lat = leaflet.leafletEvent.latlng.lat;
|
|
|
|
var lng = leaflet.leafletEvent.latlng.lng;
|
|
|
|
updateNodePosition(lat, lng);
|
|
|
|
$scope.node.coords = lat + ' ' + lng;
|
|
|
|
});
|
|
|
|
|
2014-08-10 11:37:05 +02:00
|
|
|
function withValidCoords(coords, callback, invalidCallback) {
|
|
|
|
invalidCallback = invalidCallback || function () {};
|
|
|
|
|
2014-05-30 22:25:03 +02:00
|
|
|
coords = coords || '';
|
2014-05-12 20:08:19 +02:00
|
|
|
coords = coords.trim();
|
|
|
|
if (_.isEmpty(coords)) {
|
2014-08-10 11:37:05 +02:00
|
|
|
return invalidCallback();
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.hasError('coords')) {
|
2014-08-10 11:37:05 +02:00
|
|
|
return invalidCallback();
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var parts = coords.split(/\s+/);
|
|
|
|
|
|
|
|
var lat = Number(parts[0]);
|
|
|
|
var lng = Number(parts[1]);
|
|
|
|
|
2014-08-10 11:37:05 +02:00
|
|
|
return callback(lat, lng);
|
2014-05-30 22:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$scope.updateMap = function (optCoords) {
|
2016-05-24 10:35:53 +02:00
|
|
|
var coords = optCoords || $scope.node.coords;
|
2014-05-30 22:15:04 +02:00
|
|
|
withValidCoords(coords, function (lat, lng) {
|
|
|
|
updateNodePosition(lat, lng);
|
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.resetCoords = function () {
|
|
|
|
$scope.node.coords = '';
|
2016-05-24 10:35:53 +02:00
|
|
|
$scope.map.markers = {};
|
2014-05-12 20:08:19 +02:00
|
|
|
};
|
|
|
|
|
2014-06-06 18:24:40 +02:00
|
|
|
$scope.constraints = Constraints.node;
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2014-06-06 18:24:40 +02:00
|
|
|
var submitted = false;
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2014-06-06 18:24:40 +02:00
|
|
|
$scope.hasError = function (field) {
|
2014-06-09 20:30:01 +02:00
|
|
|
return $scope.nodeForm && $scope.nodeForm[field].$invalid && submitted;
|
2014-05-12 20:08:19 +02:00
|
|
|
};
|
|
|
|
|
2016-05-18 22:50:06 +02:00
|
|
|
$scope.monitoringActive = function () {
|
|
|
|
return $scope.node.monitoring && monitoringConfirmed && $scope.node.email === initialEmail;
|
|
|
|
};
|
|
|
|
|
2016-05-18 19:32:19 +02:00
|
|
|
$scope.monitoringInitialConfirmationRequired = function () {
|
|
|
|
return $scope.node.monitoring
|
|
|
|
&& ($scope.action === 'create' || $scope.node.email !== initialEmail || !initialMonitoring);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.monitoringConfirmationPending = function () {
|
|
|
|
return $scope.node.monitoring && initialMonitoring && !monitoringConfirmed;
|
|
|
|
};
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
var duplicateError = {
|
|
|
|
hostname: 'Der Knotenname ist bereits vergeben. Bitte wähle einen anderen.',
|
|
|
|
key: 'Für den VPN-Schlüssel gibt es bereits einen Eintrag.',
|
|
|
|
mac: 'Für die MAC-Adresse gibt es bereits einen Eintrag.'
|
|
|
|
};
|
|
|
|
|
2014-08-10 11:37:05 +02:00
|
|
|
var doSubmit = function (node) {
|
2014-06-06 18:24:40 +02:00
|
|
|
if ($scope.nodeForm.$invalid) {
|
|
|
|
var firstInvalid = _.filter($element.find('form').find('input'), function (input) {
|
2016-05-17 12:02:55 +02:00
|
|
|
return (input.type === 'text' || input.type === 'email')
|
|
|
|
&& $scope.nodeForm[input.name].$invalid;
|
2014-06-06 18:24:40 +02:00
|
|
|
})[0];
|
|
|
|
if (firstInvalid) {
|
|
|
|
$window.scrollTo(0, $window.pageYOffset + firstInvalid.getBoundingClientRect().top - 100);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
$scope.error = null;
|
2016-05-18 22:50:06 +02:00
|
|
|
$scope.save(node).catch(function (response, code) {
|
|
|
|
// error
|
2014-05-12 20:08:19 +02:00
|
|
|
switch (code) {
|
|
|
|
case 409: // conflict
|
|
|
|
$scope.error = duplicateError[response.field];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$scope.error = 'Es ist ein Fehler aufgetreten. Versuche es später noch einmal.';
|
|
|
|
}
|
2014-06-01 01:52:14 +02:00
|
|
|
$window.scrollTo(0, 0);
|
2014-05-12 20:08:19 +02:00
|
|
|
});
|
2014-08-10 11:37:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.onSubmit = function (node) {
|
|
|
|
submitted = true;
|
|
|
|
|
|
|
|
withValidCoords(
|
|
|
|
node.coords,
|
|
|
|
function (lat, lng) {
|
|
|
|
if (!config.otherCommunityInfo.showInfo || inCommunityArea(lat, lng)) {
|
|
|
|
doSubmit(node);
|
|
|
|
} else {
|
|
|
|
OutsideOfCommunityDialog.open($scope.action).result.then(function () {
|
|
|
|
doSubmit(node);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
doSubmit(node);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2014-05-30 22:15:04 +02:00
|
|
|
|
|
|
|
$scope.updateMap($scope.node.coords);
|
|
|
|
withValidCoords($scope.node.coords, function (lat, lng) {
|
|
|
|
$scope.center.lat = lat;
|
|
|
|
$scope.center.lng = lng;
|
|
|
|
$scope.center.zoom = 12;
|
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
'controller': ctrl,
|
|
|
|
'restrict': 'E',
|
|
|
|
'templateUrl': 'views/directives/nodeForm.html',
|
|
|
|
'scope': {
|
|
|
|
'node': '=fNode',
|
|
|
|
'save': '=fSave',
|
|
|
|
'cancel': '=fCancel',
|
|
|
|
'action': '@fAction'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|