mirror of
https://github.com/Hopiu/django-select2.git
synced 2026-04-20 05:01:01 +00:00
Heavy no longer has any need for caching user selections in cookies or localStorage.
This commit is contained in:
parent
aa60bb36ec
commit
3fb6581737
4 changed files with 8 additions and 299 deletions
|
|
@ -74,7 +74,7 @@ The view - `Select2View`, exposed here is meant to be used with 'Heavy' fields a
|
|||
|
||||
"""
|
||||
|
||||
__version__ = "3.1.5"
|
||||
__version__ = "3.2.0"
|
||||
|
||||
__RENDER_SELECT2_STATICS = False
|
||||
|
||||
|
|
|
|||
|
|
@ -38,91 +38,8 @@ if (!window['django_select2']) {
|
|||
}
|
||||
return results;
|
||||
},
|
||||
/*setCookie: function (c_name, value) {
|
||||
document.cookie = c_name + "=" + escape(value);
|
||||
},
|
||||
getCookie: function (c_name) {
|
||||
var i, x, y,
|
||||
ARRcookies = document.cookie.split(";");
|
||||
|
||||
for (i = 0; i < ARRcookies.length; i++) {
|
||||
x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
|
||||
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
|
||||
x = x.replace(/^\s+|\s+$/g,"");
|
||||
if (x == c_name) {
|
||||
return unescape(y);
|
||||
}
|
||||
}
|
||||
},
|
||||
delCookie: function (c_name, isStartsWithPattern) {
|
||||
var i, x, ARRcookies;
|
||||
if (!isStartsWithPattern) {
|
||||
document.cookie = c_name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
||||
} else {
|
||||
ARRcookies = document.cookie.split(";");
|
||||
|
||||
for (i = 0; i < ARRcookies.length; i++) {
|
||||
x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
|
||||
x = x.replace(/^\s+|\s+$/g,"");
|
||||
if (x.indexOf(c_name) == 0) {
|
||||
document.cookie = c_name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
setData: function (c_name, value) {
|
||||
var store = django_select2.store;
|
||||
|
||||
django_select2.setCookie(django_select2.HEAVY_VAL_TXT_SET_KEY, true);
|
||||
if (store && store.enabled) {
|
||||
store.set(c_name, value);
|
||||
} else {
|
||||
django_select2.setCookie(c_name, value);
|
||||
}
|
||||
},
|
||||
getData: function (c_name) {
|
||||
var store = django_select2.store;
|
||||
|
||||
if (store && store.enabled) {
|
||||
return store.get(c_name);
|
||||
} else {
|
||||
return django_select2.getCookie(c_name);
|
||||
}
|
||||
},
|
||||
delData: function (c_name, isStartsWithPattern) {
|
||||
isStartsWithPattern = typeof(isStartsWithPattern) === 'undefined' ? true : isStartsWithPattern;
|
||||
var store = django_select2.store;
|
||||
|
||||
if (store && store.enabled) {
|
||||
if (!isStartsWithPattern) {
|
||||
store.remove(c_name);
|
||||
} else {
|
||||
store.removeAllStartsWith(c_name);
|
||||
}
|
||||
} else {
|
||||
django_select2.delCookie(c_name, isStartsWithPattern);
|
||||
}
|
||||
},*/
|
||||
onValChange: function () {
|
||||
var e = $(this);//, res, id = e.attr('id');
|
||||
django_select2.updateText(e);
|
||||
|
||||
//django_select2.delData('heavy_val:' + id + ':');
|
||||
//django_select2.delData('heavy_txt:' + id + ':');
|
||||
|
||||
/*res = django_select2.getValText(e, false);
|
||||
if (res && res[1]) {
|
||||
// HTML5 localstore or cookies are used to persist selection's text.
|
||||
// This is needed when the form springs back if there are any
|
||||
// validation failures.
|
||||
$(res[0]).each(function (idx) {
|
||||
var txt = res[1][idx];
|
||||
if (typeof(txt) !== 'undefined') {
|
||||
django_select2.setData('heavy_val:' + id + ':' + idx, this);
|
||||
django_select2.setData('heavy_txt:' + id + ':' + idx, txt);
|
||||
}
|
||||
});
|
||||
}*/
|
||||
django_select2.updateText($(this));
|
||||
},
|
||||
prepareValText: function (vals, txts, isMultiple) {
|
||||
var data = []
|
||||
|
|
@ -169,7 +86,7 @@ if (!window['django_select2']) {
|
|||
$e.txt('');
|
||||
}
|
||||
},
|
||||
getValText: function ($e, isGetFromClientStoreAllowed) {
|
||||
getValText: function ($e) {
|
||||
var val = $e.select2('val'), res = $e.data('results'), txt = $e.txt(), isMultiple = !!$e.attr('multiple'),
|
||||
f, id = $e.attr('id');
|
||||
if (val || val === 0) { // Means value is set. A numerical 0 is also a valid value.
|
||||
|
|
@ -209,23 +126,6 @@ if (!window['django_select2']) {
|
|||
return [val, txt];
|
||||
}
|
||||
}
|
||||
|
||||
/*if (isGetFromClientStoreAllowed) {
|
||||
txt = [];
|
||||
$(val).each(function (idx) {
|
||||
var value = this, clientLocalVal;
|
||||
|
||||
clientLocalVal = django_select2.getData('heavy_val:' + id + ':' + idx);
|
||||
|
||||
if (clientLocalVal == value) {
|
||||
txt.push(django_select2.getData('heavy_txt:' + id + ':' + idx));
|
||||
}
|
||||
});
|
||||
if (txt || txt === 0) {
|
||||
return [val, txt];
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
|
@ -239,7 +139,7 @@ if (!window['django_select2']) {
|
|||
|
||||
if (val || val === 0) {
|
||||
// Value is set so need to get the text.
|
||||
data = django_select2.getValText(e, false);
|
||||
data = django_select2.getValText(e);
|
||||
if (data && data[0]) {
|
||||
data = django_select2.prepareValText(data[0], data[1], !!e.attr('multiple'));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,191 +0,0 @@
|
|||
/* Copyright (c) 2010-2012 Marcus Westin (MODDED by AppleGrew for Django_Select2)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
;(function(){
|
||||
var store = {},
|
||||
win = window,
|
||||
doc = win.document,
|
||||
localStorageName = 'localStorage',
|
||||
namespace = '__storejs__',
|
||||
storage
|
||||
|
||||
store.disabled = false
|
||||
store.set = function(key, value) {}
|
||||
store.get = function(key) {}
|
||||
store.remove = function(key) {}
|
||||
store.removeAllStartsWith = function(keyStartsWith) {} // - AppleGrew
|
||||
store.clear = function() {}
|
||||
store.transact = function(key, defaultVal, transactionFn) {
|
||||
var val = store.get(key)
|
||||
if (transactionFn == null) {
|
||||
transactionFn = defaultVal
|
||||
defaultVal = null
|
||||
}
|
||||
if (typeof val == 'undefined') { val = defaultVal || {} }
|
||||
transactionFn(val)
|
||||
store.set(key, val)
|
||||
}
|
||||
store.getAll = function() {}
|
||||
|
||||
store.serialize = function(value) {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
store.deserialize = function(value) {
|
||||
if (typeof value != 'string') { return undefined }
|
||||
try { return JSON.parse(value) }
|
||||
catch(e) { return value || undefined }
|
||||
}
|
||||
|
||||
// Functions to encapsulate questionable FireFox 3.6.13 behavior
|
||||
// when about.config::dom.storage.enabled === false
|
||||
// See https://github.com/marcuswestin/store.js/issues#issue/13
|
||||
function isLocalStorageNameSupported() {
|
||||
try { return (localStorageName in win && win[localStorageName]) }
|
||||
catch(err) { return false }
|
||||
}
|
||||
|
||||
if (isLocalStorageNameSupported()) {
|
||||
storage = win[localStorageName]
|
||||
store.set = function(key, val) {
|
||||
if (val === undefined) { return store.remove(key) }
|
||||
storage.setItem(key, store.serialize(val))
|
||||
return val
|
||||
}
|
||||
store.get = function(key) { return store.deserialize(storage.getItem(key)) }
|
||||
store.remove = function(key) { storage.removeItem(key) }
|
||||
store.clear = function() { storage.clear() }
|
||||
store.getAll = function() {
|
||||
var ret = {}
|
||||
for (var i=0; i<storage.length; ++i) {
|
||||
var key = storage.key(i)
|
||||
ret[key] = store.get(key)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
} else if (doc.documentElement.addBehavior) {
|
||||
var storageOwner,
|
||||
storageContainer
|
||||
// Since #userData storage applies only to specific paths, we need to
|
||||
// somehow link our data to a specific path. We choose /favicon.ico
|
||||
// as a pretty safe option, since all browsers already make a request to
|
||||
// this URL anyway and being a 404 will not hurt us here. We wrap an
|
||||
// iframe pointing to the favicon in an ActiveXObject(htmlfile) object
|
||||
// (see: http://msdn.microsoft.com/en-us/library/aa752574(v=VS.85).aspx)
|
||||
// since the iframe access rules appear to allow direct access and
|
||||
// manipulation of the document element, even for a 404 page. This
|
||||
// document can be used instead of the current document (which would
|
||||
// have been limited to the current path) to perform #userData storage.
|
||||
try {
|
||||
storageContainer = new ActiveXObject('htmlfile')
|
||||
storageContainer.open()
|
||||
storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></frame>')
|
||||
storageContainer.close()
|
||||
storageOwner = storageContainer.w.frames[0].document
|
||||
storage = storageOwner.createElement('div')
|
||||
} catch(e) {
|
||||
// somehow ActiveXObject instantiation failed (perhaps some special
|
||||
// security settings or otherwse), fall back to per-path storage
|
||||
storage = doc.createElement('div')
|
||||
storageOwner = doc.body
|
||||
}
|
||||
function withIEStorage(storeFunction) {
|
||||
return function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0)
|
||||
args.unshift(storage)
|
||||
// See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
|
||||
// and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
|
||||
storageOwner.appendChild(storage)
|
||||
storage.addBehavior('#default#userData')
|
||||
storage.load(localStorageName)
|
||||
var result = storeFunction.apply(store, args)
|
||||
storageOwner.removeChild(storage)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
// In IE7, keys may not contain special chars. See all of https://github.com/marcuswestin/store.js/issues/40
|
||||
var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
|
||||
function ieKeyFix(key) {
|
||||
return key.replace(forbiddenCharsRegex, '___')
|
||||
}
|
||||
store.set = withIEStorage(function(storage, key, val) {
|
||||
key = ieKeyFix(key)
|
||||
if (val === undefined) { return store.remove(key) }
|
||||
storage.setAttribute(key, store.serialize(val))
|
||||
storage.save(localStorageName)
|
||||
return val
|
||||
})
|
||||
store.get = withIEStorage(function(storage, key) {
|
||||
key = ieKeyFix(key)
|
||||
return store.deserialize(storage.getAttribute(key))
|
||||
})
|
||||
store.remove = withIEStorage(function(storage, key) {
|
||||
key = ieKeyFix(key)
|
||||
storage.removeAttribute(key)
|
||||
storage.save(localStorageName)
|
||||
})
|
||||
store.clear = withIEStorage(function(storage) {
|
||||
var attributes = storage.XMLDocument.documentElement.attributes
|
||||
storage.load(localStorageName)
|
||||
for (var i=0, attr; attr=attributes[i]; i++) {
|
||||
storage.removeAttribute(attr.name)
|
||||
}
|
||||
storage.save(localStorageName)
|
||||
})
|
||||
store.removeAllStartsWith = withIEStorage(function(storage, keyStartsWith) { // New method added to remove all data
|
||||
// which have keys that start with the
|
||||
// given pattern. - AppleGrew
|
||||
var attributes = storage.XMLDocument.documentElement.attributes
|
||||
storage.load(localStorageName)
|
||||
for (var i=0, attr; attr=attributes[i]; i++) {
|
||||
if (attr.name.indexOf(keyStartsWith) == 0) {
|
||||
storage.removeAttribute(attr.name)
|
||||
}
|
||||
}
|
||||
storage.save(localStorageName)
|
||||
})
|
||||
store.getAll = withIEStorage(function(storage) {
|
||||
var attributes = storage.XMLDocument.documentElement.attributes
|
||||
storage.load(localStorageName)
|
||||
var ret = {}
|
||||
for (var i=0, attr; attr=attributes[i]; ++i) {
|
||||
ret[attr] = store.get(attr)
|
||||
}
|
||||
return ret
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
store.set(namespace, namespace)
|
||||
if (store.get(namespace) != namespace) { store.disabled = true }
|
||||
store.remove(namespace)
|
||||
} catch(e) {
|
||||
store.disabled = true
|
||||
}
|
||||
store.enabled = !store.disabled
|
||||
|
||||
//if (typeof module != 'undefined' && typeof module != 'function') { module.exports = store }
|
||||
//else if (typeof define === 'function' && define.amd) { define(store) }
|
||||
//else { this.store = store }
|
||||
if (this.django_select2) {
|
||||
this.django_select2.store = store //Adds store inside django_select2's namespace instead of global. - AppleGrew
|
||||
}
|
||||
})();
|
||||
|
|
@ -31,7 +31,7 @@ def get_select2_heavy_js_libs():
|
|||
|
||||
from django.conf import settings
|
||||
if settings.configured and settings.DEBUG:
|
||||
return libs + ('js/heavy_data.js', 'js/store.js', )
|
||||
return libs + ('js/heavy_data.js', )
|
||||
else:
|
||||
return libs + ('js/heavy_data.min.js', )
|
||||
|
||||
|
|
@ -340,9 +340,9 @@ class HeavySelect2Mixin(Select2Mixin):
|
|||
3. Otherwise, check the cached results. When the user searches in the fields then all the returned
|
||||
responses from server, which has the value and label mapping, are cached by ``heavy_data.js``.
|
||||
|
||||
4. If we still do not have the label then check the browser localStorage/cookies. When user selects
|
||||
some value then ``heavy_data.js`` stores the selected values and their labels in the browser
|
||||
localStorage/cookies.
|
||||
.. note:: Since version 3.2.0, cookies or localStorage are no longer checked or used. All
|
||||
:py:class:`~.field.HeavyChoiceField` must override :py:meth:`~.fields.HeavyChoiceField.get_val_txt`.
|
||||
If you are only using heavy widgets in your own fields then you should override :py:meth:`.render_texts`.
|
||||
"""
|
||||
self.field = None
|
||||
self.options = dict(self.options) # Making an instance specific copy
|
||||
|
|
|
|||
Loading…
Reference in a new issue