forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscKeyValues.js
272 lines (258 loc) · 9.4 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"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;
}
};
})
.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){
var focusElem;
var added = {};
var isUncommitted = function() {
return (!!$scope.key) || (!!$scope.value);
};
var checkCommitted = function() {
if(isUncommitted()) {
$scope.showCommmitWarning = true;
} else {
$scope.showCommmitWarning = false;
}
};
// checks if the key,value inputs have any text value.
// if so, sets the form name="clean" to an 'invalid' state, which will
// invalidate any parent form up the chain. This should result in an
// inability to submit that form until the user commits the new key-value pair.
var isClean = _.debounce(function() {
$scope.$applyAsync(function() {
if(!!$scope.key) {
$scope.clean.isClean.$setValidity('isClean', false);
} else if(!!$scope.value) {
$scope.clean.isClean.$setValidity('isClean', false);
} else {
$scope.clean.isClean.$setValidity('isClean', true);
}
});
}, 200);
// returns a new function bound to the set of DOM nodes provided as an array.
// allows us to treat the osc-key-values directive as a single node on blur
// events, though it is actually made up of a number of nodes. If any of the
// provided nodes is the document.activeElement, we know the osc-key-values
// directive as a whole still has focus. When the document.activeElement no
// longer matches a child node, we can test to see if there are uncommitted
// values left in the osc-key-values directive & notify the user if so.
var onBlur = function(nodes) {
return function(evt) {
$scope.$applyAsync(function() {
if(!_.includes(nodes, document.activeElement)) {
checkCommitted();
isClean();
}
});
};
};
$scope.isClean = isClean;
$scope.clear = function() {
$scope.key = '';
$scope.value = '';
checkCommitted();
isClean();
};
$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();
checkCommitted();
isClean();
focusElem.focus();
}
};
$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";
}
};
this.scope = $scope;
this.init = function(keyInput, valInput, submitBtn) {
var nodes = [keyInput[0], valInput[0], submitBtn[0]];
var boundBlur = onBlur(nodes);
focusElem = keyInput;
keyInput.on('blur', boundBlur);
valInput.on('blur', boundBlur);
submitBtn.on('blur', boundBlur);
$scope.$on('$destroy', function() {
keyInput.off('blur', boundBlur);
valInput.off('blur', boundBlur);
submitBtn.off('blur', boundBlur);
});
};
},
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 = "";
}
return {
post: function($scope, $elem, $attrs, ctrl) {
ctrl.init(
$elem.find('input[name="key"]'),
$elem.find('input[name="value"]'),
$elem.find('a.add-key-value')
);
}
};
}
};
});