瀏覽代碼

Clicking on a selected option triggers an unselect event

Kevin Brown 10 年之前
父節點
當前提交
d57f8b2203
共有 7 個文件被更改,包括 235 次插入25 次删除
  1. 47 5
      dist/js/select2.amd.full.js
  2. 47 5
      dist/js/select2.amd.js
  3. 47 5
      dist/js/select2.full.js
  4. 47 5
      dist/js/select2.js
  5. 6 0
      src/js/select2/core.js
  6. 27 4
      src/js/select2/data/select.js
  7. 14 1
      src/js/select2/results.js

+ 47 - 5
dist/js/select2.amd.full.js

@@ -190,18 +190,41 @@ define('select2/data/select',[
       this.$element.val(val);
       this.$element.trigger("change");
     }
+  };
+
+  SelectAdapter.prototype.unselect = function (data) {
+    var self = this;
+
+    if (!this.$element.prop("multiple")) {
+      return;
+    }
+
+    this.current(function (currentData) {
+      var val = [];
+
+      for (var d = 0; d < currentData.length; d++) {
+        id = currentData[d].id;
+
+        if (id !== data.id && val.indexOf(id) === -1) {
+          val.push(id);
+        }
+      }
+
+      self.$element.val(val);
+      self.$element.trigger("change");
+    });
   }
 
   SelectAdapter.prototype.bind = function (container, $container) {
     var self = this;
 
     container.on("select", function (params) {
-      var current = self.current(function (data) {
-        //
-      });
-
       self.select(params.data);
     });
+
+    container.on("unselect", function (params) {
+      self.unselect(params.data);
+    });
   }
 
   SelectAdapter.prototype.query = function (params, callback) {
@@ -323,6 +346,7 @@ define('select2/results',[
     this.on("results:all", function (data) {
       self.clear();
       self.append(data);
+
       self.setClasses();
     });
 
@@ -333,7 +357,19 @@ define('select2/results',[
     })
 
     this.$results.on("click", ".option", function (evt) {
-      var data = $(this).data("data");
+      var $this = $(this);
+
+      var data = $this.data("data");
+      if ($this.hasClass("selected")) {
+        self.trigger("unselected", {
+          originalEvent: evt,
+          data: data
+        })
+
+        self.setClasses();
+
+        return;
+      }
 
       self.trigger("selected", {
         originalEvent: evt,
@@ -615,6 +651,12 @@ define('select2/core',[
       $container.removeClass("open");
     });
 
+    this.results.on("unselected", function (params) {
+      self.trigger("unselect", params);
+
+      $container.removeClass("open");
+    });
+
     // Set the initial state
 
     this.data.current(function (initialData) {

+ 47 - 5
dist/js/select2.amd.js

@@ -190,18 +190,41 @@ define('select2/data/select',[
       this.$element.val(val);
       this.$element.trigger("change");
     }
+  };
+
+  SelectAdapter.prototype.unselect = function (data) {
+    var self = this;
+
+    if (!this.$element.prop("multiple")) {
+      return;
+    }
+
+    this.current(function (currentData) {
+      var val = [];
+
+      for (var d = 0; d < currentData.length; d++) {
+        id = currentData[d].id;
+
+        if (id !== data.id && val.indexOf(id) === -1) {
+          val.push(id);
+        }
+      }
+
+      self.$element.val(val);
+      self.$element.trigger("change");
+    });
   }
 
   SelectAdapter.prototype.bind = function (container, $container) {
     var self = this;
 
     container.on("select", function (params) {
-      var current = self.current(function (data) {
-        //
-      });
-
       self.select(params.data);
     });
+
+    container.on("unselect", function (params) {
+      self.unselect(params.data);
+    });
   }
 
   SelectAdapter.prototype.query = function (params, callback) {
@@ -323,6 +346,7 @@ define('select2/results',[
     this.on("results:all", function (data) {
       self.clear();
       self.append(data);
+
       self.setClasses();
     });
 
@@ -333,7 +357,19 @@ define('select2/results',[
     })
 
     this.$results.on("click", ".option", function (evt) {
-      var data = $(this).data("data");
+      var $this = $(this);
+
+      var data = $this.data("data");
+      if ($this.hasClass("selected")) {
+        self.trigger("unselected", {
+          originalEvent: evt,
+          data: data
+        })
+
+        self.setClasses();
+
+        return;
+      }
 
       self.trigger("selected", {
         originalEvent: evt,
@@ -615,6 +651,12 @@ define('select2/core',[
       $container.removeClass("open");
     });
 
+    this.results.on("unselected", function (params) {
+      self.trigger("unselect", params);
+
+      $container.removeClass("open");
+    });
+
     // Set the initial state
 
     this.data.current(function (initialData) {

+ 47 - 5
dist/js/select2.full.js

@@ -9727,18 +9727,41 @@ define('select2/data/select',[
       this.$element.val(val);
       this.$element.trigger("change");
     }
+  };
+
+  SelectAdapter.prototype.unselect = function (data) {
+    var self = this;
+
+    if (!this.$element.prop("multiple")) {
+      return;
+    }
+
+    this.current(function (currentData) {
+      var val = [];
+
+      for (var d = 0; d < currentData.length; d++) {
+        id = currentData[d].id;
+
+        if (id !== data.id && val.indexOf(id) === -1) {
+          val.push(id);
+        }
+      }
+
+      self.$element.val(val);
+      self.$element.trigger("change");
+    });
   }
 
   SelectAdapter.prototype.bind = function (container, $container) {
     var self = this;
 
     container.on("select", function (params) {
-      var current = self.current(function (data) {
-        //
-      });
-
       self.select(params.data);
     });
+
+    container.on("unselect", function (params) {
+      self.unselect(params.data);
+    });
   }
 
   SelectAdapter.prototype.query = function (params, callback) {
@@ -9860,6 +9883,7 @@ define('select2/results',[
     this.on("results:all", function (data) {
       self.clear();
       self.append(data);
+
       self.setClasses();
     });
 
@@ -9870,7 +9894,19 @@ define('select2/results',[
     })
 
     this.$results.on("click", ".option", function (evt) {
-      var data = $(this).data("data");
+      var $this = $(this);
+
+      var data = $this.data("data");
+      if ($this.hasClass("selected")) {
+        self.trigger("unselected", {
+          originalEvent: evt,
+          data: data
+        })
+
+        self.setClasses();
+
+        return;
+      }
 
       self.trigger("selected", {
         originalEvent: evt,
@@ -10152,6 +10188,12 @@ define('select2/core',[
       $container.removeClass("open");
     });
 
+    this.results.on("unselected", function (params) {
+      self.trigger("unselect", params);
+
+      $container.removeClass("open");
+    });
+
     // Set the initial state
 
     this.data.current(function (initialData) {

+ 47 - 5
dist/js/select2.js

@@ -618,18 +618,41 @@ define('select2/data/select',[
       this.$element.val(val);
       this.$element.trigger("change");
     }
+  };
+
+  SelectAdapter.prototype.unselect = function (data) {
+    var self = this;
+
+    if (!this.$element.prop("multiple")) {
+      return;
+    }
+
+    this.current(function (currentData) {
+      var val = [];
+
+      for (var d = 0; d < currentData.length; d++) {
+        id = currentData[d].id;
+
+        if (id !== data.id && val.indexOf(id) === -1) {
+          val.push(id);
+        }
+      }
+
+      self.$element.val(val);
+      self.$element.trigger("change");
+    });
   }
 
   SelectAdapter.prototype.bind = function (container, $container) {
     var self = this;
 
     container.on("select", function (params) {
-      var current = self.current(function (data) {
-        //
-      });
-
       self.select(params.data);
     });
+
+    container.on("unselect", function (params) {
+      self.unselect(params.data);
+    });
   }
 
   SelectAdapter.prototype.query = function (params, callback) {
@@ -751,6 +774,7 @@ define('select2/results',[
     this.on("results:all", function (data) {
       self.clear();
       self.append(data);
+
       self.setClasses();
     });
 
@@ -761,7 +785,19 @@ define('select2/results',[
     })
 
     this.$results.on("click", ".option", function (evt) {
-      var data = $(this).data("data");
+      var $this = $(this);
+
+      var data = $this.data("data");
+      if ($this.hasClass("selected")) {
+        self.trigger("unselected", {
+          originalEvent: evt,
+          data: data
+        })
+
+        self.setClasses();
+
+        return;
+      }
 
       self.trigger("selected", {
         originalEvent: evt,
@@ -1043,6 +1079,12 @@ define('select2/core',[
       $container.removeClass("open");
     });
 
+    this.results.on("unselected", function (params) {
+      self.trigger("unselect", params);
+
+      $container.removeClass("open");
+    });
+
     // Set the initial state
 
     this.data.current(function (initialData) {

+ 6 - 0
src/js/select2/core.js

@@ -71,6 +71,12 @@ define([
       $container.removeClass("open");
     });
 
+    this.results.on("unselected", function (params) {
+      self.trigger("unselect", params);
+
+      $container.removeClass("open");
+    });
+
     // Set the initial state
 
     this.data.current(function (initialData) {

+ 27 - 4
src/js/select2/data/select.js

@@ -52,18 +52,41 @@ define([
       this.$element.val(val);
       this.$element.trigger("change");
     }
+  };
+
+  SelectAdapter.prototype.unselect = function (data) {
+    var self = this;
+
+    if (!this.$element.prop("multiple")) {
+      return;
+    }
+
+    this.current(function (currentData) {
+      var val = [];
+
+      for (var d = 0; d < currentData.length; d++) {
+        id = currentData[d].id;
+
+        if (id !== data.id && val.indexOf(id) === -1) {
+          val.push(id);
+        }
+      }
+
+      self.$element.val(val);
+      self.$element.trigger("change");
+    });
   }
 
   SelectAdapter.prototype.bind = function (container, $container) {
     var self = this;
 
     container.on("select", function (params) {
-      var current = self.current(function (data) {
-        //
-      });
-
       self.select(params.data);
     });
+
+    container.on("unselect", function (params) {
+      self.unselect(params.data);
+    });
   }
 
   SelectAdapter.prototype.query = function (params, callback) {

+ 14 - 1
src/js/select2/results.js

@@ -76,6 +76,7 @@ define([
     this.on("results:all", function (data) {
       self.clear();
       self.append(data);
+
       self.setClasses();
     });
 
@@ -86,7 +87,19 @@ define([
     })
 
     this.$results.on("click", ".option", function (evt) {
-      var data = $(this).data("data");
+      var $this = $(this);
+
+      var data = $this.data("data");
+      if ($this.hasClass("selected")) {
+        self.trigger("unselected", {
+          originalEvent: evt,
+          data: data
+        })
+
+        self.setClasses();
+
+        return;
+      }
 
       self.trigger("selected", {
         originalEvent: evt,