forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscKeyValues.js
190 lines (185 loc) · 6.31 KB
/
oscKeyValues.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
/* jshint unused: false */
angular.module("openshiftConsole")
.controller("KeyValuesEntryController", function($scope){
$scope.editing = false;
$scope.edit = function(){
$scope.originalValue = $scope.value;
$scope.editing = true;
};
$scope.cancel= function(){
$scope.value = $scope.originalValue;
$scope.editing = false;
};
$scope.update = function(key, value, entries){
if(value){
entries[key] = value;
$scope.editing = false;
}
};
})
.controller("KeyValuesController", function($scope){
var added = {};
$scope.allowDelete = function(value){
if ($scope.preventEmpty && (Object.keys($scope.entries).length === 1)) {
return false;
}
if($scope.deletePolicy === "never") {
return false;
}
if($scope.deletePolicy === "added"){
return added[value] !== undefined;
}
return true;
};
$scope.addEntry = function() {
if($scope.key && $scope.value){
var readonly = $scope.readonlyKeys.split(",");
if(readonly.indexOf($scope.key) !== -1){
return;
}
added[$scope.key] = "";
$scope.entries[$scope.key] = $scope.value;
$scope.key = null;
$scope.value = null;
$scope.form.$setPristine();
$scope.form.$setUntouched();
}
};
$scope.deleteEntry = function(key) {
if ($scope.entries[key]) {
delete $scope.entries[key];
delete added[key];
$scope.form.$setDirty();
}
};
$scope.setErrorText = function(keyTitle) {
if (keyTitle === 'path') {
return "absolute path";
} else if (keyTitle === 'label') {
return "label";
} else {
return "key";
}
};
})
.directive("oscInputValidator", function(){
var validators = {
always: function(modelValue, viewValue){
return true;
},
env: function(modelValue, viewValue){
var C_IDENTIFIER_RE = /^[A-Za-z_][A-Za-z0-9_]*$/i;
if(modelValue === undefined || modelValue === null || modelValue.trim().length === 0) {
return true;
}
return C_IDENTIFIER_RE.test(viewValue);
},
label: function(modelValue, viewValue) {
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);
}
if (modelValue === undefined || modelValue === null || modelValue.trim().length === 0) {
return true;
}
var parts = viewValue.split("/");
switch(parts.length) {
case 1:
return validateLabel(parts[0]);
case 2:
return validateSubdomain(parts[0]) && validateLabel(parts[1]);
}
return false;
},
path: function(modelValue, viewValue) {
var ABS_PATH_REGEXP = /^\//;
if (modelValue === undefined || modelValue === null || modelValue.trim().length === 0) {
return true;
}
return ABS_PATH_REGEXP.test(viewValue);
}
};
return {
require: ["ngModel", "^oscKeyValues"],
restrict: "A",
link: function(scope, elm, attrs, controllers) {
var ctrl = controllers[0];
var oscKeyValues = controllers[1];
if(attrs.oscInputValidator === 'key'){
ctrl.$validators.oscKeyValid = validators[oscKeyValues.scope.keyValidator];
}else if(attrs.oscInputValidator === 'value'){
ctrl.$validators.oscValueValid = validators[oscKeyValues.scope.valueValidator];
}
}
};
})
/**
* A Directive for displaying key/value pairs. Configuration options
* via attributes:
* delimiter: the value to use to separate key/value pairs when displaying
* (e.g. foo:bar). Default: ":"
* keyTitle: The value to use as the key input's placeholder. Default: Name
* ValueTitle: The value to use as the value input's placeholder. Default: Value
* editable: true if the intention is to display values only otherwise false (default)
* keyValidator: The validator to use for validating keys
* - always: Any value is allowed (Default).
* - env: Validate as an ENV var /^[A-Za-z_][A-Za-z0-9_]*$/i
* - label: Validate as a label
* - path: Validate as an absolute path
* deletePolicy:
* - always: allow any key/value pair (Default)
* - added: allow any added not originally in entries
* - never: disallow any entries being deleted
* readonlyKeys: A comma delimted list of keys that are readonly
* keyValidationTooltip: The tool tip to display when the key validation message is visible
*/
.directive("oscKeyValues", function() {
return {
restrict: "E",
scope: {
keyTitle: "@",
valueTitle: "@",
entries: "=",
delimiter: "@",
editable: "@",
keyValidator: "@",
valueValidator: "@",
deletePolicy: "@",
readonlyKeys: "@",
keyValidationTooltip: "@",
valueValidationTooltip: "@",
preventEmpty: "=?",
},
controller: function($scope){
this.scope = $scope;
},
templateUrl: "views/directives/osc-key-values.html",
compile: function(element, attrs){
if(!attrs.delimiter){attrs.delimiter = ":";}
if(!attrs.keyTitle){attrs.keyTitle = "Name";}
if(!attrs.valueTitle){attrs.valueTitle = "Value";}
if(!attrs.editable || attrs.editable === "true"){
attrs.editable = true;
}else{
attrs.editable = false;
}
if(!attrs.keyValidator){attrs.keyValidator = "always";}
if(!attrs.valueValidator){attrs.valueValidator = "always";}
if(["always", "added", "none"].indexOf(attrs.deletePolicy) === -1){
attrs.deletePolicy = "always";
}
if(!attrs.readonlyKeys){
attrs.readonlyKeys = "";
}
}
};
});