From 63163b684e0a6c77a5ae59e64c5b9e64782c56d7 Mon Sep 17 00:00:00 2001 From: hung Date: Sat, 2 Mar 2019 22:19:25 +0100 Subject: [PATCH] Implement drag and drop functions --- dutchmanFiles/loader.js | 268 +++++++++++++++++++++++----------------- pages/order.html | 46 +++++-- scripts/dragndrop.js | 77 ++++++++++++ scripts/plusminus.js | 44 +++++++ styles/order.css | 83 +++++++++++-- 5 files changed, 383 insertions(+), 135 deletions(-) create mode 100644 scripts/dragndrop.js create mode 100644 scripts/plusminus.js diff --git a/dutchmanFiles/loader.js b/dutchmanFiles/loader.js index 0648400..a746337 100644 --- a/dutchmanFiles/loader.js +++ b/dutchmanFiles/loader.js @@ -12,7 +12,11 @@ var DB = ("DBLoaded.js"); var DB2 = ("Beverages.js"); +var beerDrinks = []; +var whiskeyDrinks = []; +var wineDrinks = []; +$( document ).ready(getDrinks); //Done by Lars function allUserNames() { @@ -27,7 +31,9 @@ function allUserNames() { function loadBeverages(){ //creates listarray with all drinktypes var alldrinktypes = []; - for (i = 0; i < DB2.spirits.length; i++) { + // var len = DB2.spirits.length; + var len = 50; + for (i = 0; i < len; i++) { alldrinktypes.push(DB2.spirits[i].varugrupp); } //Removes dupliactes from array @@ -43,77 +49,37 @@ function loadBeverages(){ //Creates divs according to how many drinks that you set "sliceddrinkList to". //that need to be shown -window.onload = createDrinkDivs; -function createDrinkDivs(drink){ - var slicedDrinkList = []; - //slice generates an error in the log but still works. - slicedDrinkList = drink.slice(0, 50); - - console.log("number of beers ===> " + drink.length); - - //Using join to convert array to string - var firstDrinkString = drink[1].join(); - var remove; - var drinkInfo; - var drinkDiv = document.createElement('div'); - - //if the string includes Ale - if(firstDrinkString.includes("Ale") == true){ - remove = "whiskeyDiv"; - eraseFunction(remove); - remove = "wineDiv"; - eraseFunction(remove); - - - for(i=0; i + + + @@ -19,29 +22,48 @@ Wine
+
+
    -
    - - +
+

tab

-
    -
  1. Beer A, 100 kr
  2. -
  3. Beer B, 50 kr
  4. -
- Total: 150 -
- + + +
+
    + +
-
+ ondragstart="drag(event)" width="336" height="69">
--> + \ No newline at end of file diff --git a/scripts/dragndrop.js b/scripts/dragndrop.js new file mode 100644 index 0000000..1015caa --- /dev/null +++ b/scripts/dragndrop.js @@ -0,0 +1,77 @@ +//======================================================================================================== +/* This function is called when a beverage is dropped on the Tab + */ +function itemsOnDrop(event) { + var id = localStorage.getItem("draggedId"); + var name = localStorage.getItem("draggedName"); + + addDraggedItemToTab(id, name); +} + +//======================================================================================================== +/* This function is called when a beverage is dragged over the Tab + */ +function itemsOnDragOver(event) { + event.preventDefault(); + event.stopPropagation(); + + // console.log("drag over"); +} + +//======================================================================================================== +/* This function is called when the dragged beverage enters the Tab + */ +function itemsOnDragEnter(event) { + event.preventDefault(); + event.stopPropagation(); + + // console.log("drag enter"); +} + +//======================================================================================================== +/* This function is called when the dragged beverage leaves the Tab + */ +function itemsOnDragLeave(event) { + event.preventDefault(); + event.stopPropagation(); + + // console.log("drag leave"); +} + +//======================================================================================================== +/* This function adds the dragged beverage to the Tab. It gets the Id and the name of that beverage. + */ +function addDraggedItemToTab (id, name) { + console.log("id " + id); + var noItemsInTab = localStorage.getItem("noItemsInTab"); + + if(noItemsInTab == null) + noItemsInTab = 1; + else + { + noItemsInTab = noItemsInTab + 1; + localStorage.setItem("noItemsInTab", noItemsInTab); + } + + var currentNumber = noItemsInTab - 1; + var matchedString = id.match(/(\w+)\-(\d+)/); + var type = matchedString[1]; + var drinkId = matchedString[2]; + + console.log("Type " + type); + console.log("drinkId " + drinkId); + + $("#items").append(` +
  • + +

    `+name+`

    +
    + + 1 + +
    +

    100

    +

    SEK

    +
  • + `); +} \ No newline at end of file diff --git a/scripts/plusminus.js b/scripts/plusminus.js new file mode 100644 index 0000000..c660a11 --- /dev/null +++ b/scripts/plusminus.js @@ -0,0 +1,44 @@ +//======================================================================================================== +/* This function is called when the plus button (in Tab) is clicked + */ +function plusQuantity(element) { + var id = element.id; + // console.log(id); + var s = id.match(/item\-\d+/); + // console.log(s[0]); + $("#" + s + "-quantity").text(function(_, oldText) { + var oldQuantity = parseInt(oldText); + var newQuantity = oldQuantity + 1; + + updatePrice("#" + s + "-price", newQuantity * 100); + + return newQuantity.toString(); + }); +} + + +//======================================================================================================== +/* This function is called when the minus button (in Tab) is clicked + */ +function minusQuantity(element) { + var id = element.id; + // console.log(id); + var s = id.match(/item\-\d+/); + // console.log(s[0]); + $("#" + s + "-quantity").text(function(_, oldText) { + var oldQuantity = parseInt(oldText); + var newQuantity = (oldQuantity-1) < 0 ? 0 : (oldQuantity-1); + + updatePrice("#" + s + "-price", newQuantity * 100); + + return newQuantity.toString(); + }); +} + +//======================================================================================================== +/* This function updates the price corresponding to the params idQuery (the string for querying) and + * newPrice (the new price to be updated to). + */ +function updatePrice(idQuery, newPrice) { + $(idQuery).text(newPrice.toString()); +} \ No newline at end of file diff --git a/styles/order.css b/styles/order.css index d4f82a1..fe9bd37 100644 --- a/styles/order.css +++ b/styles/order.css @@ -40,14 +40,19 @@ align-items: center; } .beerDiv { - display: flex; + /* display: inline-block; */ + float: left; + /* line-height: 150px; */ + text-align: center; border: solid black; height: 150px; width: 150px; + margin-right: 15px; + margin-bottom: 15px; justify-content: center; align-items: center; background-image: url('../images/beer.jpg'); - background-image:opacity: 0.5; + /* opacity: 0.5; */ background-size: contain; font-size: large; color: white; @@ -64,15 +69,18 @@ } .whiskeyDiv { - display: flex; + float: left; + /* line-height: 150px; */ + text-align: center; border: solid black; height: 150px; width: 150px; + margin-right: 15px; + margin-bottom: 15px; justify-content: center; align-items: center; background-image: url('../images/whiskey.jpeg'); - padding-bottom: - background-image:opacity: 0.5; + /* opacity: 0.5; */ background-size: contain; font-size: large; color: white; @@ -89,14 +97,18 @@ } .wineDiv { - display: flex; + float: left; + /* line-height: 150px; */ + text-align: center; border: solid black; height: 150px; width: 150px; + margin-right: 15px; + margin-bottom: 15px; justify-content: center; align-items: center; background-image: url('../images/wine.jpg'); - background-image:opacity: 0.5; + /* opacity: 0.5; */ background-size: contain; font-size: large; color: white; @@ -148,11 +160,11 @@ flex-grow: 2; height: 10%; width: 100%; - } .pay-button { height: 50px; + line-height: 50px; background-color: darkred; } @@ -169,3 +181,58 @@ text-align: end; color: white; } + +#tab-items { + height: 600px; + overflow-y: scroll; + overflow-x: hidden; +} + +#tab-items ul{ + list-style: none; +} + +.tab-item { + float: left; + /* background-color: rgba(0, 0, 0, 0.7); */ + border: 1px solid #000; +} + +.float-left { + float: left; +} + +.float-right { + float: right; +} + +.tab-item-img { + +} + +.tab-item-name { + width: 220px; + height: 50px; + font-family:sans-serif; + margin-right: 40px; +} + +.tab-item-name p { + width: 220px; + height: 50px; + display: table-cell; + vertical-align:middle; + text-align:center; + color: #000; + text-decoration: none; +} + +.tab-item-control { + line-height: 50px; + margin-right: 20px; +} + +.tab-item-price { + width: 60px; + margin-right: 10px; +} \ No newline at end of file