1
0
mirror of synced 2024-11-22 21:16:10 +03:00

Allow programmatic access of Select2

This commit is contained in:
Kevin Brown 2014-10-20 20:29:23 -04:00
parent 5dd5ad93a6
commit 5dd7df33bb
9 changed files with 177 additions and 42 deletions

View File

@ -1528,10 +1528,10 @@ define('select2/core',[
}); });
this.selection.on('open', function () { this.selection.on('open', function () {
self.trigger('open'); self.open();
}); });
this.selection.on('close', function () { this.selection.on('close', function () {
self.trigger('close'); self.close();
}); });
this.selection.on('toggle', function () { this.selection.on('toggle', function () {
self.toggleDropdown(); self.toggleDropdown();
@ -1550,19 +1550,19 @@ define('select2/core',[
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
self.trigger('close'); self.close();
}); });
this.results.on('unselected', function (params) { this.results.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('results:focus', function (params) {
@ -1600,18 +1600,36 @@ define('select2/core',[
$element.hide(); $element.hide();
$element.attr('tabindex', '-1'); $element.attr('tabindex', '-1');
$element.data('select2', this);
}; };
Utils.Extend(Select2, Utils.Observable); Utils.Extend(Select2, Utils.Observable);
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
if (this.isOpen()) { if (this.isOpen()) {
this.trigger('close'); this.close();
} else { } else {
this.trigger('open'); this.open();
} }
}; };
Select2.prototype.open = function () {
if (this.isOpen()) {
return;
}
this.trigger('open');
};
Select2.prototype.close = function () {
if (!this.isOpen()) {
return;
}
this.trigger('close');
};
Select2.prototype.isOpen = function () { Select2.prototype.isOpen = function () {
return this.$container.hasClass('open'); return this.$container.hasClass('open');
}; };
@ -1644,8 +1662,9 @@ define('jquery.select2',[
}); });
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
var instance = this.data('select2'); var instance = this.data('select2');
var args = Array.prototype.slice.call(arguments, 1);
instance[options](arguments.slice(1)); instance[options](args);
} else { } else {
throw new Error('Invalid arguments for Select2: ' + options); throw new Error('Invalid arguments for Select2: ' + options);
} }

View File

@ -1528,10 +1528,10 @@ define('select2/core',[
}); });
this.selection.on('open', function () { this.selection.on('open', function () {
self.trigger('open'); self.open();
}); });
this.selection.on('close', function () { this.selection.on('close', function () {
self.trigger('close'); self.close();
}); });
this.selection.on('toggle', function () { this.selection.on('toggle', function () {
self.toggleDropdown(); self.toggleDropdown();
@ -1550,19 +1550,19 @@ define('select2/core',[
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
self.trigger('close'); self.close();
}); });
this.results.on('unselected', function (params) { this.results.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('results:focus', function (params) {
@ -1600,18 +1600,36 @@ define('select2/core',[
$element.hide(); $element.hide();
$element.attr('tabindex', '-1'); $element.attr('tabindex', '-1');
$element.data('select2', this);
}; };
Utils.Extend(Select2, Utils.Observable); Utils.Extend(Select2, Utils.Observable);
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
if (this.isOpen()) { if (this.isOpen()) {
this.trigger('close'); this.close();
} else { } else {
this.trigger('open'); this.open();
} }
}; };
Select2.prototype.open = function () {
if (this.isOpen()) {
return;
}
this.trigger('open');
};
Select2.prototype.close = function () {
if (!this.isOpen()) {
return;
}
this.trigger('close');
};
Select2.prototype.isOpen = function () { Select2.prototype.isOpen = function () {
return this.$container.hasClass('open'); return this.$container.hasClass('open');
}; };
@ -1644,8 +1662,9 @@ define('jquery.select2',[
}); });
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
var instance = this.data('select2'); var instance = this.data('select2');
var args = Array.prototype.slice.call(arguments, 1);
instance[options](arguments.slice(1)); instance[options](args);
} else { } else {
throw new Error('Invalid arguments for Select2: ' + options); throw new Error('Invalid arguments for Select2: ' + options);
} }

View File

@ -11066,10 +11066,10 @@ define('select2/core',[
}); });
this.selection.on('open', function () { this.selection.on('open', function () {
self.trigger('open'); self.open();
}); });
this.selection.on('close', function () { this.selection.on('close', function () {
self.trigger('close'); self.close();
}); });
this.selection.on('toggle', function () { this.selection.on('toggle', function () {
self.toggleDropdown(); self.toggleDropdown();
@ -11088,19 +11088,19 @@ define('select2/core',[
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
self.trigger('close'); self.close();
}); });
this.results.on('unselected', function (params) { this.results.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('results:focus', function (params) {
@ -11138,18 +11138,36 @@ define('select2/core',[
$element.hide(); $element.hide();
$element.attr('tabindex', '-1'); $element.attr('tabindex', '-1');
$element.data('select2', this);
}; };
Utils.Extend(Select2, Utils.Observable); Utils.Extend(Select2, Utils.Observable);
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
if (this.isOpen()) { if (this.isOpen()) {
this.trigger('close'); this.close();
} else { } else {
this.trigger('open'); this.open();
} }
}; };
Select2.prototype.open = function () {
if (this.isOpen()) {
return;
}
this.trigger('open');
};
Select2.prototype.close = function () {
if (!this.isOpen()) {
return;
}
this.trigger('close');
};
Select2.prototype.isOpen = function () { Select2.prototype.isOpen = function () {
return this.$container.hasClass('open'); return this.$container.hasClass('open');
}; };
@ -11182,8 +11200,9 @@ define('jquery.select2',[
}); });
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
var instance = this.data('select2'); var instance = this.data('select2');
var args = Array.prototype.slice.call(arguments, 1);
instance[options](arguments.slice(1)); instance[options](args);
} else { } else {
throw new Error('Invalid arguments for Select2: ' + options); throw new Error('Invalid arguments for Select2: ' + options);
} }

File diff suppressed because one or more lines are too long

35
dist/js/select2.js vendored
View File

@ -1957,10 +1957,10 @@ define('select2/core',[
}); });
this.selection.on('open', function () { this.selection.on('open', function () {
self.trigger('open'); self.open();
}); });
this.selection.on('close', function () { this.selection.on('close', function () {
self.trigger('close'); self.close();
}); });
this.selection.on('toggle', function () { this.selection.on('toggle', function () {
self.toggleDropdown(); self.toggleDropdown();
@ -1979,19 +1979,19 @@ define('select2/core',[
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
self.trigger('close'); self.close();
}); });
this.results.on('unselected', function (params) { this.results.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('results:focus', function (params) {
@ -2029,18 +2029,36 @@ define('select2/core',[
$element.hide(); $element.hide();
$element.attr('tabindex', '-1'); $element.attr('tabindex', '-1');
$element.data('select2', this);
}; };
Utils.Extend(Select2, Utils.Observable); Utils.Extend(Select2, Utils.Observable);
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
if (this.isOpen()) { if (this.isOpen()) {
this.trigger('close'); this.close();
} else { } else {
this.trigger('open'); this.open();
} }
}; };
Select2.prototype.open = function () {
if (this.isOpen()) {
return;
}
this.trigger('open');
};
Select2.prototype.close = function () {
if (!this.isOpen()) {
return;
}
this.trigger('close');
};
Select2.prototype.isOpen = function () { Select2.prototype.isOpen = function () {
return this.$container.hasClass('open'); return this.$container.hasClass('open');
}; };
@ -2073,8 +2091,9 @@ define('jquery.select2',[
}); });
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
var instance = this.data('select2'); var instance = this.data('select2');
var args = Array.prototype.slice.call(arguments, 1);
instance[options](arguments.slice(1)); instance[options](args);
} else { } else {
throw new Error('Invalid arguments for Select2: ' + options); throw new Error('Invalid arguments for Select2: ' + options);
} }

File diff suppressed because one or more lines are too long

View File

@ -211,6 +211,44 @@ $(".js-example-data-array-selected").select2({
<script type="text/x-example-code" class="js-code-data-disabled"> <script type="text/x-example-code" class="js-code-data-disabled">
</script>
</div>
</section>
<section id="tagss" class="row">
<div class="col-md-4">
<h1>Programmatic access</h1>
<p>
Select2 supports methods that allow programmatic control of the
component.
</p>
<p>
<button class="js-programmatic-open btn btn-success">
Open
</button>
<button class="js-programmatic-close btn btn-success">
Close
</button>
</p>
<p>
<select class="js-example-programmatic js-states form-control"></select>
</p>
</div>
<div class="col-md-8">
<h2>Example code</h2>
<pre data-fill-from=".js-code-programmatic"></pre>
<script type="text/javascript" class="js-code-programmatic">
var $example = $(".js-example-programmatic");
$(".js-programmatic-open").on("click", function () { $example.select2("open"); });
$(".js-programmatic-close").on("click", function () { $example.select2("close"); });
</script> </script>
</div> </div>
</section> </section>
@ -367,6 +405,8 @@ require(["select2/core", "select2/utils"], function (Select2, Utils) {
$disabledResults.select2(); $disabledResults.select2();
$(".js-example-programmatic").select2();
$tags.select2({ $tags.select2({
tags: true tags: true
}); });

View File

@ -12,8 +12,9 @@ define([
}); });
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
var instance = this.data('select2'); var instance = this.data('select2');
var args = Array.prototype.slice.call(arguments, 1);
instance[options](arguments.slice(1)); instance[options](args);
} else { } else {
throw new Error('Invalid arguments for Select2: ' + options); throw new Error('Invalid arguments for Select2: ' + options);
} }

View File

@ -79,10 +79,10 @@ define([
}); });
this.selection.on('open', function () { this.selection.on('open', function () {
self.trigger('open'); self.open();
}); });
this.selection.on('close', function () { this.selection.on('close', function () {
self.trigger('close'); self.close();
}); });
this.selection.on('toggle', function () { this.selection.on('toggle', function () {
self.toggleDropdown(); self.toggleDropdown();
@ -101,19 +101,19 @@ define([
this.selection.on('unselected', function (params) { this.selection.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('selected', function (params) { this.results.on('selected', function (params) {
self.trigger('select', params); self.trigger('select', params);
self.trigger('close'); self.close();
}); });
this.results.on('unselected', function (params) { this.results.on('unselected', function (params) {
self.trigger('unselect', params); self.trigger('unselect', params);
self.trigger('close'); self.close();
}); });
this.results.on('results:focus', function (params) { this.results.on('results:focus', function (params) {
@ -151,18 +151,36 @@ define([
$element.hide(); $element.hide();
$element.attr('tabindex', '-1'); $element.attr('tabindex', '-1');
$element.data('select2', this);
}; };
Utils.Extend(Select2, Utils.Observable); Utils.Extend(Select2, Utils.Observable);
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
if (this.isOpen()) { if (this.isOpen()) {
this.trigger('close'); this.close();
} else { } else {
this.trigger('open'); this.open();
} }
}; };
Select2.prototype.open = function () {
if (this.isOpen()) {
return;
}
this.trigger('open');
};
Select2.prototype.close = function () {
if (!this.isOpen()) {
return;
}
this.trigger('close');
};
Select2.prototype.isOpen = function () { Select2.prototype.isOpen = function () {
return this.$container.hasClass('open'); return this.$container.hasClass('open');
}; };