Skip to content

Commit

Permalink
Merge pull request angular-ui#657 from catchshyam/master
Browse files Browse the repository at this point in the history
Enable tagging without multi selection (see angular-ui#597)
  • Loading branch information
Brian Feister committed Feb 16, 2015
2 parents d595a23 + 0af1915 commit 1ec798e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
15 changes: 15 additions & 0 deletions examples/demo-tagging.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ <h3>Object Tags with Tokenization (Space, Forward Slash, Comma)</h3>
</ui-select>
<p>Selected: {{multipleDemo.selectedPeople2}}</p>

<h3>Tagging without multiple</h3>
<ui-select tagging="tagTransform" ng-model="person.selected" on-select="addPerson($item, $model)" theme="bootstrap" ng-disabled="disabled" style="width: 800px;" title="Choose a person">
<ui-select-match placeholder="Select person...">{{$select.selected.name}} &lt;{{$select.selected.email}}&gt;</ui-select-match>
<ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-if="person.isTag" ng-bind-html="person.name +' <small>(new)</small>'| highlight: $select.search"></div>
<div ng-if="!person.isTag" ng-bind-html="person.name + person.isTag| highlight: $select.search"></div>
<small>
email: {{person.email}}
age: <span ng-bind-html="''+person.age | highlight: $select.search"></span>
</small>
</ui-select-choices>
</ui-select>
<p>Selected: {{person.selected}}</p>


<div style="height:500px"></div>

</body>
Expand Down
7 changes: 7 additions & 0 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ app.controller('DemoCtrl', function($scope, $http, $timeout) {
});
};

$scope.addPerson = function(item, model){
if(item.hasOwnProperty('isTag')) {
delete item.isTag;
$scope.people.push(item);
}
}

$scope.country = {};
$scope.countries = [ // Taken from https://gist.github.com/unceus/6501985
{name: 'Afghanistan', code: 'AF'},
Expand Down
38 changes: 20 additions & 18 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@
}
}
// search ctrl.selected for dupes potentially caused by tagging and return early if found
if ( ctrl.selected && ctrl.selected.filter( function (selection) { return angular.equals(selection, item); }).length > 0 ) {
if ( ctrl.selected && angular.isArray(ctrl.selected) && ctrl.selected.filter( function (selection) { return angular.equals(selection, item); }).length > 0 ) {
ctrl.close(skipFocusser);
return;
}
Expand Down Expand Up @@ -837,24 +837,26 @@
}

function _findApproxDupe(haystack, needle) {
var tempArr = angular.copy(haystack);
var dupeIndex = -1;
for (var i = 0; i <tempArr.length; i++) {
// handle the simple string version of tagging
if ( ctrl.tagging.fct === undefined ) {
// search the array for the match
if ( tempArr[i]+' '+ctrl.taggingLabel === needle ) {
dupeIndex = i;
}
// handle the object tagging implementation
} else {
var mockObj = tempArr[i];
mockObj.isTag = true;
if ( angular.equals(mockObj, needle) ) {
dupeIndex = i;
}
}
}
if(angular.isArray(haystack)) {
var tempArr = angular.copy(haystack);
for (var i = 0; i <tempArr.length; i++) {
// handle the simple string version of tagging
if ( ctrl.tagging.fct === undefined ) {
// search the array for the match
if ( tempArr[i]+' '+ctrl.taggingLabel === needle ) {
dupeIndex = i;
}
// handle the object tagging implementation
} else {
var mockObj = tempArr[i];
mockObj.isTag = true;
if ( angular.equals(mockObj, needle) ) {
dupeIndex = i;
}
}
}
}
return dupeIndex;
}

Expand Down

0 comments on commit 1ec798e

Please sign in to comment.