/**
* Provides a Cache subclass which uses HTML5 `localStorage` for persistence.
*
* @module cache
* @submodule cache-offline
*/
/**
* Extends Cache utility with offline functionality.
* @class CacheOffline
* @extends Cache
* @constructor
*/
function CacheOffline() {
}
var localStorage = null,
// Bug 2529572
try {
}
catch(e) {
}
/////////////////////////////////////////////////////////////////////////////
//
// CacheOffline events
//
/////////////////////////////////////////////////////////////////////////////
/**
* @event error
* @description Fired when an entry could not be added, most likely due to
* exceeded browser quota.
* <dl>
* <dt>error (Object)</dt> <dd>The error object.</dd>
* </dl>
*/
/////////////////////////////////////////////////////////////////////////////
//
// CacheOffline static
//
/////////////////////////////////////////////////////////////////////////////
Y.mix(CacheOffline, {
/**
* Class name.
*
* @property NAME
* @type String
* @static
* @final
* @value "cacheOffline"
*/
NAME: "cacheOffline",
ATTRS: {
/////////////////////////////////////////////////////////////////////////////
//
// CacheOffline Attributes
//
/////////////////////////////////////////////////////////////////////////////
/**
* @attribute sandbox
* @description A string that must be passed in via the constructor.
* This identifier is used to sandbox one cache instance's entries
* from another. Calling the cache instance's flush and length methods
* or get("entries") will apply to only these sandboxed entries.
* @type String
* @default "default"
* @initOnly
*/
sandbox: {
value: "default",
writeOnce: "initOnly"
},
/**
* @attribute expires
* @description Absolute Date when data expires or
* relative number of milliseconds. Zero disables expiration.
* @type Date | Number
* @default 86400000 (one day)
*/
expires: {
value: 86400000
},
/**
* @attribute max
* @description Disabled.
* @readOnly
* @default null
*/
max: {
value: null,
readOnly: true
},
/**
* @attribute uniqueKeys
* @description Always true for CacheOffline.
* @readOnly
* @default true
*/
uniqueKeys: {
value: true,
readOnly: true,
setter: function() {
return true;
}
}
},
/**
* Removes all items from all sandboxes. Useful if localStorage has
* exceeded quota. Only supported on browsers that implement HTML 5
* localStorage.
*
* @method flushAll
* @static
*/
flushAll: function() {
if(store) {
}
// FF2.x and FF3.0.x
else {
}
}
}
}
else {
}
}
});
/////////////////////////////////////////////////////////////////////////////
//
// Offline is supported
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// CacheOffline protected methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Always return null.
*
* @method _setMax
* @protected
*/
return null;
},
/**
* Gets size.
*
* @method _getSize
* @protected
*/
_getSize: function() {
var count = 0,
i=0,
for(; i<l; ++i) {
// Match sandbox id
count++;
}
}
return count;
},
/**
* Gets all entries.
*
* @method _getEntries
* @protected
*/
_getEntries: function() {
var entries = [],
i=0,
for(; i<l; ++i) {
// Match sandbox id
}
}
return entries;
},
/**
* Adds entry to cache.
*
* @method _defAddFn
* @param e {Event.Facade} Event Facade with the following properties:
* <dl>
* <dt>entry (Object)</dt> <dd>The cached entry.</dd>
* </dl>
* @protected
*/
_defAddFn: function(e) {
// Convert Dates to msecs on the way into localStorage
try {
localStorage.setItem(this.get("sandbox")+JSON.stringify({"request":request}), JSON.stringify(entry));
}
catch(error) {
}
},
/**
* Flushes cache.
*
* @method _defFlushFn
* @param e {Event.Facade} Event Facade object.
* @protected
*/
_defFlushFn: function(e) {
var key,
for(; i>-1; --i) {
// Match sandbox id
}
}
},
/////////////////////////////////////////////////////////////////////////////
//
// CacheOffline public methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Adds a new entry to the cache of the format
* {request:request, response:response, cached:cached, expires: expires}.
*
* @method add
* @param request {Object} Request value must be a String or JSON.
* @param response {Object} Response value must be a String or JSON.
*/
/**
* Retrieves cached object for given request, if available.
* Returns null if there is no cache match.
*
* @method retrieve
* @param request {Object} Request object.
* @return {Object} Cached object with the properties request, response,
* and expires, or null.
*/
try {
try {
}
catch(e) {
}
}
catch(e2) {
}
if(entry) {
// Convert msecs to Dates on the way out of localStorage
return entry;
}
}
return null;
}
} :
/////////////////////////////////////////////////////////////////////////////
//
// Offline is not supported
//
/////////////////////////////////////////////////////////////////////////////
{
/**
* Always return null.
*
* @method _setMax
* @protected
*/
return null;
}
});
Y.CacheOffline = CacheOffline;