-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload-popup.js
112 lines (96 loc) · 3.82 KB
/
load-popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*global document, window, alert, console, require, google, gapi, moment, XMLHttpRequest, $*/
/*jslint nomen: true */
var createDeleteButton, initAutocomplete, changeSaveToUpdate, cleanPath;
// Add li nodes dynamically according to the number of saved routes
function createList(all_routes) {
"use strict";
// Get the ul node
var ul = document.getElementById("myList");
// Check if the nodes have already been added before
if (!ul.hasChildNodes()) {
all_routes.forEach(function (routex) {
//Create a <li> node for each route
var node = document.createElement("li"),
text = document.createElement("text"),
date = document.createElement("date");
node.setAttribute("id", routex._id);
// Add route name to the node <li>
if (routex.route !== null) {
routex.route.forEach(function (location, i) {
if (i >= 1) {
text.appendChild(document.createTextNode(", " + location.name));
} else {
text.appendChild(document.createTextNode(location.name));
}
});
}
// Add route date to the node <li>
date.appendChild(document.createTextNode(moment(routex.created_at).fromNow()));
node.append(text);
node.append(date);
createDeleteButton(node);
node.onclick = function () {
//Clear map from previous calculations
initAutocomplete.clearMap();
document.getElementsByClassName('slideshow-container')[0].style.visibility = 'hidden';
document.getElementById('directions-panel').style.visibility = "hidden";
document.getElementById('left-panel').style.visibility = "hidden";
document.getElementById('toggleOff').style.visibility = 'hidden';
document.getElementById('toggleOn').style.visibility = 'hidden';
initAutocomplete.callUpdateButtons(routex.route);
// initAutocomplete.callCalculateAndDisplay(routex.route);
// cleanPath(routex.route);
window.location.href = '#';
changeSaveToUpdate(node);
cleanPath([]);
};
// Append route node <li> to <ul> with id="myList"
ul.appendChild(node);
});
} else {
console.log("Already contain");
}
}
// Gets all the paths by the user
function onLoad() {
"use strict";
var response = this.responseText,
all_routes = JSON.parse(response);
console.log(all_routes);
createList(all_routes);
}
// Prints error message
function onError() {
"use strict";
console.log('error receiving async AJAX call');
}
// Gets all the paths by the user
function getPaths() {
"use strict";
var req = new XMLHttpRequest(),
url = '/paths';
req.open('GET', url, true);
req.addEventListener('load', onLoad);
req.addEventListener('error', onError);
req.send();
}
// Checks if user is logged in, and load correct popup
function checkIfLoggedIn() {
"use strict";
// Signed in: load view paths popup
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
document.getElementById("view").setAttribute("href", "#popup2");
getPaths();
// Signed out: load sign-in popup
} else {
document.getElementById("view").setAttribute("href", "#popup1");
}
return false;
}
// var options = { weekday: 'long',
// year: 'numeric',
// month: 'long',
// day: 'numeric' ,
// hour: 'numeric',
// minute: 'numeric'};
// details.appendChild(document.createTextNode(new Date(routex.created_at).toLocaleString("en-US", options)));