-
Notifications
You must be signed in to change notification settings - Fork 394
/
tripit.js
182 lines (165 loc) · 4.74 KB
/
tripit.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* tripit.js - TripIt integration for OpenFlights
* Andrew Chen - achen.code on big-G's email service
*/
var CONST = {
DEBUG: false,
IMPORT_URL: "/php/submit.php",
// HARD_FAIL means "don't retry", whereas SOFT_FAIL means "try again later".
CODE_HARD_FAIL: -1,
CODE_ADDOK: 1,
};
/**
* Submit a segment import form
* @param segmentId TripIt segment ID to import
*/
function importFlight(segmentId) {
var form = $("#import" + segmentId);
if (form == null) {
// Shouldn't happen, but let's be defensive.
setStatus(segmentId, "Internal Error: Couldn't find segment " + segmentId);
return;
}
// Serialize the form into a request.
var params = form.serialize();
if (CONST.DEBUG) {
console.log("importFlight: params=" + params);
}
$.post(CONST.IMPORT_URL, params, importFlightComplete(segmentId));
}
/**
* Callback upon successful import.
* @param segmentId
*/
var importFlightComplete = function (segmentId) {
return function (data, responseText, jqXHR) {
var result = data.split(";"),
code = result[0],
text = result[1];
setStatus(segmentId, "<b>" + text + "</b>");
var showOverlay = false;
var overlayImage;
if (code == CONST.CODE_ADDOK) {
// Successful add; show checkmark.
showOverlay = true;
overlayImage = "Checkmark_green.80px.png";
} else if (code == CONST.CODE_HARD_FAIL) {
// Fatal error, don't try again.
showOverlay = true;
overlayImage = "Red_X.64px.png";
}
if (showOverlay) {
$("#import" + segmentId + " :input").attr("disabled", true);
$("#import" + segmentId).block({
message:
'<img style="height:64px; width: auto" src="/img/' +
overlayImage +
'">',
css: {
cursor: "default",
border: "none",
padding: "15px",
// Set this to #000 to add a dark box around the checkbox
backgroundColor: "transparent",
"-webkit-border-radius": "10px",
"-moz-border-radius": "10px",
// Set opacity to .5 or .6 if box enabled above.
opacity: 1,
color: "#fff",
},
overlayCSS: {
height: $("#segment" + segmentId).height(),
cursor: "default",
},
});
}
};
};
/**
* Shortcut for marking a segment as already imported when loading a TripIt list page.
* @param segmentId
*/
function markSegmentImported(segmentId) {
importFlightComplete(segmentId)(
CONST.CODE_ADDOK + ";Segment already imported.",
null,
null
);
}
/**
* Shortcut for marking a segment as invalid for import. This shouldn't normally happen.
* @param segmentId
*/
function markSegmentInvalid(segmentId) {
importFlightComplete(segmentId)(
CONST.CODE_HARD_FAIL + ";Insufficient data to import this segment.",
null,
null
);
}
/**
* Set the status field for a given segment to message.
* @param segmentId
* @param message
*/
function setStatus(segmentId, message) {
var statusSpan = document.getElementById("input_status" + segmentId);
if (statusSpan != null) {
statusSpan.innerHTML = message;
}
}
/**
* Import multiple segments at once. Under the covers, this still calls {@link #importFlight},
* so each segment will result in a call to the service to import.
* @param flights Array of flights to import.
*/
function importFlights(flights) {
for (var i = 0; i < flights.length; i++) {
importFlight(flights[i]);
}
}
/**
* Add a button to the DOM to import all segments for a given trip id.
* @param importAllButtonValue Text for the Import All button. This should be localized.
* @param tripId ID of the trip.
* @param segments Array of segments to be imported when this button is clicked.
*/
function addImportAllButton(importAllButtonValue, tripId, segments) {
var importAllDiv = document.getElementById("import_all_" + tripId);
if (importAllDiv == null) {
if (CONST.DEBUG) {
console.log(
"Couldn't find div to insert Import All button for trip " + tripId
);
}
return;
}
importAllDiv.innerHTML =
'<input type="button" onclick="importFlights(new Array(' +
segments +
'))" value="' +
importAllButtonValue +
'">';
}
/**
* Init method for the rendezvous instruction page.
*/
function rendezvousPageInit() {
$("#loginPathPartner").click(function () {
$("#loginPathSelection").hide();
$("#loginPathPartnerHelp").show();
});
$("#loginPathNative").click(function () {
window.location.href = "/php/tripit_rendezvous_start.php";
});
}
/**
* Popup a window with TripIt's login page. This is used for rendezvous with partner logins.
*/
function openTripItLogin() {
window.open(
"https://www.tripit.com/account/login",
"TripItLogin",
"width=1000,height=550,scrollbars=yes"
);
}