-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
190 lines (158 loc) · 5.67 KB
/
app.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
//The main app object
var App = {
data: {},
allVideos: [],
videos: [],
videosToShow: [],
page: 0,
params: {
$_search: '',
$_10_likes: false,
$_items_to_take: 10
}
};
//Reads and updates the parameter's object from the view
App.updateParams = function () {
App.params.$_search = $("#search").val();
App.params.$_10_likes = $('#10_likes').is(":checked");
App.params.$_items_to_take = parseInt($('input[name=items]:checked').val());
};
//Update the view with the selected videos
App.render = function () {
App.videos = App.allVideos;
App.videos = App.searchVideos(App.videos, App.params.$_search);
if (App.params.$_10_likes) {
App.videos = App.filterVideos(App.videos);
}
if (App.page < 0) App.page = 0;
else if (App.page > (App.paginateVideos(App.videos, App.params.$_items_to_take).length - 1)) App.page = App.paginateVideos(App.videos, App.params.$_items_to_take).length - 1;
App.videosToShow = App.paginateVideos(App.videos, App.params.$_items_to_take)[App.page];
var template = $('#template').html();
Mustache.parse(template);
var rendered = Mustache.render(template, App);
$('#target').html(rendered);
};
//Loads the videos from the json file
App.loadVideos = function () {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
App.data = JSON.parse(this.responseText);
App.allVideos = App.transformVideos(App.data.data);
App.render();
}
};
xhttp.open("GET", "videos.json", true);
xhttp.send();
};
//Transforms the video into a basic format with only needed properties
App.transformVideos = function (videos) {
return videos.map(function (video) {
var optVideo = {};
optVideo.id = video.resource_key;
if (video.user.pictures) {
optVideo.user_image_url = video.user.pictures.sizes[video.user.pictures.sizes.length - 1].link;
}
else {
optVideo.user_image_url = "assets/no-user-image.jpg";
}
optVideo.user_name = video.user.name;
optVideo.user_link = video.user.link;
optVideo.description = video.description;
if (optVideo.description) {
if (optVideo.description.length >= 300) {
optVideo.shortDescription =
{
content: optVideo.description.substring(0, 300)
}
optVideo.longDescription =
{
content: optVideo.description
}
}
else{
optVideo.fullDescription = {
content : optVideo.description
}
}
//optVideo.htmlDescription = optVideo.description.replace(/(?:\r\n|\r|\n)/g, '<br />');
}
optVideo.name = video.name;
optVideo.link = video.link;
if (video.pictures) {
optVideo.image_url = video.pictures.sizes[video.pictures.sizes.length - 1].link_with_play_button;
}
optVideo.comments_count = video.metadata.connections.comments.total;
optVideo.likes_count = video.metadata.connections.likes.total;
if (video.user.metadata.connections.likes.total) {
optVideo.user_likes_count = video.user.metadata.connections.likes.total;
}
return optVideo;
})
};
//Filter videos by the description containing the search text in parameter
App.searchVideos = function (videos, search) {
var tempVideos = [];
videos.forEach(function (video) {
if (video.description && video.description.indexOf(search) >= 0) {
tempVideos.push(video);
}
});
return tempVideos;
};
//Returns only the videos whose users have 10 likes or more
App.filterVideos = function (videos) {
var tempVideos = [];
videos.forEach(function (video) {
if (video.user_likes_count >= 0) tempVideos.push(video);
});
return tempVideos;
};
//Transforms the videos array to another array with the size chosen by the user
App.paginateVideos = function (videos, items_per_page) {
return _.chunk(videos, items_per_page);
};
//Show more full description
App.more = function(el){
$(el).parent().parent().find(".long").removeClass("hidden");
$(el).parent().parent().find(".short").addClass("hidden");
}
//function to show less description
App.less = function(el){
$(el).parent().parent().find(".long").addClass("hidden");
$(el).parent().parent().find(".short").removeClass("hidden");
}
$(document).ready(function () {
//On startup,load videos and render with default choices
App.loadVideos();
//When the user changes the items per page params,the view is updated
$('input[type=radio][name=items]').change(function () {
App.updateParams();
App.render();
});
//When the user enters a search text,the view is updated
$("#search").change(function () {
App.updateParams();
App.render();
});
//When the user enters a search text,the view is updated
$("#search").keyup(function () {
App.updateParams();
App.render();
});
//When the user clicks the previous button,the view is updated
$("#prev").click(function () {
App.page--;
App.render();
});
//When the user clicks the next button,the view is updated
$("#next").click(function () {
App.page++;
App.render();
});
//When the user choose to show only videos from users who have more than 10 likes,the vview is updated
$("#10_likes").change(function () {
App.updateParams();
App.render();
});
});