| 1 |
/** |
| 2 |
* Settings |
| 3 |
* |
| 4 |
* @version 0.1.4 |
| 5 |
* @returns {Object} |
| 6 |
*/ |
| 7 |
define( 'udx.settings', [ 'udx.utility' ], function settings() { |
| 8 |
console.log( 'udx.settings', 'loaded' ); |
| 9 |
|
| 10 |
function Settings( name, options ) { |
| 11 |
console.log( 'udx.settings', 'created' ); |
| 12 |
|
| 13 |
Object.defineProperties( this, { |
| 14 |
store: { |
| 15 |
value: {}, |
| 16 |
enumerable: false, |
| 17 |
configurable: true, |
| 18 |
writable: true |
| 19 |
} |
| 20 |
}); |
| 21 |
|
| 22 |
return this; |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Settings Instance Properties. |
| 28 |
* |
| 29 |
*/ |
| 30 |
Object.defineProperties( Settings.prototype, { |
| 31 |
set: { |
| 32 |
value: function set( sKey, sValue, vEnd, sPath, sDomain, bSecure ) { |
| 33 |
if( !sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test( sKey ) ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
var sExpires = ""; |
| 37 |
if( vEnd ) { |
| 38 |
switch( vEnd.constructor ) { |
| 39 |
case Number: |
| 40 |
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; |
| 41 |
break; |
| 42 |
case String: |
| 43 |
sExpires = "; expires=" + vEnd; |
| 44 |
break; |
| 45 |
case Date: |
| 46 |
sExpires = "; expires=" + vEnd.toUTCString(); |
| 47 |
break; |
| 48 |
} |
| 49 |
} |
| 50 |
document.cookie = encodeURIComponent( sKey ) + "=" + encodeURIComponent( sValue ) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); |
| 51 |
return true; |
| 52 |
|
| 53 |
}, |
| 54 |
enumerable: true, |
| 55 |
configurable: true, |
| 56 |
writable: true |
| 57 |
}, |
| 58 |
get: { |
| 59 |
value: function get( key ) { |
| 60 |
|
| 61 |
return decodeURIComponent( document.cookie.replace( new RegExp( "(?:(?:^|.*;)\\s*" + encodeURIComponent( key ).replace( /[\-\.\+\*]/g, "\\$&" ) + "\\s*\\=\\s*([^;]*).*$)|^.*$" ), "$1" ) ) || null; |
| 62 |
|
| 63 |
}, |
| 64 |
enumerable: true, |
| 65 |
configurable: true, |
| 66 |
writable: true |
| 67 |
}, |
| 68 |
remove: { |
| 69 |
value: function remove( sKey, sPath, sDomain ) { |
| 70 |
if( !sKey || !this.has( sKey ) ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
document.cookie = encodeURIComponent( sKey ) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : ""); |
| 74 |
return true; |
| 75 |
|
| 76 |
}, |
| 77 |
enumerable: true, |
| 78 |
configurable: true, |
| 79 |
writable: true |
| 80 |
}, |
| 81 |
has: { |
| 82 |
value: function has( sKey ) { |
| 83 |
return (new RegExp( "(?:^|;\\s*)" + encodeURIComponent( sKey ).replace( /[\-\.\+\*]/g, "\\$&" ) + "\\s*\\=" )).test( document.cookie ); |
| 84 |
|
| 85 |
}, |
| 86 |
enumerable: true, |
| 87 |
configurable: true, |
| 88 |
writable: true |
| 89 |
}, |
| 90 |
keys: { |
| 91 |
value: function keys() { |
| 92 |
var aKeys = document.cookie.replace( /((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "" ).split( /\s*(?:\=[^;]*)?;\s*/ ); |
| 93 |
|
| 94 |
for( var nIdx = 0; nIdx < aKeys.length; nIdx++ ) { |
| 95 |
aKeys[nIdx] = decodeURIComponent( aKeys[nIdx] ); |
| 96 |
} |
| 97 |
return aKeys; |
| 98 |
|
| 99 |
}, |
| 100 |
enumerable: true, |
| 101 |
configurable: true, |
| 102 |
writable: true |
| 103 |
}, |
| 104 |
data: { |
| 105 |
value: function data() { |
| 106 |
|
| 107 |
var data = {}; |
| 108 |
|
| 109 |
var aKeys = document.cookie.replace( /((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "" ).split( /\s*(?:\=[^;]*)?;\s*/ ); |
| 110 |
|
| 111 |
for( var nIdx = 0; nIdx < aKeys.length; nIdx++ ) { |
| 112 |
var key = decodeURIComponent( aKeys[nIdx] ); |
| 113 |
data[ key ] = this.get( key ); |
| 114 |
} |
| 115 |
|
| 116 |
return data; |
| 117 |
|
| 118 |
}, |
| 119 |
enumerable: true, |
| 120 |
configurable: true, |
| 121 |
writable: true |
| 122 |
} |
| 123 |
}); |
| 124 |
|
| 125 |
/** |
| 126 |
* Settings Constructor Properties. |
| 127 |
* |
| 128 |
*/ |
| 129 |
Object.defineProperties( Settings, { |
| 130 |
create: { |
| 131 |
value: function create( name, options ) { |
| 132 |
return new Settings( name, options ) |
| 133 |
}, |
| 134 |
enumerable: true, |
| 135 |
configurable: true, |
| 136 |
writable: true |
| 137 |
} |
| 138 |
}); |
| 139 |
|
| 140 |
return Settings; |
| 141 |
|
| 142 |
}); |