Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache objects in Utils.__cache instead of using $.data (#4014) #4346

Closed
wants to merge 13 commits into from
7 changes: 4 additions & 3 deletions src/js/jquery.select2.js
Expand Up @@ -3,8 +3,9 @@ define([
'jquery-mousewheel',

'./select2/core',
'./select2/defaults'
], function ($, _, Select2, Defaults) {
'./select2/defaults',
'./select2/utils'
], function ($, _, Select2, Defaults, Utils) {
if ($.fn.select2 == null) {
// All methods that should return the element
var thisMethods = ['open', 'close', 'destroy'];
Expand All @@ -25,7 +26,7 @@ define([
var args = Array.prototype.slice.call(arguments, 1);

this.each(function () {
var instance = $(this).data('select2');
var instance = Utils.GetData(this, 'select2');

if (instance == null && window.console && console.error) {
console.error(
Expand Down
7 changes: 4 additions & 3 deletions src/js/select2/compat/inputData.js
@@ -1,6 +1,7 @@
define([
'jquery'
], function ($) {
'jquery',
'../utils'
], function ($, Utils) {
function InputData (decorated, $element, options) {
this._currentData = [];
this._valueSeparator = options.get('valueSeparator') || ',';
Expand Down Expand Up @@ -117,7 +118,7 @@ define([

InputData.prototype.addOptions = function (_, $options) {
var options = $.map($options, function ($option) {
return $.data($option[0], 'data');
return Utils.GetData($option[0], 'data');
});

this._currentData.push.apply(this._currentData, options);
Expand Down
15 changes: 10 additions & 5 deletions src/js/select2/core.js
Expand Up @@ -5,8 +5,8 @@ define([
'./keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) {
if ($element.data('select2') != null) {
$element.data('select2').destroy();
if (Utils.GetData($element[0], 'select2') != null) {
Utils.GetData($element[0], 'select2').destroy();
}

this.$element = $element;
Expand All @@ -22,7 +22,7 @@ define([
// Set up the tabindex

var tabindex = $element.attr('tabindex') || 0;
$element.data('old-tabindex', tabindex);
Utils.StoreData($element[0], 'old-tabindex', tabindex);
$element.attr('tabindex', '-1');

// Set up containers and adapters
Expand Down Expand Up @@ -83,6 +83,9 @@ define([
// Synchronize any monitored attributes
this._syncAttributes();

Utils.StoreData($element[0], 'select2', this);

// Ensure backwards compatibility with $element.data('select2').
$element.data('select2', this);
};

Expand Down Expand Up @@ -572,10 +575,12 @@ define([
this._syncS = null;

this.$element.off('.select2');
this.$element.attr('tabindex', this.$element.data('old-tabindex'));
this.$element.attr('tabindex',
Utils.GetData(this.$element[0], 'old-tabindex'));

this.$element.removeClass('select2-hidden-accessible');
this.$element.attr('aria-hidden', 'false');
Utils.RemoveData(this.$element[0]);
this.$element.removeData('select2');

this.dataAdapter.destroy();
Expand Down Expand Up @@ -603,7 +608,7 @@ define([

this.$container.addClass('select2-container--' + this.options.get('theme'));

$container.data('element', this.$element);
Utils.StoreData($container[0], 'element', this.$element);

return $container;
};
Expand Down
8 changes: 4 additions & 4 deletions src/js/select2/data/select.js
Expand Up @@ -119,7 +119,7 @@ define([
// Remove anything added to child elements
this.$element.find('*').each(function () {
// Remove any custom data set by Select2
$.removeData(this, 'data');
Utils.RemoveData(this);
});
};

Expand Down Expand Up @@ -192,15 +192,15 @@ define([
normalizedData.element = option;

// Override the option's data with the combined data
$.data(option, 'data', normalizedData);
Utils.StoreData(option, 'data', normalizedData);

return $option;
};

SelectAdapter.prototype.item = function ($option) {
var data = {};

data = $.data($option[0], 'data');
data = Utils.GetData($option[0], 'data');

if (data != null) {
return data;
Expand Down Expand Up @@ -238,7 +238,7 @@ define([
data = this._normalizeItem(data);
data.element = $option[0];

$.data($option[0], 'data', data);
Utils.StoreData($option[0], 'data', data);

return data;
};
Expand Down
8 changes: 8 additions & 0 deletions src/js/select2/defaults.js
Expand Up @@ -352,6 +352,14 @@ define([
}

this.defaults = {
ajax: null,
placeholder: null,
tags: false,
tokenSeparators: [],
multiple: false,
dropdownCssClass: null,
allowClear: false,
containerCssClass: null,
amdBase: './',
amdLanguageBase: './i18n/',
closeOnSelect: true,
Expand Down
4 changes: 2 additions & 2 deletions src/js/select2/dropdown/attachBody.js
Expand Up @@ -90,14 +90,14 @@ define([

var $watchers = this.$container.parents().filter(Utils.hasScroll);
$watchers.each(function () {
$(this).data('select2-scroll-position', {
Utils.StoreData(this, 'select2-scroll-position', {
x: $(this).scrollLeft(),
y: $(this).scrollTop()
});
});

$watchers.on(scrollEvent, function (ev) {
var position = $(this).data('select2-scroll-position');
var position = Utils.GetData(this, 'select2-scroll-position');
$(this).scrollTop(position.y);
});

Expand Down
6 changes: 3 additions & 3 deletions src/js/select2/dropdown/selectOnClose.js
@@ -1,6 +1,6 @@
define([

], function () {
'../utils'
], function (Utils) {
function SelectOnClose () { }

SelectOnClose.prototype.bind = function (decorated, container, $container) {
Expand Down Expand Up @@ -31,7 +31,7 @@ define([
return;
}

var data = $highlightedResults.data('data');
var data = Utils.GetData($highlightedResults[0], 'data');

// Don't re-select already selected resulte
if (
Expand Down
56 changes: 48 additions & 8 deletions src/js/select2/options.js
Expand Up @@ -6,9 +6,21 @@ define([
], function (require, $, Defaults, Utils) {
function Options (options, $element) {
this.options = options;
this.jQuery1x = false;
this.elementData = null;

// Html already attempted to be read from html5-* attribs.
this.attemptedHtml5DataItems = {};

if ($element != null) {
// Override `options` with html5 data-* attribs if any.
this.fromElement($element);

// Prefetch properties.
for(var propertyName in Defaults.defaults) {
this.get(propertyName);
}

}

this.options = Defaults.apply(this.options);
Expand Down Expand Up @@ -55,7 +67,7 @@ define([
$e.prop('disabled', this.options.disabled);
$e.prop('multiple', this.options.multiple);

if ($e.data('select2Tags')) {
if (Utils.GetData($e[0], 'select2Tags')) {
if (this.options.debug && window.console && console.warn) {
console.warn(
'Select2: The `data-select2-tags` attribute has been changed to ' +
Expand All @@ -64,11 +76,11 @@ define([
);
}

$e.data('data', $e.data('select2Tags'));
$e.data('tags', true);
Utils.StoreData($e[0], 'data', Utils.GetData($e[0], 'select2Tags'));
Utils.StoreData($e[0], 'tags', true);
}

if ($e.data('ajaxUrl')) {
if (Utils.GetData($e[0], 'ajaxUrl')) {
if (this.options.debug && window.console && console.warn) {
console.warn(
'Select2: The `data-ajax-url` attribute has been changed to ' +
Expand All @@ -77,18 +89,20 @@ define([
);
}

$e.attr('ajax--url', $e.data('ajaxUrl'));
$e.data('ajax--url', $e.data('ajaxUrl'));
$e.attr('ajax--url', Utils.GetData($e[0], 'ajaxUrl'));
Utils.StoreData($e[0], 'ajax-Url', Utils.GetData($e[0], 'ajaxUrl'));

}

var dataset = {};

// Prefer the element's `dataset` attribute if it exists
// jQuery 1.x does not correctly handle data attributes with multiple dashes
if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
dataset = $.extend(true, {}, $e[0].dataset, $e.data());
this.jQuery1x = true;
dataset = $.extend(true, {}, $e[0].dataset, Utils.GetData($e[0]));
} else {
dataset = $e.data();
dataset = Utils.GetData($e[0]);
}

var data = $.extend(true, {}, dataset);
Expand All @@ -107,10 +121,36 @@ define([
}
}

// Store normalized element data for later retrieval.
this.elementData = Utils._convertData($e.data());

return this;
};

Options.prototype.get = function (key) {

if (!this.elementData || this.attemptedHtml5DataItems[key]) {
return this.options[key];
}
// If the option value is stored in html5 data-* attribs,
// fetch the value and store it for performance.
if (!this.jQuery1x) {
// For jQuery 1.x, the html5 data-* attribs have already
// been parsed above.
// Read all attribs that start with `key`.
for (var dataKey in this.elementData) {
if (dataKey.indexOf(key) === 0) {

if ($.isPlainObject(this.options[key])) {
$.extend(this.options[key], this.elementData[dataKey]);
} else {
this.options[key] = this.elementData[dataKey];
}
}
}
}
// Mark as attempted.
this.attemptedHtml5DataItems[key] = true;
return this.options[key];
};

Expand Down
10 changes: 5 additions & 5 deletions src/js/select2/results.js
Expand Up @@ -130,7 +130,7 @@ define([
$options.each(function () {
var $option = $(this);

var item = $.data(this, 'data');
var item = Utils.GetData(this, 'data');

// id needs to be converted to a string when comparing
var id = '' + item.id;
Expand Down Expand Up @@ -235,7 +235,7 @@ define([
this.template(data, option);
}

$.data(option, 'data', data);
Utils.StoreData(option, 'data', data);

return option;
};
Expand Down Expand Up @@ -321,7 +321,7 @@ define([
return;
}

var data = $highlighted.data('data');
var data = Utils.GetData($highlighted[0], 'data');

if ($highlighted.attr('aria-selected') == 'true') {
self.trigger('close', {});
Expand Down Expand Up @@ -433,7 +433,7 @@ define([
function (evt) {
var $this = $(this);

var data = $this.data('data');
var data = Utils.GetData(this, 'data');

if ($this.attr('aria-selected') === 'true') {
if (self.options.get('multiple')) {
Expand All @@ -456,7 +456,7 @@ define([

this.$results.on('mouseenter', '.select2-results__option[aria-selected]',
function (evt) {
var data = $(this).data('data');
var data = Utils.GetData(this, 'data');

self.getHighlightedResults()
.removeClass('select2-results__option--highlighted');
Expand Down
9 changes: 5 additions & 4 deletions src/js/select2/selection/allowClear.js
@@ -1,7 +1,8 @@
define([
'jquery',
'../keys'
], function ($, KEYS) {
'../keys',
'../utils'
], function ($, KEYS, Utils) {
function AllowClear () { }

AllowClear.prototype.bind = function (decorated, container, $container) {
Expand Down Expand Up @@ -43,7 +44,7 @@ define([

evt.stopPropagation();

var data = $clear.data('data');
var data = Utils.GetData($clear[0], 'data');

for (var d = 0; d < data.length; d++) {
var unselectData = {
Expand Down Expand Up @@ -88,7 +89,7 @@ define([
'&times;' +
'</span>'
);
$remove.data('data', data);
Utils.StoreData($remove[0], 'data', data);

this.$selection.find('.select2-selection__rendered').prepend($remove);
};
Expand Down
6 changes: 3 additions & 3 deletions src/js/select2/selection/base.js
Expand Up @@ -21,8 +21,8 @@ define([

this._tabindex = 0;

if (this.$element.data('old-tabindex') != null) {
this._tabindex = this.$element.data('old-tabindex');
if (Utils.GetData(this.$element[0], 'old-tabindex') != null) {
this._tabindex = Utils.GetData(this.$element[0], 'old-tabindex');
} else if (this.$element.attr('tabindex') != null) {
this._tabindex = this.$element.attr('tabindex');
}
Expand Down Expand Up @@ -130,7 +130,7 @@ define([
return;
}

var $element = $this.data('element');
var $element = Utils.GetData(this, 'element');

$element.select2('close');
});
Expand Down
4 changes: 2 additions & 2 deletions src/js/select2/selection/multiple.js
Expand Up @@ -44,7 +44,7 @@ define([
var $remove = $(this);
var $selection = $remove.parent();

var data = $selection.data('data');
var data = Utils.GetData($selection[0], 'data');

self.trigger('unselect', {
originalEvent: evt,
Expand Down Expand Up @@ -95,7 +95,7 @@ define([
$selection.append(formatted);
$selection.prop('title', selection.title || selection.text);

$selection.data('data', selection);
Utils.StoreData($selection[0], 'data', selection);

$selections.push($selection);
}
Expand Down