| 1 |
/** |
| 2 |
* Client Settings |
| 3 |
* |
| 4 |
* Attempts to utilize localStorage if available, otherwise falls back to cookies. |
| 5 |
* |
| 6 |
* @version 0.1.0 |
| 7 |
* @returns {Object} |
| 8 |
*/ |
| 9 |
define( 'udx.storage', [ 'udx.utility' ], function settings() { |
| 10 |
console.debug( 'udx.storage', 'loaded' ); |
| 11 |
|
| 12 |
function Settings( name, options ) { |
| 13 |
console.debug( 'udx.storage', 'created' ); |
| 14 |
|
| 15 |
// Return native. |
| 16 |
if( 'object' === typeof window.localStorage && 'function' === typeof window.localStorage.getItem ) { |
| 17 |
console.debug( 'udx.storage', 'Browser supports native localStorage.' ); |
| 18 |
|
| 19 |
return window.localStorage; |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
Object.defineProperties( this, { |
| 24 |
store: { |
| 25 |
value: {}, |
| 26 |
enumerable: false, |
| 27 |
configurable: true, |
| 28 |
writable: true |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
return this; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Settings Instance Properties. |
| 38 |
* |
| 39 |
*/ |
| 40 |
Object.defineProperties( Settings.prototype, { |
| 41 |
set: { |
| 42 |
value: function set( key, value, vEnd, sPath, sDomain, bSecure ) { |
| 43 |
if( !key || /^(?:expires|max\-age|path|domain|secure)$/i.test( key ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
var sExpires = ""; |
| 47 |
if( vEnd ) { |
| 48 |
switch( vEnd.constructor ) { |
| 49 |
case Number: |
| 50 |
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; |
| 51 |
break; |
| 52 |
case String: |
| 53 |
sExpires = "; expires=" + vEnd; |
| 54 |
break; |
| 55 |
case Date: |
| 56 |
sExpires = "; expires=" + vEnd.toUTCString(); |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
document.cookie = encodeURIComponent( key ) + "=" + encodeURIComponent( value ) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); |
| 61 |
return true; |
| 62 |
|
| 63 |
}, |
| 64 |
enumerable: true, |
| 65 |
configurable: true, |
| 66 |
writable: true |
| 67 |
}, |
| 68 |
get: { |
| 69 |
value: function get( key ) { |
| 70 |
|
| 71 |
return decodeURIComponent( document.cookie.replace( new RegExp( "(?:(?:^|.*;)\\s*" + encodeURIComponent( key ).replace( /[\-\.\+\*]/g, "\\$&" ) + "\\s*\\=\\s*([^;]*).*$)|^.*$" ), "$1" ) ) || null; |
| 72 |
|
| 73 |
}, |
| 74 |
enumerable: true, |
| 75 |
configurable: true, |
| 76 |
writable: true |
| 77 |
}, |
| 78 |
remove: { |
| 79 |
value: function remove( key, sPath, sDomain ) { |
| 80 |
if( !key || !this.has( key ) ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
document.cookie = encodeURIComponent( key ) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : ""); |
| 84 |
return true; |
| 85 |
|
| 86 |
}, |
| 87 |
enumerable: true, |
| 88 |
configurable: true, |
| 89 |
writable: true |
| 90 |
}, |
| 91 |
has: { |
| 92 |
value: function has( key ) { |
| 93 |
return (new RegExp( "(?:^|;\\s*)" + encodeURIComponent( key ).replace( /[\-\.\+\*]/g, "\\$&" ) + "\\s*\\=" )).test( document.cookie ); |
| 94 |
|
| 95 |
}, |
| 96 |
enumerable: true, |
| 97 |
configurable: true, |
| 98 |
writable: true |
| 99 |
}, |
| 100 |
keys: { |
| 101 |
value: function keys() { |
| 102 |
var aKeys = document.cookie.replace( /((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "" ).split( /\s*(?:\=[^;]*)?;\s*/ ); |
| 103 |
|
| 104 |
for( var nIdx = 0; nIdx < aKeys.length; nIdx++ ) { |
| 105 |
aKeys[nIdx] = decodeURIComponent( aKeys[nIdx] ); |
| 106 |
} |
| 107 |
return aKeys; |
| 108 |
|
| 109 |
}, |
| 110 |
enumerable: true, |
| 111 |
configurable: true, |
| 112 |
writable: true |
| 113 |
}, |
| 114 |
data: { |
| 115 |
value: function data() { |
| 116 |
|
| 117 |
var data = {}; |
| 118 |
|
| 119 |
var aKeys = document.cookie.replace( /((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "" ).split( /\s*(?:\=[^;]*)?;\s*/ ); |
| 120 |
|
| 121 |
for( var nIdx = 0; nIdx < aKeys.length; nIdx++ ) { |
| 122 |
var key = decodeURIComponent( aKeys[nIdx] ); |
| 123 |
data[ key ] = this.get( key ); |
| 124 |
} |
| 125 |
|
| 126 |
return data; |
| 127 |
|
| 128 |
}, |
| 129 |
enumerable: true, |
| 130 |
configurable: true, |
| 131 |
writable: true |
| 132 |
}, |
| 133 |
getItem: { |
| 134 |
value: function getItem( key ) { |
| 135 |
return this.get( key ); |
| 136 |
}, |
| 137 |
enumerable: true, |
| 138 |
configurable: true, |
| 139 |
writable: true |
| 140 |
}, |
| 141 |
setItem: { |
| 142 |
value: function setItem( key, value ) { |
| 143 |
return this.get( key, value ); |
| 144 |
}, |
| 145 |
enumerable: true, |
| 146 |
configurable: true, |
| 147 |
writable: true |
| 148 |
} |
| 149 |
}); |
| 150 |
|
| 151 |
/** |
| 152 |
* Settings Constructor Properties. |
| 153 |
* |
| 154 |
*/ |
| 155 |
Object.defineProperties( Settings, { |
| 156 |
create: { |
| 157 |
/** |
| 158 |
* |
| 159 |
* @param name {String|Null} |
| 160 |
* @param options {Object|Null} |
| 161 |
* @returns {settings.Settings} |
| 162 |
*/ |
| 163 |
value: function create( name, options ) { |
| 164 |
return new Settings( name, options ) |
| 165 |
}, |
| 166 |
enumerable: true, |
| 167 |
configurable: true, |
| 168 |
writable: true |
| 169 |
}, |
| 170 |
version: { |
| 171 |
value: '1.0.1', |
| 172 |
enumerable: false, |
| 173 |
writable: false |
| 174 |
} |
| 175 |
}); |
| 176 |
|
| 177 |
return Settings; |
| 178 |
|
| 179 |
}); |