]> gitweb.ps.run Git - lolstats/blobdiff - html/script.js
changes
[lolstats] / html / script.js
index 92be4265c04c5984e51f1002301fb59dd83e2280..11052629613b6c2d3d9407ffaabb20073b74e19a 100644 (file)
@@ -1,10 +1,33 @@
-// Start Screen
-// ------------
 function getRegions() {
 function getRegions() {
-  return [ "euw", "na", "kr", "br" ];
+  $.ajax("/lol/regions")
+    .done((data) => {
+      app.regions = JSON.parse(data);
+      setTimeout(() => $("#regionselect").val(app.region), 0);
+    });
 }
 function getChampions() {
 }
 function getChampions() {
-  return [ "Aatrox", "Annie", "Braum" ];
+  $.ajax("/lol/champions")
+    .done((data) => {
+      app.champions = JSON.parse(data);
+    });
+}
+function getMatchProps() {
+  return [
+    {text: "Champion", name: "champ"},
+    {text: "Lane", name: "lane"},
+  ];
+}
+function getMatches() {
+  $.ajax("/lol/matches?region=" + app.region + "&summoner=" + app.summoner)
+    .done((data) => {
+      app.matches = data;
+      for (p in app.matches[0]) {
+        app.matchprops.push({
+          name: p,
+          text: p.toUpperCase(),
+        });
+      }
+    });
 }
 function getInfo() {
   app.summoner = $("#nameinput").val();
 }
 function getInfo() {
   app.summoner = $("#nameinput").val();
@@ -12,76 +35,129 @@ function getInfo() {
 } 
 function setUrl() {
   window.history.pushState("object or string", "Title",
 } 
 function setUrl() {
   window.history.pushState("object or string", "Title",
-    "/?summoner=" +
+    "/lol?summoner=" +
     app.summoner +
     "&region=" +
     app.region +
     "&view=" +
     app.view);
 }
     app.summoner +
     "&region=" +
     app.region +
     "&view=" +
     app.view);
 }
-
-function toggleStart(up) {
-  $("#start").removeClass(up ? "down" : "up");
-  $("#start").addClass(up ? "up" : "down");
+function toggleStartUp() {
+  $("#start").removeClass("down");
+  $("#start").addClass("up");
+}
+function toggleStartDown() {
+  $("#start").removeClass("up");
+  $("#start").addClass("down");
 }
 }
-function slideStart() {
+function slideStartUp() {
+  $("#start").removeClass("slidedown");
   $("#start").addClass("slideup");
 }
   $("#start").addClass("slideup");
 }
-var app = new Vue({
+function slideStartDown() {
+  $("#start").removeClass("slideup");
+  $("#start").addClass("slidedown");
+}
+function setView(view) {
+  console.log("Setting view to " + view);
+  if (view == "history") {
+    toggleStartUp();
+    $("#matchhistory").show();
+  } else if (view == "stats") {
+    toggleStartUp();
+    $("#stats").show();
+  }
+  app.view = view;
+}
+function changeView(view) {
+  let oldView = app.view;
+  console.log("changing view from " + oldView + " to " + view);
+  if (oldView == "start") {
+    if (view == "history") {
+      slideStartUp();
+      $("#matchhistory").show("blind", { direction: "down" }, 300);
+    } else if (view == "stats") {
+      slideStartUp();
+      $("#stats").show("blind", { direction: "down" }, 300);
+    }
+  } else if (oldView == "history") {
+    if (view == "start") {
+      $("#matchhistory").hide("blind", { direction: "down" }, 300);
+      slideStartDown();
+    } else if (view == "stats") {
+      $("#stats").show("blind", { direction: "right" });
+      $("#matchhistory").hide("blind", { direction: "left" });
+    }
+  } else if (oldView == "stats") {
+    if (view == "start") {
+      $("#stats").hide("blind", { direction: "down" }, 300);
+      slideStartDown();
+    } else if (view == "history") {
+      $("#matchhistory").show("blind", { direction: "left" });
+      $("#stats").hide("blind", { direction: "right" });
+    }
+  }
+  app.view = view;
+}
+let app = new Vue({
   el: '#app',
   data: {
     summoner: "",
     region: "",
   el: '#app',
   data: {
     summoner: "",
     region: "",
-    view: "start",
-    regions: getRegions(),
-    champions: getChampions(),
+    view: "",
+    regions: [],
+    champions: [],
+    matchprops: [],
+    matches: [],
   },
   methods: {
     submit: function() {
       getInfo();
   },
   methods: {
     submit: function() {
       getInfo();
-      if (app.view == "start") {
-        app.view = "history";
-        setUrl();
-        slideStart();
-        $("#matchhistory").show("slide", { direction: "down" }, 300);
-      } else {
-        setUrl();
-      }
+      if (app.view == "start")
+        changeView("history");
+      setUrl();
     },
     historyToStats: function() {
     },
     historyToStats: function() {
-      app.view = "stats";
+      changeView("stats");
       setUrl();
       setUrl();
-      $("#stats").show("blind", { direction: "right" });
-      $("#matchhistory").hide("blind", { direction: "left" });
     },
     statsToHistory: function() {
     },
     statsToHistory: function() {
-      app.view = "history";
+      changeView("history");
       setUrl();
       setUrl();
-      $("#matchhistory").show("blind", { direction: "left" });
-      $("#stats").hide("blind", { direction: "right" });
+    },
+    refreshHistory: function() {
+      getMatches();
     },
   },
 });
 
     },
   },
 });
 
-// Check for URL parameters
-let url = new URL(window.location.href);
-if (url.searchParams.has("summoner")) {
-  app.summoner = url.searchParams.get("summoner");
-  $("#nameinput").val(app.summoner);
-}
-if (url.searchParams.has("region")) {
-  app.region = url.searchParams.get("region");
-  $("#regionselect").val(app.region);
-}
-if (url.searchParams.has("view")) {
-  let view = url.searchParams.get("view");
-  if (view == "history") {
-    toggleStart(true);
-    $("#matchhistory").show();
+function parseUrl() {
+  let url = new URL(window.location.href);
+  if (url.searchParams.has("summoner")) {
+    app.summoner = url.searchParams.get("summoner");
+    $("#nameinput").val(app.summoner);
   }
   }
-  else if (view == "stats") {
-    toggleStart(true);
-    $("#stats").show();
+  if (url.searchParams.has("region")) {
+    app.region = url.searchParams.get("region");
+  }
+  if (url.searchParams.has("view")) {
+    let view = url.searchParams.get("view");
+    if (view != "start" && view != "history" && view != "stats") {
+      view = "start";
+    }
+    return view;
+  } else {
+    return "start";
   }
 }
   }
 }
-// ------------
+
+window.addEventListener('popstate', () => {
+  let view = parseUrl();
+  changeView(view);
+});
+window.addEventListener('load', async () => {
+  let view = parseUrl();
+  setView(view);
+  getRegions();
+  getChampions();
+});