~ubuntu-branches/ubuntu/precise/whoopsie-daisy/precise-proposed

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/3.4.1/build/cache-offline/cache-offline.js

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-04-18 13:04:36 UTC
  • Revision ID: package-import@ubuntu.com-20120418130436-vmt93p8fds516lws
Tags: 0.1.32
Fix failing tests on powerpc and ARM.

Show diffs side-by-side

added added

removed removed

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