-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtools.tabs.history.js
124 lines (93 loc) · 2.79 KB
/
tools.tabs.history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* jQuery TOOLS plugin :: tabs.history 1.0.2
*
* Copyright (c) 2009 Tero Piirainen
* http://flowplayer.org/tools/tabs.html#history
*
* Dual licensed under MIT and GPL 2+ licenses
* http://www.opensource.org/licenses
*
* Launch : September 2009
* Date: ${date}
* Revision: ${revision}
*/
(function($) {
var t = $.tools.tabs;
t.plugins = t.plugins || {};
t.plugins.history = {
version: '1.0.2',
conf: {
api: false
}
};
var hash, iframe;
function setIframe(h) {
if (h) {
var doc = iframe.contentWindow.document;
doc.open().close();
doc.location.hash = h;
}
}
// jQuery plugin implementation
$.fn.onHash = function(fn) {
var el = this;
// IE
if ($.browser.msie && $.browser.version < '8') {
// create iframe that is constantly checked for hash changes
if (!iframe) {
iframe = $("<iframe/>").attr("src", "javascript:false;").hide().get(0);
$("body").append(iframe);
setInterval(function() {
var idoc = iframe.contentWindow.document,
h = idoc.location.hash;
if (hash !== h) {
$.event.trigger("hash", h);
hash = h;
}
}, 100);
setIframe(location.hash || '#');
}
// when link is clicked the iframe hash updated
el.bind("click.hash", function(e) {
setIframe($(this).attr("href"));
});
// other browsers scans for location.hash changes directly withou iframe hack
} else {
setInterval(function() {
var h = location.hash;
var els = el.filter("[href$=" + h + "]");
if (!els.length) {
h = h.replace("#", "");
els = el.filter("[href$=" + h + "]");
}
if (els.length && h !== hash) {
hash = h;
$.event.trigger("hash", h);
}
}, 100);
}
// bind a history listener
$(window).bind("hash", fn);
// return jQuery
return this;
};
$.fn.history = function(conf) {
var globals = $.extend({}, t.plugins.history.conf), ret;
conf = $.extend(globals, conf);
this.each(function() {
var api = $(this).tabs(),
tabs = api.getTabs();
if (api) { ret = api; }
// enable history support
tabs.onHash(function(evt, hash) {
if (!hash || hash == '#') { hash = api.getConf().initialIndex; }
api.click(hash);
});
// tab clicks perform their original action
tabs.click(function(e) {
location.hash = $(this).attr("href").replace("#", "");
});
});
return conf.api ? ret : this;
};
})(jQuery);