-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mobile.dotty.js
102 lines (95 loc) · 2.61 KB
/
jquery.mobile.dotty.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
/*
* requires underscore
*/
(function($, window, undefined) {
$.widget("mobile.dotty", $.mobile.widget, {
resize_event: ('onorientationchange' in window) ?'orientationchange' :'resize',
total: 0, // Number of Dots
highlight_index: 0,
fullsize_painter: null,
smallsize_painter: null,
_init: function () {
// Caching painter instances
this.fullsize_painter = FullsizePainter( this );
this.smallsize_painter = SmallsizePainter( this );
// There can only be one of my kind
if ( this.element.data('initialized') ) {
return;
}
this.element
.addClass( 'dotty' )
.data( 'initialized', true );
$(window).bind( this.resize_event, $.proxy(function() {
this.draw( this.highindex, this.total, true );
}, this));
},
hide: function() {
this._clearui();
},
draw: function( nof, total, forceredraw ) {
this.highindex = nof;
var painter = this._getPainter( total );
// Optimization
if ( this.total !== total || this.element.children().length === 0 || forceredraw ) {
this.total = total;
painter.drawdots( total );
painter.highlight( nof );
} else {
painter.highlight( nof );
}
},
_getPainter: function( totalDots ) {
return ( window.innerWidth > ( totalDots * 24 + 150 ) )
? this.fullsize_painter
: this.smallsize_painter;
},
_clearui: function ( ) {
this.element.empty();
}
});
function FullsizePainter( widget ) {
function drawdots ( total ) {
widget._clearui();
var el = widget.element;
_(total).times( function( idx ) {
el.append( $('<div class="dot">') );
});
}
function highlight( n ) {
$( '.pointer', widget.element ).removeClass( 'pointer' );
if ( _.isNumber(n) ) {
$( '.dot:nth-child(' + n + ')', widget.element).addClass( 'pointer' );
}
}
return {
drawdots: drawdots,
highlight: highlight
};
}
function SmallsizePainter( widget ) {
function drawNumbers( total ) {
widget._clearui();
var el = widget.element
, currentPagenumber = $('<span class="n">')
, slash = $('<span class="seperator">').html( '/' )
, totalPagecount = $('<span class="total">').html( total );
el.append([currentPagenumber, slash, totalPagecount]);
}
function drawCurrentPageNumber( n ) {
$( '.n', this.element ).html( n );
}
return {
drawdots: drawNumbers,
highlight: drawCurrentPageNumber
};
}
$(document).ready( function() {
var pager = $( '<div data-role="dotty">' );
$( 'body' ).append(pager);
pager.dotty();
$.Dotty = {
draw: function(n, total) { pager.dotty( 'draw', n, total ); },
hide: function() { pager.dotty( 'hide' ); }
};
});
})(jQuery, this);