Permalink
Browse files

convert from cookie to local storage

  • Loading branch information...
1 parent 9a2ea6d commit 2c41a2f7b56acfd2d01f8e0b945abf0523bd1a64 @octatone committed Apr 19, 2012
Showing with 20 additions and 97 deletions.
  1. +1 −1 index.html
  2. +0 −89 js/jquery.cookie.js
  3. +8 −0 js/jstorage.min.js
  4. +11 −7 js/tv.js
View
2 index.html
@@ -12,7 +12,7 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="js/jquery.escapify.js" type="text/javascript"></script>
-<script src="js/jquery.cookie.js" type="text/javascript"></script>
+<script src="js/jstorage.min.js" type="text/javascript"></script>
<script src="js/jquery.scrollTo-1.4.2-min.js" type="text/javascript"></script>
<script src="js/jquery.lazyload.min.js" type="text/javascript"></script>
<script src="js/jquery.mousewheel.js" type="text/javascript"></script>
View
89 js/jquery.cookie.js
@@ -1,89 +0,0 @@
-/*jslint browser: true */ /*global jQuery: true */
-
-/**
- * jQuery Cookie plugin
- *
- * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
- * Dual licensed under the MIT and GPL licenses:
- * http://www.opensource.org/licenses/mit-license.php
- * http://www.gnu.org/licenses/gpl.html
- *
- */
-
-// TODO JsDoc
-
-/**
- * Create a cookie with the given key and value and other optional parameters.
- *
- * @example $.cookie('the_cookie', 'the_value');
- * @desc Set the value of a cookie.
- * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
- * @desc Create a cookie with all available options.
- * @example $.cookie('the_cookie', 'the_value');
- * @desc Create a session cookie.
- * @example $.cookie('the_cookie', null);
- * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
- * used when the cookie was set.
- *
- * @param String key The key of the cookie.
- * @param String value The value of the cookie.
- * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
- * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
- * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
- * If set to null or omitted, the cookie will be a session cookie and will not be retained
- * when the the browser exits.
- * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
- * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
- * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
- * require a secure protocol (like HTTPS).
- * @type undefined
- *
- * @name $.cookie
- * @cat Plugins/Cookie
- * @author Klaus Hartl/klaus.hartl@stilbuero.de
- */
-
-/**
- * Get the value of a cookie with the given key.
- *
- * @example $.cookie('the_cookie');
- * @desc Get the value of a cookie.
- *
- * @param String key The key of the cookie.
- * @return The value of the cookie.
- * @type String
- *
- * @name $.cookie
- * @cat Plugins/Cookie
- * @author Klaus Hartl/klaus.hartl@stilbuero.de
- */
-jQuery.cookie = function (key, value, options) {
-
- // key and value given, set cookie...
- if (arguments.length > 1 && (value === null || typeof value !== "object")) {
- options = jQuery.extend({}, options);
-
- if (value === null) {
- options.expires = -1;
- }
-
- if (typeof options.expires === 'number') {
- var days = options.expires, t = options.expires = new Date();
- t.setDate(t.getDate() + days);
- }
-
- return (document.cookie = [
- encodeURIComponent(key), '=',
- options.raw ? String(value) : encodeURIComponent(String(value)),
- options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
- options.path ? '; path=' + options.path : '',
- options.domain ? '; domain=' + options.domain : '',
- options.secure ? '; secure' : ''
- ].join(''));
- }
-
- // key and possibly options given, get cookie...
- options = value || {};
- var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
- return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
-};
View
8 js/jstorage.min.js
@@ -0,0 +1,8 @@
+(function(g){function m(){if(e.jStorage)try{c=n(""+e.jStorage)}catch(a){e.jStorage="{}"}else e.jStorage="{}";j=e.jStorage?(""+e.jStorage).length:0}function h(){try{e.jStorage=o(c),d&&(d.setAttribute("jStorage",e.jStorage),d.save("jStorage")),j=e.jStorage?(""+e.jStorage).length:0}catch(a){}}function i(a){if(!a||"string"!=typeof a&&"number"!=typeof a)throw new TypeError("Key name must be string or numeric");if("__jstorage_meta"==a)throw new TypeError("Reserved key name");return!0}function k(){var a,
+b,d,e=Infinity,f=!1;clearTimeout(p);if(c.__jstorage_meta&&"object"==typeof c.__jstorage_meta.TTL){a=+new Date;d=c.__jstorage_meta.TTL;for(b in d)d.hasOwnProperty(b)&&(d[b]<=a?(delete d[b],delete c[b],f=!0):d[b]<e&&(e=d[b]));Infinity!=e&&(p=setTimeout(k,e-a));f&&h()}}if(!g||!g.toJSON&&!Object.toJSON&&!window.JSON)throw Error("jQuery, MooTools or Prototype needs to be loaded before jStorage!");var c={},e={jStorage:"{}"},d=null,j=0,o=g.toJSON||Object.toJSON||window.JSON&&(JSON.encode||JSON.stringify),
+n=g.evalJSON||window.JSON&&(JSON.decode||JSON.parse)||function(a){return(""+a).evalJSON()},f=!1,p,l={isXML:function(a){return(a=(a?a.ownerDocument||a:0).documentElement)?"HTML"!==a.nodeName:!1},encode:function(a){if(!this.isXML(a))return!1;try{return(new XMLSerializer).serializeToString(a)}catch(b){try{return a.xml}catch(c){}}return!1},decode:function(a){var b="DOMParser"in window&&(new DOMParser).parseFromString||window.ActiveXObject&&function(a){var b=new ActiveXObject("Microsoft.XMLDOM");b.async=
+"false";b.loadXML(a);return b};if(!b)return!1;a=b.call("DOMParser"in window&&new DOMParser||window,a,"text/xml");return this.isXML(a)?a:!1}};g.jStorage={version:"0.1.6.1",set:function(a,b){i(a);if(l.isXML(b))b={_is_xml:!0,xml:l.encode(b)};else{if("function"==typeof b)return;b&&"object"==typeof b&&(b=n(o(b)))}c[a]=b;h();return b},get:function(a,b){i(a);return a in c?c[a]&&"object"==typeof c[a]&&c[a]._is_xml&&c[a]._is_xml?l.decode(c[a].xml):c[a]:"undefined"==typeof b?null:b},deleteKey:function(a){i(a);
+return a in c?(delete c[a],c.__jstorage_meta&&"object"==typeof c.__jstorage_meta.TTL&&a in c.__jstorage_meta.TTL&&delete c.__jstorage_meta.TTL[a],h(),!0):!1},setTTL:function(a,b){var d=+new Date;i(a);b=Number(b)||0;if(a in c){if(!c.__jstorage_meta)c.__jstorage_meta={};if(!c.__jstorage_meta.TTL)c.__jstorage_meta.TTL={};0<b?c.__jstorage_meta.TTL[a]=d+b:delete c.__jstorage_meta.TTL[a];h();k();return!0}return!1},flush:function(){c={};h();return!0},storageObj:function(){function a(){}a.prototype=c;return new a},
+index:function(){var a=[],b;for(b in c)c.hasOwnProperty(b)&&"__jstorage_meta"!=b&&a.push(b);return a},storageSize:function(){return j},currentBackend:function(){return f},storageAvailable:function(){return!!f},reInit:function(){var a;if(d&&d.addBehavior){a=document.createElement("link");d.parentNode.replaceChild(a,d);d=a;d.style.behavior="url(#default#userData)";document.getElementsByTagName("head")[0].appendChild(d);d.load("jStorage");a="{}";try{a=d.getAttribute("jStorage")}catch(b){}e.jStorage=
+a;f="userDataBehavior"}m()}};(function(){var a=!1;if("localStorage"in window)try{window.localStorage.setItem("_tmptest","tmpval"),a=!0,window.localStorage.removeItem("_tmptest")}catch(b){}if(a)try{if(window.localStorage)e=window.localStorage,f="localStorage"}catch(c){}else if("globalStorage"in window)try{window.globalStorage&&(e=window.globalStorage[window.location.hostname],f="globalStorage")}catch(g){}else if(d=document.createElement("link"),d.addBehavior){d.style.behavior="url(#default#userData)";
+document.getElementsByTagName("head")[0].appendChild(d);d.load("jStorage");a="{}";try{a=d.getAttribute("jStorage")}catch(h){}e.jStorage=a;f="userDataBehavior"}else{d=null;return}m();k()})()})(window.jQuery||window.$);
View
18 js/tv.js
@@ -89,12 +89,12 @@ $().ready(function(){
});
$('#auto').click(function() {
Globals.auto = ($('#auto').is(':checked')) ? true : false;
- $.cookie('auto', Globals.auto, {expires: 7});
+ $.jStorage.set('auto', Globals.auto);
});
$('#shuffle').click(function() {
Globals.shuffle = ($('#shuffle').is(':checked')) ? true : false;
Globals.shuffled = []; //reset
- $.cookie('shuffle', Globals.shuffle, {expires: 7});
+ $.jStorage.set('shuffle', Globals.shuffle);
});
$('#sfw').click(function() {
Globals.sfw = ($('#sfw').is(':checked')) ? true : false;
@@ -104,7 +104,7 @@ $().ready(function(){
Globals.sfw = true;
}
}
- $.cookie('sfw', Globals.sfw, {expires: 7});
+ $.jStorage.set('sfw', Globals.sfw);
showHideNsfwThumbs(Globals.sfw, Globals.cur_chan);
});
$('#fill').click(function() {
@@ -173,7 +173,11 @@ $().ready(function(){
/* Main Functions */
function loadSettings() {
- var channels_cookie = $.parseJSON($.cookie('user_channels')), auto_cookie = $.cookie('auto'), sfw_cookie = $.cookie('sfw'), theme_cookie = $.cookie('theme'), shuffle_cookie = $.cookie('shuffle');
+ var channels_cookie = $.jStorage.get('user_channels'),
+ auto_cookie = $.jStorage.get('auto'),
+ sfw_cookie = $.jStorage.get('sfw'),
+ theme_cookie = $.jStorage.get('theme'),
+ shuffle_cookie = $.jStorage.get('shuffle');
if(auto_cookie !== null && auto_cookie !== Globals.auto){
Globals.auto = (auto_cookie === 'true') ? true : false;
@@ -200,7 +204,7 @@ function loadSettings() {
function loadTheme(id) {
$('#theme').attr('href', 'css/theme_' + id + '.css');
- $.cookie('theme', id, {expires: 7});
+ $.jStorage.set('theme', id);
}
function displayChannels() {
@@ -783,7 +787,7 @@ function addChannel(subreddit){
Globals.channels.push(c_data);
Globals.user_channels.push(c_data);
- $.cookie('user_channels', JSON.stringify(Globals.user_channels));
+ $.jStorage.set('user_channels', Globals.user_channels);
var x = Globals.channels.length - 1;
displayChannel(x);
@@ -805,7 +809,7 @@ function removeChan(chan){ //by index (integer)
$('#channel-'+chan).remove();
Globals.user_channels.splice(idx, 1);
- $.cookie('user_channels', JSON.stringify(Globals.user_channels));
+ $.jStorage('user_channels', Globals.user_channels);
//free some memory bitches
Globals.channels[chan] = {channel: '', feed: ''};
Globals.videos[chan] = undefined;

0 comments on commit 2c41a2f

Please sign in to comment.