This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnavi.js
251 lines (244 loc) · 15 KB
/
navi.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* jQuery: Navi.js Content Switcher - v1.9.1 - 7/15/2013
* http://navi.grantcr.com
* https://github.com/tgrant54/Navi.js
* Copyright (c) 2013 Tyler Grant
* Dual licensed under the MIT & Creative Commons Attribution-ShareAlike 3.0 Unported License.
*/
;(function($) {
$.fn.navi = function(o) {
this.each(function() {
var t = $(this),
d = t.data("navi"),
_ = {
hash: "#!/",
content: $("#content"),
contentSubSelector: ">ul>li",
activeClass: "active",
defaultPage: true,
useAnimation: true,
useBreadCrumbs: true,
animationSpeed: 100,
animationType: "slideUp",
usePageTitle: true,
defaultPageTitle: "Navi.js",
useAjax: true,
ajaxExtension: ".html",
ajaxFolder: "./",
init: function() {
_.contentSubSelector ? true : _.contentSubSelector = ">ul>li";
_.menu = $(">ul>li", _.me) // menu is each my > ul > li
_.cont = $(_.contentSubSelector, _.content) // cont
_.hashLen = _.hash.length // hash length
_.defaultPage ? location.hash === "" ? _.loadDefault() : false : false // load a default page?
_.setActive() // go ahead and set active
},
loadDefault: function() {
_.menu.each(function(i, e) {
if ($(e).hasClass(_.activeClass)) {
location.hash = $(">a", e).attr("href");
}
})
},
setActive: function(speed) {
_.cont.each(function(i, e) { // since it was set loop through it
if ($(e).attr("id") == location.hash.slice(_.hashLen) && location.hash.slice(0, _.hashLen) == _.hash) { // check if the id == the spliced hash (#!/home => home) and a second check
var ajaxLoad; // load a var - string for what gets loaded
_.useAjax ? ajaxLoad = String(_.ajaxFolder + $(e).attr('id') + _.ajaxExtension) : false // use ajax ? Then set the ajaxLoad to string of the folder/id/ajaxExtension or false
ajaxLoad ? _.useAnimation ? $(e).fadeIn(speed).load(ajaxLoad, function() { //ajax load ? use animation, give a fade, and load ajaxLoad
$(e).addClass(_.activeClass).siblings().html("").css("display", "none").removeClass(_.activeClass) //set classes
if (typeof _gaq !== "undefined" && _gaq !== null) { // ga stuff
_gaq.push(['_trackPageview', location.hash])
}
}) : $(e).fadeIn(0).load(ajaxLoad, function() { // else if ajax is not set, do this
$(e).addClass(_.activeClass).siblings().html("").css("display", "none").removeClass(_.activeClass)
if (typeof _gaq !== "undefined" && _gaq !== null) {
_gaq.push(['_trackPageview', location.hash])
}
}) : _.useAnimation ? $(e).fadeIn(speed).addClass(_.activeClass).siblings().css("display", "none").removeClass(_.activeClass).removeAttr("class") : $(e).fadeIn(0).addClass(_.activeClass).siblings().css("display", "none").removeClass(_.activeClass)
}
})
_.useBreadCrumbs ? _.setBreadCrumbs() : _.menu.each(function(i2, e2) {
if ($(">a", e2).attr("href") == location.hash) {
$(e2).addClass(_.activeClass).siblings().removeClass(_.activeClass)
}
})
_.setPageTitle()
},
setPageTitle: function() {
if (_.usePageTitle) {
_.menu.each(function(i, e) {
if ($(e).hasClass(_.activeClass)) {
$("title").text(_.defaultPageTitle + $(e).attr("data-title"))
}
})
}
},
setBreadCrumbs: function() {
$("[href]").each(function(n, e2) {
if ($(e2).attr('href') == location.hash) {
$(e2).parent().addClass('active').siblings().removeClass('active').removeAttr("class")
}
})
},
animation: function(type, speed, lHash) {
typeof speed == "number" ? speed = speed : speed = Number(speed);
switch (type) {
case 'slideUp':
_.content.slideUp(speed, function() {
_.setActive()
_.content.slideDown(speed, function() {
_.content.css("height", "");
})
})
break;
case 'slideUpLeft':
_.content.stop().animate({
width: "0",
height: "0",
}, speed, function() {
_.setActive()
_.content.stop().animate({
width: "100%",
height: "100%"
}, speed)
})
break;
case 'slideLeft':
_.content.stop().animate({
width: "0"
}, speed, function() {
_.setActive(speed)
_.content.stop().animate({
width: "100%"
}, speed)
})
break;
case 'fade':
_.content.fadeOut(speed, _.setActive).fadeIn(speed)
break;
case 'rotateOutDownLeft':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated rotateOutDownLeft")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated rotateOutDownLeft").addClass("animated rotateInUpLeft")
})
break;
case 'bounceOutDown':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated bounceOutDown")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated bounceOutDown").addClass("animated bounceInUp")
})
break;
case 'bounceOut':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated bounceOut")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated bounceOut").addClass("animated bounceIn")
})
break;
case 'lightSpeed':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated lightSpeedOut")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated lightSpeedOut").addClass("animated lightSpeedIn")
})
break;
case 'roll':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated rollOut")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated rollOut").addClass("animated rollIn")
})
break;
case 'flipX':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated flipOutX").removeClass("flipInX")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated flipOutX").addClass("animated flipInX")
})
break;
case 'flipY':
_.content.css({
"-webkit-animation-duration": Number(speed) + "ms",
"-moz-animation-duration": Number(speed) + "ms",
"-o-animation-duration": Number(speed) + "ms",
"animation-duration": Number(speed) + "ms"
})
_.content.addClass("animated flipOutY").removeClass("flipInY")
_.content.one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
_.setActive()
_.content.removeClass("animated flipOutY").addClass("animated flipInY")
})
break;
}
}
}
d ? _ = d : t.data({
navi: _
})
if (typeof o == 'object') {
$.extend(_, o);
}
_.me || _.init(_.me = t)
if ("onhashchange" in window) {
window.onhashchange = function() {
var cHash = location.hash;
_.useAnimation ? _.animation(_.animationType, _.animationSpeed) : false
}
}
else {
/*
* jQuery hashchange event - v1.3 - 7/21/2010
* http://benalman.com/projects/jquery-hashchange-plugin/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
;(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
$(window).hashchange(function() {
location.hash.slice(0, _.hashLen) == _.hash ? _.useAnimation ? _.animation(_.animationType, _.animationSpeed, location.hash) : _.setActive() : false
});
}
})
return this
}
}(jQuery));