This repository was archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGalleryPage.js
More file actions
228 lines (203 loc) · 5.17 KB
/
GalleryPage.js
File metadata and controls
228 lines (203 loc) · 5.17 KB
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
var Observable = require("FuseJS/Observable");
var Helpers = require("Assets/JS/Helpers.js");
var HTML = require("Assets/JS/Html.js");
var API = require("Assets/JS/API.js");
var Event = require('FuseJS/UserEvents');
var Moment = require('Assets/JS/moment.js');
var User = require('User.js');
var feed = Observable();
var features = Observable();
var currentFeature = Observable();
var IsReloading = Observable(false);
var IsLoadingMore = Observable(false);
var LoadingState = Observable("Loading");
var searchText = Observable("");
var createdBy = Observable("");
var _loader = undefined;
function Feature(title, desc, feature)
{
this.title = title;
this.desc = desc;
this.endpoint = "/photos";
this.opts = {feature: feature};
var self = this;
this.isSelected = Observable(function() {
return self === currentFeature.value;
});
this.select = function()
{
if (self !== currentFeature.value)
{
currentFeature.value = self;
API.SetPhotoStream(self.endpoint, self.opts);
LoadFeed({clear: true});
}
};
};
function SearchFeature(text)
{
this.title = text;
this.desc = "Searching photos";
this.isSelected = Observable(false);
var self = this;
this.select = function()
{
currentFeature.value = self;
API.SetSearchText(text);
LoadFeed({clear: true});
};
};
function Photo(url, image_aspect, image_url, photo_url, name, avatar_url, username, votes_count, created_at)
{
this.url = API.BASE_URL + url;
this.image_url = image_url;
this.image_aspect = image_aspect;
this.photo_url = photo_url;
this.name = HTML.unescape(name);
this.avatar_url = avatar_url;
this.username = username;
this.user_url = API.BASE_URL + "/" + username;
this.votes_count = votes_count;
this.created_at = created_at;
}
function hasImage(image_url)
{
for (var i=0; i<feed.length; i++) if (feed.getAt(i).image_url === image_url) return true;
return false;
}
function processResponse(response)
{
var result = [];
for (var i=0; i<response.photos.length; i++)
{
var responsePhoto = response.photos[i];
var image_url, photo_url;
for (var j=0; j<responsePhoto.images.length; j++)
{
if (responsePhoto.images[j].size === 30) image_url = responsePhoto.images[j].https_url;
else if (responsePhoto.images[j].size === 1080) photo_url = responsePhoto.images[j].https_url;
}
if (!hasImage(image_url))
result.push(new Photo(
responsePhoto.url,
responsePhoto.width / responsePhoto.height,
image_url,
photo_url,
responsePhoto.name,
responsePhoto.user.avatars.default.https,
responsePhoto.user.username,
responsePhoto.votes_count,
responsePhoto.created_at)
);
}
return result;
}
function LoadFeed(opts)
{
if (!opts) opts = {clear: false, more: false};
if (_loader) _loader.cancel();
LoadingState.value = "Loading";
if (opts.clear) {
feed.replaceAll([]);
IsReloading.value = false;
IsLoadingMore.value = false;
}
_loader = Helpers.Promise(function(resolve, reject)
{
var _stream = opts.more ? API.PhotoStream.More() : API.PhotoStream.Load();
_stream.then(function(response)
{
resolve(response);
})
.catch(function(err)
{
reject(err);
});
});
_loader.then(function(result)
{
var items = processResponse(result);
if (items.length > 0) {
for (var i=0; i<items.length; i++) (opts.more ? feed.add(items[i]) : feed.insertAt(i, items[i]));
LoadingState.value = "Ready";
}
else if (feed.length === 0) LoadingState.value = "NotFound";
IsReloading.value = false;
IsLoadingMore.value = false;
})
.catch(function(err)
{
if (!(err instanceof Helpers.CancellationError))
{
debug_log(err);
if (feed.length === 0) LoadingState.value = "TryAgain";
Event.raise("Error", {message: err.message || err});
}
IsReloading.value = false;
IsLoadingMore.value = false;
});
}
function Reload()
{
IsReloading.value = true;
LoadFeed();
}
function LoadMore()
{
IsLoadingMore.value = true;
LoadFeed({more: true});
}
function ScrollToTop()
{
scrollView.goto(0, 0);
}
function Search()
{
var text = searchText.value;
if (text.trim() !== "")
new SearchFeature('"' + text + '"').select();
}
function Login()
{
router.push("login");
}
function Logout()
{
router.push("profile");
}
function SelectPhoto(args)
{
var photo = args.data;
createdBy.value = Moment(photo.created_at).fromNow() + " by @" + photo.username;
}
function init()
{
features.add(new Feature("Most Popular", "Trending Right Now", "popular"));
features.add(new Feature("Highest Rated", "Photos that Have Been Popular", "highest_rated"));
features.add(new Feature("Editor's Choice", "Picked by Top Photographers", "editors"));
features.add(new Feature("Upcoming", "Promising New Uploads", "upcoming"));
features.add(new Feature("Fresh Today", "Latest from the Community", "fresh_today"));
features.getAt(0).select();
LoadFeed({clear: true});
}
init();
module.exports =
{
feed: feed,
features: features,
currentFeature: currentFeature,
IsReloading: IsReloading,
Reload: Reload,
LoadFeed: LoadFeed,
IsLoadingMore: IsLoadingMore,
LoadMore: LoadMore,
LoadingState: LoadingState,
ScrollToTop: ScrollToTop,
searchText: searchText,
Search: Search,
Login: Login,
Logout: Logout,
user: User.user,
SelectPhoto: SelectPhoto,
createdBy: createdBy
};