Skip to content

Instantly share code, notes, and snippets.

@smalljam
Created April 8, 2011 12:37
Show Gist options
  • Save smalljam/909745 to your computer and use it in GitHub Desktop.
Save smalljam/909745 to your computer and use it in GitHub Desktop.
/**
* Created by JetBrains PhpStorm.
* User: adyshev
* Date: 06.04.11
* Time: 12:58
*/
var globalVar = null;
var StrongCookies = jQuery.Class.create({
init: function() {
this.storeMethods = { 'HttpCookies' : { 'setter' : this._setHttpCookies,
'getter' : this._getHttpCookies,
'result' : null,
'weight' : 0 },
'WindowName' : { 'setter' : this._setWindowName,
'getter' : this._getWindowName,
'result' : null,
'weight' : 0 },
'LocalSharedObjects' : { 'setter' : this._setLocalSharedObject,
'getter' : this._getLocalSharedObject,
'result' : null,
'weight' : 0 }
// 'HttpCache' : null,
// 'Html5SessionCookies': null,
// 'Html5LocalStorage' : null,
// 'Html5GlobalStorage' : null,
// 'Html5Sqlite' : null,
// 'Html5PngCanvas' : null
};
},
set: function(key, value) {
this._set(key, value);
},
get: function(key, skipRefreshing, callback) {
this._get(key, this._getBoolean(skipRefreshing), callback);
},
debug: function(id) {
$.each(this.storeMethods, function(key, value) {
$(id).append(key + " : " + value.result + "<br />");
});
},
_get: function(key, skipRefreshing, callback) {
this._listen('gettersDone', function(){
var value = this._getBestCandidateValue();
if (value != null && skipRefreshing == false) {
this._listen('settersDone', function(){
callback(value);
});
this._executeAllSetters(key, value);
}
})
this._executeAllGetters(key);
},
_set: function(key, value) {
this._executeAllSetters(key, value);
},
_getBestCandidateValue: function() {
var best = { 'result' : null,
'weight' : 0 };
$.each(this.storeMethods, function(storeMethodName, service) {
if (service.weight > best.weight) {
best.result = service.result;
best.weight = service.weight;
}
});
return best.result;
},
_calculateValueWeight: function(result) {
var cnt = 0;
if (result != null) {
var cnt = 1;
$.each(this.storeMethods, function(storeMethodName, service) {
if (service.result == result) {
cnt += 1;
}
});
}
return cnt;
},
// OBSERVER
_handlers: {},
_listen: function(evnt, callback){
!this._handlers[evnt] ? this._handlers[evnt] = [] : '';
this._handlers[evnt].push(callback);
},
_fire: function(evnt){
var that = this;
$.each(this._handlers[evnt], function(index, value){
value.apply(that);
});
},
// !OBSERVER
_gettersDone: function(){
this._fire('gettersDone');
},
_settersDone: function(){
this._fire('settersDone');
},
_executeAllGetters: function(key) {
var self = this;
$.each(this.storeMethods, function(storeMethodName, service) {
service.getter(key, function(value){
service.weight = self._calculateValueWeight(value);
service.result = value;
//"if all done" this._gettersDone();
});
});
},
_executeAllSetters: function(key, value) {
$.each(this.storeMethods, function(storeMethodName, service) {
try {
service.setter(key, value);
} catch(e) {
}
});
},
_getBoolean: function(value) {
var result = true;
if ( (typeof(value) == 'undefined')
|| (value == false)
|| (value == null)
|| ( (typeof(value) == 'string') && (value.trim() == ''))
|| (value == 0))
{
result = false;
}
return result;
},
_getLocalSharedObject: function(key, callback) {
var configs = {
flash: '/resources/static/jStore.Flash.html',
json: '/resources/js/browser.json.js'
};
var that = this;
jStore.init('flash', configs, jStore.flavors.flash).engineReady(function (engine) {
callback(engine.get(key));
});
return null;
},
_setLocalSharedObject: function(key, value) {
var configs = {
flash: '/resources/static/jStore.Flash.html',
json: '/resources/js/browser.json.js'
};
jStore.init('flash', configs, jStore.flavors.flash).engineReady(function (engine) {
engine.set(key, value);
});
},
_getWindowName: function(key, callback) {
var p = window.name;
var result = null;
if (p != '') {
var ret = {},
seg = p.replace(/^.*\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
$.each(ret, function(paramName, paramValue) {
if (paramName == key) {
result = paramValue;
}
});
}
callback(result);
},
_setWindowName: function(key, value) {
var p = window.name;
if (p != '') {
var ret = {},
seg = p.replace(/^.*\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
ret[key] = value;
} else {
var ret = {};
ret[key] = value;
}
window.name = $.param(ret);
},
_getHttpCookies: function(key, callback) {
callback($.cookie(key));
},
_setHttpCookies: function(key, value) {
$.cookie(key, null, {
expires: -365 * 20,
path: "/"
});
$.cookie(key, value, {
expires: 365 * 20,
path: "/"
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment