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

Feature/disable backspace reset #1481

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="0.14.10"></a>
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this will get created automatically at build time once we fix everything up.

Please remove these changes from the file.

## [0.14.10](https://github.com/angular-ui/ui-select/compare/v0.14.9...v0.14.10) (2016-03-09)


### Features

* **uiSelectSingleDirective:** add an option to avoid backspace model reset ([d94f076](https://github.com/angular-ui/ui-select/commit/d94f076)), closes [#926](https://github.com/angular-ui/ui-select/issues/926) [#525](https://github.com/angular-ui/ui-select/issues/525)



<a name="0.14.9"></a>
## [0.14.9](https://github.com/angular-ui/ui-select/compare/v0.14.9...v0.14.9) (2016-03-06)

Expand Down
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.14.8 - 2016-02-18T22:01:43.880Z
* Version: 0.14.10 - 2016-03-09T10:25:24.679Z
* License: MIT
*/

Expand Down
73 changes: 51 additions & 22 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.14.8 - 2016-02-18T22:01:43.792Z
* Version: 0.14.10 - 2016-03-09T10:25:24.529Z
* License: MIT
*/

Expand Down Expand Up @@ -113,7 +113,8 @@ var uis = angular.module('ui.select', [])
generateId: function() {
return latestId++;
},
appendToBody: false
appendToBody: false,
disableBackspaceReset: false
})

// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
Expand Down Expand Up @@ -482,7 +483,11 @@ uis.controller('uiSelectCtrl',
ctrl.setItemsFn(data);
}else{
if ( data !== undefined ) {
var filteredItems = data.filter(function(i) {return selectedItems && selectedItems.indexOf(i) < 0;});
var filteredItems = data.filter(function(i) {
return selectedItems.every(function(selectedItem) {
return !angular.equals(i, selectedItem);
});
});
ctrl.setItemsFn(filteredItems);
}
}
Expand Down Expand Up @@ -956,6 +961,11 @@ uis.directive('uiSelect',
$select.searchEnabled = searchEnabled !== undefined ? searchEnabled : uiSelectConfig.searchEnabled;
});

scope.$watch('disableBackspaceReset', function() {
var disableBackspaceReset = scope.$eval(attrs.disableBackspaceReset);
$select.disableBackspaceReset = disableBackspaceReset !== undefined ? disableBackspaceReset : uiSelectConfig.disableBackspaceReset;
});

scope.$watch('sortable', function() {
var sortable = scope.$eval(attrs.sortable);
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;
Expand Down Expand Up @@ -1580,9 +1590,19 @@ uis.directive('uiSelectMultiple', ['uiSelectMinErr','$timeout', function(uiSelec
stashArr = stashArr.slice(1,stashArr.length);
}
newItem = $select.tagging.fct($select.search);
newItem.isTag = true;
// verify the the tag doesn't match the value of an existing item
if ( stashArr.filter( function (origItem) { return angular.equals( origItem, $select.tagging.fct($select.search) ); } ).length > 0 ) {
// verify the new tag doesn't match the value of a possible selection choice or an already selected item.
if (
stashArr.some(function (origItem) {
return angular.equals(origItem, $select.tagging.fct($select.search));
}) ||
$select.selected.some(function (origItem) {
return angular.equals(origItem, newItem);
})
) {
scope.$evalAsync(function () {
$select.activeIndex = 0;
$select.items = items;
});
return;
}
newItem.isTag = true;
Expand Down Expand Up @@ -1778,8 +1798,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
});
});
focusser.bind("keydown", function(e){

if (e.which === KEY.BACKSPACE) {
if (e.which === KEY.BACKSPACE && $select.disableBackspaceReset === false) {
e.preventDefault();
e.stopPropagation();
$select.select(undefined);
Expand Down Expand Up @@ -1977,38 +1996,48 @@ uis.service('uisRepeatParser', ['uiSelectMinErr','$parse', function(uiSelectMinE


var match;
var isObjectCollection = /\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)/.test(expression);
//var isObjectCollection = /\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)/.test(expression);
// If an array is used as collection

// if (isObjectCollection){
//00000000000000000000000000000111111111000000000000000222222222222220033333333333333333333330000444444444444444444000000000000000556666660000077777777777755000000000000000000000088888880000000
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*$/);
// 000000000000000000000000000000111111111000000000000000222222222222220033333333333333333333330000444444444444444444000000000000000055555555555000000000000000000000066666666600000000
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*$/);

// 1 Alias
// 2 Item
// 3 Key on (key,value)
// 4 Value on (key,value)
// 5 Collection expresion (only used when using an array collection)
// 6 Object that will be converted to Array when using (key,value) syntax
// 7 Filters that will be applied to #6 when using (key,value) syntax
// 8 Track by
// 5 Source expression (including filters)
// 6 Track by

if (!match) {
throw uiSelectMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
expression);
}
if (!match[6] && isObjectCollection) {
throw uiSelectMinErr('iexp', "Expected expression in form of '_item_ as (_key_, _item_) in _ObjCollection_ [ track by _id_]' but got '{0}'.",
expression);

var source = match[5],
filters = '';

// When using (key,value) ui-select requires filters to be extracted, since the object
// is converted to an array for $select.items
// (in which case the filters need to be reapplied)
if (match[3]) {
// Remove any enclosing parenthesis
source = match[5].replace(/(^\()|(\)$)/g, '');
// match all after | but not after ||
var filterMatch = match[5].match(/^\s*(?:[\s\S]+?)(?:[^\|]|\|\|)+([\s\S]*)\s*$/);
if(filterMatch && filterMatch[1].trim()) {
filters = filterMatch[1];
source = source.replace(filters, '');
}
}

return {
itemName: match[4] || match[2], // (lhs) Left-hand side,
keyName: match[3], //for (key, value) syntax
source: $parse(!match[3] ? match[5] : match[6]),
sourceName: match[6],
filters: match[7],
trackByExp: match[8],
source: $parse(source),
filters: filters,
trackByExp: match[6],
modelMapper: $parse(match[1] || match[4] || match[2]),
repeatExpression: function (grouped) {
var expression = this.itemName + ' in ' + (grouped ? '$group.items' : '$select.items');
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "git://github.com/angular-ui/ui-select.git"
},
"style": "dist/select.css",
"version": "0.14.10",
"version": "0.15.0",
"devDependencies": {
"bower": "~1.3",
"conventional-changelog": "^0.5.3",
Expand Down
3 changes: 2 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ var uis = angular.module('ui.select', [])
generateId: function() {
return latestId++;
},
appendToBody: false
appendToBody: false,
disableBackspaceReset: false
})

// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
Expand Down
5 changes: 5 additions & 0 deletions src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ uis.directive('uiSelect',
$select.searchEnabled = searchEnabled !== undefined ? searchEnabled : uiSelectConfig.searchEnabled;
});

scope.$watch('disableBackspaceReset', function() {
var disableBackspaceReset = scope.$eval(attrs.disableBackspaceReset);
$select.disableBackspaceReset = disableBackspaceReset !== undefined ? disableBackspaceReset : uiSelectConfig.disableBackspaceReset;
});

scope.$watch('sortable', function() {
var sortable = scope.$eval(attrs.sortable);
$select.sortable = sortable !== undefined ? sortable : uiSelectConfig.sortable;
Expand Down
3 changes: 1 addition & 2 deletions src/uiSelectSingleDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ uis.directive('uiSelectSingle', ['$timeout','$compile', function($timeout, $comp
});
});
focusser.bind("keydown", function(e){

if (e.which === KEY.BACKSPACE) {
if (e.which === KEY.BACKSPACE && $select.disableBackspaceReset === false) {
e.preventDefault();
e.stopPropagation();
$select.select(undefined);
Expand Down
24 changes: 24 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('ui-select tests', function() {
if (attrs.appendToBody !== undefined) { attrsHtml += ' append-to-body="' + attrs.appendToBody + '"'; }
if (attrs.allowClear !== undefined) { matchAttrsHtml += ' allow-clear="' + attrs.allowClear + '"';}
if (attrs.inputId !== undefined) { attrsHtml += ' input-id="' + attrs.inputId + '"'; }
if (attrs.disableBackspaceReset !== undefined) { attrsHtml += ' disable-backspace-reset="' + attrs.disableBackspaceReset + '"';}
}

return compileTemplate(
Expand Down Expand Up @@ -1561,6 +1562,29 @@ describe('ui-select tests', function() {

});

describe('disable backspace reset option', function(){

it('should undefined model when pressing BACKSPACE key if disableBackspaceReset=false', function() {
var el = createUiSelect();
var focusserInput = el.find('.ui-select-focusser');

clickItem(el, 'Samantha');
triggerKeydown(focusserInput, Key.Backspace);

expect(scope.selection.selected).toBeUndefined();
});

it('should NOT reset model when pressing BACKSPACE key if disableBackspaceReset=true', function() {
var el = createUiSelect({disableBackspaceReset: true});
var focusserInput = el.find('.ui-select-focusser');

clickItem(el, 'Samantha');
triggerKeydown(focusserInput, Key.Backspace);

expect(scope.selection.selected).toBe(scope.people[5]);
});

});

describe('multi selection', function() {

Expand Down