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