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

fix(parser) allow track by without filter #1477

Merged
merged 2 commits into from
Mar 6, 2016
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<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)




<a name="0.14.8"></a>
## [0.14.8](https://github.com/angular-ui/ui-select/compare/v0.14.8...v0.14.8) (2016-02-18)

Expand Down
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.9",
"version": "0.14.10",
"devDependencies": {
"bower": "~1.3",
"conventional-changelog": "^0.5.3",
Expand Down
38 changes: 24 additions & 14 deletions src/uisRepeatParserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,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
26 changes: 26 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,32 @@ describe('ui-select tests', function() {

});

it('should parse simple property binding repeat syntax with a basic filter', function () {

var locals = {};
locals.people = [{ name: 'Wladimir' }, { name: 'Samantha' }];
locals.person = locals.people[1];

var parserResult = uisRepeatParser.parse('person.name as person in people | filter: { name: \'Samantha\' }');
expect(parserResult.itemName).toBe('person');
expect(parserResult.modelMapper(locals)).toBe(locals.person.name);
expect(parserResult.source(locals)).toEqual([locals.person]);

});

it('should parse simple property binding repeat syntax with track by', function () {

var locals = {};
locals.people = [{ name: 'Wladimir' }, { name: 'Samantha' }];
locals.person = locals.people[0];

var parserResult = uisRepeatParser.parse('person.name as person in people track by person.name');
expect(parserResult.itemName).toBe('person');
expect(parserResult.modelMapper(locals)).toBe(locals.person.name);
expect(parserResult.source(locals)).toBe(locals.people);

});

it('should parse (key, value) repeat syntax', function() {

var locals = {};
Expand Down