1 | /** |
---|
2 | * Galleria History Plugin 2012-04-04 |
---|
3 | * http://galleria.io |
---|
4 | * |
---|
5 | * Licensed under the MIT license |
---|
6 | * https://raw.github.com/aino/galleria/master/LICENSE |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | (function( $, window ) { |
---|
11 | |
---|
12 | /*global jQuery, Galleria, window */ |
---|
13 | |
---|
14 | Galleria.requires(1.25, 'The History Plugin requires Galleria version 1.2.5 or later.'); |
---|
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 | |
---|