Advertisement
Guest User

App.js

a guest
Apr 10th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. $(() => {
  2. const app = Sammy('#main', function () {
  3. this.use('Handlebars', 'hbs');
  4.  
  5. // HOME Page
  6. this.get('index.html', displayHome);
  7. this.get('#/home', displayHome);
  8.  
  9. // ABOUT Page
  10. this.get('#/about', function (ctx) {
  11. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  12. ctx.username = sessionStorage.getItem('username');
  13. this.loadPartials({
  14. header: './templates/common/header.hbs',
  15. footer: './templates/common/footer.hbs'
  16. }).then(function () {
  17. this.partial('./templates/about/about.hbs');
  18. });
  19. });
  20.  
  21. //LOGIN Page
  22. this.get('#/login', function (ctx) {
  23. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  24. ctx.username = sessionStorage.getItem('username');
  25. this.loadPartials({
  26. header: './templates/common/header.hbs',
  27. footer: './templates/common/footer.hbs',
  28. loginForm: './templates/login/loginForm.hbs'
  29. }).then(function () {
  30. this.partial('./templates/login/loginPage.hbs');
  31. });
  32. });
  33.  
  34. this.post('#/login', function (ctx) {
  35. let username = ctx.params.username;
  36. let password = ctx.params.password;
  37. auth.login(username, password)
  38. .then(function (userInfo) {
  39. auth.saveSession(userInfo);
  40. auth.showInfo('Logged in!');
  41. displayHome(ctx);
  42. })
  43. .catch(auth.handleError);
  44. });
  45.  
  46. //Logout
  47. this.get('#/logout', function (ctx) {
  48. auth.logout().then(function () {
  49. sessionStorage.clear();
  50. auth.showInfo('Logged out!');
  51. displayHome(ctx);
  52. }).catch(auth.handleError);
  53. });
  54.  
  55. //Register Page
  56. this.get('#/register', function (ctx) {
  57. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  58. ctx.username = sessionStorage.getItem('username');
  59. this.loadPartials({
  60. header: './templates/common/header.hbs',
  61. footer: './templates/common/footer.hbs',
  62. registerForm: './templates/register/registerForm.hbs'
  63. }).then(function () {
  64. this.partial('./templates/register/registerPage.hbs');
  65. });
  66. });
  67.  
  68. this.post('#/register', function (ctx) {
  69. let username = ctx.params.username;
  70. let password = ctx.params.password;
  71. let repeatedPass = ctx.params.repeatPassword;
  72. if (password === repeatedPass) {
  73. auth.register(username, password)
  74. .then(function (userInfo) {
  75. auth.saveSession(userInfo);
  76. auth.showInfo('Registration successful!');
  77. displayHome(ctx);
  78. }).catch(auth.handleError);
  79. } else {
  80. auth.showError('Passwords do not math!');
  81. }
  82. });
  83.  
  84. //CATALOG Page
  85. this.get('#/catalog', displayCatalog);
  86.  
  87. //Create team page
  88. this.get('#/create', function (ctx) {
  89. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  90. ctx.username = sessionStorage.getItem('username');
  91. ctx.loadPartials({
  92. header: './templates/common/header.hbs',
  93. footer: './templates/common/footer.hbs',
  94. createForm: './templates/create/createForm.hbs'
  95. }).then(function () {
  96. this.partial('./templates/create/createPage.hbs');
  97. });
  98. });
  99.  
  100. this.post('#/create', function (ctx) {
  101. let teamName = ctx.params.name;
  102. let teamComment = ctx.params.comment;
  103.  
  104. teamsService.createTeam(teamName, teamComment)
  105. .then(function (teamInfo) {
  106. teamsService.joinTeam(teamInfo._id)
  107. .then(function (userInfo) {
  108. auth.saveSession(userInfo);
  109. auth.showInfo(`Team ${teamName} created!`);
  110. displayCatalog(ctx);
  111. })
  112. .catch(auth.handleError);
  113. })
  114. .catch(auth.handleError);
  115. });
  116.  
  117. //TEAM details page
  118. this.get('#/catalog/:id', function (ctx) {
  119. let teamId = ctx.params.id.substr(1);
  120.  
  121. teamsService.loadTeamDetails(teamId)
  122. .then(function (teamInfo) {
  123. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  124. ctx.username = sessionStorage.getItem('username');
  125. ctx.teamId = teamId;
  126. ctx.name = teamInfo.name;
  127. ctx.comment = teamInfo.comment;
  128. ctx.isOnTeam = teamInfo._id === sessionStorage.getItem('teamId');
  129. ctx.isAuthor = teamInfo._acl.creator === sessionStorage.getItem('userId');
  130. ctx.loadPartials({
  131. header: './templates/common/header.hbs',
  132. footer: './templates/common/footer.hbs',
  133. teamControls: './templates/catalog/teamControls.hbs'
  134. })
  135. .then(function () {
  136. this.partial('./templates/catalog/details.hbs');
  137. });
  138. }).catch(auth.handleError);
  139. });
  140.  
  141. //JOIN Team by id
  142. this.get('#/join/:id', function (ctx) {
  143. let teamId = ctx.params.id.substr(1);
  144. teamsService.joinTeam(teamId)
  145. .then(function (userInfo) {
  146. auth.saveSession(userInfo);
  147. auth.showInfo('Joined Team!');
  148. displayCatalog(ctx);
  149. })
  150. .catch(auth.handleError);
  151. });
  152.  
  153. //Leave Team
  154. this.get('#/leave', function (ctx) {
  155. teamsService.leaveTeam()
  156. .then(function (userInfo) {
  157. auth.saveSession(userInfo);
  158. auth.showInfo('Left the team!');
  159. displayCatalog(ctx);
  160. })
  161. .catch(auth.handleError);
  162. });
  163.  
  164. //EDIT Team Page (by id)
  165. this.get('#/edit/:id', function (ctx) {
  166. let teamId = ctx.params.id.substr(1);
  167.  
  168. teamsService.loadTeamDetails(teamId)
  169. .then(function (teamInfo) {
  170. ctx.teamId = teamId;
  171. ctx.name = teamInfo.name;
  172. ctx.comment = teamInfo.comment;
  173. ctx.loadPartials({
  174. header: './templates/common/header.hbs',
  175. footer: './templates/common/footer.hbs',
  176. editForm: './templates/edit/editForm.hbs'
  177. }).then(function () {
  178. this.partial('./templates/edit/editPage.hbs');
  179. })
  180. })
  181. .catch(auth.handleError)
  182. });
  183.  
  184. this.post('#/edit/:id', function (ctx) {
  185. let teamId = ctx.params.id.substr(1);
  186. let teamName = ctx.params.name;
  187. let teamComment = ctx.params.comment;
  188.  
  189. teamsService.edit(teamId, teamName, teamComment)
  190. .then(function () {
  191. auth.showInfo(`Team ${teamName} edited!`);
  192. displayCatalog(ctx);
  193. })
  194. .catch(auth.handleError);
  195. });
  196.  
  197. function displayHome(ctx) {
  198. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  199. ctx.username = sessionStorage.getItem('username');
  200. ctx.loadPartials({
  201. header: './templates/common/header.hbs',
  202. footer: './templates/common/footer.hbs'
  203. }).then(function () {
  204. this.partial('./templates/home/home.hbs');
  205. });
  206. }
  207.  
  208. function displayCatalog(ctx) {
  209. ctx.loggedIn = sessionStorage.getItem('authtoken') !== null;
  210. ctx.username = sessionStorage.getItem('username');
  211. teamsService.loadTeams()
  212. .then(function (teams) {
  213. ctx.hasNoTeam = sessionStorage.getItem('teamId') === null || sessionStorage.getItem('teamId') === 'undefined';
  214. ctx.teams = teams;
  215. ctx.loadPartials({
  216. header: './templates/common/header.hbs',
  217. footer: './templates/common/footer.hbs',
  218. team: './templates/catalog/team.hbs'
  219. })
  220. .then(function () {
  221. this.partial('./templates/catalog/teamCatalog.hbs')
  222. });
  223. });
  224. }
  225. });
  226.  
  227. app.run();
  228. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement