Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(() => {
  2.  
  3.     const app = Sammy("#container", function () {
  4.         this.use('Handlebars', 'hbs');
  5.  
  6.         $(document).on({
  7.             ajaxStart: function () {
  8.                 $("#loadingBox").show();
  9.             },
  10.             ajaxStop: function () {
  11.                 $("#loadingBox").hide();
  12.             }
  13.         });
  14.  
  15.         // HOME
  16.         this.get('index.html', displayHome);
  17.         this.get('#/home', displayHome);
  18.  
  19.         function displayHome(context) {
  20.  
  21.             context.isLogged = sessionStorage.getItem('username') !== null;
  22.             context.username = sessionStorage.getItem('username');
  23.  
  24.             context.loadPartials({
  25.                 header: './templates/common/header.hbs',
  26.                 footer: './templates/common/footer.hbs',
  27.                 welcome: './templates/home/welcome.hbs',
  28.             }).then(function () {
  29.                 this.partial('./templates/home/homePage.hbs');
  30.             });
  31.         }
  32.  
  33.         this.post("#/register", function (context) {
  34.             let username = context.params.username;
  35.             let password = context.params.password;
  36.             let repeatPassword = context.params.repeatPass;
  37.            
  38.             auth.register(username, password, repeatPassword)
  39.                 .then(function (userData) {
  40.                     console.log("SUCESS");
  41.                     auth.saveSession(userData);
  42.                     auth.showInfo("User registration successful.");
  43.                     context.redirect("#/catalog");
  44.                 }).catch(auth.handleError);
  45.         });
  46.  
  47.         this.get('#/logout', function (context) {
  48.  
  49.             auth.logout()
  50.                 .then(function () {
  51.                     sessionStorage.clear();
  52.                     auth.showInfo('Logout successful.');
  53.                     context.redirect('#');
  54.                 }).catch(auth.handleError);
  55.         });
  56.  
  57.         this.get("#/catalog", function (context) {
  58.             context.isLogged = sessionStorage.getItem('username') !== null;
  59.             context.username = sessionStorage.getItem('username');
  60.  
  61.             this.loadPartials({
  62.                 header: './templates/common/header.hbs',
  63.                 footer: './templates/common/footer.hbs',
  64.                 menu: './templates/posts/menu.hbs',
  65.                 posts: './templates/posts/posts.hbs',
  66.             }).then(function () {
  67.                 this.partial('./templates/posts/catalogPage.hbs');
  68.             });
  69.         });
  70.  
  71.         this.post('#/login', function (context) {
  72.             context.isLogged = sessionStorage.getItem('username') !== null;
  73.  
  74.             let username = context.params.username;
  75.             let password = context.params.password;
  76.  
  77.             auth.login(username, password)
  78.                 .then(function (userInfo) {
  79.                     auth.saveSession(userInfo);
  80.                     auth.showInfo('Login successful.');
  81.                     context.redirect('#/catalog');
  82.                 }).catch(auth.handleError);
  83.         });
  84.  
  85.         this.get("#/submit", function () {
  86.             this.partial('./templates/submit/submitForm.hbs');
  87.         });
  88.  
  89.         this.post("#/submit", function (context) {
  90.             let url = context.params.url;
  91.             let author = context.params.author;
  92.             let title = context.params.title;
  93.             let imageUrl = "http" + context.params.imageUrl;
  94.             let description = context.params.description;
  95.  
  96.             let postData = {
  97.                 url, author, title, imageUrl, description
  98.             };
  99.  
  100.             postService.post(postData)
  101.                 .then(function () {
  102.                     auth.showInfo('Post created.');
  103.                     context.redirect('#/catalog');
  104.                 }).catch(auth.handleError);
  105.  
  106.         });
  107.  
  108.     });
  109.  
  110.     function calcTime(dateIsoFormat) {
  111.         let diff = new Date - (new Date(dateIsoFormat));
  112.         diff = Math.floor(diff / 60000);
  113.         if (diff < 1) return 'less than a minute';
  114.         if (diff < 60) return diff + ' minute' + pluralize(diff);
  115.         diff = Math.floor(diff / 60);
  116.         if (diff < 24) return diff + ' hour' + pluralize(diff);
  117.         diff = Math.floor(diff / 24);
  118.         if (diff < 30) return diff + ' day' + pluralize(diff);
  119.         diff = Math.floor(diff / 30);
  120.         if (diff < 12) return diff + ' month' + pluralize(diff);
  121.         diff = Math.floor(diff / 12);
  122.         return diff + ' year' + pluralize(diff);
  123.         function pluralize(value) {
  124.             if (value !== 1) return 's';
  125.             else return '';
  126.         }
  127.     }
  128.  
  129.     app.run();
  130. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement