Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. function showVew(vewID) {
  2. $("main > section").hide();
  3.  
  4. $('#' + vewID).show();
  5. }
  6. function showHideNavLinks() {
  7. let loggedIn = sessionStorage.authToken != null;
  8. if(loggedIn){
  9. $("#linkLogin").hide();
  10. $("#linkRegister").hide();
  11. $("#linkListBooks").show();/*TODO List of books*/
  12. $("#linkCreateBook").show();
  13. $("#linkLogout").show();
  14. }
  15. else {
  16. $("#linkLogin").show();
  17. $("#linkRegister").show();
  18. $("#linkListBooks").hide();
  19. $("#linkCreateBook").hide();
  20. $("#linkLogout").hide();
  21.  
  22. }
  23. }
  24. function showHomeVew() {
  25. showVew('vewHome')
  26. }
  27. function showLoginVew() {
  28. showVew('vewLogin')
  29. }
  30. function showRegisterVew() {
  31. showVew('vewRegister')
  32. }
  33. function showListBooksVew() {
  34. showVew('vewListBooks')
  35. }
  36. function showCreateBookVew() {
  37. showVew('vewCreatedBooks')
  38. }
  39. function logout() {
  40. alert('logout');
  41. showVew('vewHome')
  42. }
  43.  
  44. $(function () {
  45. $("#linkHome").click(showHomeVew);
  46. $("#linkLogin").click(showLoginVew);
  47. $("#linkRegister").click(showRegisterVew);
  48. $("#linkListBooks").click(showListBooksVew);
  49. $("#linkCreateBook").click(showCreateBookVew);
  50. $("#linkLogout").click(logout);
  51.  
  52. $("#loginButton").click(login);
  53. $("#registerButton").click(register);
  54. $("#createBookButton").click(createBook);
  55.  
  56. showHomeVew();
  57. showHideNavLinks();
  58. });
  59.  
  60. /*
  61. $("#formLogin").submit(function (e) {
  62. e.preventDefault(); login();
  63. });
  64. $("#formRegister").submit(function (e) { /!*!!!!!!!!!!*!/
  65. e.preventDefault(); register();
  66. });
  67. $("#formCreateBook").submit(function (e) {
  68. e.preventDefault(); createBook();
  69. });*/
  70.  
  71.  
  72. const kinveyBaseUrl = "https://baas.kinvey.com/";
  73. const kinveyAppKey = "kid_SJOSvBYB";
  74. const kinveyAppSecret = "0274d97941d7422e8954ba4fb3fefea0";
  75.  
  76.  
  77.  
  78. function showView(viewName) {
  79. $('main > section').hide();
  80. $('#' + viewName).show();
  81.  
  82. }
  83. function showInfo(message) {
  84. $('#infoBox').text(message);
  85. $('#infoBox').show();
  86. setTimeout(function () {
  87. $('#infobox').fadeOut()}, 3000)
  88. }
  89. function showError(errorMsg) {
  90. $('#errorBox').text("Error: " + errorMsg);
  91. $('#errorBox').show();
  92. }
  93. $(function () {
  94. showHideNavLinks();
  95. showVew('viewHome');
  96. })
  97. $("#linkHome").click(showHomeVew());
  98. $("#linkLogin").click(showLoginVew());
  99. $("#linkRegister").click(showRegisterVew());
  100. $("#linkListBooks").click(showListBooksVew());
  101. $("#linkCreateBook").click(showCreateBookVew());
  102. $("#linkLogout").click(logout());
  103.  
  104. $("#formLogin").submit(function(e){e.preventDefault(); login() });
  105. $("#formRegister").submit(function (e) {e.preventDefault(); register() });
  106. $("#formCreateBook").submit(function (e) {e.preventDefault(); createBook() });
  107. $(document).on({
  108. ajaxStart: function() { $("#loadingBox").show()},
  109. ajaxStop: function(){$("#loadingBox").hide()}
  110. })
  111. function login() {
  112. const kinveyLoginUrl = kinveyBaseUrl + "user/" + kinveyAppKey + "/login";
  113. const kinveyAuthHeaders = {
  114. 'Authorization' : "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret)
  115. };
  116. let userData = {
  117. username: $('#loginUser').val(),
  118. password: $('#userPass').val()
  119. };
  120. console.log("logna se");
  121. $.ajax({
  122. method: "POST",
  123. url: kinveyLoginUrl,
  124. headers: kinveyAuthHeaders,
  125. data: userData,
  126. success: loginSuccess,
  127. error: handleAjaxError
  128. });
  129. }
  130. function loginSuccess(response){
  131. let userAuth = response._kmd.authtoken;
  132. sessionStorage.setItem('authToken', userAuth);
  133. showHideNavLinks();
  134. showListBooksVew();
  135. showInfo('Login successful.');
  136. }
  137. function handleAjaxError(response) {
  138. let errorMsg = JSON.stringify(response);
  139. if(response.readyState === 0){
  140. errorMsg = "Cannot connect due to network error.";
  141. }
  142. if(response.responseJSON && response.responseJSON.description){
  143. errorMsg = response.responseJSON.description;
  144. }
  145. showError(errorMsg);
  146. }
  147. function showRegisterView() {
  148. showView('viewRegister')
  149. }
  150. function register() {
  151. const kinveyRegisterUrl = kinveyBaseUrl + "user/" + kinveyAppKey + "/";
  152. const kinveyAuthHeaders = {'Authorization' : "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret)};
  153. let userData = {
  154. username: $('#registerUser').val(),
  155. password: $('#registerPass').val()
  156. };
  157. $.ajax({
  158. method: "POST",
  159. url: kinveyRegisterUrl,
  160. headers: kinveyAuthHeaders,
  161. data: userData,
  162. success: registerSuccess,
  163. error: handleAjaxError
  164. });
  165. }
  166. function registerSuccess(response) {
  167. let userAuth = response._kmd.authtoken;
  168. sessionStorage.setItem('authToken', userAuth);
  169. showHideNavLinks();
  170. showListBooksVew();
  171. showInfo('User registration successful.')
  172. }
  173. function listBooks() {
  174. $('#books').empty();
  175. showView('vewListBooks');
  176.  
  177. const kinveyBooksUrl = kinveyBaseUrl + "appdata/" + kinveyAppKey + "/books";
  178. const kinveyAuthHeaders = {'Authorization' : "Kinvey " + sessionStorage.getItem('authToken')};
  179.  
  180. $.ajax({
  181. method: "GET",
  182. url: kinveyBooksUrl,
  183. headers: kinveyAuthHeaders,
  184. success: loadBooksSuccess,
  185. error: handleAjaxError
  186. })
  187. }
  188. function loadBooksSuccess(books) {
  189. showInfo('Book loaded.');
  190. if(books.length == 0){
  191. $('#books').text('No books in the library.');
  192. }
  193. else{
  194. let booksTable = $('<table>')
  195. .append($('<tr>').append(
  196. '<th>Title</th>',
  197. '<th>Author</th>',
  198. '<th>Description</th>'
  199. ));
  200. for (let book of books){
  201. booksTable.append($('<tr>').append(
  202. $('<td>').text(book.title),
  203. $('<td>').text(book.author),
  204. $('<td>').text(book.description))
  205. );
  206. }
  207. $('#books').append(booksTable);
  208. }
  209. }
  210. function showCreateBookView() {
  211. showView('viewCreateBook');
  212. }
  213. function createBook() {
  214. console.log("vlezna v function-a");
  215. const kinveyBooksUrl = kinveyBaseUrl + "appdata/" + kinveyAppKey + "/books";
  216. const kinveyAuthHeaders = {'Authorization' : "Kinvey " + sessionStorage.getItem('authToken')};
  217. let bookData = {
  218. title: $('#bookTitle').val(),
  219. author: $('#bookAuthor').val(),
  220. description: $('#bookDescription').val()
  221. };
  222.  
  223. $.ajax({
  224. method: "POST",
  225. url: kinveyBooksUrl,
  226. headers: kinveyAuthHeaders,
  227. data: bookData,
  228. success: createBookSuccess,
  229. error: handleAjaxError
  230. });
  231.  
  232. function createBookSuccess(response) {
  233. listBooks();
  234. showInfo('Book created.');
  235. }
  236. console.log("suzdade kniga");
  237. }
  238. function logout() {
  239. sessionStorage.clear();
  240. showHideNavLinks();
  241. showVew('viewHome');
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement