Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Noblecraft/1192 #1193

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/select.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* ui-select
* http://github.com/angular-ui/ui-select
* Version: 0.12.1 - 2015-07-28T03:50:59.080Z
* Version: 0.12.1 - 2015-09-13T04:08:15.468Z
* License: MIT
*/

Expand Down
86 changes: 62 additions & 24 deletions dist/select.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* ui-select
* http://github.com/angular-ui/ui-select
* Version: 0.12.1 - 2015-07-28T03:50:59.076Z
* Version: 0.12.1 - 2015-09-13T04:08:15.462Z
* License: MIT
*/

Expand Down Expand Up @@ -382,11 +382,11 @@ uis.controller('uiSelectCtrl',
data = data || ctrl.parserResult.source($scope);
var selectedItems = ctrl.selected;
//TODO should implement for single mode removeSelected
if ((angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.removeSelected) {
if (ctrl.isEmpty() || (angular.isArray(selectedItems) && !selectedItems.length) || !ctrl.removeSelected) {
ctrl.setItemsFn(data);
}else{
if ( data !== undefined ) {
var filteredItems = data.filter(function(i) {return selectedItems.indexOf(i) < 0;});
var filteredItems = data.filter(function(i) {return selectedItems && selectedItems.indexOf(i) < 0;});
ctrl.setItemsFn(filteredItems);
}
}
Expand Down Expand Up @@ -778,7 +778,7 @@ uis.directive('uiSelect',

//Multiple or Single depending if multiple attribute presence
if (angular.isDefined(tAttrs.multiple))
tElement.append("<ui-select-multiple/>").removeAttr('multiple');
tElement.append("<ui-select-multiple dict=\"" + tAttrs.dict + "\"/>").removeAttr('multiple');
else
tElement.append("<ui-select-single/>");

Expand All @@ -803,6 +803,9 @@ uis.directive('uiSelect',
$select.onSelectCallback = $parse(attrs.onSelect);
$select.onRemoveCallback = $parse(attrs.onRemove);

//Limit the number of selections allowed
$select.limit = (angular.isDefined(attrs.limit)) ? parseInt(attrs.limit, 10) : undefined;

//Set reference to ngModel from uiSelectCtrl
$select.ngModel = ngModel;

Expand Down Expand Up @@ -905,8 +908,8 @@ uis.directive('uiSelect',
if (!contains && !$select.clickTriggeredSelect) {
//Will lose focus only with certain targets
var focusableControls = ['input','button','textarea'];
var targetScope = angular.element(e.target).scope(); //To check if target is other ui-select
var skipFocusser = targetScope && targetScope.$select && targetScope.$select !== $select; //To check if target is other ui-select
var targetController = angular.element(e.target).controller('uiSelect'); //To check if target is other ui-select
var skipFocusser = targetController && targetController !== $select; //To check if target is other ui-select
if (!skipFocusser) skipFocusser = ~focusableControls.indexOf(e.target.tagName.toLowerCase()); //Check if target is input, button or textarea
$select.close(skipFocusser);
scope.$digest();
Expand Down Expand Up @@ -1087,7 +1090,7 @@ uis.directive('uiSelectMatch', ['uiSelectConfig', function(uiSelectConfig) {
};
}]);

uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelectMinErr, $timeout) {
uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', '$q', '$parse', function(uiSelectMinErr, $timeout, $q, $parse) {
return {
restrict: 'EA',
require: ['^uiSelect', '^ngModel'],
Expand Down Expand Up @@ -1145,7 +1148,7 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec

ctrl.getPlaceholder = function(){
//Refactor single?
if($select.selected.length) return;
if($select.selected && $select.selected.length) return;
return $select.placeholder;
};

Expand All @@ -1155,6 +1158,10 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec

link: function(scope, element, attrs, ctrls) {

if (angular.isDefined(attrs.dict)) {
scope.dict = $parse(attrs.dict)(scope);
}

var $select = ctrls[0];
var ngModel = scope.ngModel = ctrls[1];
var $selectMultiple = scope.$selectMultiple;
Expand All @@ -1167,22 +1174,31 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
//Input that will handle focus
$select.focusInput = $select.searchInput;

var parserFn = function (dict) {
return function() {
var locals = {},
result,
resultMultiple = [];
for (var j = $select.selected.length - 1; j >= 0; j--) {
locals = {};
locals[$select.parserResult.itemName] = $select.selected[j];
result = $select.parserResult.modelMapper(scope, locals);
resultMultiple.unshift(result ? result : $select.parserResult.modelMapper(scope, dict));
}
return resultMultiple;
};
};

//From view --> model
ngModel.$parsers.unshift(function () {
var locals = {},
result,
resultMultiple = [];
for (var j = $select.selected.length - 1; j >= 0; j--) {
locals = {};
locals[$select.parserResult.itemName] = $select.selected[j];
result = $select.parserResult.modelMapper(scope, locals);
resultMultiple.unshift(result);
}
return resultMultiple;
});
if (angular.isDefined(scope.dict)) {
$q.when(scope.dict).then(function(dict) {
ngModel.$parsers.unshift(parserFn(dict));
});
} else {
ngModel.$parsers.unshift(parserFn());
}

// From model --> view
ngModel.$formatters.unshift(function (inputValue) {
var formatterFn = function (inputValue, dict) {
var data = $select.parserResult.source (scope, { $select : {search:''}}), //Overwrite $search
locals = {},
result;
Expand Down Expand Up @@ -1214,12 +1230,30 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
//Check model array of all items available
if (!checkFnMultiple(data, inputValue[k])){
//If not found on previous lists, just add it directly to resultMultiple
resultMultiple.unshift(inputValue[k]);
// HACKHACK - bad code smell too many nested conditions!!!
if (angular.isDefined(dict)) {
if (!checkFnMultiple(dict, inputValue[k])) {
resultMultiple.unshift(inputValue[k]);
}
} else {
resultMultiple.unshift(inputValue[k]);
}
}
}
}
return resultMultiple;
});
};

// From model --> view
if (angular.isDefined(scope.dict)) {
$q.when(scope.dict).then(function(dict) {
ngModel.$formatters.unshift(function(inputValue) {
return formatterFn(inputValue, dict);
});
});
} else {
ngModel.$formatters.unshift(formatterFn);
}

//Watch for external model changes
scope.$watchCollection(function(){ return ngModel.$modelValue; }, function(newValue, oldValue) {
Expand All @@ -1244,6 +1278,9 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
};

scope.$on('uis:select', function (event, item) {
if($select.selected.length >= $select.limit) {
return;
}
$select.selected.push(item);
$selectMultiple.updateModel();
});
Expand Down Expand Up @@ -1488,6 +1525,7 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
}
};
}]);

uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $compile) {
return {
restrict: 'EA',
Expand Down
2 changes: 1 addition & 1 deletion dist/select.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/select.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ gulp.task('karma-watch', ['build'], function() {
var handleError = function (err) {
console.log(err.toString());
this.emit('end');
};
};
2 changes: 1 addition & 1 deletion src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ uis.directive('uiSelect',

//Multiple or Single depending if multiple attribute presence
if (angular.isDefined(tAttrs.multiple))
tElement.append("<ui-select-multiple/>").removeAttr('multiple');
tElement.append("<ui-select-multiple dict=\"" + tAttrs.dict + "\"/>").removeAttr('multiple');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should switch to single quote strings to avoid the awkward escaping of the double quote.

else
tElement.append("<ui-select-single/>");

Expand Down
65 changes: 48 additions & 17 deletions src/uiSelectMultipleDirective.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelectMinErr, $timeout) {
uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', '$q', '$parse', function(uiSelectMinErr, $timeout, $q, $parse) {
return {
restrict: 'EA',
require: ['^uiSelect', '^ngModel'],
Expand Down Expand Up @@ -66,6 +66,10 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec

link: function(scope, element, attrs, ctrls) {

if (angular.isDefined(attrs.dict)) {
scope.dict = $parse(attrs.dict)(scope);
}

var $select = ctrls[0];
var ngModel = scope.ngModel = ctrls[1];
var $selectMultiple = scope.$selectMultiple;
Expand All @@ -78,22 +82,31 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
//Input that will handle focus
$select.focusInput = $select.searchInput;

var parserFn = function (dict) {
return function() {
var locals = {},
result,
resultMultiple = [];
for (var j = $select.selected.length - 1; j >= 0; j--) {
locals = {};
locals[$select.parserResult.itemName] = $select.selected[j];
result = $select.parserResult.modelMapper(scope, locals);
resultMultiple.unshift(result ? result : $select.parserResult.modelMapper(scope, dict));
}
return resultMultiple;
};
};

//From view --> model
ngModel.$parsers.unshift(function () {
var locals = {},
result,
resultMultiple = [];
for (var j = $select.selected.length - 1; j >= 0; j--) {
locals = {};
locals[$select.parserResult.itemName] = $select.selected[j];
result = $select.parserResult.modelMapper(scope, locals);
resultMultiple.unshift(result);
}
return resultMultiple;
});
if (angular.isDefined(scope.dict)) {
$q.when(scope.dict).then(function(dict) {
ngModel.$parsers.unshift(parserFn(dict));
});
} else {
ngModel.$parsers.unshift(parserFn());
}

// From model --> view
ngModel.$formatters.unshift(function (inputValue) {
var formatterFn = function (inputValue, dict) {
var data = $select.parserResult.source (scope, { $select : {search:''}}), //Overwrite $search
locals = {},
result;
Expand Down Expand Up @@ -125,12 +138,30 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
//Check model array of all items available
if (!checkFnMultiple(data, inputValue[k])){
//If not found on previous lists, just add it directly to resultMultiple
resultMultiple.unshift(inputValue[k]);
// HACKHACK - bad code smell too many nested conditions!!!
if (angular.isDefined(dict)) {
if (!checkFnMultiple(dict, inputValue[k])) {
resultMultiple.unshift(inputValue[k]);
}
} else {
resultMultiple.unshift(inputValue[k]);
}
}
}
}
return resultMultiple;
});
};

// From model --> view
if (angular.isDefined(scope.dict)) {
$q.when(scope.dict).then(function(dict) {
ngModel.$formatters.unshift(function(inputValue) {
return formatterFn(inputValue, dict);
});
});
} else {
ngModel.$formatters.unshift(formatterFn);
}

//Watch for external model changes
scope.$watchCollection(function(){ return ngModel.$modelValue; }, function(newValue, oldValue) {
Expand Down