source: extensions/stripped-galleria/galleria/plugins/history/galleria.history.js @ 12975

Last change on this file since 12975 was 12975, checked in by Zaphod, 12 years ago

version 1.2.0

File size: 3.2 KB
Line 
1/**
2 * @preserve Galleria History Plugin 2011-08-01
3 * http://galleria.aino.se
4 *
5 * Copyright 2011, Aino
6 * Licensed under the MIT license.
7 *
8 */
9
10/*global jQuery, Galleria, window */
11
12Galleria.requires(1.25, 'The History Plugin requires Galleria version 1.2.5 or later.');
13
14(function( $, window ) {
15
16    Galleria.History = (function() {
17
18        var onloads = [],
19
20            init = false,
21
22            loc = window.location,
23
24            doc = window.document,
25
26            ie = Galleria.IE,
27
28            support = 'onhashchange' in window && ( doc.mode === undefined || doc.mode > 7 ),
29
30            iframe,
31
32            get = function( winloc ) {
33                if( iframe && !support && Galleria.IE ) {
34                    winloc = winloc || iframe.location;
35                }  else {
36                    winloc = loc;
37                }
38                return parseInt( winloc.hash.substr(2), 10 );
39            },
40
41            saved = get( loc ),
42
43            callbacks = [],
44
45            onchange = function() {
46                $.each( callbacks, function( i, fn ) {
47                    fn.call( window, get() );
48                });
49            },
50
51            ready = function() {
52                $.each( onloads, function(i, fn) {
53                    fn();
54                });
55
56                init = true;
57            },
58
59            setHash = function( val ) {
60                return '/' + val;
61            };
62
63        // always remove support if IE < 8
64        if ( support && ie < 8 ) {
65            support = false;
66        }
67
68        if ( !support ) {
69
70            $(function() {
71
72                var interval = window.setInterval(function() {
73
74                    var hash = get();
75
76                    if ( !isNaN( hash ) && hash != saved ) {
77                        saved = hash;
78                        loc.hash = setHash( hash );
79                        onchange();
80                    }
81
82                }, 50);
83
84                if ( ie ) {
85
86                    $('<iframe tabindex="-1" title="empty">').hide().attr( 'src', 'about:blank' ).one('load', function() {
87
88                        iframe = this.contentWindow;
89
90                        ready();
91
92                    }).insertAfter(doc.body);
93
94                } else {
95                    ready();
96                }
97            });
98        } else {
99            ready();
100        }
101
102        return {
103
104            change: function( fn ) {
105
106                callbacks.push( fn );
107
108                if( support ) {
109                    window.onhashchange = onchange;
110                }
111            },
112
113            set: function( val ) {
114
115                if ( isNaN( val ) ) {
116                    return;
117                }
118
119                if ( !support && ie ) {
120
121                    this.ready(function() {
122
123                        var idoc = iframe.document;
124                        idoc.open();
125                        idoc.close();
126
127                        iframe.location.hash = setHash( val );
128
129                    });
130                }
131
132                loc.hash = setHash( val );
133            },
134
135            ready: function(fn) {
136                if (!init) {
137                    onloads.push(fn);
138                } else {
139                    fn();
140                }
141            }
142        };
143    }());
144
145}( jQuery, this ));
146
Note: See TracBrowser for help on using the repository browser.