File: api-d/4/js-api/library.js/core/plugin.storage.js

Recommend this page to a friend!
  Classes of Till Wehowski   µ.Flow   api-d/4/js-api/library.js/core/plugin.storage.js   Download  
File: api-d/4/js-api/library.js/core/plugin.storage.js
Role: Class source
Content type: text/plain
Description: localStorage managing
Class: µ.Flow
General purpose library of objects
Author: By
Last change:
Date: 8 years ago
Size: 5,005 bytes
 

Contents

Class file image Download
/* @copyright (c) Till Wehowski - All rights reserved @license (Basic/EULA) http://look-up.webfan.de/webdof-license @license (Source Code Re-Usage) http://look-up.webfan.de/bsd-license Copyright (c) 2015, Till Wehowski All rights reserved. @link https://github.com/frdl/-Flow */ (function() { var Store = function(options, register){ var o = { prefix : '', user :null, pwd : null, ready : false, enc : function(val,options,store){ val=JSON.stringify(val); if(true===options.crypt){ val=frdl.cipher.encrypt(options.pwd, val); } return val; }, dec: function(val,options,store){ try{ if('string'!== typeof val)return undefined; if(true===options.crypt){ val=frdl.cipher.decrypt(options.pwd, val); } val=JSON.parse(val); return val; }catch(err){ return undefined; } }, crypt : false, loggedin : false }; this.opt = function(){ var args = Array.prototype.slice.call(arguments); if(1 === args.length && 'object' === typeof args[0]){ for(var k in args[0]){ o[k]=args[0][k]; } return this; }else if(1 === args.length && 'string' === typeof args[0]){ return o[args[0]]; }else if(2 === args.length && 'string' === typeof args[0]){ o[args[0]] = args[1]; return this; }else if(0 === args.length){ return { prefix : o.prefix, user : o.user , crypt : o.crypt, loggedin : o.loggedin, ready : o.ready }; }else{ return undefined; } }; this.enabled = function(verbose){ var i = (typeof localStorage !=="undefined") ? true : false; if(true === verbose && false === i)console.warn('No local storage available!'); return i; }; this.pfx = function(PFX){ if('string' !== typeof PFX){ return o.prefix; }else{ o.prefix=PFX; return this; } }; this.clear = function(PFX){ if(!this.enabled())return this; localStorage.clear(); return this; }; this.del = function(k){ if(!this.enabled())return this; localStorage.removeItem(o.prefix + k); return this; }; this.save = function (k,v) { if(!this.enabled())return this; localStorage.setItem(o.prefix + k, o.enc(v, o, this)); return this; }; this.load = function (k) { if(!this.enabled())return undefined; return o.dec(localStorage.getItem(o.prefix + k), o, this); }; if('object'===typeof options){ for(var k in options){ o[k]=options[k]; } this.enabled(true); if(true===register && 'function'===typeof this.register)this.register(); } return this; }; Store.prototype.login = function(user, pwd, fN){ var success = false; try{ if('function'===typeof fN){ success=(!!fN(user,pwd)) ? true : false; }else{ if(true===this.opt('crypt')){ var hash = frdl.$DB.load("frdl://$L/schema.info/DATABASE/"+this.pfx()+"hash"); var check =Sha1.hash(frdl.Url().getScheme()+ frdl.Url().getHost()+ 'f 4 '+JSON.stringify(this.opt())); }else{ var hash = null; var check = null; } success=(hash===check) ? true : false; } if(true===success){ this.user(user); this.pwd(pwd); this.opt('user', user); this.opt('pwd', pwd); } }catch(err){ success=false; } this.opt('loggedin', success); return success; }; Store.prototype.logout = function(){ this.login(null, null, function(){ return false; }); return this; }; Store.prototype.pwd = function(pwd, localK){ var _k = (!!localK) ? this.pfx() : ''; var k = "frdl://$L/schema.info/"+_k+"pwd/"; if('string' !== typeof pwd){ return (sessionStorage.getItem(k)) ? sessionStorage.getItem(k) : false; } sessionStorage.setItem(k, pwd); return this; }; Store.prototype.user = function(user, localK){ var _k = (!!localK) ? this.pfx() : ''; var k = "frdl://$L/schema.info/"+_k+"user/"; if('string' !== typeof user){ return (sessionStorage.getItem(k)) ? sessionStorage.getItem(k) : false; } sessionStorage.setItem(k, user); return this; }; Store.prototype.register = function(){ var store= this; if(true===this.opt('crypt')){ /* 'http://'+HOST_API+'/cdn/frdl/flow/libraries/sjcl/sjcl.js' */ if('undefined'===typeof frdl.cipher){ frdl.getScript('http://'+frdl.route('HOST_API')+'/cdn/frdl/flow/libraries/sjcl/sjcl.js', function(){ store.opt('ready', true) ; },true,false); }else{ this.opt('ready', true) ; } }else{ this.opt('ready', true) ; } var k = "frdl://$L/schema.info/DATABASE/"+this.pfx()+"hash"; if(true===this.opt('crypt')){ frdl.$DB.save(k, Sha1.hash(frdl.Url().getScheme()+ frdl.Url().getHost()+ 'f 4 '+JSON.stringify(this.opt()))); } return this; }; frdl.$S = Store; frdl.$DB = new frdl.$S({ prefix : 'frdl://$S/schema.info/', user :null, crypt : false, pwd : null }, false); }());