forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscUnique.js
53 lines (51 loc) · 1.9 KB
/
oscUnique.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
'use strict';
// oscUnique is a validation directive
// use:
// Put it on an input or other DOM node with an ng-model attribute.
// Pass a list (array, or object) via osc-unique="list"
//
// Sets model $valid true||false
// - model is valid so long as the item is not already in the list
//
// Key off $valid to enable/disable/sow/etc other objects
//
// Validates that the ng-model is unique in a list of values.
// ng-model: 'foo'
// oscUnique: ['foo', 'bar', 'baz'] // false, the string 'foo' is in the list
// oscUnique: [1,2,4] // true, the string 'foo' is not in the list
// oscUnique: {foo: true, bar: false} // false, the object has key 'foo'
// NOTES:
// - non-array values passed to oscUnqiue will be transformed into an array.
// - oscUnqiue: 'foo' => [0,1,2] (probably not what you want, so don't pass a string)
// - objects passed will be converted to a list of object keys.
// - { foo: false } would still be invalid, because the key exists (value is ignored)
// - recommended to pass an array
//
// Example:
// - prevent a button from being clickable if the input value has already been used
// <input ng-model="key" osc-unique="keys" />
// <button ng-disabled="form.key.$error.oscUnique" ng-click="submit()">Submit</button>
//
angular.module('openshiftConsole')
.directive('oscUnique', function() {
return {
restrict: 'A',
scope: {
oscUnique: '='
},
require: 'ngModel',
link: function($scope, $elem, $attrs, ctrl) {
var list = [];
$scope.$watchCollection('oscUnique', function(newVal) {
list = _.isArray(newVal) ?
newVal :
_.keys(newVal);
});
ctrl.$parsers.unshift(function(value) {
// is valid so long as it doesn't already exist
ctrl.$setValidity('oscUnique', !_.includes(list, value));
return value;
});
}
};
});