-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathselectize.js
More file actions
executable file
·137 lines (110 loc) · 4.18 KB
/
selectize.js
File metadata and controls
executable file
·137 lines (110 loc) · 4.18 KB
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
/**
* Angular Selectize2
* https://github.com/machineboy2045/angular-selectize
**/
angular.module('selectize', []).value('selectizeConfig', {}).directive("selectize", ['selectizeConfig', function(selectizeConfig) {
return {
restrict: 'EA',
require: '^ngModel',
scope: {ngModel: '=', config: '=?', options: '=?', ngDisabled: '=', ngRequired: '&'},
link: function(scope, element, attrs, modelCtrl) {
Selectize.defaults.maxItems = null; //default to tag editor
var selectize,
config = angular.extend({}, Selectize.defaults, selectizeConfig, scope.config);
modelCtrl.$isEmpty = function(val){
return (val === undefined || val === null || !val.length); //override to support checking empty arrays
}
function createItem(input) {
var data = {};
data[config.labelField] = input;
data[config.valueField] = input;
return data;
}
function toggle(disabled){
disabled ? selectize.disable() : selectize.enable();
}
var validate = function() {
var isInvalid = (scope.ngRequired() || attrs.required || config.required) && modelCtrl.$isEmpty(scope.ngModel);
modelCtrl.$setValidity('required', !isInvalid)
};
function generateOptions(data){
if(!data)
return [];
data = angular.isArray(data) ? data : [data]
return $.map(data, function(opt){
return typeof opt === 'string' ? createItem(opt) : opt;
});
}
function updateSelectize(){
validate();
selectize.$control.toggleClass('ng-valid', modelCtrl.$valid)
selectize.$control.toggleClass('ng-invalid', modelCtrl.$invalid)
selectize.$control.toggleClass('ng-dirty', modelCtrl.$dirty)
selectize.$control.toggleClass('ng-pristine', modelCtrl.$pristine)
if( !angular.equals(selectize.items, scope.ngModel) ){
selectize.addOption(generateOptions(scope.ngModel))
selectize.setValue(scope.ngModel)
}
}
var onChange = config.onChange,
onOptionAdd = config.onOptionAdd;
config.onChange = function(){
if( !angular.equals(selectize.items, scope.ngModel) )
scope.$evalAsync(function(){
var value = selectize.items.slice();
if (config.maxItems == 1) {
value = value[0]
}
modelCtrl.$setViewValue( value );
});
if (onChange) {
onChange.apply(this, arguments);
}
}
config.onOptionAdd = function(value, data) {
if( scope.options.indexOf(data) === -1 )
scope.options.push(data);
if (onOptionAdd) {
onOptionAdd.apply(this, arguments);
}
}
// ngModel (ie selected items) is included in this because if no options are specified, we
// need to create the corresponding options for the items to be visible
scope.options = generateOptions( (scope.options || config.options || scope.ngModel).slice() );
var angularCallback = config.onInitialize;
config.onInitialize = function(){
selectize = element[0].selectize;
selectize.addOption(scope.options)
selectize.setValue(scope.ngModel)
//provides a way to access the selectize element from an
//angular controller
if(angularCallback){
angularCallback(selectize);
}
scope.$watch(function(){
return scope.options.map(function(r){
angular.forEach(r, function(val, key) {
if ([config.valueField, config.labelField].indexOf(key) == -1) {
delete r[key];
}
});
return r;
});
}, function(val){
selectize.clearOptions();
selectize.addOption(scope.options)
selectize.setValue(scope.ngModel)
}, true);
scope.$watchCollection('ngModel', updateSelectize);
scope.$watch('ngDisabled', toggle);
}
element.selectize(config);
element.on('$destroy', function() {
if (selectize) {
selectize.destroy();
element = null;
}
});
}
};
}]);