2013-06-01 07:57:50 -04:00
var Lawnchair = function ( e , h ) { if ( ! ( this instanceof Lawnchair ) ) return new Lawnchair ( e , h ) ; if ( ! JSON ) throw "JSON unavailable! Include http://www.json.org/json2.js to fix." ; if ( arguments . length <= 2 ) { h = typeof arguments [ 0 ] === "function" ? arguments [ 0 ] : arguments [ 1 ] ; e = typeof arguments [ 0 ] === "function" ? { } : arguments [ 0 ] || { } } else throw "Incorrect # of ctor args!" ; this . record = e . record || "record" ; this . name = e . name || "records" ; var a ; if ( e . adapter ) { if ( typeof e . adapter === "string" ) e . adapter = [ e . adapter ] ; for ( var b = 0 , c = e . adapter . length ; b <
c ; b ++ ) { for ( var d = Lawnchair . adapters . length - 1 ; d >= 0 ; d -- ) if ( Lawnchair . adapters [ d ] . adapter === e . adapter [ b ] ) if ( a = Lawnchair . adapters [ d ] . valid ( ) ? Lawnchair . adapters [ d ] : undefined ) break ; if ( a ) break } } else { d = 0 ; for ( c = Lawnchair . adapters . length ; d < c ; d ++ ) if ( a = Lawnchair . adapters [ d ] . valid ( ) ? Lawnchair . adapters [ d ] : undefined ) break } if ( ! a ) throw "No valid adapter." ; for ( b in a ) this [ b ] = a [ b ] ; d = 0 ; for ( c = Lawnchair . plugins . length ; d < c ; d ++ ) Lawnchair . plugins [ d ] . call ( this ) ; this . init ( e , h ) } ; Lawnchair . adapters = [ ] ;
Lawnchair . adapter = function ( e , h ) { h . adapter = e ; var a = "adapter valid init keys save batch get exists all remove nuke" . split ( " " ) , b = this . prototype . indexOf , c ; for ( c in h ) if ( b ( a , c ) === - 1 ) throw "Invalid adapter! Nonstandard method: " + c ; Lawnchair . adapters . splice ( 0 , 0 , h ) } ; Lawnchair . plugins = [ ] ; Lawnchair . plugin = function ( e ) { for ( var h in e ) h === "init" ? Lawnchair . plugins . push ( e [ h ] ) : this . prototype [ h ] = e [ h ] } ;
2013-03-13 11:58:46 -04:00
Lawnchair . prototype = { isArray : Array . isArray || function ( e ) { return Object . prototype . toString . call ( e ) === "[object Array]" } , indexOf : function ( e , h , a , b ) { if ( e . indexOf ) return e . indexOf ( h ) ; a = 0 ; for ( b = e . length ; a < b ; a ++ ) if ( e [ a ] === h ) return a ; return - 1 } , lambda : function ( e ) { return this . fn ( this . record , e ) } , fn : function ( e , h ) { return typeof h == "string" ? new Function ( e , h ) : h } , uuid : function ( ) { var e = function ( ) { return ( ( 1 + Math . random ( ) ) * 65536 | 0 ) . toString ( 16 ) . substring ( 1 ) } ; return e ( ) + e ( ) + "-" + e ( ) + "-" + e ( ) + "-" + e ( ) + "-" + e ( ) + e ( ) + e ( ) } , each : function ( e ) { var h =
this . lambda ( e ) ; if ( this . _ _results ) { e = 0 ; for ( var a = this . _ _results . length ; e < a ; e ++ ) h . call ( this , this . _ _results [ e ] , e ) } else this . all ( function ( b ) { for ( var c = 0 , d = b . length ; c < d ; c ++ ) h . call ( this , b [ c ] , c ) } ) ; return this } } ;
Lawnchair . adapter ( "window-name" , function ( ) { if ( typeof window === "undefined" ) window = { top : { } } ; var e = { } ; try { e = JSON . parse ( window . top . name ) } catch ( h ) { } return { valid : function ( ) { return typeof window . top . name != "undefined" } , init : function ( a , b ) { e [ this . name ] = e [ this . name ] || { index : [ ] , store : { } } ; this . index = e [ this . name ] . index ; this . store = e [ this . name ] . store ; this . fn ( this . name , b ) . call ( this , this ) ; return this } , keys : function ( a ) { this . fn ( "keys" , a ) . call ( this , this . index ) ; return this } , save : function ( a , b ) { var c = a . key || this . uuid ( ) ;
this . exists ( c , function ( d ) { if ( ! d ) { a . key && delete a . key ; this . index . push ( c ) } this . store [ c ] = a ; try { window . top . name = JSON . stringify ( e ) } catch ( g ) { if ( ! d ) { this . index . pop ( ) ; delete this . store [ c ] } throw g ; } if ( b ) { a . key = c ; this . lambda ( b ) . call ( this , a ) } } ) ; return this } , batch : function ( a , b ) { for ( var c = [ ] , d = 0 , g = a . length ; d < g ; d ++ ) this . save ( a [ d ] , function ( f ) { c . push ( f ) } ) ; b && this . lambda ( b ) . call ( this , c ) ; return this } , get : function ( a , b ) { var c ; if ( this . isArray ( a ) ) { c = [ ] ; for ( var d = 0 , g = a . length ; d < g ; d ++ ) c . push ( this . store [ a [ d ] ] ) } else if ( c =
this . store [ a ] ) c . key = a ; b && this . lambda ( b ) . call ( this , c ) ; return this } , exists : function ( a , b ) { this . lambda ( b ) . call ( this , ! ! this . store [ a ] ) ; return this } , all : function ( a ) { for ( var b = [ ] , c = 0 , d = this . index . length ; c < d ; c ++ ) { var g = this . store [ this . index [ c ] ] ; g . key = this . index [ c ] ; b . push ( g ) } this . fn ( this . name , a ) . call ( this , b ) ; return this } , remove : function ( a , b ) { for ( var c = this . isArray ( a ) ? a : [ a ] , d = 0 , g = c . length ; d < g ; d ++ ) { var f = c [ d ] . key ? c [ d ] . key : c [ d ] , i = this . indexOf ( this . index , f ) ; if ( ! ( i < 0 ) ) { delete this . store [ f ] ; this . index . splice ( i , 1 ) } } window . top . name =
JSON . stringify ( e ) ; b && this . lambda ( b ) . call ( this ) ; return this } , nuke : function ( a ) { this . store = e [ this . name ] . store = { } ; this . index = e [ this . name ] . index = [ ] ; window . top . name = JSON . stringify ( e ) ; a && this . lambda ( a ) . call ( this ) ; return this } } } ( ) ) ;
2013-06-01 07:57:50 -04:00
Lawnchair . adapter ( "dom" , function ( ) { var e = window . localStorage , h = function ( a ) { return { key : a + "._index_" , all : function ( ) { var b = e . getItem ( JSON . stringify ( this . key ) ) ; if ( b ) b = JSON . parse ( b ) ; b === null && e . setItem ( JSON . stringify ( this . key ) , JSON . stringify ( [ ] ) ) ; return JSON . parse ( e . getItem ( JSON . stringify ( this . key ) ) ) } , add : function ( b ) { var c = this . all ( ) ; c . push ( b ) ; e . setItem ( JSON . stringify ( this . key ) , JSON . stringify ( c ) ) } , del : function ( b ) { for ( var c = this . all ( ) , d = [ ] , g = 0 , f = c . length ; g < f ; g ++ ) c [ g ] != b && d . push ( c [ g ] ) ; e . setItem ( JSON . stringify ( this . key ) ,
JSON . stringify ( d ) ) } , find : function ( b ) { for ( var c = this . all ( ) , d = 0 , g = c . length ; d < g ; d ++ ) if ( b === c [ d ] ) return d ; return false } } } ; return { valid : function ( ) { return ! ! e && function ( ) { var a = true , b = Math . random ( ) ; try { e . setItem ( b , b ) } catch ( c ) { a = false } e . removeItem ( b ) ; return a } ( ) } , init : function ( a , b ) { this . indexer = h ( this . name ) ; b && this . fn ( this . name , b ) . call ( this , this ) } , save : function ( a , b ) { var c = a . key ? this . name + "." + a . key : this . name + "." + this . uuid ( ) ; delete a . key ; e . setItem ( c , JSON . stringify ( a ) ) ; this . indexer . find ( c ) === false && this . indexer . add ( c ) ;
a . key = c . slice ( this . name . length + 1 ) ; b && this . lambda ( b ) . call ( this , a ) ; return this } , batch : function ( a , b ) { for ( var c = [ ] , d = 0 , g = a . length ; d < g ; d ++ ) this . save ( a [ d ] , function ( f ) { c . push ( f ) } ) ; b && this . lambda ( b ) . call ( this , c ) ; return this } , keys : function ( a ) { if ( a ) { var b = this . name , c = this . indexer . all ( ) , d = [ ] ; if ( Array . prototype . map ) d = c . map ( function ( f ) { return f . replace ( b + "." , "" ) } ) ; else for ( var g in c ) d . push ( g . replace ( b + "." , "" ) ) ; this . fn ( "keys" , a ) . call ( this , d ) } return this } , get : function ( a , b ) { if ( this . isArray ( a ) ) { for ( var c = [ ] , d = 0 ,
g = a . length ; d < g ; d ++ ) { var f = this . name + "." + a [ d ] ; if ( f = e . getItem ( f ) ) { f = JSON . parse ( f ) ; f . key = a [ d ] } c . push ( f ) } b && this . lambda ( b ) . call ( this , c ) } else { f = this . name + "." + a ; if ( f = e . getItem ( f ) ) { f = JSON . parse ( f ) ; f . key = a } b && this . lambda ( b ) . call ( this , f ) } return this } , exists : function ( a , b ) { var c = this . indexer . find ( this . name + "." + a ) === false ? false : true ; this . lambda ( b ) . call ( this , c ) ; return this } , all : function ( a ) { for ( var b = this . indexer . all ( ) , c = [ ] , d , g , f = 0 , i = b . length ; f < i ; f ++ ) { g = b [ f ] ; d = JSON . parse ( e . getItem ( g ) ) ; d . key = g . replace ( this . name +
"." , "" ) ; c . push ( d ) } a && this . fn ( this . name , a ) . call ( this , c ) ; return this } , remove : function ( a , b ) { var c = this ; if ( this . isArray ( a ) ) { var d , g = a . length , f = function ( i ) { c . remove ( a [ i ] , function ( ) { -- g > 0 || b && c . lambda ( b ) . call ( c ) } ) } ; for ( d = 0 ; d < a . length ; d ++ ) f ( d ) ; return this } d = this . name + "." + ( a . key ? a . key : a ) ; this . indexer . del ( d ) ; e . removeItem ( d ) ; b && this . lambda ( b ) . call ( this ) ; return this } , nuke : function ( a ) { this . all ( function ( b ) { for ( var c = 0 , d = b . length ; c < d ; c ++ ) this . remove ( b [ c ] ) ; a && this . lambda ( a ) . call ( this ) } ) ; return this } } } ( ) ) ;