Skip to content

Commit

Permalink
cleanup. more ng-class support
Browse files Browse the repository at this point in the history
  • Loading branch information
machineboy2045 committed Dec 20, 2014
1 parent d3f6314 commit abf1799
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions dist/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
var selectize,
config = angular.extend({}, Selectize.defaults, selectizeConfig, scope.config);


//override to support checking empty arrays
modelCtrl.$isEmpty = function(val){
return (!val || !val.length);
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;
data[config.searchField] = input;
data[config.sortField] = input;
return data;
}

Expand All @@ -39,66 +34,56 @@ angular.module('selectize', []).value('selectizeConfig', {}).directive("selectiz
if(!config.required)
return true;

if (modelCtrl.$isEmpty(modelValue)) {
selectize.$control.toggleClass('ng-invalid', true)
return false;
}else{
selectize.$control.toggleClass('ng-invalid', false)
return true
}
return !modelCtrl.$isEmpty(modelValue);
};

config.onChange = function(){
if(!angular.equals(selectize.items, scope.ngModel))
if( !angular.equals(selectize.items, scope.ngModel) )
modelCtrl.$setViewValue( angular.copy(selectize.items) );
}

function updateSelectize(){
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.setValue(scope.ngModel)
}

config.onOptionAdd = function(value, data) {
if( scope.options.indexOf(data) === -1 )
scope.options.push(data);
}

function updateSelectizeOptions(){
selectize.addOption(scope.options)
}

function updateSelectizeValue(){
if(!angular.equals(selectize.items, scope.ngModel))
selectize.setValue(scope.ngModel);
}


// 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 = scope.options || config.options || scope.ngModel || [];


scope.options = $.map(scope.options, function(opt){
if(typeof opt === 'string')
return createItem(opt)
else
return opt
})
return typeof opt === 'string' ? createItem(opt) : opt;
});

config.onInitialize = function(){
selectize = element[0].selectize;

selectize.addOption(scope.options)
selectize.setValue(scope.ngModel)

scope.$watchCollection('options', updateSelectizeOptions);
scope.$watch('ngModel', updateSelectizeValue, true);
scope.$watch('ngDisabled', toggle)
scope.$watchCollection('options', selectize.addOption.bind(selectize));
scope.$watch('ngModel', updateSelectize);
scope.$watch('ngDisabled', toggle);
}

element.selectize(config);

element.on('$destroy', function() {
if (selectize) {
selectize.destroy();
element = null;
selectize.destroy();
element = null;
}
});


}
};
}]);

0 comments on commit abf1799

Please sign in to comment.