Files
Lentolaskuri/app/scripts/libs/airports.js
Ismo Vuorinen b0258437cd - Added section on configuration to README.md
- Made the application more configurable by the user
- Made installation easier
- API location is now dynamic based on window.location
- Cleaned up some older code that was pointing to dev stuff
- Now includes the SQL-table required to run the application
- Application now exposes config to console for easier debugging
- Search now supports user specified table name
- Updated the OpenFlights airport data file source
2013-11-19 15:34:42 +02:00

76 lines
1.8 KiB
JavaScript

'use strict';
define([
'jquery',
'lodash',
'config'
], function ($, _, config) {
var airportById = function (element, callback) {
$.get( config.api + '/search.php?i=' + element.val(), null, function (data) {
callback(data[0]);
});
};
var data = function (term, page) {
return {
s: term
};
};
var results = function (data, page) {
return {results: data};
};
var findArea = function (icao) {
for (var i = 0; i < config.parameters.length; ++i) {
var param = config.parameters[i];
if (param.regex && icao.match(param.regex)) {
return i;
}
}
return config.parameters.length - 2;
};
var selectArea = function (fromCode, toCode) {
var outside = config.parameters.length - 2;
if (fromCode === outside && toCode === outside) {
return config.parameters.length - 1;
}
return Math.max(fromCode, toCode);
};
var selectRange = function (dist) {
for (var i = 0; i < config.distanceRanges.length; ++i) {
if (config.distanceRanges[i].min && dist <= config.distanceRanges[i].min) {
return i;
}
}
return 0;
};
// Should select "greater of two", finland to western europe -> west europe
// west europe to south europe -> south europe
var parameters = function (from, to, range) {
var i = selectArea(findArea(from.icao), findArea(to.icao));
var p = config.parameters[i];
return {
name: p.name,
co2factor: p.co2factor[range],
ltoCycle: p.ltoCycle[range],
load: p.load[range],
freight: p.freight[range]
};
};
return {
ajax: {
url: config.api + '/search.php',
dataType: 'json',
data: data,
results: results
},
airportById: airportById,
selectRange: selectRange,
parameters: parameters
};
});