2 Persist
=(function(){var VERSION
='0.1.0',P
,B
,esc
,init
,empty
,ec
;ec
=(function(){var EPOCH
='Thu, 01-Jan-1970 00:00:01 GMT',RATIO
=1000*60*60*24,KEYS
=['expires','path','domain'],esc
=escape
,un
=unescape
,doc
=document
,me
;var get_now=function(){var r
=new Date();r
.setTime(r
.getTime());return r
;}
3 var cookify=function(c_key
,c_val
){var i
,key
,val
,r
=[],opt
=(arguments
.length
>2)?arguments
[2]:{};r
.push(esc(c_key
)+'='+esc(c_val
));for(i
=0;i
<KEYS
.length
;i
++){key
=KEYS
[i
];if(val
=opt
[key
])
6 r
.push('secure');return r
.join('; ');}
7 var alive=function(){var k
='__EC_TEST__',v
=new Date();v
=v
.toGMTString();this.set(k
,v
);this.enabled
=(this.remove(k
)==v
);return this.enabled
;}
8 me
={set:function(key
,val
){var opt
=(arguments
.length
>2)?arguments
[2]:{},now
=get_now(),expire_at
,cfg
={};if(opt
.expires
){opt
.expires
*=RATIO
;cfg
.expires
=new Date(now
.getTime()+opt
.expires
);cfg
.expires
=cfg
.expires
.toGMTString();}
9 var keys
=['path','domain','secure'];for(i
=0;i
<keys
.length
;i
++)
11 cfg
[keys
[i
]]=opt
[keys
[i
]];var r
=cookify(key
,val
,cfg
);doc
.cookie
=r
;return val
;},has:function(key
){key
=esc(key
);var c
=doc
.cookie
,ofs
=c
.indexOf(key
+'='),len
=ofs
+key
.length
+1,sub
=c
.substring(0,key
.length
);return((!ofs
&&key
!=sub
)||ofs
<0)?false:true;},get:function(key
){key
=esc(key
);var c
=doc
.cookie
,ofs
=c
.indexOf(key
+'='),len
=ofs
+key
.length
+1,sub
=c
.substring(0,key
.length
),end
;if((!ofs
&&key
!=sub
)||ofs
<0)
12 return null;end
=c
.indexOf(';',len
);if(end
<0)
13 end
=c
.length
;return un(c
.substring(len
,end
));},remove:function(k
){var r
=me
.get(k
),opt
={expires
:EPOCH
};doc
.cookie
=cookify(k
,'',opt
);return r
;},keys:function(){var c
=doc
.cookie
,ps
=c
.split('; '),i
,p
,r
=[];for(i
=0;i
<ps
.length
;i
++){p
=ps
[i
].split('=');r
.push(un(p
[0]));}
14 return r
;},all:function(){var c
=doc
.cookie
,ps
=c
.split('; '),i
,p
,r
=[];for(i
=0;i
<ps
.length
;i
++){p
=ps
[i
].split('=');r
.push([un(p
[0]),un(p
[1])]);}
15 return r
;},version
:'0.2.1',enabled
:false};me
.enabled
=alive
.call(me
);return me
;}());empty=function(){};esc=function(str
){return'PS'+str
.replace(/_/g,'__').replace(/ /g,'_s');};C={search_order:['gears','localstorage','whatwg_db','globalstorage','flash','ie','cookie'],name_re:/^[a
-z
][a
-z0
-9_
-]+$/i
,methods
:['init','get','set','remove','load','save'],sql
:{version
:'1',create
:"CREATE TABLE IF NOT EXISTS persist_data (k TEXT UNIQUE NOT NULL PRIMARY KEY, v TEXT NOT NULL)",get:"SELECT v FROM persist_data WHERE k = ?",set:"INSERT INTO persist_data(k, v) VALUES (?, ?)",remove
:"DELETE FROM persist_data WHERE k = ?"},flash
:{div_id
:'_persist_flash_wrap',id
:'_persist_flash',path
:'persist.swf',size
:{w
:1,h
:1},args
:{autostart
:true}}};B
={gears
:{size
:-1,test:function(){return(window
.google
&&window
.google
.gears
)?true:false;},methods
:{transaction:function(fn
){var db
=this.db
;db
.execute('BEGIN').close();fn
.call(this,db
);db
.execute('COMMIT').close();},init:function(){var db
;db
=this.db
=google
.gears
.factory
.create('beta.database');db
.open(esc(this.name
));db
.execute(C
.sql
.create
).close();},get:function(key
,fn
,scope
){var r
,sql
=C
.sql
.get;if(!fn
)
16 return;this.transaction(function(t
){r
=t
.execute(sql
,[key
]);if(r
.isValidRow())
17 fn
.call(scope
||this,true,r
.field(0));else
18 fn
.call(scope
||this,false,null);r
.close();});},set:function(key
,val
,fn
,scope
){var rm_sql
=C
.sql
.remove
,sql
=C
.sql
.set,r
;this.transaction(function(t
){t
.execute(rm_sql
,[key
]).close();t
.execute(sql
,[key
,val
]).close();if(fn
)
19 fn
.call(scope
||this,true,val
);});},remove:function(key
,fn
,scope
){var get_sql
=C
.sql
.get;var r
,val
;sql
=C
.sql
.remove
,r
,val
;this.transaction(function(t
){if(fn
){r
=t
.execute(get_sql
,[key
]);if(r
.isValidRow()){val
=r
.field(0);t
.execute(sql
,[key
]).close();fn
.call(scope
||this,true,val
);}else{fn
.call(scope
||this,false,null);}
20 r
.close();}else{t
.execute(sql
,[key
]).close();}});}}},whatwg_db
:{size
:200*1024,test:function(){var name
='PersistJS Test',desc
='Persistent database test.';if(!window
.openDatabase
)
21 return false;if(!window
.openDatabase(name
,C
.sql
.version
,desc
,B
.whatwg_db
.size
))
22 return false;return true;},methods
:{transaction:function(fn
){if(!this.db_created
){var sql
=C
.sql
.create
;this.db
.transaction(function(t
){t
.executeSql(sql
,[],function(){this.db_created
=true;});},empty
);}
23 this.db
.transaction(fn
);},init:function(){var desc
,size
;desc
=this.o
.about
||"Persistent storage for "+this.name
;size
=this.o
.size
||B
.whatwg_db
.size
;this.db
=openDatabase(this.name
,C
.sql
.version
,desc
,size
);},get:function(key
,fn
,scope
){var sql
=C
.sql
.get;if(!fn
)
24 return;scope
=scope
||this;this.transaction(function(t
){t
.executeSql(sql
,[key
],function(t
,r
){if(r
.rows
.length
>0)
25 fn
.call(scope
,true,r
.rows
.item(0)['v']);else
26 fn
.call(scope
,false,null);});});},set:function(key
,val
,fn
,scope
){var rm_sql
=C
.sql
.remove
,sql
=C
.sql
.set;this.transaction(function(t
){t
.executeSql(rm_sql
,[key
],function(){t
.executeSql(sql
,[key
,val
],function(t
,r
){if(fn
)
27 fn
.call(scope
||this,true,val
);});});});return val
;},remove:function(key
,fn
,scope
){var get_sql
=C
.sql
.get;sql
=C
.sql
.remove
;this.transaction(function(t
){if(fn
){t
.executeSql(get_sql
,[key
],function(t
,r
){if(r
.rows
.length
>0){var val
=r
.rows
.item(0)['v'];t
.executeSql(sql
,[key
],function(t
,r
){fn
.call(scope
||this,true,val
);});}else{fn
.call(scope
||this,false,null);}});}else{t
.executeSql(sql
,[key
]);}});}}},globalstorage
:{size
:5*1024*1024,test:function(){return window
.globalStorage
?true:false;},methods
:{key:function(key
){return esc(this.name
)+esc(key
);},init:function(){this.store
=globalStorage
[this.o
.domain
];},get:function(key
,fn
,scope
){key
=this.key(key
);if(fn
)
28 fn
.call(scope
||this,true,this.store
.getItem(key
));},set:function(key
,val
,fn
,scope
){key
=this.key(key
);this.store
.setItem(key
,val
);if(fn
)
29 fn
.call(scope
||this,true,val
);},remove:function(key
,fn
,scope
){var val
;key
=this.key(key
);val
=this.store
[key
];this.store
.removeItem(key
);if(fn
)
30 fn
.call(scope
||this,(val
!==null),val
);}}},localstorage
:{size
:-1,test:function(){return window
.localStorage
?true:false;},methods
:{key:function(key
){return esc(this.name
)+esc(key
);},init:function(){this.store
=localStorage
;},get:function(key
,fn
,scope
){key
=this.key(key
);if(fn
)
31 fn
.call(scope
||this,true,this.store
.getItem(key
));},set:function(key
,val
,fn
,scope
){key
=this.key(key
);this.store
.setItem(key
,val
);if(fn
)
32 fn
.call(scope
||this,true,val
);},remove:function(key
,fn
,scope
){var val
;key
=this.key(key
);val
=this.store
.getItem(key
);this.store
.removeItem(key
);if(fn
)
33 fn
.call(scope
||this,(val
!==null),val
);}}},ie
:{prefix
:'_persist_data-',size
:64*1024,test:function(){return window
.ActiveXObject
?true:false;},make_userdata:function(id
){var el
=document
.createElement('div');el
.id
=id
;el
.style
.display
='none';el
.addBehavior('#default#userData');document
.body
.appendChild(el
);return el
;},methods
:{init:function(){var id
=B
.ie
.prefix
+esc(this.name
);this.el
=B
.ie
.make_userdata(id
);if(this.o
.defer
)
34 this.load();},get:function(key
,fn
,scope
){var val
;key
=esc(key
);if(!this.o
.defer
)
35 this.load();val
=this.el
.getAttribute(key
);if(fn
)
36 fn
.call(scope
||this,val
?true:false,val
);},set:function(key
,val
,fn
,scope
){key
=esc(key
);this.el
.setAttribute(key
,val
);if(!this.o
.defer
)
38 fn
.call(scope
||this,true,val
);},load:function(){this.el
.load(esc(this.name
));},save:function(){this.el
.save(esc(this.name
));}}},cookie
:{delim
:':',size
:4000,test:function(){return P
.Cookie
.enabled
?true:false;},methods
:{key:function(key
){return this.name
+B
.cookie
.delim
+key
;},get:function(key
,val
,fn
,scope
){key
=this.key(key
);val
=ec
.get(key
);if(fn
)
39 fn
.call(scope
||this,val
!=null,val
);},set:function(key
,val
,fn
,scope
){key
=this.key(key
);ec
.set(key
,val
,this.o
);if(fn
)
40 fn
.call(scope
||this,true,val
);},remove:function(key
,val
,fn
,scope
){var val
;key
=this.key(key
);val
=ec
.remove(key
)
42 fn
.call(scope
||this,val
!=null,val
);}}},flash
:{test:function(){if(!window
.SWFObject
||!deconcept
||!deconcept
.SWFObjectUtil
)
43 return false;var major
=deconcept
.SWFObjectUtil
.getPlayerVersion().major
;return(major
>=8)?true:false;},methods
:{init:function(){if(!B
.flash
.el
){var o
,key
,el
,cfg
=C
.flash
;el
=document
.createElement('div');el
.id
=cfg
.div_id
;document
.body
.appendChild(el
);o
=new SWFObject(this.o
.swf_path
||cfg
.path
,cfg
.id
,cfg
.size
.w
,cfg
.size
.h
,'8');for(key
in cfg
.args
)
44 o
.addVariable(key
,cfg
.args
[key
]);o
.write(el
);B
.flash
.el
=document
.getElementById(cfg
.id
);}
45 this.el
=B
.flash
.el
;},get:function(key
,fn
,scope
){var val
;key
=esc(key
);val
=this.el
.get(this.name
,key
);if(fn
)
46 fn
.call(scope
||this,val
!==null,val
);},set:function(key
,val
,fn
,scope
){var old_val
;key
=esc(key
);old_val
=this.el
.set(this.name
,key
,val
);if(fn
)
47 fn
.call(scope
||this,true,val
);},remove:function(key
,fn
,scope
){var val
;key
=esc(key
);val
=this.el
.remove(this.name
,key
);if(fn
)
48 fn
.call(scope
||this,true,val
);}}}};var init=function(){var i
,l
,b
,key
,fns
=C
.methods
,keys
=C
.search_order
;for(i
=0,l
=fns
.length
;i
<l
;i
++)
49 P
.Store
.prototype[fns
[i
]]=empty
;P
.type
=null;P
.size
=-1;for(i
=0,l
=keys
.length
;!P
.type
&&i
<l
;i
++){b
=B
[keys
[i
]];if(b
.test()){P
.type
=keys
[i
];P
.size
=b
.size
;for(key
in b
.methods
)
50 P
.Store
.prototype[key
]=b
.methods
[key
];}}
51 P
._init
=true;};P
={VERSION
:VERSION
,type
:null,size
:0,add:function(o
){B
[o
.id
]=o
;C
.search_order
=[o
.id
].concat(C
.search_order
);init();},remove:function(id
){var ofs
=C
.search_order
.indexOf(id
);if(ofs
<0)
52 return;C
.search_order
.splice(ofs
,1);delete B
[id
];init();},Cookie
:ec
,Store:function(name
,o
){if(!C
.name_re
.exec(name
))
53 throw new Error("Invalid name");if(!P
.type
)
54 throw new Error("No suitable storage found");o
=o
||{};this.name
=name
;o
.domain
=o
.domain
||location
.hostname
||'localhost.localdomain';this.o
=o
;o
.expires
=o
.expires
||365*2;o
.path
=o
.path
||'/';this.init();}};init();return P
;})();
This page took 0.584542 seconds and 4 git commands to generate.