提交 | 用户 | 时间
|
58d006
|
1 |
/** |
A |
2 |
<b>Scrollbars for sidebar <i>(second style)</i></b>. This approach can be used on fixed or normal sidebar. |
|
3 |
It uses <u>"overflow:hidden"</u> so you can't use <u>.hover</u> submenus and it will be disabled when sidebar is minimized. |
|
4 |
It may also be slightly faster especially when resizing browser window. |
|
5 |
<i class="glyphicon glyphicon-flash"></i> <u class="text-primary">Native browser scrollbars</u> are used in touch devices. |
|
6 |
*/ |
|
7 |
|
|
8 |
(function($ , undefined) { |
|
9 |
//if( !$.fn.ace_scroll ) return; |
|
10 |
|
|
11 |
var hasTouch = ace.vars['touch']; |
|
12 |
var nativeScroll = /**ace.vars['old_ie'] ||*/ hasTouch; |
|
13 |
|
|
14 |
var old_safari = ace.vars['safari'] && navigator.userAgent.match(/version\/[1-5]/i) |
|
15 |
//NOTE |
|
16 |
//Safari on windows has not been updated for a long time. |
|
17 |
//And it has a problem when sidebar is fixed&scrollable and there is a CSS3 animation inside page content. |
|
18 |
//Very probably windows users of safari have migrated to another browser by now! |
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
var is_element_pos = |
|
23 |
'getComputedStyle' in window ? |
|
24 |
//el.offsetHeight is used to force redraw and recalculate 'el.style.position' esp. for webkit! |
|
25 |
function(el, pos) { el.offsetHeight; return window.getComputedStyle(el).position == pos } |
|
26 |
: |
|
27 |
function(el, pos) { el.offsetHeight; return $(el).css('position') == pos } |
|
28 |
|
|
29 |
|
|
30 |
function Sidebar_Scroll(sidebar , settings) { |
|
31 |
var self = this; |
|
32 |
|
|
33 |
var $window = $(window); |
|
34 |
|
|
35 |
var $sidebar = $(sidebar), |
|
36 |
$nav = $sidebar.find('.nav-list'), |
|
37 |
$toggle = $sidebar.find('.sidebar-toggle').eq(0), |
|
38 |
$shortcuts = $sidebar.find('.sidebar-shortcuts').eq(0), |
|
39 |
|
|
40 |
nav = $nav.get(0); |
|
41 |
|
|
42 |
if(!nav) return; |
|
43 |
|
|
44 |
|
|
45 |
var attrib_values = ace.helper.getAttrSettings(sidebar, $.fn.ace_sidebar_scroll.defaults); |
|
46 |
this.settings = $.extend({}, $.fn.ace_sidebar_scroll.defaults, settings, attrib_values); |
|
47 |
|
|
48 |
var scroll_to_active = self.settings.scroll_to_active; |
|
49 |
this.only_if_fixed = self.settings.only_if_fixed; |
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
var ace_sidebar = $sidebar.ace_sidebar('ref'); |
|
54 |
$sidebar.attr('data-sidebar-scroll', 'true'); |
|
55 |
|
|
56 |
var submenu_hover = function() { |
|
57 |
return $sidebar.first('li.hover > .submenu').css('position') == 'absolute' |
|
58 |
} |
|
59 |
|
|
60 |
|
|
61 |
var scroll_div = null, |
|
62 |
scroll_content = null, |
|
63 |
scroll_content_div = null, |
|
64 |
bar = null, |
|
65 |
ace_scroll = null; |
|
66 |
|
|
67 |
this.is_scrolling = false; |
|
68 |
var _initiated = false; |
|
69 |
this.sidebar_fixed = is_element_pos(sidebar, 'fixed'); |
|
70 |
|
|
71 |
var $avail_height, $content_height; |
|
72 |
|
|
73 |
var available_height = function() { |
|
74 |
//available window space |
|
75 |
var offset = $nav.parent().offset();//because `$nav.offset()` considers the "scrolled top" amount as well |
|
76 |
if(self.sidebar_fixed) offset.top -= ace.helper.scrollTop(); |
|
77 |
|
|
78 |
return $window.innerHeight() - offset.top - ( self.settings.include_toggle ? 0 : $toggle.outerHeight() ) + 1; |
|
79 |
} |
|
80 |
var content_height = function() { |
|
81 |
return nav.scrollHeight; |
|
82 |
} |
|
83 |
|
|
84 |
var initiate = function(on_page_load) { |
|
85 |
if( _initiated ) return; |
|
86 |
if( (self.only_if_fixed && !self.sidebar_fixed) || submenu_hover() ) return;//eligible?? |
|
87 |
//return if we want scrollbars only on "fixed" sidebar and sidebar is not "fixed" yet! |
|
88 |
|
|
89 |
//initiate once |
|
90 |
$nav.wrap('<div class="nav-wrap-up" />'); |
|
91 |
if(self.settings.include_shortcuts && $shortcuts.length != 0) $nav.parent().prepend($shortcuts); |
|
92 |
if(self.settings.include_toggle && $toggle.length != 0) $nav.parent().append($toggle); |
|
93 |
|
|
94 |
if(!nativeScroll) { |
|
95 |
scroll_div = $nav.parent() |
|
96 |
.ace_scroll({ |
|
97 |
size: available_height(), |
|
98 |
reset: true, |
|
99 |
mouseWheelLock: true, |
|
100 |
lockAnyway: self.settings.lock_anyway, |
|
101 |
styleClass: self.settings.scroll_style, |
|
102 |
hoverReset: false |
|
103 |
}) |
|
104 |
.closest('.ace-scroll').addClass('nav-scroll'); |
|
105 |
|
|
106 |
ace_scroll = scroll_div.data('ace_scroll'); |
|
107 |
|
|
108 |
scroll_content = scroll_div.find('.scroll-content').eq(0); |
|
109 |
|
|
110 |
if(old_safari && !self.settings.include_toggle) { |
|
111 |
var toggle = $toggle.get(0); |
|
112 |
if(toggle) scroll_content.on('scroll.safari', function() { |
|
113 |
ace.helper.redraw(toggle); |
|
114 |
}); |
|
115 |
} |
|
116 |
} |
|
117 |
else { |
|
118 |
$nav.parent().addClass('sidebar-scroll-native').css('max-height', available_height()); |
|
119 |
ace_scroll = true; |
|
120 |
scroll_div = scroll_content = $nav.parent(); |
|
121 |
} |
|
122 |
|
|
123 |
_initiated = true; |
|
124 |
|
|
125 |
//if the active item is not visible, scroll down so that it becomes visible |
|
126 |
//only the first time, on page load |
|
127 |
if(on_page_load == true) { |
|
128 |
self.reset();//try resetting at first |
|
129 |
|
|
130 |
if( scroll_to_active ) { |
|
131 |
self.scroll_to_active(); |
|
132 |
} |
|
133 |
scroll_to_active = false; |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
this.scroll_to_active = function() { |
|
139 |
if( !nativeScroll && (!ace_scroll || !ace_scroll.is_active()) ) return; |
|
140 |
try { |
|
141 |
//sometimes there's no active item or not 'offsetTop' property |
|
142 |
var $active; |
|
143 |
|
|
144 |
var vars = ace_sidebar['vars'](); |
|
145 |
|
|
146 |
var nav_list = $sidebar.find('.nav-list') |
|
147 |
if(vars['minimized'] && !vars['collapsible']) { |
|
148 |
$active = nav_list.find('> .active') |
|
149 |
} |
|
150 |
else { |
|
151 |
$active = $nav.find('> .active.hover') |
|
152 |
if($active.length == 0) $active = $nav.find('.active:not(.open)') |
|
153 |
} |
|
154 |
|
|
155 |
var top = $active.outerHeight(); |
|
156 |
|
|
157 |
nav_list = nav_list.get(0); |
|
158 |
var active = $active.get(0); |
|
159 |
while(active != nav_list) { |
|
160 |
top += active.offsetTop; |
|
161 |
active = active.parentNode; |
|
162 |
} |
|
163 |
|
|
164 |
var scroll_amount = top - scroll_div.height(); |
|
165 |
if(scroll_amount > 0) { |
|
166 |
scroll_content.scrollTop(scroll_amount); |
|
167 |
} |
|
168 |
}catch(e){} |
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
} |
|
173 |
|
|
174 |
|
|
175 |
this.reset = function(recalc) { |
|
176 |
if(recalc === true) { |
|
177 |
this.sidebar_fixed = is_element_pos(sidebar, 'fixed'); |
|
178 |
} |
|
179 |
|
|
180 |
if( (this.only_if_fixed && !this.sidebar_fixed) || submenu_hover() ) { |
|
181 |
this.disable(); |
|
182 |
return;//eligible?? |
|
183 |
} |
|
184 |
//return if we want scrollbars only on "fixed" sidebar and sidebar is not "fixed" yet! |
|
185 |
|
|
186 |
if( !_initiated ) initiate(); |
|
187 |
//initiate scrollbars if not yet |
|
188 |
|
|
189 |
$sidebar.addClass('sidebar-scroll'); |
|
190 |
|
|
191 |
var vars = ace_sidebar['vars'](); |
|
192 |
|
|
193 |
|
|
194 |
//enable if: |
|
195 |
//menu is not minimized |
|
196 |
//menu is not collapsible mode (responsive navbar-collapse mode which has default browser scroller) |
|
197 |
//menu is not horizontal or horizontal but mobile view (which is not navbar-collapse) |
|
198 |
//and available height is less than nav's height |
|
199 |
var enable_scroll = !vars['minimized'] && !vars['collapsible'] && !vars['horizontal'] |
|
200 |
&& ($avail_height = available_height()) < ($content_height = nav.parentNode.scrollHeight); |
|
201 |
|
|
202 |
this.is_scrolling = true; |
|
203 |
if( enable_scroll && ace_scroll ) { |
|
204 |
//scroll_content_div.css({height: $content_height, width: 8}); |
|
205 |
//scroll_div.prev().css({'max-height' : $avail_height}) |
|
206 |
if(!nativeScroll) { |
|
207 |
ace_scroll.update({size: $avail_height}); |
|
208 |
ace_scroll.enable(); |
|
209 |
ace_scroll.reset(); |
|
210 |
} |
|
211 |
else { |
|
212 |
$nav.parent().addClass('sidebar-scroll-native').css('max-height', $avail_height); |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
if(!nativeScroll) { |
|
217 |
if( !enable_scroll || !ace_scroll.is_active() ) { |
|
218 |
if(this.is_scrolling) this.disable(); |
|
219 |
} |
|
220 |
} |
|
221 |
else { |
|
222 |
if( !enable_scroll && this.is_scrolling ) this.disable(); |
|
223 |
} |
|
224 |
|
|
225 |
//return is_scrolling; |
|
226 |
} |
|
227 |
|
|
228 |
this.disable = function() { |
|
229 |
this.is_scrolling = false; |
|
230 |
if(ace_scroll) { |
|
231 |
if(!nativeScroll) ace_scroll.disable(); |
|
232 |
else $nav.parent().removeClass('sidebar-scroll-native').css('max-height', ''); |
|
233 |
} |
|
234 |
|
|
235 |
$sidebar.removeClass('sidebar-scroll'); |
|
236 |
} |
|
237 |
|
|
238 |
this.prehide = function(height_change) { |
|
239 |
if(!this.is_scrolling || ace_sidebar.get('minimized')) return;//when minimized submenu's toggle should have no effect |
|
240 |
|
|
241 |
if(content_height() + height_change < available_height()) { |
|
242 |
this.disable(); |
|
243 |
} |
|
244 |
else if(height_change < 0) { |
|
245 |
//if content height is decreasing |
|
246 |
//let's move nav down while a submenu is being hidden |
|
247 |
var scroll_top = scroll_content.scrollTop() + height_change |
|
248 |
if(scroll_top < 0) return; |
|
249 |
|
|
250 |
scroll_content.scrollTop(scroll_top); |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
this._reset = function(recalc) { |
|
255 |
if(recalc === true) { |
|
256 |
this.sidebar_fixed = is_element_pos(sidebar, 'fixed'); |
|
257 |
} |
|
258 |
|
|
259 |
if(ace.vars['webkit']) |
|
260 |
setTimeout(function() { self.reset() } , 0); |
|
261 |
else this.reset(); |
|
262 |
} |
|
263 |
|
|
264 |
this.get = function(name) { |
|
265 |
if(this.hasOwnProperty(name)) return this[name]; |
|
266 |
} |
|
267 |
this.set = function(name, value) { |
|
268 |
if(this.hasOwnProperty(name)) this[name] = value; |
|
269 |
} |
|
270 |
this.ref = function() { |
|
271 |
//return a reference to self |
|
272 |
return this; |
|
273 |
} |
|
274 |
|
|
275 |
this.updateStyle = function(styleClass) { |
|
276 |
if(!ace_scroll || nativeScroll) return; |
|
277 |
ace_scroll.update({styleClass: styleClass}); |
|
278 |
} |
|
279 |
|
|
280 |
|
|
281 |
//change scrollbar size after a submenu is hidden/shown |
|
282 |
//but don't change if sidebar is minimized |
|
283 |
$sidebar.on('hidden.ace.submenu.sidebar_scroll shown.ace.submenu.sidebar_scroll', '.submenu', function(e) { |
|
284 |
e.stopPropagation(); |
|
285 |
|
|
286 |
if( !ace_sidebar.get('minimized') ) { |
|
287 |
//webkit has a little bit of a glitch!!! |
|
288 |
self._reset(); |
|
289 |
} |
|
290 |
}) |
|
291 |
|
|
292 |
initiate(true);//true = on_page_load |
|
293 |
} |
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
//reset on document and window changes |
|
298 |
$(document).on('settings.ace.sidebar_scroll', function(ev, event_name, event_val){ |
|
299 |
$('.sidebar[data-sidebar-scroll=true]').each(function() { |
|
300 |
var $this = $(this); |
|
301 |
var sidebar_scroll = $this.ace_sidebar_scroll('ref'); |
|
302 |
|
|
303 |
if( event_name == 'sidebar_collapsed' ) { |
|
304 |
if( $this.attr('data-sidebar-hover') == 'true' ) $this.ace_sidebar_hover('reset'); |
|
305 |
|
|
306 |
if(event_val == true) sidebar_scroll.disable();//disable scroll if collapsed |
|
307 |
else sidebar_scroll.reset(); |
|
308 |
} |
|
309 |
else if( event_name === 'sidebar_fixed' || event_name === 'navbar_fixed' ) { |
|
310 |
var is_scrolling = sidebar_scroll.get('is_scrolling'); |
|
311 |
var sidebar_fixed = is_element_pos(this, 'fixed') |
|
312 |
sidebar_scroll.set('sidebar_fixed', sidebar_fixed); |
|
313 |
|
|
314 |
if(sidebar_fixed && !is_scrolling) { |
|
315 |
sidebar_scroll.reset(); |
|
316 |
} |
|
317 |
else if( !sidebar_fixed && sidebar_scroll.get('only_if_fixed') ) { |
|
318 |
sidebar_scroll.disable(); |
|
319 |
} |
|
320 |
} |
|
321 |
}) |
|
322 |
}) |
|
323 |
|
|
324 |
$(window).on('resize.ace.sidebar_scroll', function(){ |
|
325 |
$('.sidebar[data-sidebar-scroll=true]').each(function() { |
|
326 |
var sidebar_scroll = $(this).ace_sidebar_scroll('ref'); |
|
327 |
|
|
328 |
var sidebar_fixed = is_element_pos(this, 'fixed') |
|
329 |
sidebar_scroll.set('sidebar_fixed', sidebar_fixed); |
|
330 |
sidebar_scroll.reset(); |
|
331 |
}); |
|
332 |
}) |
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
///////////////////////////////////////////// |
|
337 |
if(!$.fn.ace_sidebar_scroll) { |
|
338 |
$.fn.ace_sidebar_scroll = function (option, value) { |
|
339 |
var method_call; |
|
340 |
|
|
341 |
var $set = this.each(function () { |
|
342 |
var $this = $(this); |
|
343 |
var data = $this.data('ace_sidebar_scroll'); |
|
344 |
var options = typeof option === 'object' && option; |
|
345 |
|
|
346 |
if (!data) $this.data('ace_sidebar_scroll', (data = new Sidebar_Scroll(this, options))); |
|
347 |
if (typeof option === 'string' && typeof data[option] === 'function') { |
|
348 |
method_call = data[option](value); |
|
349 |
} |
|
350 |
}); |
|
351 |
|
|
352 |
return (method_call === undefined) ? $set : method_call; |
|
353 |
} |
|
354 |
|
|
355 |
$.fn.ace_sidebar_scroll.defaults = { |
|
356 |
'scroll_to_active': true, |
|
357 |
'include_shortcuts': true, |
|
358 |
'include_toggle': false, |
|
359 |
'scroll_style': '', |
|
360 |
'lock_anyway': false, |
|
361 |
'only_if_fixed': true |
|
362 |
} |
|
363 |
|
|
364 |
} |
|
365 |
|
|
366 |
})(window.jQuery); |