Skip to content

Commit 3e05e32

Browse files
committed
Small changes in preparation for more adapters
1 parent d57f8b2 commit 3e05e32

9 files changed

Lines changed: 105 additions & 40 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,17 @@ define('select2/data/select',[
245245
};
246246

247247
SelectAdapter.prototype.item = function ($option) {
248-
var data = {
249-
id: $option.val(),
250-
text: $option.html()
251-
};
248+
var data = $option.data("data");
249+
250+
// If the data has already be generated, use it
251+
if (data == null) {
252+
data = {
253+
id: $option.val(),
254+
text: $option.html()
255+
};
256+
257+
$option.data("data", data);
258+
}
252259

253260
return data;
254261
};
@@ -356,7 +363,7 @@ define('select2/results',[
356363
self.setClasses();
357364
})
358365

359-
this.$results.on("click", ".option", function (evt) {
366+
this.$results.on("mouseup", ".option", function (evt) {
360367
var $this = $(this);
361368

362369
var data = $this.data("data");
@@ -441,7 +448,7 @@ define('select2/selection/single',[
441448
SingleSelection.prototype.bind = function (container, $container) {
442449
var self = this;
443450

444-
this.$selection.on('click', function (evt) {
451+
this.$selection.on('mousedown', function (evt) {
445452
self.trigger("toggle", {
446453
originalEvent: evt
447454
});
@@ -561,7 +568,7 @@ define('select2/options',[
561568
function Options (options) {
562569
this.options = options;
563570

564-
this.dataAdapter = SelectData;
571+
this.dataAdapter = options.dataAdapter || SelectData;
565572
this.resultsAdapter = ResultsList;
566573
this.dropdownAdapter = options.dropdownAdapter || Dropdown;
567574
this.selectionAdapter = options.selectionAdapter;
@@ -660,12 +667,18 @@ define('select2/core',[
660667
// Set the initial state
661668

662669
this.data.current(function (initialData) {
663-
self.selection.update(initialData);
670+
self.trigger("selection:update", {
671+
data: initialData
672+
});
664673
});
665674

666675
this.data.query({}, function (data) {
667676
self.results.trigger("results:all", data);
668677
});
678+
679+
// Hide the original select
680+
681+
$element.hide();
669682
};
670683

671684
Utils.Extend(Select2, Utils.Observable);

dist/js/select2.amd.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,17 @@ define('select2/data/select',[
245245
};
246246

247247
SelectAdapter.prototype.item = function ($option) {
248-
var data = {
249-
id: $option.val(),
250-
text: $option.html()
251-
};
248+
var data = $option.data("data");
249+
250+
// If the data has already be generated, use it
251+
if (data == null) {
252+
data = {
253+
id: $option.val(),
254+
text: $option.html()
255+
};
256+
257+
$option.data("data", data);
258+
}
252259

253260
return data;
254261
};
@@ -356,7 +363,7 @@ define('select2/results',[
356363
self.setClasses();
357364
})
358365

359-
this.$results.on("click", ".option", function (evt) {
366+
this.$results.on("mouseup", ".option", function (evt) {
360367
var $this = $(this);
361368

362369
var data = $this.data("data");
@@ -441,7 +448,7 @@ define('select2/selection/single',[
441448
SingleSelection.prototype.bind = function (container, $container) {
442449
var self = this;
443450

444-
this.$selection.on('click', function (evt) {
451+
this.$selection.on('mousedown', function (evt) {
445452
self.trigger("toggle", {
446453
originalEvent: evt
447454
});
@@ -561,7 +568,7 @@ define('select2/options',[
561568
function Options (options) {
562569
this.options = options;
563570

564-
this.dataAdapter = SelectData;
571+
this.dataAdapter = options.dataAdapter || SelectData;
565572
this.resultsAdapter = ResultsList;
566573
this.dropdownAdapter = options.dropdownAdapter || Dropdown;
567574
this.selectionAdapter = options.selectionAdapter;
@@ -660,12 +667,18 @@ define('select2/core',[
660667
// Set the initial state
661668

662669
this.data.current(function (initialData) {
663-
self.selection.update(initialData);
670+
self.trigger("selection:update", {
671+
data: initialData
672+
});
664673
});
665674

666675
this.data.query({}, function (data) {
667676
self.results.trigger("results:all", data);
668677
});
678+
679+
// Hide the original select
680+
681+
$element.hide();
669682
};
670683

671684
Utils.Extend(Select2, Utils.Observable);

dist/js/select2.full.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9782,10 +9782,17 @@ define('select2/data/select',[
97829782
};
97839783

97849784
SelectAdapter.prototype.item = function ($option) {
9785-
var data = {
9786-
id: $option.val(),
9787-
text: $option.html()
9788-
};
9785+
var data = $option.data("data");
9786+
9787+
// If the data has already be generated, use it
9788+
if (data == null) {
9789+
data = {
9790+
id: $option.val(),
9791+
text: $option.html()
9792+
};
9793+
9794+
$option.data("data", data);
9795+
}
97899796

97909797
return data;
97919798
};
@@ -9893,7 +9900,7 @@ define('select2/results',[
98939900
self.setClasses();
98949901
})
98959902

9896-
this.$results.on("click", ".option", function (evt) {
9903+
this.$results.on("mouseup", ".option", function (evt) {
98979904
var $this = $(this);
98989905

98999906
var data = $this.data("data");
@@ -9978,7 +9985,7 @@ define('select2/selection/single',[
99789985
SingleSelection.prototype.bind = function (container, $container) {
99799986
var self = this;
99809987

9981-
this.$selection.on('click', function (evt) {
9988+
this.$selection.on('mousedown', function (evt) {
99829989
self.trigger("toggle", {
99839990
originalEvent: evt
99849991
});
@@ -10098,7 +10105,7 @@ define('select2/options',[
1009810105
function Options (options) {
1009910106
this.options = options;
1010010107

10101-
this.dataAdapter = SelectData;
10108+
this.dataAdapter = options.dataAdapter || SelectData;
1010210109
this.resultsAdapter = ResultsList;
1010310110
this.dropdownAdapter = options.dropdownAdapter || Dropdown;
1010410111
this.selectionAdapter = options.selectionAdapter;
@@ -10197,12 +10204,18 @@ define('select2/core',[
1019710204
// Set the initial state
1019810205

1019910206
this.data.current(function (initialData) {
10200-
self.selection.update(initialData);
10207+
self.trigger("selection:update", {
10208+
data: initialData
10209+
});
1020110210
});
1020210211

1020310212
this.data.query({}, function (data) {
1020410213
self.results.trigger("results:all", data);
1020510214
});
10215+
10216+
// Hide the original select
10217+
10218+
$element.hide();
1020610219
};
1020710220

1020810221
Utils.Extend(Select2, Utils.Observable);

dist/js/select2.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,17 @@ define('select2/data/select',[
673673
};
674674

675675
SelectAdapter.prototype.item = function ($option) {
676-
var data = {
677-
id: $option.val(),
678-
text: $option.html()
679-
};
676+
var data = $option.data("data");
677+
678+
// If the data has already be generated, use it
679+
if (data == null) {
680+
data = {
681+
id: $option.val(),
682+
text: $option.html()
683+
};
684+
685+
$option.data("data", data);
686+
}
680687

681688
return data;
682689
};
@@ -784,7 +791,7 @@ define('select2/results',[
784791
self.setClasses();
785792
})
786793

787-
this.$results.on("click", ".option", function (evt) {
794+
this.$results.on("mouseup", ".option", function (evt) {
788795
var $this = $(this);
789796

790797
var data = $this.data("data");
@@ -869,7 +876,7 @@ define('select2/selection/single',[
869876
SingleSelection.prototype.bind = function (container, $container) {
870877
var self = this;
871878

872-
this.$selection.on('click', function (evt) {
879+
this.$selection.on('mousedown', function (evt) {
873880
self.trigger("toggle", {
874881
originalEvent: evt
875882
});
@@ -989,7 +996,7 @@ define('select2/options',[
989996
function Options (options) {
990997
this.options = options;
991998

992-
this.dataAdapter = SelectData;
999+
this.dataAdapter = options.dataAdapter || SelectData;
9931000
this.resultsAdapter = ResultsList;
9941001
this.dropdownAdapter = options.dropdownAdapter || Dropdown;
9951002
this.selectionAdapter = options.selectionAdapter;
@@ -1088,12 +1095,18 @@ define('select2/core',[
10881095
// Set the initial state
10891096

10901097
this.data.current(function (initialData) {
1091-
self.selection.update(initialData);
1098+
self.trigger("selection:update", {
1099+
data: initialData
1100+
});
10921101
});
10931102

10941103
this.data.query({}, function (data) {
10951104
self.results.trigger("results:all", data);
10961105
});
1106+
1107+
// Hide the original select
1108+
1109+
$element.hide();
10971110
};
10981111

10991112
Utils.Extend(Select2, Utils.Observable);

src/js/select2/core.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,18 @@ define([
8080
// Set the initial state
8181

8282
this.data.current(function (initialData) {
83-
self.selection.update(initialData);
83+
self.trigger("selection:update", {
84+
data: initialData
85+
});
8486
});
8587

8688
this.data.query({}, function (data) {
8789
self.results.trigger("results:all", data);
8890
});
91+
92+
// Hide the original select
93+
94+
$element.hide();
8995
};
9096

9197
Utils.Extend(Select2, Utils.Observable);

src/js/select2/data/select.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,17 @@ define([
107107
};
108108

109109
SelectAdapter.prototype.item = function ($option) {
110-
var data = {
111-
id: $option.val(),
112-
text: $option.html()
113-
};
110+
var data = $option.data("data");
111+
112+
// If the data has already be generated, use it
113+
if (data == null) {
114+
data = {
115+
id: $option.val(),
116+
text: $option.html()
117+
};
118+
119+
$option.data("data", data);
120+
}
114121

115122
return data;
116123
};

src/js/select2/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define([
99
function Options (options) {
1010
this.options = options;
1111

12-
this.dataAdapter = SelectData;
12+
this.dataAdapter = options.dataAdapter || SelectData;
1313
this.resultsAdapter = ResultsList;
1414
this.dropdownAdapter = options.dropdownAdapter || Dropdown;
1515
this.selectionAdapter = options.selectionAdapter;

src/js/select2/results.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ define([
8686
self.setClasses();
8787
})
8888

89-
this.$results.on("click", ".option", function (evt) {
89+
this.$results.on("mouseup", ".option", function (evt) {
9090
var $this = $(this);
9191

9292
var data = $this.data("data");

src/js/select2/selection/single.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ define([
2525
SingleSelection.prototype.bind = function (container, $container) {
2626
var self = this;
2727

28-
this.$selection.on('click', function (evt) {
28+
this.$selection.on('mousedown', function (evt) {
2929
self.trigger("toggle", {
3030
originalEvent: evt
3131
});

0 commit comments

Comments
 (0)