-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.js
executable file
·246 lines (206 loc) · 7.48 KB
/
channels.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
$("#rightpane").after(
'<div id="scroll-feature" class="horiz-scroll">' +
'<div class="scroller">' +
'<div class="left-scroll invisible">' +
'<p class="fa fa-angle-left"></p>' +
'</div>' +
'<div class="right-scroll">' +
'<p class="fa fa-angle-right"></p>' +
'</div>' +
// The HorizontalScroller Class accepts a jQuery object as its only argument
// The argument is the parent container of the scrolling element
// The element requires an ID to differentiate HorizontalScroller instances
function HorizontalScroller(elem) {
this.scrollbox = elem; // The scrollers viewable area
this.scrollImages = this.scrollbox.find("img");
this.leftScrollControl = this.scrollbox.siblings(".left-scroll");
this.rightScrollControl = this.scrollbox.siblings(".right-scroll");
// Listener to change visibility of left and right controls
// when at scroll extremes
this.scrollbox.on("scroll", this.evaluateControlVisibility.bind(this));
};
HorizontalScroller.prototype = {
scrollboxWidth: function() {
return this.scrollbox.outerWidth(true);
},
currentScrollPosition: function() {
return this.scrollbox.scrollLeft();
},
currentRightPosition: function() {
return this.currentScrollPosition() + this.scrollboxWidth() - this.totalWidths();
},
// Maps the image width of each image in the scroller
imageWidths: function() {
return $.map(this.scrollImages, function(img) {
return $(img).outerWidth(true);
})
},
// Returns the total width of all the images, that is,
// the total of the visible and overflow content.
totalWidths: function() {
return this.imageWidths().reduce(function(a,b) { return a+b});
},
// Returns the average width of all the images
avgWidth: function() {
return this.totalWidths() / this.imageWidths().length;
},
// Determines the number of images in view area.
// Number of images changes with responsive CSS
imagesAcross: function() {
return Math.round( this.scrollboxWidth() / this.avgWidth() );
},
// maps the offset x-distance of each image
// from the left edge of the view area
imageOffsets: function() {
return $.map(this.scrollImages, function(img) {
return Math.round($(img).position().left);
});
},
// Returns the index of the first number in the given array
// greater than the given value, or, returns the index of
// the first positive number in the array
indexOfFirst: function(array, value) {
value = value || 0;
var firstIndex;
var i = 0;
while (firstIndex === undefined && array.length > i) {
if (array[i] >= value)
firstIndex = i;
i += 1;
}
return firstIndex;
},
// Returns the index of first image that is completely in view
// within the scrollbox
firstVisibleImageIndex: function() {
return this.indexOfFirst(this.imageOffsets());
},
// Returns the first image that is completely in view
// within the scrollbox
firstVisibleImage: function() {
return this.scrollImages[this.firstVisibleImageIndex()];
},
// Returns the index of the last image with its left edge in view
// within the scrollbox
lastVisibleImageIndex: function() {
return this.firstVisibleImageIndex() + this.imagesAcross();
},
// Returns the last image with its left edge in view
// within the scrollbox
lastVisibleImage: function() {
return this.scrollImages[this.lastVisibleImageIndex()];
},
// Returns the difference between the scrollboxes left edge
// and the left edge of the first fully visible image, that is,
// how far in the first fully visible image is
offset: function() {
var offset = $(this.firstVisibleImage()).position().left;
return Math.round(offset);
},
// Returns the combined scroll amount that the images have to travel
// in order to land evenly within the scroll window. The resulting
nextScrollPosition: function(direction) {
var nextScrollPosition = this.currentScrollPosition() + this.offset();
switch(direction) {
case "left":
nextScrollPosition -= this.scrollboxWidth();
if (($(this.firstVisibleImage()).outerWidth(true) - this.offset()) < 0) {
nextScrollPosition -= $(this.firstVisibleImage()).outerWidth(true);
}
break;
case "right":
nextScrollPosition += this.scrollboxWidth();
if (this.offset() > 0) {
nextScrollPosition -= $(this.firstVisibleImage()).outerWidth(true);
}
break;
}
return nextScrollPosition;
},
// Triggers the animation
animateScroll: function(direction) {
resetFocusedImg();
var scroller = this;
setTimeout(function() {
scroller.scrollbox.animate({
scrollLeft: scroller.nextScrollPosition(direction)
}, this.scrollboxWidth())
}.bind(this), 100);
},
hideScrollControl: function(control) {
control.addClass("invisible");
},
showScrollControl: function(control) {
control.removeClass("invisible");
},
scrollControlVisibility: function(control) {
return control.hasClass("invisible");
},
scrollAtZero: function() {
return this.currentScrollPosition() == 0;
},
scrollAtMax: function() {
return this.currentRightPosition() >= -1;
},
evaluateControlVisibility: function() {
var left = this.leftScrollControl;
var right = this.rightScrollControl;
var leftIsInvisible = this.scrollControlVisibility(left);
var rightIsInvisible = this.scrollControlVisibility(right);
if (this.scrollAtZero()) this.hideScrollControl(left);
if (this.scrollAtMax()) this.hideScrollControl(right);
if (!this.scrollAtZero() && leftIsInvisible) this.showScrollControl(left);
if (!this.scrollAtMax() && rightIsInvisible) this.showScrollControl(right);
}
};
// End HorizontalScroller.prototype
var scrollers = {};
// Detects scrollers in the DOM
function detectScrollers() {
return $.map($(".horiz-scroll"), function(scroller) {
return $(scroller).attr("id");
});
}
// Generates a new HorizontalScroller for each scroller in DOM
function mapScrollers(scrollerIds) {
scrollerIds.forEach(function(elem, i , arr) {
var scroller = "#" + elem + " .scroll-images";
scrollers[elem] = new HorizontalScroller( $(scroller) );
});
}
// Gets the scroll direction to pass to animation function
function getScrollDirection(button) {
return (button.hasClass("left-scroll")) ? "left" : "right"
}
// Triggers the scroll animation for specific scroller
// in a specific direction
function triggerAnimation(button) {
var scrollId = button.closest(".horiz-scroll").attr("id");
var scrollDirection = getScrollDirection(button);
scrollers[scrollId].animateScroll(scrollDirection);
}
// Scroll buttons listener
function listenForScroll() {
$(".left-scroll, .right-scroll").on("click", function() {
var button = $(this);
triggerAnimation(button);
});
}
function resetFocusedImg() {
$(".focused").removeClass("focused");
}
// listener for click, slides up
var horizontalScrollImg = $(".horiz-scroll .scroll-images img");
horizontalScrollImg.on("click", function() {
if (!$(this).hasClass("focused"))
resetFocusedImg();
$(this).toggleClass("focused");
});
// Registers scrollers and initiates listeners
function scrollerInit() {
var scrollerIds = detectScrollers();
mapScrollers(scrollerIds);
listenForScroll();
}
// Begins the fun
scrollerInit();