Light12306/RwTicketAssistantV3/app/air/js/util/Filler.js
2015-07-03 21:04:37 +08:00

147 lines
4.1 KiB
JavaScript

var Filler = {
fill: function(container, data, adapter) {
var getData = function(key, element) {
var keys = key.split('.'),
filledData = data,
filledAdapter = adapter;
for (var i = 0; i < keys.length; i++) {
if (filledData !== undefined) {
filledData = filledData[keys[i]];
}
if (filledAdapter !== undefined) {
filledAdapter = filledAdapter[keys[i]];
}
}
if (filledAdapter !== undefined) {
filledData = filledAdapter(data, element);
}
if (filledData === undefined) {
return undefined;
}
return filledData;
};
var fillInnerDataHandler = function(element) {
var rules = element.attr('_innerData').split('|'),
rule;
for (var i = 0; i < rules.length; i++) {
rule = rules[i].split(':');
element.attr(rule[0], getData(rule[1], element));
}
};
if (container.attr('_innerData') !== undefined) {
fillInnerDataHandler(container);
}
$('[_innerData]', container).each(function() {
fillInnerDataHandler($(this));
});
var fillDataHandler = function(element) {
var filledData = getData(element.attr('_data'), element);
if (element.is('input')) {
element.val(filledData);
} else {
element.html(filledData);
}
};
if (container.attr('_data') !== undefined) {
fillDataHandler(container);
}
$('[_data]', container).each(function() {
fillDataHandler($(this));
});
},
loop: function(container, data, adapter) {
var templateElement = $($('[_template]', container)[0]),
element,
i;
container.empty();
for (i in data) {
element = templateElement.clone();
Filler.fill(element, data[i], adapter);
Filler.fill(element, {__id:i});
element.appendTo(container);
}
}
/*
ticketDetail: function(ticket, flightInfo, temp) {
if (!arguments[1]) {
flightInfo = FlightStorage.getQuery();
}
if (!arguments[2]) {
temp = false;
}
//填充详细机票信息
$('#theTicketDate').text(Util.date.formatToFull(Util.date.parse(ticket.date)));
$('#theTicketFrom').text(flightInfo.from);
$('#theTicketTo').text(flightInfo.to);
$('#theTicketAirline').text(ticket.airline);
$('#theTicketFlight').text(ticket.flight);
$('#theTicketFlighttype').text(ticket.flighttype1);
$('#theTicketPriceBig').text(ticket.price);
$('#theTicketLaunchtime').text(ticket.launchtime);
$('#theTicketZhundian').text(ticket.zhundian ? ticket.zhundian : '-');
$('#theTicketPriceSmall').text('¥' + ticket.price);
$('#theTicketLandtime').text(ticket.landtime);
$('#theTicketDiscount').text(parseInt(ticket.discount)/10 + '折');
$('#theTicketAfee').text(ticket.afee);
$('#theTicketBfee').text(ticket.bfee);
$('#theTicketTotal').text(new String(parseInt(ticket.price) + parseInt(ticket.afee) + parseInt(ticket.bfee)));
$('#ticketBuyIt').unbind('click').click(function() {
var buyUrl = ticket.url + '?' + ticket.date + config.urls.kuxunSuffix;
Statistics.trigger(16, encodeURIComponent(buyUrl));
chrome.tabs.create({url: buyUrl});
});
var lastShowedPrice = Storage.get('lastShowedPrice');
if (lastShowedPrice && ticket.price != parseInt(lastShowedPrice)) {
if (ticket.price > parseInt(lastShowedPrice)) {
$('#theTicketTrend').removeClass('fallTrend').addClass('riseTrend').show();
}
if (ticket.price < parseInt(lastShowedPrice)) {
$('#theTicketTrend').removeClass('riseTrend').addClass('fallTrend').show();
}
$('#theTicketTrend').show();
} else {
$('#theTicketTrend').hide();
}
if (!temp) {
Storage.set('lastShowedPrice', ticket.price);
}
},
ticketOthers: function(others, lowestPrice) {
var i = 0;
$('li', $('#otherTickets')).each(function() {
if (others[i]) {
var url = others[i].url + '?' + others[i].date + config.urls.kuxunSuffix;
$('a', $(this)).attr('href', url);
$('.otherTicketsDate', $(this)).text(Util.date.formatToShort(Util.date.parse(others[i].date)));
$('.otherTicketsPrice', $(this)).text('¥' + others[i].price);
if (parseInt(others[i].price) <= parseInt(lowestPrice)) {
$('.lower', $(this)).show();
} else {
$('.lower', $(this)).hide();
}
} else {
$(this).hide();
}
i++;
});
}*/
};