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

Commit d94f076

Browse files
author
Pierre Gasté
committed
feat(uiSelectSingleDirective): add an option to avoid backspace model reset
Currently, when you select an option and you press the backspace key, it will reset the model. With this option, we could disable this behavior to avoid the model resetting. Closes #926 #525
1 parent 9230b43 commit d94f076

8 files changed

+87
-29
lines changed

dist/select.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* ui-select
33
* http://github.com/angular-ui/ui-select
4-
* Version: 0.14.8 - 2016-02-18T22:01:43.880Z
4+
* Version: 0.14.10 - 2016-03-09T10:25:24.679Z
55
* License: MIT
66
*/
77

dist/select.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* ui-select
33
* http://github.com/angular-ui/ui-select
4-
* Version: 0.14.8 - 2016-02-18T22:01:43.792Z
4+
* Version: 0.14.10 - 2016-03-09T10:25:24.529Z
55
* License: MIT
66
*/
77

@@ -113,7 +113,8 @@ var uis = angular.module('ui.select', [])
113113
generateId: function() {
114114
return latestId++;
115115
},
116-
appendToBody: false
116+
appendToBody: false,
117+
disableBackspaceReset: false
117118
})
118119

119120
// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
@@ -482,7 +483,11 @@ uis.controller('uiSelectCtrl',
482483
ctrl.setItemsFn(data);
483484
}else{
484485
if ( data !== undefined ) {
485-
var filteredItems = data.filter(function(i) {return selectedItems && selectedItems.indexOf(i) < 0;});
486+
var filteredItems = data.filter(function(i) {
487+
return selectedItems.every(function(selectedItem) {
488+
return !angular.equals(i, selectedItem);
489+
});
490+
});
486491
ctrl.setItemsFn(filteredItems);
487492
}
488493
}
@@ -956,6 +961,11 @@ uis.directive('uiSelect',
956961
$select.searchEnabled = searchEnabled !== undefined ? searchEnabled : uiSelectConfig.searchEnabled;
957962
});
958963

964+
scope.$watch('disableBackspaceReset', function() {
965+
var disableBackspaceReset = scope.$eval(attrs.disableBackspaceReset);
966+
$select.disableBackspaceReset = disableBackspaceReset !== undefined ? disableBackspaceReset : uiSelectConfig.disableBackspaceReset;
967+
});
968+
959969
scope.$watch('sortable', function() {
960970
var sortable = scope.$eval(attrs.sortable);
961971
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;
@@ -1580,9 +1590,19 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
15801590
stashArr = stashArr.slice(1,stashArr.length);
15811591
}
15821592
newItem = $select.tagging.fct($select.search);
1583-
newItem.isTag = true;
1584-
// verify the the tag doesn't match the value of an existing item
1585-
if ( stashArr.filter( function (origItem) { return angular.equals( origItem, $select.tagging.fct($select.search) ); } ).length > 0 ) {
1593+
// verify the new tag doesn't match the value of a possible selection choice or an already selected item.
1594+
if (
1595+
stashArr.some(function (origItem) {
1596+
return angular.equals(origItem, $select.tagging.fct($select.search));
1597+
}) ||
1598+
$select.selected.some(function (origItem) {
1599+
return angular.equals(origItem, newItem);
1600+
})
1601+
) {
1602+
scope.$evalAsync(function () {
1603+
$select.activeIndex = 0;
1604+
$select.items = items;
1605+
});
15861606
return;
15871607
}
15881608
newItem.isTag = true;
@@ -1778,8 +1798,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
17781798
});
17791799
});
17801800
focusser.bind("keydown", function(e){
1781-
1782-
if (e.which === KEY.BACKSPACE) {
1801+
if (e.which === KEY.BACKSPACE && $select.disableBackspaceReset === false) {
17831802
e.preventDefault();
17841803
e.stopPropagation();
17851804
$select.select(undefined);
@@ -1977,38 +1996,48 @@ uis.service('uisRepeatParser', ['uiSelectMinErr','$parse', function(uiSelectMinE
19771996

19781997

19791998
var match;
1980-
var isObjectCollection = /\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)/.test(expression);
1999+
//var isObjectCollection = /\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)/.test(expression);
19812000
// If an array is used as collection
19822001

19832002
// if (isObjectCollection){
1984-
//00000000000000000000000000000111111111000000000000000222222222222220033333333333333333333330000444444444444444444000000000000000556666660000077777777777755000000000000000000000088888880000000
1985-
match = expression.match(/^\s*(?:([\s\S]+?)\s+as\s+)?(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(([\w\.]+)?\s*(|\s*[\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
2003+
// 000000000000000000000000000000111111111000000000000000222222222222220033333333333333333333330000444444444444444444000000000000000055555555555000000000000000000000066666666600000000
2004+
match = expression.match(/^\s*(?:([\s\S]+?)\s+as\s+)?(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(\s*[\s\S]+?)?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
19862005

19872006
// 1 Alias
19882007
// 2 Item
19892008
// 3 Key on (key,value)
19902009
// 4 Value on (key,value)
1991-
// 5 Collection expresion (only used when using an array collection)
1992-
// 6 Object that will be converted to Array when using (key,value) syntax
1993-
// 7 Filters that will be applied to #6 when using (key,value) syntax
1994-
// 8 Track by
2010+
// 5 Source expression (including filters)
2011+
// 6 Track by
19952012

19962013
if (!match) {
19972014
throw uiSelectMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
19982015
expression);
19992016
}
2000-
if (!match[6] && isObjectCollection) {
2001-
throw uiSelectMinErr('iexp', "Expected expression in form of '_item_ as (_key_, _item_) in _ObjCollection_ [ track by _id_]' but got '{0}'.",
2002-
expression);
2017+
2018+
var source = match[5],
2019+
filters = '';
2020+
2021+
// When using (key,value) ui-select requires filters to be extracted, since the object
2022+
// is converted to an array for $select.items
2023+
// (in which case the filters need to be reapplied)
2024+
if (match[3]) {
2025+
// Remove any enclosing parenthesis
2026+
source = match[5].replace(/(^\()|(\)$)/g, '');
2027+
// match all after | but not after ||
2028+
var filterMatch = match[5].match(/^\s*(?:[\s\S]+?)(?:[^\|]|\|\|)+([\s\S]*)\s*$/);
2029+
if(filterMatch && filterMatch[1].trim()) {
2030+
filters = filterMatch[1];
2031+
source = source.replace(filters, '');
2032+
}
20032033
}
20042034

20052035
return {
20062036
itemName: match[4] || match[2], // (lhs) Left-hand side,
20072037
keyName: match[3], //for (key, value) syntax
2008-
source: $parse(!match[3] ? match[5] : match[6]),
2009-
sourceName: match[6],
2010-
filters: match[7],
2011-
trackByExp: match[8],
2038+
source: $parse(source),
2039+
filters: filters,
2040+
trackByExp: match[6],
20122041
modelMapper: $parse(match[1] || match[4] || match[2]),
20132042
repeatExpression: function (grouped) {
20142043
var expression = this.itemName + ' in ' + (grouped ? '$group.items' : '$select.items');

dist/select.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/select.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ var uis = angular.module('ui.select', [])
103103
generateId: function() {
104104
return latestId++;
105105
},
106-
appendToBody: false
106+
appendToBody: false,
107+
disableBackspaceReset: false
107108
})
108109

109110
// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913

src/uiSelectDirective.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ uis.directive('uiSelect',
7777
$select.searchEnabled = searchEnabled !== undefined ? searchEnabled : uiSelectConfig.searchEnabled;
7878
});
7979

80+
scope.$watch('disableBackspaceReset', function() {
81+
var disableBackspaceReset = scope.$eval(attrs.disableBackspaceReset);
82+
$select.disableBackspaceReset = disableBackspaceReset !== undefined ? disableBackspaceReset : uiSelectConfig.disableBackspaceReset;
83+
});
84+
8085
scope.$watch('sortable', function() {
8186
var sortable = scope.$eval(attrs.sortable);
8287
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;

src/uiSelectSingleDirective.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
8484
});
8585
});
8686
focusser.bind("keydown", function(e){
87-
88-
if (e.which === KEY.BACKSPACE) {
87+
if (e.which === KEY.BACKSPACE && $select.disableBackspaceReset === false) {
8988
e.preventDefault();
9089
e.stopPropagation();
9190
$select.select(undefined);

test/select.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ describe('ui-select tests', function() {
128128
if (attrs.appendToBody !== undefined) { attrsHtml += ' append-to-body="' + attrs.appendToBody + '"'; }
129129
if (attrs.allowClear !== undefined) { matchAttrsHtml += ' allow-clear="' + attrs.allowClear + '"';}
130130
if (attrs.inputId !== undefined) { attrsHtml += ' input-id="' + attrs.inputId + '"'; }
131+
if (attrs.disableBackspaceReset !== undefined) { attrsHtml += ' disable-backspace-reset="' + attrs.disableBackspaceReset + '"';}
131132
}
132133

133134
return compileTemplate(
@@ -1561,6 +1562,29 @@ describe('ui-select tests', function() {
15611562

15621563
});
15631564

1565+
describe('disable backspace reset option', function(){
1566+
1567+
it('should undefined model when pressing BACKSPACE key if disableBackspaceReset=false', function() {
1568+
var el = createUiSelect();
1569+
var focusserInput = el.find('.ui-select-focusser');
1570+
1571+
clickItem(el, 'Samantha');
1572+
triggerKeydown(focusserInput, Key.Backspace);
1573+
1574+
expect(scope.selection.selected).toBeUndefined();
1575+
});
1576+
1577+
it('should NOT reset model when pressing BACKSPACE key if disableBackspaceReset=true', function() {
1578+
var el = createUiSelect({disableBackspaceReset: true});
1579+
var focusserInput = el.find('.ui-select-focusser');
1580+
1581+
clickItem(el, 'Samantha');
1582+
triggerKeydown(focusserInput, Key.Backspace);
1583+
1584+
expect(scope.selection.selected).toBe(scope.people[5]);
1585+
});
1586+
1587+
});
15641588

15651589
describe('multi selection', function() {
15661590

0 commit comments

Comments
 (0)