~ahasenack/lazr-js/lazr-js-11.03-packaging

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/history/history-hash.js

  • Committer: Sidnei da Silva
  • Date: 2010-09-18 14:54:13 UTC
  • mfrom: (166.11.12 toolchain)
  • Revision ID: sidnei.da.silva@canonical.com-20100918145413-8scojue3rodcm0f4
- Merge from lazr-js trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.com/yui/license.html
 
5
version: 3.2.0
 
6
build: 2676
 
7
*/
 
8
YUI.add('history-hash', function(Y) {
 
9
 
 
10
/**
 
11
 * Provides browser history management backed by
 
12
 * <code>window.location.hash</code>, as well as convenience methods for working
 
13
 * with the location hash and a synthetic <code>hashchange</code> event that
 
14
 * normalizes differences across browsers.
 
15
 *
 
16
 * @module history
 
17
 * @submodule history-hash
 
18
 * @since 3.2.0
 
19
 * @class HistoryHash
 
20
 * @extends HistoryBase
 
21
 * @constructor
 
22
 * @param {Object} config (optional) Configuration object. See the HistoryBase
 
23
 *   documentation for details.
 
24
 */
 
25
 
 
26
var HistoryBase = Y.HistoryBase,
 
27
    Lang        = Y.Lang,
 
28
    YArray      = Y.Array,
 
29
    GlobalEnv   = YUI.namespace('Env.HistoryHash'),
 
30
 
 
31
    SRC_HASH    = 'hash',
 
32
 
 
33
    hashNotifiers,
 
34
    oldHash,
 
35
    oldUrl,
 
36
    win             = Y.config.win,
 
37
    location        = win.location,
 
38
    useHistoryHTML5 = Y.config.useHistoryHTML5;
 
39
 
 
40
function HistoryHash() {
 
41
    HistoryHash.superclass.constructor.apply(this, arguments);
 
42
}
 
43
 
 
44
Y.extend(HistoryHash, HistoryBase, {
 
45
    // -- Initialization -------------------------------------------------------
 
46
    _init: function (config) {
 
47
        var bookmarkedState = HistoryHash.parseHash();
 
48
 
 
49
        // If an initialState was provided, merge the bookmarked state into it
 
50
        // (the bookmarked state wins).
 
51
        config = config || {};
 
52
 
 
53
        this._initialState = config.initialState ?
 
54
                Y.merge(config.initialState, bookmarkedState) : bookmarkedState;
 
55
 
 
56
        // Subscribe to the synthetic hashchange event (defined below) to handle
 
57
        // changes.
 
58
        Y.after('hashchange', Y.bind(this._afterHashChange, this), win);
 
59
 
 
60
        HistoryHash.superclass._init.apply(this, arguments);
 
61
    },
 
62
 
 
63
    // -- Protected Methods ----------------------------------------------------
 
64
    _storeState: function (src, newState) {
 
65
        var newHash = HistoryHash.createHash(newState);
 
66
 
 
67
        HistoryHash.superclass._storeState.apply(this, arguments);
 
68
 
 
69
        // Update the location hash with the changes, but only if the new hash
 
70
        // actually differs from the current hash (this avoids creating multiple
 
71
        // history entries for a single state).
 
72
        if (HistoryHash.getHash() !== newHash) {
 
73
            HistoryHash[src === HistoryBase.SRC_REPLACE ? 'replaceHash' : 'setHash'](newHash);
 
74
        }
 
75
    },
 
76
 
 
77
    // -- Protected Event Handlers ---------------------------------------------
 
78
 
 
79
    /**
 
80
     * Handler for hashchange events.
 
81
     *
 
82
     * @method _afterHashChange
 
83
     * @param {Event} e
 
84
     * @protected
 
85
     */
 
86
    _afterHashChange: function (e) {
 
87
        this._resolveChanges(SRC_HASH, HistoryHash.parseHash(e.newHash), {});
 
88
    }
 
89
}, {
 
90
    // -- Public Static Properties ---------------------------------------------
 
91
    NAME: 'historyHash',
 
92
 
 
93
    /**
 
94
     * Constant used to identify state changes originating from
 
95
     * <code>hashchange</code> events.
 
96
     *
 
97
     * @property SRC_HASH
 
98
     * @type String
 
99
     * @static
 
100
     * @final
 
101
     */
 
102
    SRC_HASH: SRC_HASH,
 
103
 
 
104
    /**
 
105
     * <p>
 
106
     * Prefix to prepend when setting the hash fragment. For example, if the
 
107
     * prefix is <code>!</code> and the hash fragment is set to
 
108
     * <code>#foo=bar&baz=quux</code>, the final hash fragment in the URL will
 
109
     * become <code>#!foo=bar&baz=quux</code>. This can be used to help make an
 
110
     * Ajax application crawlable in accordance with Google's guidelines at
 
111
     * <a href="http://code.google.com/web/ajaxcrawling/">http://code.google.com/web/ajaxcrawling/</a>.
 
112
     * </p>
 
113
     *
 
114
     * <p>
 
115
     * Note that this prefix applies to all HistoryHash instances. It's not
 
116
     * possible for individual instances to use their own prefixes since they
 
117
     * all operate on the same URL.
 
118
     * </p>
 
119
     *
 
120
     * @property hashPrefix
 
121
     * @type String
 
122
     * @default ''
 
123
     * @static
 
124
     */
 
125
    hashPrefix: '',
 
126
 
 
127
    // -- Protected Static Properties ------------------------------------------
 
128
 
 
129
    /**
 
130
     * Regular expression used to parse location hash/query strings.
 
131
     *
 
132
     * @property _REGEX_HASH
 
133
     * @type RegExp
 
134
     * @protected
 
135
     * @static
 
136
     * @final
 
137
     */
 
138
    _REGEX_HASH: /([^\?#&]+)=([^&]+)/g,
 
139
 
 
140
    // -- Public Static Methods ------------------------------------------------
 
141
 
 
142
    /**
 
143
     * Creates a location hash string from the specified object of key/value
 
144
     * pairs.
 
145
     *
 
146
     * @method createHash
 
147
     * @param {Object} params object of key/value parameter pairs
 
148
     * @return {String} location hash string
 
149
     * @static
 
150
     */
 
151
    createHash: function (params) {
 
152
        var encode = HistoryHash.encode,
 
153
            hash   = [];
 
154
 
 
155
        Y.Object.each(params, function (value, key) {
 
156
            if (Lang.isValue(value)) {
 
157
                hash.push(encode(key) + '=' + encode(value));
 
158
            }
 
159
        });
 
160
 
 
161
        return hash.join('&');
 
162
    },
 
163
 
 
164
    /**
 
165
     * Wrapper around <code>decodeURIComponent()</code> that also converts +
 
166
     * chars into spaces.
 
167
     *
 
168
     * @method decode
 
169
     * @param {String} string string to decode
 
170
     * @return {String} decoded string
 
171
     * @static
 
172
     */
 
173
    decode: function (string) {
 
174
        return decodeURIComponent(string.replace(/\+/g, ' '));
 
175
    },
 
176
 
 
177
    /**
 
178
     * Wrapper around <code>encodeURIComponent()</code> that converts spaces to
 
179
     * + chars.
 
180
     *
 
181
     * @method encode
 
182
     * @param {String} string string to encode
 
183
     * @return {String} encoded string
 
184
     * @static
 
185
     */
 
186
    encode: function (string) {
 
187
        return encodeURIComponent(string).replace(/%20/g, '+');
 
188
    },
 
189
 
 
190
    /**
 
191
     * Gets the raw (not decoded) current location hash, minus the preceding '#'
 
192
     * character and the hashPrefix (if one is set).
 
193
     *
 
194
     * @method getHash
 
195
     * @return {String} current location hash
 
196
     * @static
 
197
     */
 
198
    getHash: (Y.UA.gecko ? function () {
 
199
        // Gecko's window.location.hash returns a decoded string and we want all
 
200
        // encoding untouched, so we need to get the hash value from
 
201
        // window.location.href instead. We have to use UA sniffing rather than
 
202
        // feature detection, since the only way to detect this would be to
 
203
        // actually change the hash.
 
204
        var matches = /#(.*)$/.exec(location.href),
 
205
            hash    = matches && matches[1] || '',
 
206
            prefix  = HistoryHash.hashPrefix;
 
207
 
 
208
        return prefix && hash.indexOf(prefix) === 0 ?
 
209
                    hash.replace(prefix, '') : hash;
 
210
    } : function () {
 
211
        var hash   = location.hash.substr(1),
 
212
            prefix = HistoryHash.hashPrefix;
 
213
 
 
214
        // Slight code duplication here, but execution speed is of the essence
 
215
        // since getHash() is called every 50ms to poll for changes in browsers
 
216
        // that don't support native onhashchange. An additional function call
 
217
        // would add unnecessary overhead.
 
218
        return prefix && hash.indexOf(prefix) === 0 ?
 
219
                    hash.replace(prefix, '') : hash;
 
220
    }),
 
221
 
 
222
    /**
 
223
     * Gets the current bookmarkable URL.
 
224
     *
 
225
     * @method getUrl
 
226
     * @return {String} current bookmarkable URL
 
227
     * @static
 
228
     */
 
229
    getUrl: function () {
 
230
        return location.href;
 
231
    },
 
232
 
 
233
    /**
 
234
     * Parses a location hash string into an object of key/value parameter
 
235
     * pairs. If <i>hash</i> is not specified, the current location hash will
 
236
     * be used.
 
237
     *
 
238
     * @method parseHash
 
239
     * @param {String} hash (optional) location hash string
 
240
     * @return {Object} object of parsed key/value parameter pairs
 
241
     * @static
 
242
     */
 
243
    parseHash: function (hash) {
 
244
        var decode = HistoryHash.decode,
 
245
            i,
 
246
            len,
 
247
            matches,
 
248
            param,
 
249
            params = {},
 
250
            prefix = HistoryHash.hashPrefix,
 
251
            prefixIndex;
 
252
 
 
253
        hash = Lang.isValue(hash) ? hash : HistoryHash.getHash();
 
254
 
 
255
        if (prefix) {
 
256
            prefixIndex = hash.indexOf(prefix);
 
257
 
 
258
            if (prefixIndex === 0 || (prefixIndex === 1 && hash.charAt(0) === '#')) {
 
259
                hash = hash.replace(prefix, '');
 
260
            }
 
261
        }
 
262
 
 
263
        matches = hash.match(HistoryHash._REGEX_HASH) || [];
 
264
 
 
265
        for (i = 0, len = matches.length; i < len; ++i) {
 
266
            param = matches[i].split('=');
 
267
            params[decode(param[0])] = decode(param[1]);
 
268
        }
 
269
 
 
270
        return params;
 
271
    },
 
272
 
 
273
    /**
 
274
     * Replaces the browser's current location hash with the specified hash
 
275
     * and removes all forward navigation states, without creating a new browser
 
276
     * history entry. Automatically prepends the <code>hashPrefix</code> if one
 
277
     * is set.
 
278
     *
 
279
     * @method replaceHash
 
280
     * @param {String} hash new location hash
 
281
     * @static
 
282
     */
 
283
    replaceHash: function (hash) {
 
284
        if (hash.charAt(0) === '#') {
 
285
            hash = hash.substr(1);
 
286
        }
 
287
 
 
288
        location.replace('#' + (HistoryHash.hashPrefix || '') + hash);
 
289
    },
 
290
 
 
291
    /**
 
292
     * Sets the browser's location hash to the specified string. Automatically
 
293
     * prepends the <code>hashPrefix</code> if one is set.
 
294
     *
 
295
     * @method setHash
 
296
     * @param {String} hash new location hash
 
297
     * @static
 
298
     */
 
299
    setHash: function (hash) {
 
300
        if (hash.charAt(0) === '#') {
 
301
            hash = hash.substr(1);
 
302
        }
 
303
 
 
304
        location.hash = (HistoryHash.hashPrefix || '') + hash;
 
305
    }
 
306
});
 
307
 
 
308
// -- Synthetic hashchange Event -----------------------------------------------
 
309
 
 
310
// TODO: YUIDoc currently doesn't provide a good way to document synthetic DOM
 
311
// events. For now, we're just documenting the hashchange event on the YUI
 
312
// object, which is about the best we can do until enhancements are made to
 
313
// YUIDoc.
 
314
 
 
315
/**
 
316
 * <p>
 
317
 * Synthetic <code>window.onhashchange</code> event that normalizes differences
 
318
 * across browsers and provides support for browsers that don't natively support
 
319
 * <code>onhashchange</code>.
 
320
 * </p>
 
321
 *
 
322
 * <p>
 
323
 * This event is provided by the <code>history-hash</code> module.
 
324
 * </p>
 
325
 *
 
326
 * <p>
 
327
 * <strong>Usage example:</strong>
 
328
 * </p>
 
329
 *
 
330
 * <code><pre>
 
331
 * YUI().use('history-hash', function (Y) {
 
332
 * &nbsp;&nbsp;Y.on('hashchange', function (e) {
 
333
 * &nbsp;&nbsp;&nbsp;&nbsp;// Handle hashchange events on the current window.
 
334
 * &nbsp;&nbsp;}, Y.config.win);
 
335
 * });
 
336
 * </pre></code>
 
337
 *
 
338
 * @event hashchange
 
339
 * @param {EventFacade} e Event facade with the following additional
 
340
 *   properties:
 
341
 *
 
342
 * <dl>
 
343
 *   <dt>oldHash</dt>
 
344
 *   <dd>
 
345
 *     Previous hash fragment value before the change.
 
346
 *   </dd>
 
347
 *
 
348
 *   <dt>oldUrl</dt>
 
349
 *   <dd>
 
350
 *     Previous URL (including the hash fragment) before the change.
 
351
 *   </dd>
 
352
 *
 
353
 *   <dt>newHash</dt>
 
354
 *   <dd>
 
355
 *     New hash fragment value after the change.
 
356
 *   </dd>
 
357
 *
 
358
 *   <dt>newUrl</dt>
 
359
 *   <dd>
 
360
 *     New URL (including the hash fragment) after the change.
 
361
 *   </dd>
 
362
 * </dl>
 
363
 * @for YUI
 
364
 * @since 3.2.0
 
365
 */
 
366
 
 
367
hashNotifiers = GlobalEnv._notifiers;
 
368
 
 
369
if (!hashNotifiers) {
 
370
    hashNotifiers = GlobalEnv._notifiers = [];
 
371
}
 
372
 
 
373
Y.Event.define('hashchange', {
 
374
    on: function (node, subscriber, notifier) {
 
375
        // Ignore this subscription if the node is anything other than the
 
376
        // window or document body, since those are the only elements that
 
377
        // should support the hashchange event. Note that the body could also be
 
378
        // a frameset, but that's okay since framesets support hashchange too.
 
379
        if (node.compareTo(win) || node.compareTo(Y.config.doc.body)) {
 
380
            hashNotifiers.push(notifier);
 
381
        }
 
382
    },
 
383
 
 
384
    detach: function (node, subscriber, notifier) {
 
385
        var index = YArray.indexOf(hashNotifiers, notifier);
 
386
 
 
387
        if (index !== -1) {
 
388
            hashNotifiers.splice(index, 1);
 
389
        }
 
390
    }
 
391
});
 
392
 
 
393
oldHash = HistoryHash.getHash();
 
394
oldUrl  = HistoryHash.getUrl();
 
395
 
 
396
if (HistoryBase.nativeHashChange) {
 
397
    // Wrap the browser's native hashchange event.
 
398
    Y.Event.attach('hashchange', function (e) {
 
399
        var newHash = HistoryHash.getHash(),
 
400
            newUrl  = HistoryHash.getUrl();
 
401
 
 
402
        // Iterate over a copy of the hashNotifiers array since a subscriber
 
403
        // could detach during iteration and cause the array to be re-indexed.
 
404
        YArray.each(hashNotifiers.concat(), function (notifier) {
 
405
            notifier.fire({
 
406
                _event : e,
 
407
                oldHash: oldHash,
 
408
                oldUrl : oldUrl,
 
409
                newHash: newHash,
 
410
                newUrl : newUrl
 
411
            });
 
412
        });
 
413
 
 
414
        oldHash = newHash;
 
415
        oldUrl  = newUrl;
 
416
    }, win);
 
417
} else {
 
418
    // Begin polling for location hash changes if there's not already a global
 
419
    // poll running.
 
420
    if (!GlobalEnv._hashPoll) {
 
421
        if (Y.UA.webkit && !Y.UA.chrome &&
 
422
                navigator.vendor.indexOf('Apple') !== -1) {
 
423
            // Attach a noop unload handler to disable Safari's back/forward
 
424
            // cache. This works around a nasty Safari bug when the back button
 
425
            // is used to return from a page on another domain, but results in
 
426
            // slightly worse performance. This bug is not present in Chrome.
 
427
            //
 
428
            // Unfortunately a UA sniff is unavoidable here, but the
 
429
            // consequences of a false positive are minor.
 
430
            //
 
431
            // Current as of Safari 5.0 (6533.16).
 
432
            // See: https://bugs.webkit.org/show_bug.cgi?id=34679
 
433
            Y.on('unload', function () {}, win);
 
434
        }
 
435
 
 
436
        GlobalEnv._hashPoll = Y.later(50, null, function () {
 
437
            var newHash = HistoryHash.getHash(),
 
438
                newUrl;
 
439
 
 
440
            if (oldHash !== newHash) {
 
441
                newUrl = HistoryHash.getUrl();
 
442
 
 
443
                YArray.each(hashNotifiers, function (notifier) {
 
444
                    notifier.fire({
 
445
                        oldHash: oldHash,
 
446
                        oldUrl : oldUrl,
 
447
                        newHash: newHash,
 
448
                        newUrl : newUrl
 
449
                    });
 
450
                });
 
451
 
 
452
                oldHash = newHash;
 
453
                oldUrl  = newUrl;
 
454
            }
 
455
        }, null, true);
 
456
    }
 
457
}
 
458
 
 
459
Y.HistoryHash = HistoryHash;
 
460
 
 
461
// HistoryHash will never win over HistoryHTML5 unless useHistoryHTML5 is false.
 
462
if (useHistoryHTML5 === false || (!Y.History && useHistoryHTML5 !== true &&
 
463
        (!HistoryBase.html5 || !Y.HistoryHTML5))) {
 
464
    Y.History = HistoryHash;
 
465
}
 
466
 
 
467
 
 
468
}, '3.2.0' ,{requires:['event-synthetic', 'history-base', 'yui-later']});