2014-11-26 13:39:59 +01:00
|
|
|
angular.module('syncthing.core')
|
|
|
|
|
.directive('validDeviceid', function ($http) {
|
|
|
|
|
return {
|
|
|
|
|
require: 'ngModel',
|
|
|
|
|
link: function (scope, elm, attrs, ctrl) {
|
|
|
|
|
ctrl.$parsers.unshift(function (viewValue) {
|
|
|
|
|
if (scope.editingExisting) {
|
|
|
|
|
// we shouldn't validate
|
|
|
|
|
ctrl.$setValidity('validDeviceid', true);
|
|
|
|
|
} else {
|
2015-04-07 13:57:26 +01:00
|
|
|
$http.get(urlbase + '/svc/deviceid?id=' + viewValue).success(function (resp) {
|
2014-11-26 13:39:59 +01:00
|
|
|
if (resp.error) {
|
|
|
|
|
ctrl.$setValidity('validDeviceid', false);
|
|
|
|
|
} else {
|
|
|
|
|
ctrl.$setValidity('validDeviceid', true);
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-12-29 22:42:19 -05:00
|
|
|
//Prevents user from adding a duplicate ID
|
|
|
|
|
var matches = scope.devices.filter(function (n) {
|
|
|
|
|
return n.deviceID == viewValue;
|
|
|
|
|
}).length;
|
|
|
|
|
if (matches > 0) {
|
|
|
|
|
ctrl.$setValidity('unique', false);
|
|
|
|
|
} else {
|
|
|
|
|
ctrl.$setValidity('unique', true);
|
|
|
|
|
}
|
2014-11-26 13:39:59 +01:00
|
|
|
}
|
|
|
|
|
return viewValue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|