2016-03-18 00:05:37 +01:00
|
|
|
angular.module('syncthing.core')
|
|
|
|
.directive('pathIsSubDir', function () {
|
|
|
|
return {
|
|
|
|
require: 'ngModel',
|
|
|
|
link: function (scope, elm, attrs, ctrl) {
|
|
|
|
ctrl.$parsers.unshift(function (viewValue) {
|
2017-01-14 13:18:48 +01:00
|
|
|
// This function checks whether ydir is a subdirectory of xdir,
|
2016-03-18 00:05:37 +01:00
|
|
|
// e.g. it would return true if xdir = "/home/a", ydir = "/home/a/b".
|
|
|
|
function isSubDir(xdir, ydir) {
|
|
|
|
var xdirArr = xdir.split(scope.system.pathSeparator);
|
|
|
|
var ydirArr = ydir.split(scope.system.pathSeparator);
|
|
|
|
if (xdirArr.slice(-1).pop() === "") {
|
|
|
|
xdirArr = xdirArr.slice(0, -1);
|
|
|
|
}
|
|
|
|
if (xdirArr.length > ydirArr.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return xdirArr.map(function(e, i) {
|
|
|
|
return xdirArr[i] === ydirArr[i];
|
|
|
|
}).every(function(e) { return e });
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.pathIsSubFolder = false;
|
2017-01-14 13:18:48 +01:00
|
|
|
scope.pathIsParentFolder = false;
|
2016-03-18 00:05:37 +01:00
|
|
|
scope.otherFolder = "";
|
2017-01-14 13:18:48 +01:00
|
|
|
scope.otherFolderLabel = "";
|
2016-03-18 00:05:37 +01:00
|
|
|
for (var folderID in scope.folders) {
|
|
|
|
if (isSubDir(scope.folders[folderID].path, viewValue)) {
|
|
|
|
scope.otherFolder = folderID;
|
2017-01-14 13:18:48 +01:00
|
|
|
scope.otherFolderLabel = scope.folders[folderID].label;
|
2016-03-18 00:05:37 +01:00
|
|
|
scope.pathIsSubFolder = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-14 13:18:48 +01:00
|
|
|
if (viewValue !== "" &&
|
|
|
|
isSubDir(viewValue, scope.folders[folderID].path)) {
|
|
|
|
scope.otherFolder = folderID;
|
|
|
|
scope.otherFolderLabel = scope.folders[folderID].label;
|
|
|
|
scope.pathIsParentFolder = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-18 00:05:37 +01:00
|
|
|
}
|
|
|
|
return viewValue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|