DatStorm

Untitled

Feb 27th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. //Run this function when we have loaded the HTML document
  2. window.onload = function () {
  3. //This code is called when the body element has been loaded and the application starts
  4.  
  5. //Request items from the server. The server expects no request body, so we set it to null
  6. sendRequest("GET", "rest/shop/testing", null, function (itemsText) {
  7. //This code is called when the server has sent its data
  8. var items = JSON.parse(itemsText);
  9. addItemsToTable(items);
  10. });
  11.  
  12. //Register an event listener for button clicks
  13. var updateButton = document.getElementById("update");
  14. addEventListener(updateButton, "click", function () {
  15. //Same as above, get the items from the server
  16. sendRequest("GET", "rest/shop/testing", null, function (itemsText) {
  17. //This code is called when the server has sent its data
  18. var items = JSON.parse(itemsText);
  19. addItemsToTable(items);
  20. });
  21. });
  22.  
  23. $("#create-customer-submit-button").click(function () {
  24. var login = document.getElementById("login-customer-submit-button");
  25. var create = document.getElementById("create-customer-submit-button");
  26. createCustomer();
  27. create.disabled = true;
  28. login.disabled = true;
  29.  
  30. });
  31.  
  32. $("#login-customer-submit-button").on("click",function () {
  33. loginCustomer();
  34. });
  35. /*
  36. $("#login-customer-submit-button").click(function () {
  37. loginCustomer();
  38. });
  39. */
  40.  
  41. eow();
  42. addItemsToCartWhenClick();
  43. };
  44.  
  45.  
  46.  
  47. function createCustomer() {
  48. var createCustomerName = document.getElementById("createUserName").value;
  49. var createCustomerPassword = document.getElementById("createUserPassword").value;
  50.  
  51. $.post("/rest/shop/createCustomer",{
  52. username: createCustomerName,
  53. password: createCustomerPassword
  54. },function (name) {
  55. var login = document.getElementById("login-customer-submit-button");
  56. var create = document.getElementById("create-customer-submit-button");
  57. if(name =="true"){
  58. errorMessage("Your are now a customer");
  59. login.disabled = true;
  60. create.disabled = true;
  61. }else {
  62. errorMessage()
  63. }
  64. });
  65.  
  66. }
  67.  
  68. function loginCustomer() {
  69. var customerName = document.getElementById("createUserName").value;
  70. var customerPass = document.getElementById("createUserPassword").value;
  71.  
  72. $.post("/rest/shop/loginCustomer",{
  73. username: customerName,
  74. password: customerPass
  75. },function (name) {
  76. var login = document.getElementById("login-customer-submit-button");
  77. var create = document.getElementById("create-customer-submit-button");
  78. if(name =="true"){
  79. errorMessage("You are now logged in. Boolean is " + name);
  80. create.disabled = true;
  81. login.disabled = true;
  82. }else {
  83. errorMessage("We could not log you in. Boolean is " + name);
  84. login.disabled = false;
  85. create.disabled = false;
  86. }
  87. //alert(name);
  88. });
  89. }
  90.  
  91. function errorMessage(name) {
  92. $("#login-message-error-area").text(name);
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. function addItemsToTable(items) {
  100. //Get the table body we we can add items to it
  101. var tableBody = document.getElementById("itemtablebody");
  102. //Remove all contents of the table body (if any exist)
  103. tableBody.innerHTML = "";
  104.  
  105. //Loop through the items from the server
  106. for (var i = 0; i < items.length; i++) {
  107. (function (i) {
  108. var item = items[i];
  109. //Create a new line for this item
  110. var tr = document.createElement("tr");
  111.  
  112. // ITEM NAME
  113. var nameCell = document.createElement("td");
  114. nameCell.textContent = item.itemName;
  115. tr.appendChild(nameCell);
  116.  
  117. // ITEM PRICE
  118. var priceCell = document.createElement("td");
  119. priceCell.textContent = item.itemPrice;
  120. tr.appendChild(priceCell);
  121.  
  122. //ITEM PRICE
  123. var htmlItemDescription = item.itemDescription;
  124. var htmlConverter = document.createElement("td");
  125. htmlConverter.innerHTML = htmlItemDescription;
  126. tr.appendChild(htmlConverter);
  127.  
  128. // ITEM STOCK
  129. var itemStock = document.createElement("td");
  130. itemStock.textContent = item.itemStock;
  131. tr.appendChild(itemStock);
  132.  
  133. // IN CART FIXME
  134. var itemsInCart = document.createElement("td");
  135. var inputField = document.createElement("span");
  136. inputField.className = "inputInCard";
  137. itemsInCart.appendChild(inputField);
  138. itemsInCart.className ="itemsInCart";
  139. tr.appendChild(itemsInCart);
  140.  
  141. // ADD TO CART
  142. var addToCart = document.createElement("td");
  143. var addToCartButton = document.createElement("BUTTON");
  144. addToCartButton.className = "addItemButton";
  145. var buttonText = document.createTextNode("Add Item");
  146. addToCartButton.appendChild(buttonText);
  147. addToCart.appendChild(addToCartButton);
  148. tr.appendChild(addToCart);
  149.  
  150. $(addToCartButton).click(function () {
  151. //FIXME
  152. });
  153.  
  154. // ITEM ID
  155. var itemID = document.createElement("td");
  156. itemID.textContent = item.itemID;
  157. tr.appendChild(itemID);
  158.  
  159. tableBody.appendChild(tr);
  160. })(i);
  161. }
  162. }
  163.  
  164.  
  165. function eow() {
  166.  
  167. $('.minus-button').click(function(e) {
  168. e.preventDefault();
  169. var $this = $(this);
  170. var $input = $this.closest('div').find('input');
  171. var value = parseInt($input.val());
  172. var $getPrice = $this.closest('div').next('.price');
  173. var $totalPrice = $this.closest('div').closest('div').next(".total-price");
  174. var $prices = document.getElementsByClassName('.price');
  175.  
  176. const $originalPrice = parseInt($getPrice.attr('name'));
  177.  
  178. if (value >= 1) {
  179. value--;
  180. } else {
  181. value = 0;
  182. }
  183.  
  184. var valueNumber = value*$originalPrice;
  185. var valueString = valueNumber + "kr.";
  186.  
  187. $input.val(value);
  188. $getPrice.replaceWith("<div class='price' name='" + $originalPrice + "'>" + valueString + "</div>");
  189.  
  190.  
  191.  
  192. });
  193.  
  194. $('.plus-button').click(function(e) {
  195. e.preventDefault();
  196. var $this = $(this);
  197. var $input = $this.closest('div').find('input');
  198. var value = parseInt($input.val());
  199. var $getPrice = $this.closest('div').next('.price');
  200. var $originalPrice = parseInt($getPrice.attr('name'));
  201.  
  202. if (value < 100) {
  203. value++;
  204. } else {
  205. value =100;
  206. }
  207.  
  208. var valueNumber = value*$originalPrice;
  209. var valueString = valueNumber + "kr.";
  210.  
  211. $input.val(value);
  212. $getPrice.replaceWith("<div class='price' name='" + $originalPrice + "'>" + valueString + "</div>");
  213. });
  214.  
  215. $(".input").on("input", function () {
  216.  
  217.  
  218. var $this = $(this);
  219. var $input = $this;
  220. var value = parseInt($input.val());
  221. var $getPrice = $this.closest('div').next('.price');
  222. var $price = parseInt($getPrice.text());
  223. var $originalPrice = parseInt($getPrice.attr('name'));
  224.  
  225. var valueNumber = value * $originalPrice;
  226. var valueString = valueNumber + "kr.";
  227.  
  228. var c = parseFloat($input.val().replace(",","."));
  229. if(!isNaN(c)) {
  230. $input.val(c);
  231. } else {
  232. $input.val(0);
  233. }
  234. if($getPrice.text() !== "NaNkr.") {
  235. $input.val(value);
  236. }
  237. $getPrice.replaceWith("<div class='price' name='" + $originalPrice + "'>" + valueString + "</div>");
  238. });
  239.  
  240. }
  241.  
  242.  
  243.  
  244.  
  245. /////////////////////////////////////////////////////
  246. // Code from slides
  247. /////////////////////////////////////////////////////
  248.  
  249. /**
  250. * A function that can add event listeners in any browser
  251. */
  252. function addEventListener(myNode, eventType, myHandlerFunc) {
  253. if (myNode.addEventListener)
  254. myNode.addEventListener(eventType, myHandlerFunc, false);
  255. else
  256. myNode.attachEvent("on" + eventType,
  257. function (event) {
  258. myHandlerFunc.call(myNode, event);
  259. });
  260. }
  261.  
  262. var http;
  263. if (!XMLHttpRequest)
  264. http = new ActiveXObject("Microsoft.XMLHTTP");
  265. else
  266. http = new XMLHttpRequest();
  267.  
  268. function sendRequest(httpMethod, url, body, responseHandler) {
  269. http.open(httpMethod, url);
  270. if (httpMethod == "POST") {
  271. http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  272. }
  273. http.onreadystatechange = function () {
  274. if (http.readyState == 4 && http.status == 200) {
  275. responseHandler(http.responseText);
  276. }
  277. };
  278. http.send(body);
  279. }
Advertisement
Add Comment
Please, Sign In to add comment