mirror of
https://github.com/Ekokumppanit/Lentolaskuri.git
synced 2026-02-15 21:47:39 +00:00
Initial commit
This commit is contained in:
75
app/scripts/libs/airports.js
Normal file
75
app/scripts/libs/airports.js
Normal file
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
define([
|
||||
'jquery',
|
||||
'lodash',
|
||||
'config'
|
||||
], function ($, _, config) {
|
||||
var airportById = function (element, callback) {
|
||||
$.get('http://localhost:8000/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
|
||||
};
|
||||
});
|
||||
9
app/scripts/libs/gmaps.js
Normal file
9
app/scripts/libs/gmaps.js
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
// convert Google Maps into an AMD module
|
||||
define([
|
||||
'async!http://maps.google.com/maps/api/js?v=3&sensor=false'
|
||||
], function() {
|
||||
// return the gmaps namespace for brevity
|
||||
return window.google.maps;
|
||||
});
|
||||
10
app/scripts/libs/handlebar_helpers.js
Normal file
10
app/scripts/libs/handlebar_helpers.js
Normal file
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
define([
|
||||
'handlebars'
|
||||
], function (Handlebars) {
|
||||
Handlebars.registerHelper('displayFloat', function (num, precision) {
|
||||
precision = precision;
|
||||
return ''+(num.toFixed(precision)).replace('.', ',');
|
||||
});
|
||||
});
|
||||
120
app/scripts/libs/maps.js
Normal file
120
app/scripts/libs/maps.js
Normal file
@@ -0,0 +1,120 @@
|
||||
'use strict';
|
||||
|
||||
define([
|
||||
'require',
|
||||
'jquery',
|
||||
'lodash',
|
||||
'backbone'
|
||||
], function (require, $, _, Backbone) {
|
||||
var gmaps;
|
||||
|
||||
function glatlng(latlng) {
|
||||
if (_.isArray(latlng)) {
|
||||
return new gmaps.LatLng(latlng[0], latlng[1]);
|
||||
} else if (latlng instanceof Backbone.Model) {
|
||||
return new gmaps.LatLng(latlng.get('lat'), latlng.get('long'));
|
||||
} else {
|
||||
return new gmaps.LatLng(latlng.lat, latlng.long);
|
||||
}
|
||||
}
|
||||
|
||||
var maps = {};
|
||||
|
||||
var deferred = $.Deferred();
|
||||
|
||||
var i = 0;
|
||||
var Map = function (el, opts) {
|
||||
var num = i;
|
||||
deferred.done(function () {
|
||||
maps[num] = new gmaps.Map(el, {
|
||||
center: glatlng([0, 0]),
|
||||
mapTypeId: gmaps.MapTypeId.ROADMAP,
|
||||
zoom: 0,
|
||||
streetViewControl: false,
|
||||
mapTypeControl: false,
|
||||
draggable: false,
|
||||
scrollwheel: false,
|
||||
zoomControl: false
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
id: i++
|
||||
}
|
||||
};
|
||||
|
||||
var Line = function (attr) {
|
||||
var data;
|
||||
deferred.done(function () {
|
||||
attr = attr || {};
|
||||
attr.map = maps[attr.map.id];
|
||||
|
||||
data = new gmaps.Polyline(attr);
|
||||
});
|
||||
|
||||
return {
|
||||
clear: function () {
|
||||
deferred.done(function () {
|
||||
data.setPath([]);
|
||||
});
|
||||
},
|
||||
add: function (latlng) {
|
||||
deferred.done(function () {
|
||||
data.getPath().push(glatlng(latlng));
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
var Bounds = function (map) {
|
||||
var map;
|
||||
var i;
|
||||
var data;
|
||||
|
||||
var reset = function () {
|
||||
i = 0;
|
||||
map.setZoom(1);
|
||||
map.setCenter(glatlng([0, 0]));
|
||||
};
|
||||
|
||||
deferred.done(function () {
|
||||
data = new gmaps.LatLngBounds();
|
||||
map = maps[map.id];
|
||||
|
||||
reset();
|
||||
});
|
||||
|
||||
return {
|
||||
clear: function () {
|
||||
deferred.done(function () {
|
||||
reset();
|
||||
});
|
||||
},
|
||||
add: function (latlng) {
|
||||
deferred.done(function () {
|
||||
data.extend(glatlng(latlng));
|
||||
++i;
|
||||
});
|
||||
},
|
||||
use: function () {
|
||||
deferred.done(function () {
|
||||
if (i >= 2) {
|
||||
map.fitBounds(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
require(['libs/gmaps'], function (_gmaps) {
|
||||
gmaps = _gmaps;
|
||||
deferred.resolve();
|
||||
});
|
||||
|
||||
return {
|
||||
Map: Map,
|
||||
Line: Line,
|
||||
Bounds: Bounds
|
||||
};
|
||||
|
||||
});
|
||||
26
app/scripts/libs/math.js
Normal file
26
app/scripts/libs/math.js
Normal file
@@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
define([
|
||||
'config',
|
||||
'libs/airports'
|
||||
], function (cfg, airports) {
|
||||
var toRad = function toRad(n) {
|
||||
return n * Math.PI / 180;
|
||||
};
|
||||
|
||||
var haversine = function haversine(from, to) {
|
||||
var dLat = toRad(to.get('lat') - from.get('lat'));
|
||||
var dLon = toRad(to.get('long') - from.get('long'));
|
||||
var lat1 = toRad(from.get('lat'));
|
||||
var lat2 = toRad(to.get('lat'));
|
||||
|
||||
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
|
||||
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return cfg.R * c;
|
||||
};
|
||||
|
||||
return {
|
||||
toRad: toRad,
|
||||
haversine: haversine
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user