Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(() => {
- const app = Sammy("#container", function () {
- this.use('Handlebars', 'hbs');
- $(document).on({
- ajaxStart: function () {
- $("#loadingBox").show();
- },
- ajaxStop: function () {
- $("#loadingBox").hide();
- }
- });
- // HOME
- this.get('index.html', displayHome);
- this.get('#/home', displayHome);
- function displayHome(context) {
- context.isLogged = sessionStorage.getItem('username') !== null;
- context.username = sessionStorage.getItem('username');
- context.loadPartials({
- header: './templates/common/header.hbs',
- footer: './templates/common/footer.hbs',
- welcome: './templates/home/welcome.hbs',
- }).then(function () {
- this.partial('./templates/home/homePage.hbs');
- });
- }
- this.post("#/register", function (context) {
- let username = context.params.username;
- let password = context.params.password;
- let repeatPassword = context.params.repeatPass;
- auth.register(username, password, repeatPassword)
- .then(function (userData) {
- console.log("SUCESS");
- auth.saveSession(userData);
- auth.showInfo("User registration successful.");
- context.redirect("#/catalog");
- }).catch(auth.handleError);
- });
- this.get('#/logout', function (context) {
- auth.logout()
- .then(function () {
- sessionStorage.clear();
- auth.showInfo('Logout successful.');
- context.redirect('#');
- }).catch(auth.handleError);
- });
- this.get("#/catalog", function (context) {
- context.isLogged = sessionStorage.getItem('username') !== null;
- context.username = sessionStorage.getItem('username');
- this.loadPartials({
- header: './templates/common/header.hbs',
- footer: './templates/common/footer.hbs',
- menu: './templates/posts/menu.hbs',
- posts: './templates/posts/posts.hbs',
- }).then(function () {
- this.partial('./templates/posts/catalogPage.hbs');
- });
- });
- this.post('#/login', function (context) {
- context.isLogged = sessionStorage.getItem('username') !== null;
- let username = context.params.username;
- let password = context.params.password;
- auth.login(username, password)
- .then(function (userInfo) {
- auth.saveSession(userInfo);
- auth.showInfo('Login successful.');
- context.redirect('#/catalog');
- }).catch(auth.handleError);
- });
- this.get("#/submit", function () {
- this.partial('./templates/submit/submitForm.hbs');
- });
- this.post("#/submit", function (context) {
- let url = context.params.url;
- let author = context.params.author;
- let title = context.params.title;
- let imageUrl = "http" + context.params.imageUrl;
- let description = context.params.description;
- let postData = {
- url, author, title, imageUrl, description
- };
- postService.post(postData)
- .then(function () {
- auth.showInfo('Post created.');
- context.redirect('#/catalog');
- }).catch(auth.handleError);
- });
- });
- 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 '';
- }
- }
- app.run();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement