forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels.js
98 lines (91 loc) · 3.07 KB
/
labels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
angular.module('openshiftConsole')
.directive('labels', function($location, $timeout, LabelFilter) {
return {
restrict: 'E',
scope: {
labels: '=',
// if you specify clickable, then everything below is required unless specified as optional
clickable: "@?",
kind: "@?",
projectName: "@?",
limit: '=?',
titleKind: '@?', // optional, instead of putting kind into that part of the hover
// title, it will put this string instead, e.g. if you want 'builds for build config foo'
navigateUrl: '@?', // optional to override the default
filterCurrentPage: '=?' //optional don't navigate, just filter here
},
templateUrl: 'views/directives/labels.html',
link: function(scope) {
scope.filterAndNavigate = function(key, value) {
if (scope.kind && scope.projectName) {
if (!scope.filterCurrentPage) {
$location.url(scope.navigateUrl || ("/project/" + scope.projectName + "/browse/" + scope.kind));
}
$timeout(function() {
var selector = {};
selector[key] = value;
LabelFilter.setLabelSelector(new LabelSelector(selector, true));
}, 1);
}
};
}
};
})
.directive('labelEditor', function() {
var LABEL_REGEXP = /^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$/;
var LABEL_MAXLENGTH = 63;
var SUBDOMAIN_REGEXP = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$/;
var SUBDOMAIN_MAXLENGTH = 253;
function validateSubdomain(str) {
if (str.length > SUBDOMAIN_MAXLENGTH) { return false; }
return SUBDOMAIN_REGEXP.test(str);
}
function validateLabel(str) {
if (str.length > LABEL_MAXLENGTH) { return false; }
return LABEL_REGEXP.test(str);
}
return {
restrict: 'E',
scope: {
labels: "=",
systemLabels: "=",
expand: "=?",
canToggle: "=?",
// Delete policy for osc-key-values (default: "added")
deletePolicy: "@?",
// Optional help text to show with the label controls
helpText: "@?"
},
templateUrl: 'views/directives/label-editor.html',
link: function(scope, element, attrs) {
if (!angular.isDefined(attrs.canToggle)) {
scope.canToggle = true;
}
},
controller: [
'$scope',
function($scope) {
// simulate a regex, object w/.test() method
var labelValidator = {
test: function(val) {
var parts = val.split("/");
switch(parts.length) {
case 1:
return validateLabel(parts[0]);
case 2:
return validateSubdomain(parts[0]) && validateLabel(parts[1]);
}
return false;
}
};
angular.extend($scope, {
validator: {
key: labelValidator,
value: labelValidator
}
});
}
]
};
});