Mini Kabibi Habibi
DXTravel.HotDeals = function(params) {
var NYImages = [
"images/new_york/01.jpg",
"images/new_york/02.jpg",
"images/new_york/03.jpg",
"images/new_york/04.jpg",
"images/new_york/05.jpg",
"images/new_york/06.jpg",
"images/new_york/07.jpg",
"images/new_york/08.jpg",
"images/new_york/09.jpg",
"images/new_york/10.jpg"
];
var flightsMapFn = function(flights) {
model.loading(false);
if(!flights.itineraries || !flights.itineraries.length) {
model.notResults(true);
return;
}
var imageIndex = 0;
var mappedFlights = $.map(flights.itineraries, function(item, index) {
var currencyKoeff = 1,
from,
to,
price,
layover,
duration,
image = NYImages[imageIndex];
imageIndex++;
if(imageIndex >= NYImages.length)
imageIndex = 0;
if(item.price) {
if(item.price.currencyCode == "RUB")
currencyKoeff = 31;
if(item.price.currencyCode == "INR")
currencyKoeff = 55;
price = '$ ' + Number(item.price.totalAmount / currencyKoeff).toFixed(2);
}
if (item.outboundInfo) {
var outboundStartAirport = item.outboundInfo.airports[0];
var outboundAirportEnd = item.outboundInfo.airports[item.outboundInfo.airports.length - 1];
$.each(item.location, function (index, locationItem) {
if (locationItem.code == outboundStartAirport)
from = locationItem;
if (locationItem.code == outboundAirportEnd)
to = locationItem;
});
layover = item.outboundInfo.layoverInMin;
duration = item.outboundInfo.durationInMin;
}
return {
id: index,
image: image,
price: price,
from: from,
to: to,
layover: Number(layover / 60).toFixed(1),
duration: Number(duration / 60).toFixed(1),
timeInAir: Number((duration - layover) / 60).toFixed(1),
}
});
model.flights(mappedFlights.slice(0, 30));
sessionStorage.setItem("foundFlights", JSON.stringify(mappedFlights));
};
var SEARCH_START_DATE_STRING = Globalize.format(new Date($.now() + 15 * DXTravel.MS_PER_DAY), "yyyy-MM-dd"),
SEARCH_END_DATE_STRING = Globalize.format(new Date($.now() + 22 * DXTravel.MS_PER_DAY), "yyyy-MM-dd");
var model = {
handlePurchaseClick: function() {
var url = "http://demos.devexpress.com/DXTREME/Travel/DXTravel.Web/#FindFlight/NYC/SEA/" + SEARCH_START_DATE_STRING + "/" + SEARCH_END_DATE_STRING;
if(window.WinJS) {
Windows.System.Launcher.launchUriAsync(new Windows.Foundation.Uri(url));
} else {
window.open(url);
}
},
flights: ko.observableArray(),
selectedFlight: ko.observable(),
notResults: ko.observable(),
loading: ko.observable(true),
flightItemClick: function(e) {
if(model.selectedFlight()) {
model.selectedFlight().selected(false);
}
e.itemData.selected(true);
model.selectedFlight(e.itemData);
},
viewShown: function () {
var flights = $.parseJSON(sessionStorage.getItem("foundFlights")) || [];
if (!flights.length) {
DXTravel.FlightUtils.searchFlight({
from: 'SEA',
to: 'NYC',
outboundDate: SEARCH_START_DATE_STRING,
inboundDate: SEARCH_END_DATE_STRING
}, flightsMapFn);
}
else {
model.flights(flights.slice(0, 30));
model.loading(false);
}
}
};
model.flights.subscribe(function (items) {
$.map(items, function (item) {
item.selected = ko.observable();
});
if (items.length && !model.selectedFlight()) {
items[0].selected(true);
model.selectedFlight(items[0]);
}
});
return model;
};