-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.mobile.alphascroll.js
129 lines (111 loc) · 4.18 KB
/
jquery.mobile.alphascroll.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
( function( $ ) {
$.fn.extend({
alphascroll: function() {
return this.each( function() {
var content = $( this ),
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'],
shortAlphabet = ['a','d','g','j','m','p','s','w','z'],
dividers = [],
dividerClass,
scrollbar = '';
// attach classes to list autodividers
$( content ).find( '.ui-li-divider' ).each( function() {
dividerClass = $( this ).html().toLowerCase();
dividers.push( dividerClass );
$( this ).addClass( dividerClass );
});
// create and display the scrollbar
function createScrollbar() {
// generate scrollbar HTML
$( alphabet ).each( function( index, value ) {
// attach the alphascroll-item class to each letter if there is a corresponding divider (acts as a link)
if ( $.inArray( value, dividers ) > -1 ) {
scrollbar += '<li id="alphascroll-' + value + '" class="alphascroll-item" unselectable="on">' + value.toUpperCase() + '</li>';
}
else {
scrollbar += '<li id="alphascroll-' + value + '" unselectable="on">' + value.toUpperCase() + '</li>';
}
});
// attach scrollbar to page
$( content ).wrap( '<div />' );
var wrapper = $( content ).parent();
$( wrapper ).prepend( '<ul class="alphascroll">' + scrollbar + '</ul>' );
var alphascroll = $( content ).closest( 'div' ).children( '.alphascroll' );
// bind touch event to scrollbar (for touch devices)
$( alphascroll ).bind( 'touchmove', function( event ) {
event.preventDefault();
var touch = event.originalEvent.touches[0] || event.originalEvent.changedTouches[0];
// scroll to divider position
alphaScroll( touch.pageY );
});
// bind mouse events to scrollbar (for desktop browsers)
$( alphascroll ).bind( 'mousedown', function() {
$( '.ui-page-active' ).bind( 'mousemove', function( event ) {
// prevent text selection while scrolling
$( this ).css({
"-webkit-user-select" : "none",
"-moz-user-select" : "none",
"-ms-user-select" : "none",
"user-select" : "none"
});
// scroll to divider position
alphaScroll( event.pageY );
});
// return page to normal functioning after mouseup
$( '.ui-page-active' ).bind( 'mouseup', function() {
// release mousemove event control
$( '.ui-page-active' ).unbind( 'mousemove' );
// return text selection to default
$( this ).css({
"-webkit-user-select" : "text",
"-moz-user-select" : "text",
"-ms-user-select" : "text",
"user-select" : "text"
});
});
});
// use short scrollbar if screen is short (like landscape on an iPhone)
if ( $( window ).height() <= 320 ) {
truncateScrollbar();
}
}
// handle orientation changes
$( window ).bind( 'orientationchange', function() {
$( '.alphascroll' ).unwrap().remove();
scrollbar = '';
createScrollbar();
});
function truncateScrollbar() {
$( '.alphascroll li' ).each( function( index, value ) {
if ( $.inArray( $( this ).html().toLowerCase(), shortAlphabet ) < 0 ) {
$( this ).html( '·' ).addClass( 'truncated' );
}
});
}
// do the scroll
function alphaScroll( y ) {
$( '.alphascroll-item' ).each( function() {
if ( ! ( y <= $( this ).offset().top || y >= $( this ).offset().top + $( this ).outerHeight() ) ) {
var scroll_id = $( this ).attr( 'id' ),
letter = scroll_id.split( '-' ),
target = $( '.' + letter[1] ),
position = target.position(),
header_height;
// offset scroll-top if header is displayed
if ( $( '.ui-page-active [data-role="header"]' ).hasClass( 'ui-fixed-hidden' ) ) {
header_height = 0;
}
else {
header_height = $( '.ui-page-active [data-role="header"]' ).height();
}
// scroll the page
$.mobile.silentScroll( position.top - header_height );
}
});
}
// generate scrollbar on invokation
createScrollbar();
});
}
});
})( jQuery );