Advertisement
Guest User

Prodavachnik - js file

a guest
Nov 25th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. function startApp() {
  2.  
  3. //Clear User Auth Data:
  4. sessionStorage.clear();
  5.  
  6. //Show & Hide Menu Links:
  7. showHideMenuLinks();
  8.  
  9. // Bind The Navigation Menu Links:
  10. $("#linkHome").click(showHomeView);
  11. $("#linkLogin").click(showLoginView);
  12. $("#linkRegister").click(registerView);
  13. $("#linkListAds").click(listAds);
  14. $("#linkCreateAd").click(showCreatedAds);
  15. $("#linkLogout").click(logoutUser);
  16.  
  17. //Bind The Form Submit Buttons:
  18. $("#buttonLoginUser").click(loginUser);
  19. $("#buttonRegisterUser").click(registerUser);
  20. $("#buttonCreateAd").click(createAd);
  21. $("#buttonEditAd").click(editAd);
  22.  
  23. //Bind the info / error boxes: hide on click:
  24. $("#infoBox, #errorBox").click(function () {
  25. $(this).fadeOut();
  26. });
  27.  
  28. //Create Constants:
  29. const kinveyBaseUrl = "https://baas.kinvey.com/";
  30. const kinveyAppKey = "kid_rkf7DvrMg";
  31. const kinveyAppSecret = "201fdc29f52c4e70925f1112df485a1c";
  32. const kinveyAppAuthHeaders = {
  33. 'Authorization' : "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret),
  34. };
  35.  
  36. // Attach AJAX "loading" event listener
  37. $(document).on({
  38. ajaxStart: function () {$("#loadingBox").show()},
  39. ajaxStop: function () {$("#loadingBox").hide()}
  40. });
  41.  
  42. // Functions:
  43. function getKinveyUserAuthHeaders() {
  44. return {
  45. 'Authorization': "Kinvey " +
  46. sessionStorage.getItem('authToken'),
  47. };
  48. }
  49.  
  50. function saveAuthInSession(userInfo) {
  51. sessionStorage.setItem("username", userInfo.username);
  52. sessionStorage.setItem("authToken", userInfo._kmd.authtoken);
  53.  
  54. $("#loggedInUser").text("Welcome, " + userInfo.username);
  55. listAds();
  56. }
  57.  
  58. // Show & Hide View Functions:
  59. function showHideMenuLinks() {
  60. $("#menu a").hide();
  61.  
  62. if (sessionStorage.getItem("authToken")) {
  63. //Logged In User:
  64. $("#linkHome").show();
  65. $("#linkListAds").show();
  66. $("#linkCreateAd").show();
  67. $("#linkLogout").show();
  68. } else {
  69. //Not Logged In User:
  70. $("#linkHome").show();
  71. $("#linkLogin").show();
  72. $("#linkRegister").show();
  73. }
  74. }
  75.  
  76. function showView(viewName) {
  77. //Hide All Views And Show The Selected View Only:
  78. $('main > section').hide();
  79. $("#" + viewName).show();
  80. }
  81.  
  82. function showHomeView() {
  83. showView("viewHome");
  84. }
  85.  
  86. function showLoginView() {
  87. showView("viewLogin");
  88. $("#formLogin").trigger('reset');
  89. }
  90.  
  91. function registerView() {
  92. showView("viewRegister");
  93. $("#formRegister").trigger('reset');
  94. }
  95.  
  96. function showCreatedAds() {
  97. showView("viewCreateAd");
  98. $("#formCreateAd").trigger('reset');
  99. }
  100.  
  101. function showInfo(message) {
  102. $('#infoBox').text(message);
  103. $('#infoBox').show();
  104. setTimeout(function () {
  105. $('#infoBox').fadeOut;
  106. }, 3000);
  107. }
  108.  
  109. function showError(errorMsg) {
  110. $('#errorBox').text("Error: " + errorMsg);
  111. $('#errorBox').show();
  112. }
  113.  
  114. //Func That Handle Ajax Error:
  115. function handleAjaxError(response) {
  116. let errorMsg = JSON.stringify(response);
  117. if (response.readyState === 0)
  118. errorMsg = "Cannot connect due to network error.";
  119. if (response.responseJSON &&
  120. response.responseJSON.description)
  121. errorMsg = response.responseJSON.description;
  122. showError(errorMsg);
  123. }
  124.  
  125. //Functions for Login/Logout/Register & CRUD Operations:
  126. function logoutUser() {
  127. sessionStorage.clear();
  128. $("#loggedInUser").text("");
  129. showView("viewHome");
  130. showHideMenuLinks();
  131. showInfo("Logout Successful");
  132. }
  133.  
  134. function loginUser() {
  135. let userData = {
  136. username: $("#formLogin input[name=username]").val(),
  137. password: $("#formLogin input[name=passwd]").val()
  138. };
  139.  
  140. $.ajax({
  141. method: "POST",
  142. url: kinveyBaseUrl + "user/" + kinveyAppKey + "/login",
  143. data: userData,
  144. headers: kinveyAppAuthHeaders,
  145. success: loginSuccess,
  146. error: handleAjaxError
  147. });
  148.  
  149. function loginSuccess(userInfo) {
  150. saveAuthInSession(userInfo);
  151. showHideMenuLinks();
  152. listAds();
  153. showInfo("Login Successful");
  154. }
  155. }
  156.  
  157. function registerUser() {
  158. let userData = {
  159. username: $("#formRegister input[name=username]").val(),
  160. password: $("#formRegister input[name=passwd]").val()
  161. };
  162. $.ajax({
  163. method: "POST",
  164. url: kinveyBaseUrl + "user/" + kinveyAppKey + "/",
  165. data: JSON.stringify(userData),
  166. contentType: "application/json",
  167. headers: kinveyAppAuthHeaders,
  168. success: registerUserSuccess,
  169. error: handleAjaxError
  170. });
  171. function registerUserSuccess(userInfo) {
  172. saveAuthInSession(userInfo);
  173. showHideMenuLinks();
  174. listAds();
  175. showInfo("User Registration Successful.");
  176. }
  177. }
  178.  
  179. function createAd() {
  180.  
  181. }
  182.  
  183. function editAd() {
  184.  
  185. }
  186. function listAds(ads) {
  187.  
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement