Bläddra i källkod

Better support for tagging

This removes the special case for `<select>` elements and instead
handles everything tagging related within the `Tags` decorator.
Kevin Brown 10 år sedan
förälder
incheckning
8bdd83c44f

+ 21 - 11
dist/js/select2.amd.full.js

@@ -955,16 +955,6 @@ define('select2/data/select',[
   SelectAdapter.prototype.select = function (data) {
     var self = this;
 
-    // Create items marked as tags
-    if (data._tag === true) {
-      // Clear the tag flag from it
-      delete data._tag;
-
-      // Create and add the option
-      var $option = this.option(data);
-      this.$element.append($option);
-    }
-
     if (this.$element.prop('multiple')) {
       this.current(function (currentData) {
         var val = [];
@@ -1257,6 +1247,8 @@ define('select2/data/tags',[
   Tags.prototype.query = function (decorated, params, callback) {
     var self = this;
 
+    this._removeOldTags();
+
     if (params.term == null || params.term === '' || params.page != null) {
       decorated.call(this, params, callback);
       return;
@@ -1288,7 +1280,11 @@ define('select2/data/tags',[
       }
 
       var tag = self.createTag(params);
-      tag._tag = true;
+
+      var $option = self.option(tag);
+      $option.attr('data-select2-tag', true);
+
+      self.$element.append($option);
 
       data.unshift(tag);
 
@@ -1305,6 +1301,20 @@ define('select2/data/tags',[
     };
   };
 
+  Tags.prototype._removeOldTags = function (_) {
+    var tag = this._lastTag;
+
+    var $options = this.$element.find('option[data-select2-tag]');
+
+    $options.each(function () {
+      if (this.selected) {
+        return;
+      }
+
+      $(this).remove();
+    });
+  };
+
   return Tags;
 });
 

+ 21 - 11
dist/js/select2.amd.js

@@ -955,16 +955,6 @@ define('select2/data/select',[
   SelectAdapter.prototype.select = function (data) {
     var self = this;
 
-    // Create items marked as tags
-    if (data._tag === true) {
-      // Clear the tag flag from it
-      delete data._tag;
-
-      // Create and add the option
-      var $option = this.option(data);
-      this.$element.append($option);
-    }
-
     if (this.$element.prop('multiple')) {
       this.current(function (currentData) {
         var val = [];
@@ -1257,6 +1247,8 @@ define('select2/data/tags',[
   Tags.prototype.query = function (decorated, params, callback) {
     var self = this;
 
+    this._removeOldTags();
+
     if (params.term == null || params.term === '' || params.page != null) {
       decorated.call(this, params, callback);
       return;
@@ -1288,7 +1280,11 @@ define('select2/data/tags',[
       }
 
       var tag = self.createTag(params);
-      tag._tag = true;
+
+      var $option = self.option(tag);
+      $option.attr('data-select2-tag', true);
+
+      self.$element.append($option);
 
       data.unshift(tag);
 
@@ -1305,6 +1301,20 @@ define('select2/data/tags',[
     };
   };
 
+  Tags.prototype._removeOldTags = function (_) {
+    var tag = this._lastTag;
+
+    var $options = this.$element.find('option[data-select2-tag]');
+
+    $options.each(function () {
+      if (this.selected) {
+        return;
+      }
+
+      $(this).remove();
+    });
+  };
+
   return Tags;
 });
 

+ 21 - 11
dist/js/select2.full.js

@@ -10490,16 +10490,6 @@ define('select2/data/select',[
   SelectAdapter.prototype.select = function (data) {
     var self = this;
 
-    // Create items marked as tags
-    if (data._tag === true) {
-      // Clear the tag flag from it
-      delete data._tag;
-
-      // Create and add the option
-      var $option = this.option(data);
-      this.$element.append($option);
-    }
-
     if (this.$element.prop('multiple')) {
       this.current(function (currentData) {
         var val = [];
@@ -10792,6 +10782,8 @@ define('select2/data/tags',[
   Tags.prototype.query = function (decorated, params, callback) {
     var self = this;
 
+    this._removeOldTags();
+
     if (params.term == null || params.term === '' || params.page != null) {
       decorated.call(this, params, callback);
       return;
@@ -10823,7 +10815,11 @@ define('select2/data/tags',[
       }
 
       var tag = self.createTag(params);
-      tag._tag = true;
+
+      var $option = self.option(tag);
+      $option.attr('data-select2-tag', true);
+
+      self.$element.append($option);
 
       data.unshift(tag);
 
@@ -10840,6 +10836,20 @@ define('select2/data/tags',[
     };
   };
 
+  Tags.prototype._removeOldTags = function (_) {
+    var tag = this._lastTag;
+
+    var $options = this.$element.find('option[data-select2-tag]');
+
+    $options.each(function () {
+      if (this.selected) {
+        return;
+      }
+
+      $(this).remove();
+    });
+  };
+
   return Tags;
 });
 

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
dist/js/select2.full.min.js


+ 21 - 11
dist/js/select2.js

@@ -1383,16 +1383,6 @@ define('select2/data/select',[
   SelectAdapter.prototype.select = function (data) {
     var self = this;
 
-    // Create items marked as tags
-    if (data._tag === true) {
-      // Clear the tag flag from it
-      delete data._tag;
-
-      // Create and add the option
-      var $option = this.option(data);
-      this.$element.append($option);
-    }
-
     if (this.$element.prop('multiple')) {
       this.current(function (currentData) {
         var val = [];
@@ -1685,6 +1675,8 @@ define('select2/data/tags',[
   Tags.prototype.query = function (decorated, params, callback) {
     var self = this;
 
+    this._removeOldTags();
+
     if (params.term == null || params.term === '' || params.page != null) {
       decorated.call(this, params, callback);
       return;
@@ -1716,7 +1708,11 @@ define('select2/data/tags',[
       }
 
       var tag = self.createTag(params);
-      tag._tag = true;
+
+      var $option = self.option(tag);
+      $option.attr('data-select2-tag', true);
+
+      self.$element.append($option);
 
       data.unshift(tag);
 
@@ -1733,6 +1729,20 @@ define('select2/data/tags',[
     };
   };
 
+  Tags.prototype._removeOldTags = function (_) {
+    var tag = this._lastTag;
+
+    var $options = this.$element.find('option[data-select2-tag]');
+
+    $options.each(function () {
+      if (this.selected) {
+        return;
+      }
+
+      $(this).remove();
+    });
+  };
+
   return Tags;
 });
 

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
dist/js/select2.min.js


+ 0 - 10
src/js/select2/data/select.js

@@ -29,16 +29,6 @@ define([
   SelectAdapter.prototype.select = function (data) {
     var self = this;
 
-    // Create items marked as tags
-    if (data._tag === true) {
-      // Clear the tag flag from it
-      delete data._tag;
-
-      // Create and add the option
-      var $option = this.option(data);
-      this.$element.append($option);
-    }
-
     if (this.$element.prop('multiple')) {
       this.current(function (currentData) {
         var val = [];

+ 21 - 1
src/js/select2/data/tags.js

@@ -10,6 +10,8 @@ define([
   Tags.prototype.query = function (decorated, params, callback) {
     var self = this;
 
+    this._removeOldTags();
+
     if (params.term == null || params.term === '' || params.page != null) {
       decorated.call(this, params, callback);
       return;
@@ -41,7 +43,11 @@ define([
       }
 
       var tag = self.createTag(params);
-      tag._tag = true;
+
+      var $option = self.option(tag);
+      $option.attr('data-select2-tag', true);
+
+      self.$element.append($option);
 
       data.unshift(tag);
 
@@ -58,5 +64,19 @@ define([
     };
   };
 
+  Tags.prototype._removeOldTags = function (_) {
+    var tag = this._lastTag;
+
+    var $options = this.$element.find('option[data-select2-tag]');
+
+    $options.each(function () {
+      if (this.selected) {
+        return;
+      }
+
+      $(this).remove();
+    });
+  };
+
   return Tags;
 });

Vissa filer visades inte eftersom för många filer har ändrats