|
@@ -77,7 +77,7 @@ define([
|
|
|
});
|
|
|
|
|
|
// Hide the original select
|
|
|
- $element.addClass('select2-hidden-accessible');
|
|
|
+ $element[0].classList.add('select2-hidden-accessible');
|
|
|
$element.attr('aria-hidden', 'true');
|
|
|
|
|
|
// Synchronize any monitored attributes
|
|
@@ -197,42 +197,15 @@ define([
|
|
|
this._syncA = Utils.bind(this._syncAttributes, this);
|
|
|
this._syncS = Utils.bind(this._syncSubtree, this);
|
|
|
|
|
|
- if (this.$element[0].attachEvent) {
|
|
|
- this.$element[0].attachEvent('onpropertychange', this._syncA);
|
|
|
- }
|
|
|
-
|
|
|
- var observer = window.MutationObserver ||
|
|
|
- window.WebKitMutationObserver ||
|
|
|
- window.MozMutationObserver
|
|
|
- ;
|
|
|
-
|
|
|
- if (observer != null) {
|
|
|
- this._observer = new observer(function (mutations) {
|
|
|
- self._syncA();
|
|
|
- self._syncS(null, mutations);
|
|
|
- });
|
|
|
- this._observer.observe(this.$element[0], {
|
|
|
- attributes: true,
|
|
|
- childList: true,
|
|
|
- subtree: false
|
|
|
- });
|
|
|
- } else if (this.$element[0].addEventListener) {
|
|
|
- this.$element[0].addEventListener(
|
|
|
- 'DOMAttrModified',
|
|
|
- self._syncA,
|
|
|
- false
|
|
|
- );
|
|
|
- this.$element[0].addEventListener(
|
|
|
- 'DOMNodeInserted',
|
|
|
- self._syncS,
|
|
|
- false
|
|
|
- );
|
|
|
- this.$element[0].addEventListener(
|
|
|
- 'DOMNodeRemoved',
|
|
|
- self._syncS,
|
|
|
- false
|
|
|
- );
|
|
|
- }
|
|
|
+ this._observer = new window.MutationObserver(function (mutations) {
|
|
|
+ self._syncA();
|
|
|
+ self._syncS(mutations);
|
|
|
+ });
|
|
|
+ this._observer.observe(this.$element[0], {
|
|
|
+ attributes: true,
|
|
|
+ childList: true,
|
|
|
+ subtree: false
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
Select2.prototype._registerDataEvents = function () {
|
|
@@ -256,7 +229,7 @@ define([
|
|
|
});
|
|
|
|
|
|
this.selection.on('*', function (name, params) {
|
|
|
- if ($.inArray(name, nonRelayEvents) !== -1) {
|
|
|
+ if (nonRelayEvents.indexOf(name) !== -1) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -284,23 +257,23 @@ define([
|
|
|
var self = this;
|
|
|
|
|
|
this.on('open', function () {
|
|
|
- self.$container.addClass('select2-container--open');
|
|
|
+ self.$container[0].classList.add('select2-container--open');
|
|
|
});
|
|
|
|
|
|
this.on('close', function () {
|
|
|
- self.$container.removeClass('select2-container--open');
|
|
|
+ self.$container[0].classList.remove('select2-container--open');
|
|
|
});
|
|
|
|
|
|
this.on('enable', function () {
|
|
|
- self.$container.removeClass('select2-container--disabled');
|
|
|
+ self.$container[0].classList.remove('select2-container--disabled');
|
|
|
});
|
|
|
|
|
|
this.on('disable', function () {
|
|
|
- self.$container.addClass('select2-container--disabled');
|
|
|
+ self.$container[0].classList.add('select2-container--disabled');
|
|
|
});
|
|
|
|
|
|
this.on('blur', function () {
|
|
|
- self.$container.removeClass('select2-container--focus');
|
|
|
+ self.$container[0].classList.remove('select2-container--focus');
|
|
|
});
|
|
|
|
|
|
this.on('query', function (params) {
|
|
@@ -376,49 +349,30 @@ define([
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- Select2.prototype._isChangeMutation = function (evt, mutations) {
|
|
|
- var changed = false;
|
|
|
+ Select2.prototype._isChangeMutation = function (mutations) {
|
|
|
var self = this;
|
|
|
|
|
|
- // Ignore any mutation events raised for elements that aren't options or
|
|
|
- // optgroups. This handles the case when the select element is destroyed
|
|
|
- if (
|
|
|
- evt && evt.target && (
|
|
|
- evt.target.nodeName !== 'OPTION' && evt.target.nodeName !== 'OPTGROUP'
|
|
|
- )
|
|
|
- ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!mutations) {
|
|
|
- // If mutation events aren't supported, then we can only assume that the
|
|
|
- // change affected the selections
|
|
|
- changed = true;
|
|
|
- } else if (mutations.addedNodes && mutations.addedNodes.length > 0) {
|
|
|
+ if (mutations.addedNodes && mutations.addedNodes.length > 0) {
|
|
|
for (var n = 0; n < mutations.addedNodes.length; n++) {
|
|
|
var node = mutations.addedNodes[n];
|
|
|
|
|
|
if (node.selected) {
|
|
|
- changed = true;
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|
|
|
} else if (mutations.removedNodes && mutations.removedNodes.length > 0) {
|
|
|
- changed = true;
|
|
|
- } else if ($.isArray(mutations)) {
|
|
|
- $.each(mutations, function(evt, mutation) {
|
|
|
- if (self._isChangeMutation(evt, mutation)) {
|
|
|
- // We've found a change mutation.
|
|
|
- // Let's escape from the loop and continue
|
|
|
- changed = true;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ return true;
|
|
|
+ } else if (Array.isArray(mutations)) {
|
|
|
+ return mutations.some(function (mutation) {
|
|
|
+ return self._isChangeMutation(mutation);
|
|
|
});
|
|
|
}
|
|
|
- return changed;
|
|
|
+
|
|
|
+ return false;
|
|
|
};
|
|
|
|
|
|
- Select2.prototype._syncSubtree = function (evt, mutations) {
|
|
|
- var changed = this._isChangeMutation(evt, mutations);
|
|
|
+ Select2.prototype._syncSubtree = function (mutations) {
|
|
|
+ var changed = this._isChangeMutation(mutations);
|
|
|
var self = this;
|
|
|
|
|
|
// Only re-pull the data if we think there is a change
|
|
@@ -523,11 +477,11 @@ define([
|
|
|
};
|
|
|
|
|
|
Select2.prototype.isOpen = function () {
|
|
|
- return this.$container.hasClass('select2-container--open');
|
|
|
+ return this.$container[0].classList.contains('select2-container--open');
|
|
|
};
|
|
|
|
|
|
Select2.prototype.hasFocus = function () {
|
|
|
- return this.$container.hasClass('select2-container--focus');
|
|
|
+ return this.$container[0].classList.contains('select2-container--focus');
|
|
|
};
|
|
|
|
|
|
Select2.prototype.focus = function (data) {
|
|
@@ -536,7 +490,7 @@ define([
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- this.$container.addClass('select2-container--focus');
|
|
|
+ this.$container[0].classList.add('select2-container--focus');
|
|
|
this.trigger('focus', {});
|
|
|
};
|
|
|
|
|
@@ -590,8 +544,8 @@ define([
|
|
|
|
|
|
var newVal = args[0];
|
|
|
|
|
|
- if ($.isArray(newVal)) {
|
|
|
- newVal = $.map(newVal, function (obj) {
|
|
|
+ if (Array.isArray(newVal)) {
|
|
|
+ newVal = newVal.map(function (obj) {
|
|
|
return obj.toString();
|
|
|
});
|
|
|
}
|
|
@@ -602,21 +556,8 @@ define([
|
|
|
Select2.prototype.destroy = function () {
|
|
|
this.$container.remove();
|
|
|
|
|
|
- if (this.$element[0].detachEvent) {
|
|
|
- this.$element[0].detachEvent('onpropertychange', this._syncA);
|
|
|
- }
|
|
|
-
|
|
|
- if (this._observer != null) {
|
|
|
- this._observer.disconnect();
|
|
|
- this._observer = null;
|
|
|
- } else if (this.$element[0].removeEventListener) {
|
|
|
- this.$element[0]
|
|
|
- .removeEventListener('DOMAttrModified', this._syncA, false);
|
|
|
- this.$element[0]
|
|
|
- .removeEventListener('DOMNodeInserted', this._syncS, false);
|
|
|
- this.$element[0]
|
|
|
- .removeEventListener('DOMNodeRemoved', this._syncS, false);
|
|
|
- }
|
|
|
+ this._observer.disconnect();
|
|
|
+ this._observer = null;
|
|
|
|
|
|
this._syncA = null;
|
|
|
this._syncS = null;
|
|
@@ -625,7 +566,7 @@ define([
|
|
|
this.$element.attr('tabindex',
|
|
|
Utils.GetData(this.$element[0], 'old-tabindex'));
|
|
|
|
|
|
- this.$element.removeClass('select2-hidden-accessible');
|
|
|
+ this.$element[0].classList.remove('select2-hidden-accessible');
|
|
|
this.$element.attr('aria-hidden', 'false');
|
|
|
Utils.RemoveData(this.$element[0]);
|
|
|
this.$element.removeData('select2');
|
|
@@ -653,7 +594,8 @@ define([
|
|
|
|
|
|
this.$container = $container;
|
|
|
|
|
|
- this.$container.addClass('select2-container--' + this.options.get('theme'));
|
|
|
+ this.$container[0].classList
|
|
|
+ .add('select2-container--' + this.options.get('theme'));
|
|
|
|
|
|
Utils.StoreData($container[0], 'element', this.$element);
|
|
|
|