-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_carousel.js
277 lines (252 loc) · 8.96 KB
/
board_carousel.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Populates the board by creating jQuery DOM elements for given bobs,
* Bobs with Meme flavor appended to memesStream (carousel),
* Bobs with Moment flavor appeneded to momentsStream (carousel)
* @param {Object[]} bobs - Array of Bob objects to be displayed on the board
*/
function popluateBoard(bobs) {
let $momentsStream = $(".moments");
for (var i = 0; i < bobs.length; i++) {
let bob = bobs[i];
$momentsStream.append(createBoardElement(bob));
}
// Initalizes carousels for both moments and memes streams
initCarousel();
}
function initCarousel(){
let $momentsStream = $(".moments");
$momentsStream.carousel({
fullWidth: true,
onCycleTo: function(activeItem){
updateVoteLabel(activeItem);
loadVideo(activeItem);
}
});
}
/**
* Creates and Returns a new html element from a given Bob object
* @param {Object} - Javascript Bob object from Mongoose
* @return {Object} - jQuery html elment of the created Bob
*/
function createBoardElement(bob) {
var $html = $('<div>', {
id: bob.id,
class: "carousel-item"
});
// Bob flavor decides how each bob is rendered
switch (bob.flavor) {
case 'Quote':
$html.addClass('quote-bobble').attr("id", bob._id)
.append($('<div>', {class: "quote-holder"})
.append($('<p>', {class: "quote", text: bob.data.Text}))
.append($('<p>', {class: "author", text: bob.data.Author}))
);
break;
case 'Text':
$html.addClass('text-bobble').attr("id", bob._id)
.append($('<div>', {class: "text-holder"})
.append($('<p>', {text: bob.data.Text}))
);
break;
case 'Video':
$html.addClass('video-bobble ').attr("id", bob._id)
.append(($('<video loop muted poster>', {css:{'background':'black'}}).attr("preload", "none").attr("poster", '/static/images/test-pump.gif'))
.append($('<source>', {src:bob.data.Link})));
break;
case 'Moment':
$html.addClass('image-bobble flip').attr("id", bob._id)
.append($('<div />', {class: "image-holder front", css: {'background-color': "black", 'background-image': "url(" + bob.data.Link + ")", 'image-orientation': '0deg', 'background-size': "contain", 'background-position': "center center"}}))
.append($('<div />', {class: "text-holder back"})
.append($('<p>', {class: "author", text: bob.data.Title})));
break;
case 'Meme':
$html.addClass('image-bobble flip').attr("id", bob._id)
.append($('<div />', {class: "image-holder front", css: {'background-color': "black", 'background-image': "url(" + bob.data.Link + ")", 'image-orientation': '0deg', 'background-size': "contain", 'background-position': "center center"}}))
.append($('<div />', {class: "text-holder back"})
.append($('<p>', {class: "author", text: bob.data.Title})));
break;
default:
console.log("Unhandled type" + bob);
$html = null;
}
var description = bob.description || bob.data.Description;
if (description !== undefined) {
$html.append($('<div>', {class: "description"}).append(
$('<p>', {text: description}))
);
}
return $html;
}
/**
* Generates jQuery selector for a carousel depending on bob flavor,
* then calls addToCarousel function, passing bob and carousel as params.
* @param {Object} bob - A single Bob object received via socket
*/
function addBoardElement(bob) {
carouselSelector = ".moments"; //jQuery selector for target carousel
if ($("#slideshow > div").length > 20) {
$("#slideshow > div:last-child").remove(); // deletes the oldest bob
}
appendToCarousel(bob, carouselSelector);
}
/**
* Creates a jQuery element of a new bob object,
* then appends it to the target carousel DOM element
* @param {Object} bob - A single Bob object to be added to the carousel
* @param {string} carousel - jQuery selector string for the target carousel
*/
function appendToCarousel(bob, carouselSelector) {
$carousel = $(carouselSelector);
$activeItem = $(carouselSelector + " .carousel-item.active");
if (bob !== null) {
$activeItem.after(createBoardElement(bob));
}
var $before = $activeItem.prevAll();
$carousel.append($before.clone());
$before.remove();
if ($carousel.hasClass('initialized')) {
$carousel.removeClass('initialized');
}
//reinit the carousel
initCarousel();
}
/**
* Updates the caraousel item with updated bob data
* Called when update_element socket received
* @param {Object} bobData - contains updated bob data
*/
function updateBoardElement(bobData){
var bobId = bobData._id;
var $bobToUpdate = $("#" + bobId);
var $imageHolder = $bobToUpdate.find(".image-holder");
var newImage = "background-image: url(" + bobData.data.Link + ")";
$imageHolder.attr("style", newImage);
var $textHolder = $bobToUpdate.find(".text-holder");
$textHolder.find(".author").attr("text", bobData.Title);
$textHolder.find(".description").attr("text", bobData.Description);
}
/**
* Deletes the html bob element with bobid from carousel
* @param {string} bobid - id of the bob to be deleted
*/
function deleteElement(bobid){
$("#" + bobid).remove();
if ($('#slideshow').hasClass('initialized')) {
$('#slideshow').removeClass('initialized');
}
// reinit the carousel
initCarousel();
// force move to next slide
carouselControl("right");
}
/**
* @param {object} activeItem - Vanilla JS Object of activeItem
* Updates the label with the votes label for currently activeItem
*/
function updateVoteLabel(activeItem){
var $labelToUpdate = $("#votes");
var $activeBobID = $(activeItem).attr("id");
var votes = $.get('/api/bobs/' + $activeBobID + "/votes", function(res){
$labelToUpdate.attr("data-badge-caption", "+" + res.votes);
});
}
/**
* Updates the label with the votes returned from socket
* @param {object} res - response from the socket which contains bobid and votes
*/
function incrementVote(res){
$("#votes").attr("data-badge-caption", "+" + res.votes);
}
/**
* @param {object} activeItem - Vanilla JS Object of activeItem
* Pauses the previous video, plays the current video, loads the next video
*/
function loadVideo(activeItem){
var $activeItem = $(activeItem); // wrapping jQuery to vanilla JS Object
if ($activeItem.hasClass("video-bobble")){
$activeItem.find("video")[0].play();
}
if ($(".active").next().hasClass("video-bobble")){
$(".active").next().find("video")[0].load();
}
if ($(".active").prev().hasClass("video-bobble")){
$(".active").prev().find("video")[0].pause();
}
}
/**
* When Document is ready,
* Defines time interval for carousel auto slides,
* Listens on click and touch events for flip, swap buttons
* Listens on click and touch events for plusOne, flag buttons
* Listens on tab buttons for toggling the events
* Time Unit : ms. Default Settings : 10s, 7s(small slide)
*/
var carouselInterval = null;
$(function(){
// initial interval setting
$.get('/api/bobs/active', popluateBoard);
carouselInterval = setInterval(function() {
$('#slideshow').carousel('next');
}, 12000);
$(".plusOne").on("click", function(){
var activeBobID = $("#slideshow").find(".active").attr("id");
$.post('/api/bobs/' + activeBobID + "/votes");
});
$(".flag").on("click", function(){
var activeBobID = $("#slideshow").find(".active").attr("id");
$.post('/api/bobs/' + activeBobID + "/flags");
});
$( window ).on( "swipe", function( event ) {
$.event.special.swipe.horizontalDistanceThreshold = (screen.availWidth) / 80;
});
$('#slideshow').on("swipeleft", function(){
$('#slideshow').stop();
resetInterval(carouselControl("left"));
});
$('#slideshow').on("swiperight", function(){
$('#slideshow').stop();
resetInterval(carouselControl("right"));
});
});
/**
* Changes active item to either previous or next item depending on direction
* @param {string} direction - direction of moving : left, right
*/
function carouselControl(direction){
if (direction == "left") {
$('#slideshow').carousel('prev', 1); // Move next n times.
} else if (direction == "right") {
$('#slideshow').carousel('next', 1); // Move next n times.
}
}
/**
* Resets time interval for the main carousel
* Time Unit : ms. Default Settings : 10s
*/
function resetInterval() {
// Clears the existing timers
clearInterval(carouselInterval);
// Reinits the timers
carouselInterval = setInterval(function() {
$('#slideshow').carousel('next');
}, 12000);
}
/**
* Listens on jQuery events for keyboard controls
* @param {event} e - jQuery event obejct
*/
$(document).keydown(function(e) {
if (e.keyCode == 37) {
// press left arrow key to go back to previous slide
resetInterval(carouselControl("left"));
}
if (e.keyCode == 39) {
// press right arrow key to go to next slide
resetInterval(carouselControl("right"));
}
});
var socket = io();
socket.on('add_element', addBoardElement);
socket.on('update_element', updateBoardElement);
socket.on('upvote', incrementVote);
socket.on('delete', deleteElement);