add an indicator when login fails
[lantea.git] / js / library.js
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 function showLibrary() {
6   document.getElementById("libraryArea").classList.remove("hidden");
7   var tlist = document.getElementById("libTrackList");
8   while (tlist.firstChild) { tlist.removeChild(tlist.firstChild); }
9   var litem = document.createElement("li");
10   var load_img = document.createElement("img");
11   load_img.setAttribute("src", "style/loading_action.png");
12   litem.appendChild(load_img);
13   litem.textContent = "Loading list...";
14   litem.id = "libLoadingItem";
15   tlist.appendChild(litem);
16   fetchBackend("tracks", "GET", null,
17     function(aResult, aStatusCode) {
18       document.getElementById("libLoadingItem").classList.add("hidden");
19       if (aStatusCode >= 400) {
20         var litem = document.createElement("li");
21         litem.textContent = "Error: " + aResult;
22         tlist.appendChild(litem);
23       }
24       else if (!aResult.length) {
25         var litem = document.createElement("li");
26         litem.textContent = "No tracks uploaded yet.";
27         tlist.appendChild(litem);
28       }
29       else {
30         for (var i = 0; i < aResult.length; i++) {
31           var litem = document.createElement("li");
32           litem.textContent = dtformat(aResult[i]["time_created"]) + " - ";
33           var llink = document.createElement("a");
34           llink.setAttribute("href", gBackendURL + "/track_gpx?id=" + aResult[i]["id"]);
35           llink.textContent = aResult[i]["comment"];
36           litem.appendChild(llink);
37           if (aResult[i]["devicename"]) {
38             litem.appendChild(document.createTextNode(" (" + aResult[i]["devicename"] +  ")"));
39           }
40           tlist.appendChild(litem);
41         }
42       }
43     }
44   );
45 }
46
47 function hideLibrary() {
48   document.getElementById("libraryArea").classList.add("hidden");
49 }
50
51 function dtformat(aUnixTimestamp) {
52   // Library display time format: YYYY-MM-DD HH:mm
53   // Note that JS has millisecond timestamps while standard/unix has seconds.
54   var tsDate = new Date(aUnixTimestamp * 1000);
55   // Note that .getUTCMonth() returns a number between 0 and 11 (0 for January)!
56   return tsDate.getUTCFullYear() + "-" +
57          (tsDate.getUTCMonth() < 9 ? "0" : "") + (tsDate.getUTCMonth() + 1 ) + "-" +
58          (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + " " +
59          (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
60          (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes();
61 }
62