Skip to content

Commit 5dd7df3

Browse files
committed
Allow programmatic access of Select2
1 parent 5dd5ad9 commit 5dd7df3

9 files changed

Lines changed: 177 additions & 42 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,10 +1528,10 @@ define('select2/core',[
15281528
});
15291529

15301530
this.selection.on('open', function () {
1531-
self.trigger('open');
1531+
self.open();
15321532
});
15331533
this.selection.on('close', function () {
1534-
self.trigger('close');
1534+
self.close();
15351535
});
15361536
this.selection.on('toggle', function () {
15371537
self.toggleDropdown();
@@ -1550,19 +1550,19 @@ define('select2/core',[
15501550
this.selection.on('unselected', function (params) {
15511551
self.trigger('unselect', params);
15521552

1553-
self.trigger('close');
1553+
self.close();
15541554
});
15551555

15561556
this.results.on('selected', function (params) {
15571557
self.trigger('select', params);
15581558

1559-
self.trigger('close');
1559+
self.close();
15601560
});
15611561

15621562
this.results.on('unselected', function (params) {
15631563
self.trigger('unselect', params);
15641564

1565-
self.trigger('close');
1565+
self.close();
15661566
});
15671567

15681568
this.results.on('results:focus', function (params) {
@@ -1600,16 +1600,34 @@ define('select2/core',[
16001600

16011601
$element.hide();
16021602
$element.attr('tabindex', '-1');
1603+
1604+
$element.data('select2', this);
16031605
};
16041606

16051607
Utils.Extend(Select2, Utils.Observable);
16061608

16071609
Select2.prototype.toggleDropdown = function () {
16081610
if (this.isOpen()) {
1609-
this.trigger('close');
1611+
this.close();
16101612
} else {
1611-
this.trigger('open');
1613+
this.open();
1614+
}
1615+
};
1616+
1617+
Select2.prototype.open = function () {
1618+
if (this.isOpen()) {
1619+
return;
16121620
}
1621+
1622+
this.trigger('open');
1623+
};
1624+
1625+
Select2.prototype.close = function () {
1626+
if (!this.isOpen()) {
1627+
return;
1628+
}
1629+
1630+
this.trigger('close');
16131631
};
16141632

16151633
Select2.prototype.isOpen = function () {
@@ -1644,8 +1662,9 @@ define('jquery.select2',[
16441662
});
16451663
} else if (typeof options === 'string') {
16461664
var instance = this.data('select2');
1665+
var args = Array.prototype.slice.call(arguments, 1);
16471666

1648-
instance[options](arguments.slice(1));
1667+
instance[options](args);
16491668
} else {
16501669
throw new Error('Invalid arguments for Select2: ' + options);
16511670
}

dist/js/select2.amd.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,10 +1528,10 @@ define('select2/core',[
15281528
});
15291529

15301530
this.selection.on('open', function () {
1531-
self.trigger('open');
1531+
self.open();
15321532
});
15331533
this.selection.on('close', function () {
1534-
self.trigger('close');
1534+
self.close();
15351535
});
15361536
this.selection.on('toggle', function () {
15371537
self.toggleDropdown();
@@ -1550,19 +1550,19 @@ define('select2/core',[
15501550
this.selection.on('unselected', function (params) {
15511551
self.trigger('unselect', params);
15521552

1553-
self.trigger('close');
1553+
self.close();
15541554
});
15551555

15561556
this.results.on('selected', function (params) {
15571557
self.trigger('select', params);
15581558

1559-
self.trigger('close');
1559+
self.close();
15601560
});
15611561

15621562
this.results.on('unselected', function (params) {
15631563
self.trigger('unselect', params);
15641564

1565-
self.trigger('close');
1565+
self.close();
15661566
});
15671567

15681568
this.results.on('results:focus', function (params) {
@@ -1600,16 +1600,34 @@ define('select2/core',[
16001600

16011601
$element.hide();
16021602
$element.attr('tabindex', '-1');
1603+
1604+
$element.data('select2', this);
16031605
};
16041606

16051607
Utils.Extend(Select2, Utils.Observable);
16061608

16071609
Select2.prototype.toggleDropdown = function () {
16081610
if (this.isOpen()) {
1609-
this.trigger('close');
1611+
this.close();
16101612
} else {
1611-
this.trigger('open');
1613+
this.open();
1614+
}
1615+
};
1616+
1617+
Select2.prototype.open = function () {
1618+
if (this.isOpen()) {
1619+
return;
16121620
}
1621+
1622+
this.trigger('open');
1623+
};
1624+
1625+
Select2.prototype.close = function () {
1626+
if (!this.isOpen()) {
1627+
return;
1628+
}
1629+
1630+
this.trigger('close');
16131631
};
16141632

16151633
Select2.prototype.isOpen = function () {
@@ -1644,8 +1662,9 @@ define('jquery.select2',[
16441662
});
16451663
} else if (typeof options === 'string') {
16461664
var instance = this.data('select2');
1665+
var args = Array.prototype.slice.call(arguments, 1);
16471666

1648-
instance[options](arguments.slice(1));
1667+
instance[options](args);
16491668
} else {
16501669
throw new Error('Invalid arguments for Select2: ' + options);
16511670
}

dist/js/select2.full.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11066,10 +11066,10 @@ define('select2/core',[
1106611066
});
1106711067

1106811068
this.selection.on('open', function () {
11069-
self.trigger('open');
11069+
self.open();
1107011070
});
1107111071
this.selection.on('close', function () {
11072-
self.trigger('close');
11072+
self.close();
1107311073
});
1107411074
this.selection.on('toggle', function () {
1107511075
self.toggleDropdown();
@@ -11088,19 +11088,19 @@ define('select2/core',[
1108811088
this.selection.on('unselected', function (params) {
1108911089
self.trigger('unselect', params);
1109011090

11091-
self.trigger('close');
11091+
self.close();
1109211092
});
1109311093

1109411094
this.results.on('selected', function (params) {
1109511095
self.trigger('select', params);
1109611096

11097-
self.trigger('close');
11097+
self.close();
1109811098
});
1109911099

1110011100
this.results.on('unselected', function (params) {
1110111101
self.trigger('unselect', params);
1110211102

11103-
self.trigger('close');
11103+
self.close();
1110411104
});
1110511105

1110611106
this.results.on('results:focus', function (params) {
@@ -11138,16 +11138,34 @@ define('select2/core',[
1113811138

1113911139
$element.hide();
1114011140
$element.attr('tabindex', '-1');
11141+
11142+
$element.data('select2', this);
1114111143
};
1114211144

1114311145
Utils.Extend(Select2, Utils.Observable);
1114411146

1114511147
Select2.prototype.toggleDropdown = function () {
1114611148
if (this.isOpen()) {
11147-
this.trigger('close');
11149+
this.close();
1114811150
} else {
11149-
this.trigger('open');
11151+
this.open();
11152+
}
11153+
};
11154+
11155+
Select2.prototype.open = function () {
11156+
if (this.isOpen()) {
11157+
return;
1115011158
}
11159+
11160+
this.trigger('open');
11161+
};
11162+
11163+
Select2.prototype.close = function () {
11164+
if (!this.isOpen()) {
11165+
return;
11166+
}
11167+
11168+
this.trigger('close');
1115111169
};
1115211170

1115311171
Select2.prototype.isOpen = function () {
@@ -11182,8 +11200,9 @@ define('jquery.select2',[
1118211200
});
1118311201
} else if (typeof options === 'string') {
1118411202
var instance = this.data('select2');
11203+
var args = Array.prototype.slice.call(arguments, 1);
1118511204

11186-
instance[options](arguments.slice(1));
11205+
instance[options](args);
1118711206
} else {
1118811207
throw new Error('Invalid arguments for Select2: ' + options);
1118911208
}

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,10 +1957,10 @@ define('select2/core',[
19571957
});
19581958

19591959
this.selection.on('open', function () {
1960-
self.trigger('open');
1960+
self.open();
19611961
});
19621962
this.selection.on('close', function () {
1963-
self.trigger('close');
1963+
self.close();
19641964
});
19651965
this.selection.on('toggle', function () {
19661966
self.toggleDropdown();
@@ -1979,19 +1979,19 @@ define('select2/core',[
19791979
this.selection.on('unselected', function (params) {
19801980
self.trigger('unselect', params);
19811981

1982-
self.trigger('close');
1982+
self.close();
19831983
});
19841984

19851985
this.results.on('selected', function (params) {
19861986
self.trigger('select', params);
19871987

1988-
self.trigger('close');
1988+
self.close();
19891989
});
19901990

19911991
this.results.on('unselected', function (params) {
19921992
self.trigger('unselect', params);
19931993

1994-
self.trigger('close');
1994+
self.close();
19951995
});
19961996

19971997
this.results.on('results:focus', function (params) {
@@ -2029,16 +2029,34 @@ define('select2/core',[
20292029

20302030
$element.hide();
20312031
$element.attr('tabindex', '-1');
2032+
2033+
$element.data('select2', this);
20322034
};
20332035

20342036
Utils.Extend(Select2, Utils.Observable);
20352037

20362038
Select2.prototype.toggleDropdown = function () {
20372039
if (this.isOpen()) {
2038-
this.trigger('close');
2040+
this.close();
20392041
} else {
2040-
this.trigger('open');
2042+
this.open();
2043+
}
2044+
};
2045+
2046+
Select2.prototype.open = function () {
2047+
if (this.isOpen()) {
2048+
return;
20412049
}
2050+
2051+
this.trigger('open');
2052+
};
2053+
2054+
Select2.prototype.close = function () {
2055+
if (!this.isOpen()) {
2056+
return;
2057+
}
2058+
2059+
this.trigger('close');
20422060
};
20432061

20442062
Select2.prototype.isOpen = function () {
@@ -2073,8 +2091,9 @@ define('jquery.select2',[
20732091
});
20742092
} else if (typeof options === 'string') {
20752093
var instance = this.data('select2');
2094+
var args = Array.prototype.slice.call(arguments, 1);
20762095

2077-
instance[options](arguments.slice(1));
2096+
instance[options](args);
20782097
} else {
20792098
throw new Error('Invalid arguments for Select2: ' + options);
20802099
}

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,44 @@ <h2>Example code</h2>
211211

212212
<script type="text/x-example-code" class="js-code-data-disabled">
213213

214+
</script>
215+
</div>
216+
</section>
217+
218+
<section id="tagss" class="row">
219+
<div class="col-md-4">
220+
<h1>Programmatic access</h1>
221+
222+
<p>
223+
Select2 supports methods that allow programmatic control of the
224+
component.
225+
</p>
226+
227+
<p>
228+
<button class="js-programmatic-open btn btn-success">
229+
Open
230+
</button>
231+
232+
<button class="js-programmatic-close btn btn-success">
233+
Close
234+
</button>
235+
</p>
236+
237+
<p>
238+
<select class="js-example-programmatic js-states form-control"></select>
239+
</p>
240+
241+
</div>
242+
<div class="col-md-8">
243+
<h2>Example code</h2>
244+
245+
<pre data-fill-from=".js-code-programmatic"></pre>
246+
247+
<script type="text/javascript" class="js-code-programmatic">
248+
var $example = $(".js-example-programmatic");
249+
250+
$(".js-programmatic-open").on("click", function () { $example.select2("open"); });
251+
$(".js-programmatic-close").on("click", function () { $example.select2("close"); });
214252
</script>
215253
</div>
216254
</section>
@@ -367,6 +405,8 @@ <h2>Example code</h2>
367405

368406
$disabledResults.select2();
369407

408+
$(".js-example-programmatic").select2();
409+
370410
$tags.select2({
371411
tags: true
372412
});

0 commit comments

Comments
 (0)