Skip to content

Commit 315b1b3

Browse files
committed
Added array adapter
This should add support for dynamically generated array data that still works with a basic `<select>` element.
1 parent 3e05e32 commit 315b1b3

9 files changed

Lines changed: 455 additions & 7 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ define('select2/selection/single',[
449449
var self = this;
450450

451451
this.$selection.on('mousedown', function (evt) {
452+
// Only respond to left clicks
453+
if (evt.which !== 1) {
454+
return;
455+
}
456+
452457
self.trigger("toggle", {
453458
originalEvent: evt
454459
});
@@ -557,12 +562,74 @@ define('select2/selection/multiple',[
557562
return MultipleSelection;
558563
});
559564

565+
define('select2/data/array',[
566+
"./select",
567+
"../utils"
568+
], function (SelectAdapter, Utils) {
569+
function ArrayAdapter ($element, options) {
570+
this.data = options.options.data;
571+
this.selection = [];
572+
573+
ArrayAdapter.__super__.constructor.call(this, $element, options);
574+
}
575+
576+
Utils.Extend(ArrayAdapter, SelectAdapter);
577+
578+
ArrayAdapter.prototype.select = function (data) {
579+
var self = this;
580+
581+
this.$element.find("option").each(function () {
582+
var $option = $(this);
583+
var option = self.item($option);
584+
585+
if (option.id == data.id) {
586+
$option.remove();
587+
}
588+
});
589+
590+
var $option = this.option(data);
591+
592+
this.$element.append($option);
593+
594+
ArrayAdapter.__super__.select.call(this, data);
595+
}
596+
597+
ArrayAdapter.prototype.option = function (data) {
598+
var $option = $("<option></option>");
599+
600+
$option.text(data.text);
601+
$option.val(data.id);
602+
$option.data("data", data);
603+
604+
return $option;
605+
}
606+
607+
ArrayAdapter.prototype.query = function (params, callback) {
608+
var matches = [];
609+
var self = this;
610+
611+
$.each(this.data, function () {
612+
var option = this;
613+
614+
if (self.matches(params, option)) {
615+
matches.push(option);
616+
}
617+
});
618+
619+
callback(matches);
620+
}
621+
622+
return ArrayAdapter;
623+
});
624+
560625
define('select2/options',[
561626
'./data/select',
562627
'./results',
563628
'./dropdown',
564629
'./selection/single',
565-
'./selection/multiple'
630+
'./selection/multiple',
631+
632+
'./data/array'
566633
], function (SelectData, ResultsList, Dropdown, SingleSelection,
567634
MultipleSelection) {
568635
function Options (options) {

dist/js/select2.amd.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ define('select2/selection/single',[
449449
var self = this;
450450

451451
this.$selection.on('mousedown', function (evt) {
452+
// Only respond to left clicks
453+
if (evt.which !== 1) {
454+
return;
455+
}
456+
452457
self.trigger("toggle", {
453458
originalEvent: evt
454459
});
@@ -557,12 +562,74 @@ define('select2/selection/multiple',[
557562
return MultipleSelection;
558563
});
559564

565+
define('select2/data/array',[
566+
"./select",
567+
"../utils"
568+
], function (SelectAdapter, Utils) {
569+
function ArrayAdapter ($element, options) {
570+
this.data = options.options.data;
571+
this.selection = [];
572+
573+
ArrayAdapter.__super__.constructor.call(this, $element, options);
574+
}
575+
576+
Utils.Extend(ArrayAdapter, SelectAdapter);
577+
578+
ArrayAdapter.prototype.select = function (data) {
579+
var self = this;
580+
581+
this.$element.find("option").each(function () {
582+
var $option = $(this);
583+
var option = self.item($option);
584+
585+
if (option.id == data.id) {
586+
$option.remove();
587+
}
588+
});
589+
590+
var $option = this.option(data);
591+
592+
this.$element.append($option);
593+
594+
ArrayAdapter.__super__.select.call(this, data);
595+
}
596+
597+
ArrayAdapter.prototype.option = function (data) {
598+
var $option = $("<option></option>");
599+
600+
$option.text(data.text);
601+
$option.val(data.id);
602+
$option.data("data", data);
603+
604+
return $option;
605+
}
606+
607+
ArrayAdapter.prototype.query = function (params, callback) {
608+
var matches = [];
609+
var self = this;
610+
611+
$.each(this.data, function () {
612+
var option = this;
613+
614+
if (self.matches(params, option)) {
615+
matches.push(option);
616+
}
617+
});
618+
619+
callback(matches);
620+
}
621+
622+
return ArrayAdapter;
623+
});
624+
560625
define('select2/options',[
561626
'./data/select',
562627
'./results',
563628
'./dropdown',
564629
'./selection/single',
565-
'./selection/multiple'
630+
'./selection/multiple',
631+
632+
'./data/array'
566633
], function (SelectData, ResultsList, Dropdown, SingleSelection,
567634
MultipleSelection) {
568635
function Options (options) {

dist/js/select2.full.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9986,6 +9986,11 @@ define('select2/selection/single',[
99869986
var self = this;
99879987

99889988
this.$selection.on('mousedown', function (evt) {
9989+
// Only respond to left clicks
9990+
if (evt.which !== 1) {
9991+
return;
9992+
}
9993+
99899994
self.trigger("toggle", {
99909995
originalEvent: evt
99919996
});
@@ -10094,12 +10099,74 @@ define('select2/selection/multiple',[
1009410099
return MultipleSelection;
1009510100
});
1009610101

10102+
define('select2/data/array',[
10103+
"./select",
10104+
"../utils"
10105+
], function (SelectAdapter, Utils) {
10106+
function ArrayAdapter ($element, options) {
10107+
this.data = options.options.data;
10108+
this.selection = [];
10109+
10110+
ArrayAdapter.__super__.constructor.call(this, $element, options);
10111+
}
10112+
10113+
Utils.Extend(ArrayAdapter, SelectAdapter);
10114+
10115+
ArrayAdapter.prototype.select = function (data) {
10116+
var self = this;
10117+
10118+
this.$element.find("option").each(function () {
10119+
var $option = $(this);
10120+
var option = self.item($option);
10121+
10122+
if (option.id == data.id) {
10123+
$option.remove();
10124+
}
10125+
});
10126+
10127+
var $option = this.option(data);
10128+
10129+
this.$element.append($option);
10130+
10131+
ArrayAdapter.__super__.select.call(this, data);
10132+
}
10133+
10134+
ArrayAdapter.prototype.option = function (data) {
10135+
var $option = $("<option></option>");
10136+
10137+
$option.text(data.text);
10138+
$option.val(data.id);
10139+
$option.data("data", data);
10140+
10141+
return $option;
10142+
}
10143+
10144+
ArrayAdapter.prototype.query = function (params, callback) {
10145+
var matches = [];
10146+
var self = this;
10147+
10148+
$.each(this.data, function () {
10149+
var option = this;
10150+
10151+
if (self.matches(params, option)) {
10152+
matches.push(option);
10153+
}
10154+
});
10155+
10156+
callback(matches);
10157+
}
10158+
10159+
return ArrayAdapter;
10160+
});
10161+
1009710162
define('select2/options',[
1009810163
'./data/select',
1009910164
'./results',
1010010165
'./dropdown',
1010110166
'./selection/single',
10102-
'./selection/multiple'
10167+
'./selection/multiple',
10168+
10169+
'./data/array'
1010310170
], function (SelectData, ResultsList, Dropdown, SingleSelection,
1010410171
MultipleSelection) {
1010510172
function Options (options) {

dist/js/select2.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ define('select2/selection/single',[
877877
var self = this;
878878

879879
this.$selection.on('mousedown', function (evt) {
880+
// Only respond to left clicks
881+
if (evt.which !== 1) {
882+
return;
883+
}
884+
880885
self.trigger("toggle", {
881886
originalEvent: evt
882887
});
@@ -985,12 +990,74 @@ define('select2/selection/multiple',[
985990
return MultipleSelection;
986991
});
987992

993+
define('select2/data/array',[
994+
"./select",
995+
"../utils"
996+
], function (SelectAdapter, Utils) {
997+
function ArrayAdapter ($element, options) {
998+
this.data = options.options.data;
999+
this.selection = [];
1000+
1001+
ArrayAdapter.__super__.constructor.call(this, $element, options);
1002+
}
1003+
1004+
Utils.Extend(ArrayAdapter, SelectAdapter);
1005+
1006+
ArrayAdapter.prototype.select = function (data) {
1007+
var self = this;
1008+
1009+
this.$element.find("option").each(function () {
1010+
var $option = $(this);
1011+
var option = self.item($option);
1012+
1013+
if (option.id == data.id) {
1014+
$option.remove();
1015+
}
1016+
});
1017+
1018+
var $option = this.option(data);
1019+
1020+
this.$element.append($option);
1021+
1022+
ArrayAdapter.__super__.select.call(this, data);
1023+
}
1024+
1025+
ArrayAdapter.prototype.option = function (data) {
1026+
var $option = $("<option></option>");
1027+
1028+
$option.text(data.text);
1029+
$option.val(data.id);
1030+
$option.data("data", data);
1031+
1032+
return $option;
1033+
}
1034+
1035+
ArrayAdapter.prototype.query = function (params, callback) {
1036+
var matches = [];
1037+
var self = this;
1038+
1039+
$.each(this.data, function () {
1040+
var option = this;
1041+
1042+
if (self.matches(params, option)) {
1043+
matches.push(option);
1044+
}
1045+
});
1046+
1047+
callback(matches);
1048+
}
1049+
1050+
return ArrayAdapter;
1051+
});
1052+
9881053
define('select2/options',[
9891054
'./data/select',
9901055
'./results',
9911056
'./dropdown',
9921057
'./selection/single',
993-
'./selection/multiple'
1058+
'./selection/multiple',
1059+
1060+
'./data/array'
9941061
], function (SelectData, ResultsList, Dropdown, SingleSelection,
9951062
MultipleSelection) {
9961063
function Options (options) {

0 commit comments

Comments
 (0)