Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const SammyHelpers = (function () {
- function _renderPage(templateName, addPartialsFunc, appendDataFunc) {
- addPartialsFunc && addPartialsFunc(TemplateLayout);
- this.loadPartials(
- TemplateLayout
- ).then(function () {
- const data = {
- userIsLogged: User.isLogged()
- };
- appendDataFunc && appendDataFunc(data);
- this.partial(templateName, data);
- });
- }
- function _calcTime(dateIsoFormat) {
- let diff = new Date - (new Date(dateIsoFormat));
- diff = Math.floor(diff / 60000);
- if (diff < 1) return 'less than a minute';
- if (diff < 60) return diff + ' minute' + pluralize(diff);
- diff = Math.floor(diff / 60);
- if (diff < 24) return diff + ' hour' + pluralize(diff);
- diff = Math.floor(diff / 24);
- if (diff < 30) return diff + ' day' + pluralize(diff);
- diff = Math.floor(diff / 30);
- if (diff < 12) return diff + ' month' + pluralize(diff);
- diff = Math.floor(diff / 12);
- return diff + ' year' + pluralize(diff);
- function pluralize(value) {
- if (value !== 1) return 's';
- else return '';
- }
- }
- function showHome() {
- if (User.isLogged()) {
- const ajaxFeed = User.getFeed();
- const ajaxChirps = User.getChirps();
- const ajaxFollowing = User.getFollowing();
- const ajaxFollowers = User.getFollowers();
- Promise.all([ajaxFeed, ajaxChirps, ajaxFollowing, ajaxFollowers])
- .then(([feed, chirps, following, followers]) => {
- feed.forEach(f => f['elapsedTime'] = _calcTime(f._kmd.ect));
- _renderPage.call(this,
- TemplateConstants.path.home,
- (template) => {
- template.chirpForm = TemplateConstants.path.chirpForm;
- },
- (data) => {
- data['username'] = User.getUsername();
- data['feed'] = feed;
- data['chirps'] = chirps.length;
- data['following'] = following[0].subscriptions.length;
- data['followers'] = followers.length;
- });
- });
- } else {
- _renderPage.call(this, TemplateConstants.path.loginForm);
- }
- }
- function showLogin() {
- _renderPage.call(this, TemplateConstants.path.loginForm);
- }
- function showRegister() {
- _renderPage.call(this, TemplateConstants.path.registerForm);
- }
- function showProfile(username, isMyProfile) {
- const ajaxFeed = User.getFeed(username);
- const ajaxChirps = User.getChirps(username);
- const ajaxFollowing = User.getFollowing(username);
- const ajaxFollowers = User.getFollowers(username);
- Promise.all([ajaxFeed, ajaxChirps, ajaxFollowing, ajaxFollowers])
- .then(([feed, chirps, following, followers]) => {
- feed.forEach(f => {
- f['elapsedTime'] = _calcTime(f._kmd.ect);
- f['isMyProfile'] = isMyProfile;
- });
- const userId = chirps.length ? chirps[0]._acl.creator : '';
- const subs = JSON.parse(User.getSubscriptions());
- const isFollowed = Boolean(subs.find(u => u === username));
- _renderPage.call(this,
- TemplateConstants.path.profile,
- (template) => {
- template.chirpForm = TemplateConstants.path.chirpForm;
- },
- (data) => {
- data['isMyProfile'] = isMyProfile;
- data['feed'] = feed;
- data['userId'] = userId;
- data['isFollowed'] = isFollowed;
- data['username'] = username;
- data['chirps'] = chirps.length;
- data['following'] = following[0].subscriptions.length;
- data['followers'] = followers.length;
- });
- });
- }
- function showDiscover() {
- const authToken = User.getAuthToken();
- const self = this;
- const ajaxUsers = KinveyService.getAllUsers(authToken)
- .then(async function (users) {
- users = users.filter(u => u.username !== User.getUsername());
- const ajaxes = [];
- for (const user of users) {
- ajaxes.push(KinveyService.getFollowers(authToken, user.username));
- }
- Promise.all(ajaxes)
- .then(function (followers) {
- for (let index = 0; index < users.length; index++) {
- const user = users[index];
- user.followers = followers[index].length;
- }
- _renderPage.call(self, TemplateConstants.path.discover, null, (data) => data['users'] = users);
- });
- });
- }
- function clearForm() {
- $(this.target).find('input[type=text], input[type=password], textarea').val('');
- }
- return {
- showHome,
- showLogin,
- showRegister,
- clearForm,
- showProfile,
- showDiscover
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement