Ver Fonte

Allow programmatic access of Select2

Kevin Brown há 10 anos atrás
pai
commit
5dd7df33bb

+ 27 - 8
dist/js/select2.amd.full.js

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

+ 27 - 8
dist/js/select2.amd.js

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

+ 27 - 8
dist/js/select2.full.js

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

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/js/select2.full.min.js


+ 27 - 8
dist/js/select2.js

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

Diff do ficheiro suprimidas por serem muito extensas
+ 0 - 0
dist/js/select2.min.js


+ 40 - 0
docs/examples.html

@@ -211,6 +211,44 @@ $(".js-example-data-array-selected").select2({
 
 <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>
     </div>
   </section>
@@ -367,6 +405,8 @@ require(["select2/core", "select2/utils"], function (Select2, Utils) {
 
   $disabledResults.select2();
 
+  $(".js-example-programmatic").select2();
+
   $tags.select2({
     tags: true
   });

+ 2 - 1
src/js/jquery.select2.js

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

+ 25 - 7
src/js/select2/core.js

@@ -79,10 +79,10 @@ define([
     });
 
     this.selection.on('open', function () {
-      self.trigger('open');
+      self.open();
     });
     this.selection.on('close', function () {
-      self.trigger('close');
+      self.close();
     });
     this.selection.on('toggle', function () {
       self.toggleDropdown();
@@ -101,19 +101,19 @@ define([
     this.selection.on('unselected', function (params) {
       self.trigger('unselect', params);
 
-      self.trigger('close');
+      self.close();
     });
 
     this.results.on('selected', function (params) {
       self.trigger('select', params);
 
-      self.trigger('close');
+      self.close();
     });
 
     this.results.on('unselected', function (params) {
       self.trigger('unselect', params);
 
-      self.trigger('close');
+      self.close();
     });
 
     this.results.on('results:focus', function (params) {
@@ -151,16 +151,34 @@ define([
 
     $element.hide();
     $element.attr('tabindex', '-1');
+
+    $element.data('select2', this);
   };
 
   Utils.Extend(Select2, Utils.Observable);
 
   Select2.prototype.toggleDropdown = function () {
     if (this.isOpen()) {
-      this.trigger('close');
+      this.close();
     } 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 () {

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff