This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
toolbar.js
104 lines (78 loc) · 3.19 KB
/
toolbar.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
define(["joshlib!ui/list","joshlib!utils/dollar","joshlib!vendor/underscore"], function(UIList,$,_) {
var UIToolbar = UIList.extend({
tagName: 'div',
className: 'joshfire-toolbar',
initialize: function(options) {
this.maxOnScreen = options.maxOnScreen || 5;
this.margin = options.margin || 5;
//this.arrowWidth = options.arrowWidth || .15;
this.currPage = 0;
this.useWindowWidth = options.useWindowWidth || false;
UIList.prototype.initialize.call(this, options);
},
enhance: function() {
var self = this;
var resize = function() {
if(!this.collection) return;
this.$('.arrow').remove();
var width;
var itemWidth;
if(self.useWindowWidth) {
width = window.innerWidth;
} else {
var rect = this.el.getBoundingClientRect();
width = rect.width;
}
if(this.collection.length <= this.maxOnScreen) {
this.$('ul').width(width);
itemWidth = (width - this.margin) / this.collection.length;
this.$('li').css({
width: itemWidth - this.margin + 'px',
'padding-left': this.margin + 'px'
});
} else {
var numPages = Math.ceil(this.collection.length / (this.maxOnScreen - 2));
this.$('ul').css({width: numPages * width + 'px'});
itemWidth = Math.round((width - this.margin) / this.maxOnScreen);
var $lis = this.$('li').css({
width: itemWidth - this.margin + 'px',
'padding-left': this.margin + 'px'
});
for (var i = this.maxOnScreen - 2; i < $lis.length; i += this.maxOnScreen - 2) {
if(i + 1 < $lis.length) {
$($lis[i]).after('<li class="arrow"><a class="next">next<span></span></a></li><li class="arrow"><a class="prev">prev<span></span></a></li>');
}
}
this.$('.next').parent().css({
width: itemWidth - this.margin + 'px',
'padding-left': this.margin + 'px'
});
this.$('.prev').parent().css({
width: itemWidth - this.margin + 'px',
'padding-left': this.margin * 2 + 'px'
});
this.$('.next').bind('click', _.bind(function(e) {
e.preventDefault();
this.currPage = Math.min(numPages, this.currPage + 1);
this.iScroller.scrollToPage.call(this.iScroller, this.currPage, 0, 200);
return false;
}, this));
this.$('.prev').bind('click', _.bind(function(e) {
e.preventDefault();
this.currPage = Math.max(0, this.currPage - 1);
this.iScroller.scrollToPage.call(this.iScroller, this.currPage, 0, 200);
return false;
}, this));
}
};
// Resize the element (e.g. to fit the window's width)
resize.call(this);
// Add the scroller (done by the phone's adapted UIElement)
// Note it's important to add the scroller *after* the element
// has been resized otherwise it won't work as expected.
UIList.prototype.enhance.call(this);
window.addEventListener('resize', _.bind(resize, this));
}
});
return UIToolbar;
});