]> gitweb.ps.run Git - lolstats/blob - html/script.js
changes
[lolstats] / html / script.js
1 function getRegions() {
2   $.ajax("/lol/regions")
3     .done((data) => {
4       app.regions = JSON.parse(data);
5       setTimeout(() => $("#regionselect").val(app.region), 0);
6     });
7 }
8 function getChampions() {
9   $.ajax("/lol/champions")
10     .done((data) => {
11       app.champions = JSON.parse(data);
12     });
13 }
14 function getMatchProps() {
15   return [
16     {text: "Champion", name: "champ"},
17     {text: "Lane", name: "lane"},
18   ];
19 }
20 function getMatches() {
21   $.ajax("/lol/matches?region=" + app.region + "&summoner=" + app.summoner)
22     .done((data) => {
23       app.matches = data;
24       for (p in app.matches[0]) {
25         app.matchprops.push({
26           name: p,
27           text: p.toUpperCase(),
28         });
29       }
30     });
31 }
32 function getInfo() {
33   app.summoner = $("#nameinput").val();
34   app.region = $("#regionselect").val();
35
36 function setUrl() {
37   window.history.pushState("object or string", "Title",
38     "/lol?summoner=" +
39     app.summoner +
40     "&region=" +
41     app.region +
42     "&view=" +
43     app.view);
44 }
45 function toggleStartUp() {
46   $("#start").removeClass("down");
47   $("#start").addClass("up");
48 }
49 function toggleStartDown() {
50   $("#start").removeClass("up");
51   $("#start").addClass("down");
52 }
53 function slideStartUp() {
54   $("#start").removeClass("slidedown");
55   $("#start").addClass("slideup");
56 }
57 function slideStartDown() {
58   $("#start").removeClass("slideup");
59   $("#start").addClass("slidedown");
60 }
61 function setView(view) {
62   console.log("Setting view to " + view);
63   if (view == "history") {
64     toggleStartUp();
65     $("#matchhistory").show();
66   } else if (view == "stats") {
67     toggleStartUp();
68     $("#stats").show();
69   }
70   app.view = view;
71 }
72 function changeView(view) {
73   let oldView = app.view;
74   console.log("changing view from " + oldView + " to " + view);
75   if (oldView == "start") {
76     if (view == "history") {
77       slideStartUp();
78       $("#matchhistory").show("blind", { direction: "down" }, 300);
79     } else if (view == "stats") {
80       slideStartUp();
81       $("#stats").show("blind", { direction: "down" }, 300);
82     }
83   } else if (oldView == "history") {
84     if (view == "start") {
85       $("#matchhistory").hide("blind", { direction: "down" }, 300);
86       slideStartDown();
87     } else if (view == "stats") {
88       $("#stats").show("blind", { direction: "right" });
89       $("#matchhistory").hide("blind", { direction: "left" });
90     }
91   } else if (oldView == "stats") {
92     if (view == "start") {
93       $("#stats").hide("blind", { direction: "down" }, 300);
94       slideStartDown();
95     } else if (view == "history") {
96       $("#matchhistory").show("blind", { direction: "left" });
97       $("#stats").hide("blind", { direction: "right" });
98     }
99   }
100   app.view = view;
101 }
102 let app = new Vue({
103   el: '#app',
104   data: {
105     summoner: "",
106     region: "",
107     view: "",
108     regions: [],
109     champions: [],
110     matchprops: [],
111     matches: [],
112   },
113   methods: {
114     submit: function() {
115       getInfo();
116       if (app.view == "start")
117         changeView("history");
118       setUrl();
119     },
120     historyToStats: function() {
121       changeView("stats");
122       setUrl();
123     },
124     statsToHistory: function() {
125       changeView("history");
126       setUrl();
127     },
128     refreshHistory: function() {
129       getMatches();
130     },
131   },
132 });
133
134 function parseUrl() {
135   let url = new URL(window.location.href);
136   if (url.searchParams.has("summoner")) {
137     app.summoner = url.searchParams.get("summoner");
138     $("#nameinput").val(app.summoner);
139   }
140   if (url.searchParams.has("region")) {
141     app.region = url.searchParams.get("region");
142   }
143   if (url.searchParams.has("view")) {
144     let view = url.searchParams.get("view");
145     if (view != "start" && view != "history" && view != "stats") {
146       view = "start";
147     }
148     return view;
149   } else {
150     return "start";
151   }
152 }
153
154 window.addEventListener('popstate', () => {
155   let view = parseUrl();
156   changeView(view);
157 });
158 window.addEventListener('load', async () => {
159   let view = parseUrl();
160   setView(view);
161   getRegions();
162   getChampions();
163 });