~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/cache-offline/cache-offline-debug.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('cache-offline', function(Y) {
 
8
 
 
9
/**
 
10
 * Provides a Cache subclass which uses HTML5 `localStorage` for persistence.
 
11
 * 
 
12
 * @module cache
 
13
 * @submodule cache-offline
 
14
 */
 
15
 
 
16
/**
 
17
 * Extends Cache utility with offline functionality.
 
18
 * @class CacheOffline
 
19
 * @extends Cache
 
20
 * @constructor
 
21
 */
 
22
function CacheOffline() {
 
23
    CacheOffline.superclass.constructor.apply(this, arguments);
 
24
}
 
25
 
 
26
var localStorage = null,
 
27
    JSON = Y.JSON;
 
28
 
 
29
// Bug 2529572
 
30
try {
 
31
    localStorage = Y.config.win.localStorage;
 
32
}
 
33
catch(e) {
 
34
    Y.log("Could not access localStorage.", "warn", "cache");
 
35
}
 
36
 
 
37
/////////////////////////////////////////////////////////////////////////////
 
38
//
 
39
// CacheOffline events
 
40
//
 
41
/////////////////////////////////////////////////////////////////////////////
 
42
 
 
43
/**
 
44
* @event error
 
45
* @description Fired when an entry could not be added, most likely due to
 
46
* exceeded browser quota.
 
47
* <dl>
 
48
* <dt>error (Object)</dt> <dd>The error object.</dd>
 
49
* </dl>
 
50
*/
 
51
 
 
52
/////////////////////////////////////////////////////////////////////////////
 
53
//
 
54
// CacheOffline static
 
55
//
 
56
/////////////////////////////////////////////////////////////////////////////
 
57
Y.mix(CacheOffline, {
 
58
    /**
 
59
     * Class name.
 
60
     *
 
61
     * @property NAME
 
62
     * @type String
 
63
     * @static
 
64
     * @final
 
65
     * @value "cacheOffline"
 
66
     */
 
67
    NAME: "cacheOffline",
 
68
 
 
69
    ATTRS: {
 
70
        /////////////////////////////////////////////////////////////////////////////
 
71
        //
 
72
        // CacheOffline Attributes
 
73
        //
 
74
        /////////////////////////////////////////////////////////////////////////////
 
75
 
 
76
        /**
 
77
        * @attribute sandbox
 
78
        * @description A string that must be passed in via the constructor.
 
79
        * This identifier is used to sandbox one cache instance's entries
 
80
        * from another. Calling the cache instance's flush and length methods
 
81
        * or get("entries") will apply to only these sandboxed entries.
 
82
        * @type String
 
83
        * @default "default"
 
84
        * @initOnly
 
85
        */
 
86
        sandbox: {
 
87
            value: "default",
 
88
            writeOnce: "initOnly"
 
89
        },
 
90
 
 
91
        /**
 
92
        * @attribute expires
 
93
        * @description Absolute Date when data expires or
 
94
        * relative number of milliseconds. Zero disables expiration.
 
95
        * @type Date | Number
 
96
        * @default 86400000 (one day)
 
97
        */
 
98
        expires: {
 
99
            value: 86400000
 
100
        },
 
101
 
 
102
        /**
 
103
        * @attribute max
 
104
        * @description Disabled.
 
105
        * @readOnly
 
106
        * @default null
 
107
        */
 
108
        max: {
 
109
            value: null,
 
110
            readOnly: true
 
111
        },
 
112
 
 
113
        /**
 
114
        * @attribute uniqueKeys
 
115
        * @description Always true for CacheOffline.
 
116
        * @readOnly
 
117
        * @default true
 
118
        */
 
119
        uniqueKeys: {
 
120
            value: true,
 
121
            readOnly: true,
 
122
            setter: function() {
 
123
                return true;
 
124
            }
 
125
        }
 
126
    },
 
127
 
 
128
    /**
 
129
     * Removes all items from all sandboxes. Useful if localStorage has
 
130
     * exceeded quota. Only supported on browsers that implement HTML 5
 
131
     * localStorage.
 
132
     *
 
133
     * @method flushAll
 
134
     * @static
 
135
     */
 
136
    flushAll: function() {
 
137
        var store = localStorage, key;
 
138
        if(store) {
 
139
            if(store.clear) {
 
140
                store.clear();
 
141
            }
 
142
            // FF2.x and FF3.0.x
 
143
            else {
 
144
                for (key in store) {
 
145
                    if (store.hasOwnProperty(key)) {
 
146
                        store.removeItem(key);
 
147
                        delete store[key];
 
148
                    }
 
149
                }
 
150
            }
 
151
            Y.log("All sandboxes of OfflineCache flushed", "info", "cache");
 
152
        }
 
153
        else {
 
154
            Y.log("Could not flush all OfflineCache sandboxes.", "warn", "cache");
 
155
        }
 
156
    }
 
157
});
 
158
 
 
159
Y.extend(CacheOffline, Y.Cache, localStorage ? {
 
160
/////////////////////////////////////////////////////////////////////////////
 
161
//
 
162
// Offline is supported
 
163
//
 
164
/////////////////////////////////////////////////////////////////////////////
 
165
 
 
166
    /////////////////////////////////////////////////////////////////////////////
 
167
    //
 
168
    // CacheOffline protected methods
 
169
    //
 
170
    /////////////////////////////////////////////////////////////////////////////
 
171
    /**
 
172
     * Always return null.
 
173
     *
 
174
     * @method _setMax
 
175
     * @protected
 
176
     */
 
177
    _setMax: function(value) {
 
178
        return null;
 
179
    },
 
180
 
 
181
    /**
 
182
     * Gets size.
 
183
     *
 
184
     * @method _getSize
 
185
     * @protected
 
186
     */
 
187
    _getSize: function() {
 
188
        var count = 0,
 
189
            i=0,
 
190
            l=localStorage.length;
 
191
        for(; i<l; ++i) {
 
192
            // Match sandbox id
 
193
            if(localStorage.key(i).indexOf(this.get("sandbox")) === 0) {
 
194
                count++;
 
195
            }
 
196
        }
 
197
        return count;
 
198
    },
 
199
 
 
200
    /**
 
201
     * Gets all entries.
 
202
     *
 
203
     * @method _getEntries
 
204
     * @protected
 
205
     */
 
206
    _getEntries: function() {
 
207
        var entries = [],
 
208
            i=0,
 
209
            l=localStorage.length,
 
210
            sandbox = this.get("sandbox");
 
211
        for(; i<l; ++i) {
 
212
            // Match sandbox id
 
213
            if(localStorage.key(i).indexOf(sandbox) === 0) {
 
214
                entries[i] = JSON.parse(localStorage.key(i).substring(sandbox.length));
 
215
            }
 
216
        }
 
217
        return entries;
 
218
    },
 
219
 
 
220
    /**
 
221
     * Adds entry to cache.
 
222
     *
 
223
     * @method _defAddFn
 
224
     * @param e {Event.Facade} Event Facade with the following properties:
 
225
     * <dl>
 
226
     * <dt>entry (Object)</dt> <dd>The cached entry.</dd>
 
227
     * </dl>
 
228
     * @protected
 
229
     */
 
230
    _defAddFn: function(e) {
 
231
        var entry = e.entry,
 
232
            request = entry.request,
 
233
            cached = entry.cached,
 
234
            expires = entry.expires;
 
235
 
 
236
        // Convert Dates to msecs on the way into localStorage
 
237
        entry.cached = cached.getTime();
 
238
        entry.expires = expires ? expires.getTime() : expires;
 
239
 
 
240
        try {
 
241
            localStorage.setItem(this.get("sandbox")+JSON.stringify({"request":request}), JSON.stringify(entry));
 
242
            Y.log("Cached offline entry: " + Y.dump(entry), "info", "cache");
 
243
        }
 
244
        catch(error) {
 
245
            this.fire("error", {error:error});
 
246
            Y.log("Could not cache offline entry: " + Y.dump(entry) +
 
247
            " due to error: " + Y.dump(error), "warn", "cache");
 
248
        }
 
249
    },
 
250
 
 
251
    /**
 
252
     * Flushes cache.
 
253
     *
 
254
     * @method _defFlushFn
 
255
     * @param e {Event.Facade} Event Facade object.
 
256
     * @protected
 
257
     */
 
258
    _defFlushFn: function(e) {
 
259
        var key,
 
260
            i=localStorage.length-1;
 
261
        for(; i>-1; --i) {
 
262
            // Match sandbox id
 
263
            key = localStorage.key(i);
 
264
            if(key.indexOf(this.get("sandbox")) === 0) {
 
265
                localStorage.removeItem(key);
 
266
            }
 
267
        }
 
268
    },
 
269
 
 
270
    /////////////////////////////////////////////////////////////////////////////
 
271
    //
 
272
    // CacheOffline public methods
 
273
    //
 
274
    /////////////////////////////////////////////////////////////////////////////
 
275
    /**
 
276
     * Adds a new entry to the cache of the format
 
277
     * {request:request, response:response, cached:cached, expires: expires}.
 
278
     *
 
279
     * @method add
 
280
     * @param request {Object} Request value must be a String or JSON.
 
281
     * @param response {Object} Response value must be a String or JSON.
 
282
     */
 
283
 
 
284
    /**
 
285
     * Retrieves cached object for given request, if available.
 
286
     * Returns null if there is no cache match.
 
287
     *
 
288
     * @method retrieve
 
289
     * @param request {Object} Request object.
 
290
     * @return {Object} Cached object with the properties request, response,
 
291
     * and expires, or null.
 
292
     */
 
293
    retrieve: function(request) {
 
294
        this.fire("request", {request: request});
 
295
 
 
296
        var entry, expires, sandboxedrequest;
 
297
 
 
298
        try {
 
299
            sandboxedrequest = this.get("sandbox")+JSON.stringify({"request":request});
 
300
            try {
 
301
                entry = JSON.parse(localStorage.getItem(sandboxedrequest));
 
302
            }
 
303
            catch(e) {
 
304
            }
 
305
        }
 
306
        catch(e2) {
 
307
        }
 
308
 
 
309
        if(entry) {
 
310
            // Convert msecs to Dates on the way out of localStorage
 
311
            entry.cached = new Date(entry.cached);
 
312
            expires = entry.expires;
 
313
            expires = !expires ? null : new Date(expires);
 
314
            entry.expires = expires;
 
315
 
 
316
            if(this._isMatch(request, entry)) {
 
317
                this.fire("retrieve", {entry: entry});
 
318
                Y.log("Retrieved offlinecached response: " + Y.dump(entry) +
 
319
                        " for request: " + Y.dump(request), "info", "cache");
 
320
                return entry;
 
321
            }
 
322
        }
 
323
        return null;
 
324
    }
 
325
} :
 
326
/////////////////////////////////////////////////////////////////////////////
 
327
//
 
328
// Offline is not supported
 
329
//
 
330
/////////////////////////////////////////////////////////////////////////////
 
331
{
 
332
    /**
 
333
     * Always return null.
 
334
     *
 
335
     * @method _setMax
 
336
     * @protected
 
337
     */
 
338
    _setMax: function(value) {
 
339
        return null;
 
340
    }
 
341
});
 
342
 
 
343
 
 
344
Y.CacheOffline = CacheOffline;
 
345
 
 
346
 
 
347
}, '3.5.1' ,{requires:['cache-base', 'json']});