Skip to content

Commit ed98443

Browse files
committed
Added matcher example and reworked compat
The matcher example now matches the old matcher example, and the compatibility module has been turned into a function decorator instead of a class decorator.
1 parent 6e43367 commit ed98443

10 files changed

Lines changed: 466 additions & 176 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,7 @@ define('select2/data/select',[
968968
], function (BaseAdapter, Utils, $) {
969969
function SelectAdapter ($element, options) {
970970
this.$element = $element;
971+
this.options = options;
971972

972973
SelectAdapter.__super__.constructor.call(this);
973974
}
@@ -1143,34 +1144,9 @@ define('select2/data/select',[
11431144
};
11441145

11451146
SelectAdapter.prototype.matches = function (params, data) {
1146-
var match = $.extend(true, {}, data);
1147-
1148-
if (data.children) {
1149-
for (var c = data.children.length - 1; c >= 0; c--) {
1150-
var child = data.children[c];
1151-
1152-
var matches = this.matches(params, child);
1153-
1154-
// If there wasn't a match, remove the object in the array
1155-
if (matches === null) {
1156-
match.children.splice(c, 1);
1157-
}
1158-
}
1159-
1160-
if (match.children.length > 0) {
1161-
return match;
1162-
}
1163-
}
1164-
1165-
if ($.trim(params.term) === '') {
1166-
return match;
1167-
}
1147+
var matcher = this.options.get('matcher');
11681148

1169-
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
1170-
return match;
1171-
}
1172-
1173-
return null;
1149+
return matcher(params, data);
11741150
};
11751151

11761152
return SelectAdapter;
@@ -1660,6 +1636,49 @@ define('select2/i18n/en',[],function () {
16601636
};
16611637
});
16621638

1639+
define('select2/compat/matcher',[
1640+
1641+
], function () {
1642+
function oldMatcher (matcher) {
1643+
function wrappedMatcher (params, data) {
1644+
var match = $.extend(true, {}, data);
1645+
1646+
if (params.term == null || $.trim(params.term) === '') {
1647+
return match;
1648+
}
1649+
1650+
if (data.children) {
1651+
for (var c = data.children.length - 1; c >= 0; c--) {
1652+
var child = data.children[c];
1653+
1654+
// Check if the child object matches
1655+
// The old matcher returned a boolean true or false
1656+
var doesMatch = matcher(params.term, child.text, child);
1657+
1658+
// If the child didn't match, pop it off
1659+
if (!doesMatch) {
1660+
match.children.splice(c, 1);
1661+
}
1662+
}
1663+
1664+
if (match.children.length > 0) {
1665+
return match;
1666+
}
1667+
}
1668+
1669+
if (matcher(params.term, data.text, data)) {
1670+
return match;
1671+
}
1672+
1673+
return null;
1674+
}
1675+
1676+
return wrappedMatcher;
1677+
}
1678+
1679+
return oldMatcher;
1680+
});
1681+
16631682
define('select2/defaults',[
16641683
'jquery',
16651684
'./results',
@@ -1682,7 +1701,8 @@ define('select2/defaults',[
16821701
'./dropdown/hidePlaceholder',
16831702
'./dropdown/infiniteScroll',
16841703

1685-
'./i18n/en'
1704+
'./i18n/en',
1705+
'./compat/matcher'
16861706
], function ($, ResultsList,
16871707
SingleSelection, MultipleSelection, Placeholder,
16881708
Utils, Translation,
@@ -1791,8 +1811,40 @@ define('select2/defaults',[
17911811
};
17921812

17931813
Defaults.prototype.reset = function () {
1814+
function matcher (params, data) {
1815+
var match = $.extend(true, {}, data);
1816+
1817+
if (data.children) {
1818+
for (var c = data.children.length - 1; c >= 0; c--) {
1819+
var child = data.children[c];
1820+
1821+
var matches = matcher(params, child);
1822+
1823+
// If there wasn't a match, remove the object in the array
1824+
if (matches === null) {
1825+
match.children.splice(c, 1);
1826+
}
1827+
}
1828+
1829+
if (match.children.length > 0) {
1830+
return match;
1831+
}
1832+
}
1833+
1834+
if ($.trim(params.term) === '') {
1835+
return match;
1836+
}
1837+
1838+
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
1839+
return match;
1840+
}
1841+
1842+
return null;
1843+
}
1844+
17941845
this.defaults = {
17951846
language: ['select2/i18n/en'],
1847+
matcher: matcher,
17961848
minimumInputLength: 0,
17971849
templateResult: function (result) {
17981850
return result.text;

dist/js/select2.amd.js

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,7 @@ define('select2/data/select',[
968968
], function (BaseAdapter, Utils, $) {
969969
function SelectAdapter ($element, options) {
970970
this.$element = $element;
971+
this.options = options;
971972

972973
SelectAdapter.__super__.constructor.call(this);
973974
}
@@ -1143,34 +1144,9 @@ define('select2/data/select',[
11431144
};
11441145

11451146
SelectAdapter.prototype.matches = function (params, data) {
1146-
var match = $.extend(true, {}, data);
1147-
1148-
if (data.children) {
1149-
for (var c = data.children.length - 1; c >= 0; c--) {
1150-
var child = data.children[c];
1151-
1152-
var matches = this.matches(params, child);
1153-
1154-
// If there wasn't a match, remove the object in the array
1155-
if (matches === null) {
1156-
match.children.splice(c, 1);
1157-
}
1158-
}
1159-
1160-
if (match.children.length > 0) {
1161-
return match;
1162-
}
1163-
}
1164-
1165-
if ($.trim(params.term) === '') {
1166-
return match;
1167-
}
1147+
var matcher = this.options.get('matcher');
11681148

1169-
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
1170-
return match;
1171-
}
1172-
1173-
return null;
1149+
return matcher(params, data);
11741150
};
11751151

11761152
return SelectAdapter;
@@ -1660,6 +1636,49 @@ define('select2/i18n/en',[],function () {
16601636
};
16611637
});
16621638

1639+
define('select2/compat/matcher',[
1640+
1641+
], function () {
1642+
function oldMatcher (matcher) {
1643+
function wrappedMatcher (params, data) {
1644+
var match = $.extend(true, {}, data);
1645+
1646+
if (params.term == null || $.trim(params.term) === '') {
1647+
return match;
1648+
}
1649+
1650+
if (data.children) {
1651+
for (var c = data.children.length - 1; c >= 0; c--) {
1652+
var child = data.children[c];
1653+
1654+
// Check if the child object matches
1655+
// The old matcher returned a boolean true or false
1656+
var doesMatch = matcher(params.term, child.text, child);
1657+
1658+
// If the child didn't match, pop it off
1659+
if (!doesMatch) {
1660+
match.children.splice(c, 1);
1661+
}
1662+
}
1663+
1664+
if (match.children.length > 0) {
1665+
return match;
1666+
}
1667+
}
1668+
1669+
if (matcher(params.term, data.text, data)) {
1670+
return match;
1671+
}
1672+
1673+
return null;
1674+
}
1675+
1676+
return wrappedMatcher;
1677+
}
1678+
1679+
return oldMatcher;
1680+
});
1681+
16631682
define('select2/defaults',[
16641683
'jquery',
16651684
'./results',
@@ -1682,7 +1701,8 @@ define('select2/defaults',[
16821701
'./dropdown/hidePlaceholder',
16831702
'./dropdown/infiniteScroll',
16841703

1685-
'./i18n/en'
1704+
'./i18n/en',
1705+
'./compat/matcher'
16861706
], function ($, ResultsList,
16871707
SingleSelection, MultipleSelection, Placeholder,
16881708
Utils, Translation,
@@ -1791,8 +1811,40 @@ define('select2/defaults',[
17911811
};
17921812

17931813
Defaults.prototype.reset = function () {
1814+
function matcher (params, data) {
1815+
var match = $.extend(true, {}, data);
1816+
1817+
if (data.children) {
1818+
for (var c = data.children.length - 1; c >= 0; c--) {
1819+
var child = data.children[c];
1820+
1821+
var matches = matcher(params, child);
1822+
1823+
// If there wasn't a match, remove the object in the array
1824+
if (matches === null) {
1825+
match.children.splice(c, 1);
1826+
}
1827+
}
1828+
1829+
if (match.children.length > 0) {
1830+
return match;
1831+
}
1832+
}
1833+
1834+
if ($.trim(params.term) === '') {
1835+
return match;
1836+
}
1837+
1838+
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
1839+
return match;
1840+
}
1841+
1842+
return null;
1843+
}
1844+
17941845
this.defaults = {
17951846
language: ['select2/i18n/en'],
1847+
matcher: matcher,
17961848
minimumInputLength: 0,
17971849
templateResult: function (result) {
17981850
return result.text;

0 commit comments

Comments
 (0)