Making heavy use localStorage instead of cookies and few more heavy related fixes.

This commit is contained in:
AppleGrew (applegrew) 2013-01-13 10:02:39 +05:30
parent 8dea1a6446
commit aa60bb36ec
5 changed files with 348 additions and 42 deletions

View file

@ -346,8 +346,11 @@ class ModelChoiceFieldMixin(object):
def __init__(self, *args, **kwargs):
queryset = kwargs.pop('queryset', None)
# This filters out kwargs not supported by Field but are still passed as it is required
# by other codes. If new args are added to Field then make sure they are added here too.
kargs = extract_some_key_val(kwargs, [
'empty_label', 'cache_choices', 'required', 'label', 'initial', 'help_text',
'validators', 'localize',
])
kargs['widget'] = kwargs.pop('widget', getattr(self, 'widget', None))
kargs['to_field_name'] = kwargs.pop('to_field_name', 'pk')

View file

@ -38,11 +38,12 @@ if (!window['django_select2']) {
}
return results;
},
setCookie: function (c_name, value) {
/*setCookie: function (c_name, value) {
document.cookie = c_name + "=" + escape(value);
},
getCookie: function (c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
var i, x, y,
ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
@ -55,9 +56,8 @@ if (!window['django_select2']) {
},
delCookie: function (c_name, isStartsWithPattern) {
var i, x, ARRcookies;
if (!isStartsWithPattern) {
document.cookie = c_name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = c_name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
} else {
ARRcookies = document.cookie.split(";");
@ -70,25 +70,59 @@ if (!window['django_select2']) {
}
}
},
onValChange: function () {
var e = $(this), res, id = e.attr('id');
res = django_select2.getValText(e, false);
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]) {
// Cookies are used to persist selection's text. This is needed
// when the form springs back if there is any validation failure.
// 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.setCookie(id + '_heavy_val:' + idx, this);
django_select2.setCookie(id + '_heavy_txt:' + idx, txt);
django_select2.setData('heavy_val:' + id + ':' + idx, this);
django_select2.setData('heavy_txt:' + id + ':' + idx, txt);
}
});
} else {
django_select2.delCookie(id + '_heavy_val:', true);
django_select2.delCookie(id + '_heavy_txt:', true);
}
}*/
},
prepareValText: function (vals, txts, isMultiple) {
var data = []
@ -105,7 +139,37 @@ if (!window['django_select2']) {
}
}
},
getValText: function ($e, isGetFromCookieAllowed) {
updateText: function ($e) {
var val = $e.select2('val'), data = $e.select2('data'), txt = $e.txt(), isMultiple = !!$e.attr('multiple'),
diff;
if (val || val === 0) { // Means value is set. A numerical 0 is also a valid value.
if (isMultiple) {
if (val.length !== txt.length) {
txt = [];
$(val).each(function (idx) {
var i, value = this, id;
for (i in data) {
id = data [i].id;
if (id instanceof String) {
id = id.valueOf();
}
if (id == value) {
txt.push(data[i].text);
}
}
});
}
} else {
txt = data.text;
}
$e.txt(txt);
} else {
$e.txt('');
}
},
getValText: function ($e, isGetFromClientStoreAllowed) {
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.
@ -117,7 +181,7 @@ if (!window['django_select2']) {
}
}
if (txt || txt === 0) {
if (txt === 0 || (txt && val.length === txt.length)) {
return [val, txt];
}
@ -146,21 +210,21 @@ if (!window['django_select2']) {
}
}
if (isGetFromCookieAllowed) {
/*if (isGetFromClientStoreAllowed) {
txt = [];
$(val).each(function (idx) {
var value = this, cookieVal;
var value = this, clientLocalVal;
cookieVal = django_select2.getCookie(id + '_heavy_val:' + idx);
clientLocalVal = django_select2.getData('heavy_val:' + id + ':' + idx);
if (cookieVal == value) {
txt.push(django_select2.getCookie(id + '_heavy_txt:' + idx));
if (clientLocalVal == value) {
txt.push(django_select2.getData('heavy_txt:' + id + ':' + idx));
}
});
if (txt || txt === 0) {
return [val, txt];
}
}
}*/
}
return null;
@ -175,7 +239,7 @@ if (!window['django_select2']) {
if (val || val === 0) {
// Value is set so need to get the text.
data = django_select2.getValText(e);
data = django_select2.getValText(e, false);
if (data && data[0]) {
data = django_select2.prepareValText(data[0], data[1], !!e.attr('multiple'));
}
@ -184,6 +248,7 @@ if (!window['django_select2']) {
e.val(null); // Nulling out set value so as not to confuse users.
}
callback(data); // Change for 2.3.x
django_select2.updateText(e);
},
onMultipleHiddenChange: function () {
var $e = $(this), valContainer = $e.data('valContainer'), name = $e.data('name'), vals = $e.val();
@ -219,7 +284,13 @@ if (!window['django_select2']) {
var args = Array.prototype.slice.call(arguments);
return f.apply($('#' + id).get(0), args);
}
}
},
logErr: function () {
if (console && console.error) {
args = Array.prototype.slice.call(arguments);
console.error.apply(console, args);
}
},
};
(function( $ ){
@ -236,12 +307,18 @@ if (!window['django_select2']) {
}
}
this.attr('txt', val);
} else {
this.removeAttr('txt');
}
return this;
} else {
val = this.attr('txt');
if (val && this.attr('multiple')) {
val = val.split(django_select2.MULTISEPARATOR);
if (this.attr('multiple')) {
if (val) {
val = val.split(django_select2.MULTISEPARATOR);
} else {
val = [];
}
}
return val;
}

View file

@ -0,0 +1,191 @@
/* 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
}
})();

View file

@ -19,13 +19,21 @@ from . import __RENDER_SELECT2_STATICS as RENDER_SELECT2_STATICS
logger = logging.getLogger(__name__)
def get_select2_js_path():
def get_select2_js_libs():
from django.conf import settings
if settings.configured and settings.DEBUG:
return 'js/select2.js'
return ('js/select2.js', )
else:
return 'js/select2.min.js'
return ('js/select2.min.js', )
def get_select2_heavy_js_libs():
libs = get_select2_js_libs()
from django.conf import settings
if settings.configured and settings.DEBUG:
return libs + ('js/heavy_data.js', 'js/store.js', )
else:
return libs + ('js/heavy_data.min.js', )
### Light mixin and widgets ###
@ -198,7 +206,7 @@ class Select2Mixin(object):
return mark_safe(s)
class Media:
js = (get_select2_js_path(), )
js = get_select2_js_libs()
css = {'screen': ('css/select2.css', 'css/extra.css', )}
@ -332,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 cookies. When user selects some value then
``heavy_data.js`` stores the selected values and their labels in the cookies. These are cleared
when browser is closed.
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.
"""
self.field = None
self.options = dict(self.options) # Making an instance specific copy
@ -431,7 +439,7 @@ class HeavySelect2Mixin(Select2Mixin):
return js
class Media:
js = (get_select2_js_path(), 'js/heavy_data.js', )
js = get_select2_heavy_js_libs()
css = {'screen': ('css/select2.css', 'css/extra.css', )}

View file

@ -4,6 +4,11 @@ from django_select2 import *
from .models import Employee, Dept, ClassRoom, Lab, Word
from django.core.exceptions import ValidationError
def validate_fail_always(value):
raise ValidationError(u'%s not valid. Infact nothing is valid!' % value)
############# Choice fields ###################
class EmployeeChoices(AutoModelSelect2Field):
@ -23,17 +28,27 @@ class WordChoices(AutoModelSelect2Field):
search_fields = ['word__icontains', ]
class SelfChoices(AutoSelect2Field):
def get_val_txt(self, value):
if not hasattr(self, 'res_map'):
self.res_map = {}
return self.res_map.get(value, None)
def get_results(self, request, term, page, context):
if not hasattr(self, 'res_map'):
self.res_map = {}
mlen = len(self.res_map)
res = []
for i in range(1, 6):
res.append((i, term * i,))
idx = i + mlen
res.append((idx, term * i,))
self.res_map[idx] = term * i
self.choices = res
return (NO_ERR_RESP, False, res)
class SelfMultiChoices(AutoSelect2MultipleField):
big_data = {
1: "First", 2: "Second", 3: "Third",
1: u"First", 2: u"Second", 3: u"Third",
}
def validate_value(self, value):
@ -46,12 +61,20 @@ class SelfMultiChoices(AutoSelect2MultipleField):
return int(value)
def get_val_txt(self, value):
return self.big_data.get(value, None)
if not hasattr(self, '_big_data'):
self._big_data = dict(self.big_data)
return self._big_data.get(value, None)
def get_results(self, request, term, page, context):
res = [(v, self.big_data[v]) for v in self.big_data]
for i in range(len(res), 6):
res.append((i, term * i,))
if not hasattr(self, '_big_data'):
self._big_data = dict(self.big_data)
res = [(v, self._big_data[v]) for v in self._big_data]
blen = len(res)
for i in range(1, 6):
idx = i + blen
res.append((idx, term * i,))
self._big_data[idx] = term * i
self.choices = res
return (NO_ERR_RESP, False, res)
@ -89,6 +112,10 @@ class MixedForm(forms.Form):
}
)
)
always_fail_rooms = ClassRoomSingleChoices(validators=[validate_fail_always])
always_fail_rooms_multi = ClassRoomChoices(validators=[validate_fail_always])
always_fail_self_choice = SelfChoices(validators=[validate_fail_always], auto_id='always_fail_self_choice')
always_fail_self_choice_multi = SelfMultiChoices(validators=[validate_fail_always], auto_id='always_fail_self_choice_multi')
# These are just for testing Auto registration of fields
EmployeeChoices() # Should already be registered