Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('history-hash-ie', function (Y, NAME) {
2
 
3
/**
4
 * Improves IE6/7 support in history-hash by using a hidden iframe to create
5
 * entries in IE's browser history. This module is only needed if IE6/7 support
6
 * is necessary; it's not needed for any other browser.
7
 *
8
 * @module history
9
 * @submodule history-hash-ie
10
 * @since 3.2.0
11
 */
12
 
13
// Combination of a UA sniff to ensure this is IE (or a browser that wants us to
14
// treat it like IE) and feature detection for native hashchange support (false
15
// for IE < 8 or IE8/9 in IE7 mode).
16
if (Y.UA.ie && !Y.HistoryBase.nativeHashChange) {
17
    var Do          = Y.Do,
18
        GlobalEnv   = YUI.namespace('Env.HistoryHash'),
19
        HistoryHash = Y.HistoryHash,
20
 
21
        iframe = GlobalEnv._iframe,
22
        win    = Y.config.win;
23
 
24
    /**
25
     * Gets the raw (not decoded) current location hash from the IE iframe,
26
     * minus the preceding '#' character and the hashPrefix (if one is set).
27
     *
28
     * @method getIframeHash
29
     * @return {String} current iframe hash
30
     * @static
31
     */
32
    HistoryHash.getIframeHash = function () {
33
        if (!iframe || !iframe.contentWindow) {
34
            return '';
35
        }
36
 
37
        var prefix = HistoryHash.hashPrefix,
38
            hash   = iframe.contentWindow.location.hash.substr(1);
39
 
40
        return prefix && hash.indexOf(prefix) === 0 ?
41
                    hash.replace(prefix, '') : hash;
42
    };
43
 
44
    /**
45
     * Updates the history iframe with the specified hash.
46
     *
47
     * @method _updateIframe
48
     * @param {String} hash location hash
49
     * @param {Boolean} replace (optional) if <code>true</code>, the current
50
     *   history state will be replaced without adding a new history entry
51
     * @protected
52
     * @static
53
     * @for HistoryHash
54
     */
55
    HistoryHash._updateIframe = function (hash, replace) {
56
        var iframeDoc      = iframe && iframe.contentWindow && iframe.contentWindow.document,
57
            iframeLocation = iframeDoc && iframeDoc.location;
58
 
59
        if (!iframeDoc || !iframeLocation) {
60
            return;
61
        }
62
 
63
 
64
        if (replace) {
65
            iframeLocation.replace(hash.charAt(0) === '#' ? hash : '#' + hash);
66
        } else {
67
            iframeDoc.open().close();
68
            iframeLocation.hash = hash;
69
        }
70
    };
71
 
72
    Do.before(HistoryHash._updateIframe, HistoryHash, 'replaceHash', HistoryHash, true);
73
 
74
    if (!iframe) {
75
        Y.on('domready', function () {
76
            var lastUrlHash = HistoryHash.getHash();
77
 
78
            // Create a hidden iframe to store history state, following the
79
            // iframe-hiding recommendations from
80
            // http://www.paciellogroup.com/blog/?p=604.
81
            //
82
            // This iframe will allow history navigation within the current page
83
            // context. After navigating to another page, all but the most
84
            // recent history state will be lost.
85
            //
86
            // Earlier versions of the YUI History Utility attempted to work
87
            // around this limitation by having the iframe load a static
88
            // resource. This workaround was extremely fragile and tended to
89
            // break frequently (and silently) since it was entirely dependent
90
            // on IE's inconsistent handling of iframe history.
91
            //
92
            // Since this workaround didn't work much of the time anyway and
93
            // added significant complexity, it has been removed, and IE6 and 7
94
            // now get slightly degraded history support.
95
 
96
            iframe = GlobalEnv._iframe = Y.Node.getDOMNode(Y.Node.create(
97
                '<iframe src="javascript:0" style="display:none" height="0" width="0" tabindex="-1" title="empty"/>'
98
            ));
99
 
100
            // Append the iframe to the documentElement rather than the body.
101
            // Keeping it outside the body prevents scrolling on the initial
102
            // page load (hat tip to Ben Alman and jQuery BBQ for this
103
            // technique).
104
            Y.config.doc.documentElement.appendChild(iframe);
105
 
106
            // Update the iframe with the initial location hash, if any. This
107
            // will create an initial history entry that the user can return to
108
            // after the state has changed.
109
            HistoryHash._updateIframe(lastUrlHash || '#');
110
 
111
            // Listen for hashchange events and keep the iframe's hash in sync
112
            // with the parent frame's hash.
113
            Y.on('hashchange', function (e) {
114
                lastUrlHash = e.newHash;
115
 
116
                if (HistoryHash.getIframeHash() !== lastUrlHash) {
117
                    HistoryHash._updateIframe(lastUrlHash);
118
                }
119
            }, win);
120
 
121
            // Watch the iframe hash in order to detect back/forward navigation.
122
            Y.later(50, null, function () {
123
                var iframeHash = HistoryHash.getIframeHash();
124
 
125
                if (iframeHash !== lastUrlHash) {
126
                    HistoryHash.setHash(iframeHash);
127
                }
128
            }, null, true);
129
        });
130
    }
131
}
132
 
133
 
134
}, '3.18.1', {"requires": ["history-hash", "node-base"]});