Mini Kabibi Habibi
DXTravel.DetailFlight = function(params) {
var flight = ko.observable();
var flightInStorage = $.parseJSON(sessionStorage.getItem("foundFlights"))[params.id];
var flightParams = {
itineraryId: flightInStorage.itineraryId,
outboundDate: flightInStorage.outboundDate,
inboundDate: flightInStorage.inboundDate
};
var arrayToObject = function(arr) {
var obj = {};
$.each(arr || [], function() {
var item = this;
obj[item.code] = {
name: item.name
};
});
return obj;
};
var objToArray = function(obj) {
var result = [];
for(prop in obj) {
result.push({
code: prop,
name: obj[prop].name,
departureDateTime: obj[prop].departureDateTime
});
}
return result;
};
var parseOutboundSegments = function(segments, resultFrom, resultTo) {
$.each(segments, function() {
var location = this;
var departureTime = Globalize.format(new Date(location.departureDateTime), "h:mm tt");
var arrivalDateTime = Globalize.format(new Date(location.arrivalDateTime), "h:mm tt");
if(location.departureStation == resultFrom.code) {
resultFrom.departureDateTime = departureTime;
}
else
connections[location.departureStation].departureDateTime = departureTime;
if(location.arrivalStation == resultTo.code)
resultFrom.arrivalDateTime = arrivalDateTime;
});
};
var parseInboundSegments = function(segments, resultFrom, resultTo) {
$.each(segments, function(index) {
var location = this;
var departureTime = Globalize.format(new Date(location.departureDateTime), "h:mm tt");
var arrivalDateTime = Globalize.format(new Date(location.arrivalDateTime), "h:mm tt");
if(location.departureStation == resultTo.code) {
resultTo.departureDateTime = departureTime;
} else {
if(connectionsReturn[location.departureStation])
connectionsReturn[location.departureStation].departureDateTime = departureTime;
}
if(location.arrivalStation == resultFrom.code || index == (segments.length - 1))
resultTo.arrivalDateTime = arrivalDateTime;
});
};
var connections = arrayToObject(flightInStorage.connections),
connectionsReturn = arrayToObject(flightInStorage.connectionsReturn),
durationInHours = Number(flightInStorage.durationInMin / 60).toFixed(1),
layoverInHours = Number(flightInStorage.layoverInMin / 60).toFixed(1);
DXTravel.FlightUtils.flightItinerary(flightParams, function(response){
var resultFrom = {
name: flightInStorage.from.name,
code: flightInStorage.from.code,
};
var resultTo = {
name: flightInStorage.to.name,
code: flightInStorage.to.code
};
parseOutboundSegments(response.details.outboundSegments.list, resultFrom, resultTo);
parseInboundSegments(response.details.inboundSegments.list, resultFrom, resultTo);
var result = {
price: flightInStorage.price,
from: resultFrom,
to: resultTo,
durationInHours: durationInHours,
layoverInHours: layoverInHours,
timeInAir: Number(durationInHours - layoverInHours).toFixed(1),
connections: objToArray(connections),
connectionsReturn: objToArray(connectionsReturn),
parchaseLink: "http://www.wego.com/api/flights/redirect.html?" +
"apiKey=767dd5399aee75200f49&" +
"bookingCode=" + flightInStorage.bookingCode + "&" +
"providerId=" + flightInStorage.providerId + "&" +
"dlfrom=" + flightInStorage.dlfrom + "&" +
"dlto=" + flightInStorage.dlto + "&" +
"instanceId=" + sessionStorage.getItem("wegoInstanceId") + "&" +
"ts_code=a7557"
};
flight(result);
});
var currentSearchCriteria = $.parseJSON(sessionStorage.getItem("searchCriteria"));
var model = {
back: function() {
DXTravel.app.navigate("FindFlight/" + flightInStorage.dlfrom + "/" +
flightInStorage.dlto + "/" +
currentSearchCriteria.outboundDate + "/" +
currentSearchCriteria.inboundDate);
},
flight: flight,
navigation: DXTravel.navigation,
viewShowing: DXTravel.viewShowing
};
return model;
};