Skip to content

Commit

Permalink
Treat user entered options differently from original options
Browse files Browse the repository at this point in the history
Or programmatically changed options.
Fixes issue machineboy2045#69.
Also fixes machineboy2045#110...

Demo at http://plnkr.co/edit/2352rt
  • Loading branch information
PhiLhoSoft committed Mar 3, 2016
1 parent d0d3fbf commit daa7dd0
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions dist/angular-selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,37 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
scope.config = scope.config || {};

var isEmpty = function(val) {
return val === undefined || val === null || !val.length; //support checking empty arrays
return val === undefined || val === null || val.length === 0; // support checking empty arrays / strings
};

var toggle = function(disabled) {
disabled ? selectize.disable() : selectize.enable();
}
};

var validate = function() {
var isInvalid = (scope.ngRequired() || attrs.required || settings.required) && isEmpty(scope.ngModel);
modelCtrl.$setValidity('required', !isInvalid);
};

var setSelectizeOptions = function(curr, prev) {
angular.forEach(prev, function(opt){
if(curr.indexOf(opt) === -1){
if (scope._noUpdate) { // Internal changes to scope.options, eschew the watch mechanism
scope._noUpdate = false;
return;
}
angular.forEach(prev, function(opt) {
if (curr.indexOf(opt) === -1) {
var value = opt[settings.valueField];
selectize.removeOption(value, true);
}
});
angular.forEach(curr, function(opt) {
selectize.registerOption(opt);
});
selectize.lastQuery = undefined; // Hack because of a Selectize bug...
selectize.refreshOptions(false); // Update the content of the drop-down

selectize.addOption(curr, true);

selectize.refreshOptions(false); // updates results if user has entered a query
setSelectizeValue();
}
};

var setSelectizeValue = function() {
validate();
Expand All @@ -54,28 +60,48 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
if (!angular.equals(selectize.items, scope.ngModel)) {
selectize.setValue(scope.ngModel, true);
}
}
};

settings.onChange = function(value) {
var value = angular.copy(selectize.items);
if (settings.maxItems == 1) {
value = value[0]
var items = angular.copy(selectize.items);
if (settings.maxItems === 1) {
items = items[0];
}
modelCtrl.$setViewValue( value );
modelCtrl.$setViewValue(items);

if (scope.config.onChange) {
scope.config.onChange.apply(this, arguments);
}
};

// User entered a new tag.
settings.onOptionAdd = function(value, data) {
if( scope.options.indexOf(data) === -1 ) {
if (scope.options.indexOf(data) === -1) {
scope._noUpdate = true;
scope.options.push(data);
}
if (scope.config.onOptionAdd) {
scope.config.onOptionAdd.apply(this, arguments);
}
};

if (scope.config.onOptionAdd) {
scope.config.onOptionAdd.apply(this, arguments);
// User removed a tag they entered.
// Note: it is not called if persist is true.
settings.onOptionRemove = function(value) {
var idx = -1;
for (var i = 0; i < scope.options.length; i++) {
if (scope.options[i][scope.config.valueField] === value) {
idx = i;
break;
}
}
if (idx !== -1 ) {
scope._noUpdate = true;
scope.options.splice(idx, 1);
}
if (scope.config.onOptionRemove) {
scope.config.onOptionRemove.apply(this, arguments);
}
};

settings.onInitialize = function() {
Expand Down

0 comments on commit daa7dd0

Please sign in to comment.