format date/time from a unix timestamp we get from backend
[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 {
25         for (var i = 0; i < aResult.length; i++) {
26           var litem = document.createElement("li");
27           litem.textContent = dtformat(aResult[i]["time_created"]) + " - ";
28           var llink = document.createElement("a");
29           llink.setAttribute("href", gBackendURL + "/track_gpx?id=" + aResult[i]["id"]);
30           llink.textContent = aResult[i]["comment"];
31           litem.appendChild(llink);
32           if (aResult[i]["devicename"]) {
33             litem.appendChild(document.createTextNode(" (" + aResult[i]["devicename"] +  ")"));
34           }
35           tlist.appendChild(litem);
36         }
37       }
38     }
39   );
40 }
41
42 function hideLibrary() {
43   document.getElementById("libraryArea").classList.add("hidden");
44 }
45
46 function dtformat(aUnixTimestamp) {
47   // Library display time format: YYYY-MM-DD HH:mm
48   // Note that JS has millisecond timestamps while standard/unix has seconds.
49   var tsDate = new Date(aUnixTimestamp * 1000);
50   // Note that .getUTCMonth() returns a number between 0 and 11 (0 for January)!
51   return tsDate.getUTCFullYear() + "-" +
52          (tsDate.getUTCMonth() < 9 ? "0" : "") + (tsDate.getUTCMonth() + 1 ) + "-" +
53          (tsDate.getUTCDate() < 10 ? "0" : "") + tsDate.getUTCDate() + " " +
54          (tsDate.getUTCHours() < 10 ? "0" : "") + tsDate.getUTCHours() + ":" +
55          (tsDate.getUTCMinutes() < 10 ? "0" : "") + tsDate.getUTCMinutes();
56 }
57