Weatherscan/webroot/js/weather.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-09-08 19:22:46 +00:00
function WeatherManager() {
var mainloc
2018-09-08 19:22:46 +00:00
var mainMap, miniMap, slides,
dataMan, loops, //weatherAudio,
2018-09-08 19:22:46 +00:00
that = this;
$(function(){
2018-09-08 19:22:46 +00:00
// init marquees
function refreshMarquee () {
$('#marquee-container')
.marquee('destroy')
.marquee({speed: 200, pauseOnHover:true, delayBeforeStart:3000})
.on('finished', refreshMarquee);
}
2018-09-08 19:22:46 +00:00
refreshMarquee();
2018-09-08 19:22:46 +00:00
$('#marquee2').marquee({
speed: 170, pauseOnHover: true
});
2018-09-08 19:22:46 +00:00
weatherAudio.playCallback = function(tags) {
$('.track-info').text('playing "' + tags.title + '" by ' + tags.artist);
}
2018-09-08 19:22:46 +00:00
// this little guy runs the date and time
setInterval(
function () {
2018-09-08 19:22:46 +00:00
var today = new Date();
$('#date').text( today.toString().slice(4,10).trimRight() );
2018-09-08 19:22:46 +00:00
$('#time').text( today.toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, minute: 'numeric', second: 'numeric' }).replace(/ /g,'') );
}
, 1000);
2018-09-08 19:22:46 +00:00
initDataPull();
2018-09-08 19:22:46 +00:00
});
function initDataPull() {
// get the main location data
// on initialization ready, init local forecast loop
// on return of fully ready, begin display loops
2018-09-08 19:22:46 +00:00
// check the url for a specific location
var queryString = window.location.search;
2018-09-08 19:22:46 +00:00
if (queryString) {
$.getJSON("https://api.weather.com/v3/location/search?query="+queryString.split("?")[1]+"&language=en-US&format=json&apiKey=" + api_key, function(data) {
dataMan = createDataManager( data.location.latitude[0]+','+data.location.longitude[0] );
mainloc = data.location.city[0]
});
2018-09-08 19:22:46 +00:00
} else {
2018-09-08 19:22:46 +00:00
// get lat lon from user's ip
$.getJSON("http://ip-api.com/json/?callback=?", function(data) {
2018-09-08 19:22:46 +00:00
dataMan = createDataManager( data.lat+','+data.lon );
mainloc = data.city
2018-09-08 19:22:46 +00:00
});
}
2018-09-08 19:22:46 +00:00
function initDisplayLoops(){
loops = new Loops(dataMan.locations[0]);
2018-09-08 19:22:46 +00:00
}
function initSlidesLoop() {
slides = new Slides(dataMan);
2018-09-08 19:22:46 +00:00
}
function createDataManager(searchString) {
var dataManager = new DataManager();
2018-09-08 19:22:46 +00:00
$(dataManager)
.on('refresh', refreshObservationDisplay)
.on('ready:main', initDisplayLoops)
.on('allinit', initSlidesLoop);
2018-09-08 19:22:46 +00:00
dataManager.init(searchString);
2018-09-08 19:22:46 +00:00
return dataManager;
2018-09-08 19:22:46 +00:00
}
2018-09-08 19:22:46 +00:00
}
2018-09-08 19:22:46 +00:00
function refreshObservationDisplay() {
var data = dataMan.locations[0].observations(0),
cond = data.wxPhraseLong;
2018-09-08 19:22:46 +00:00
if (mainMap===undefined) {
mainMap = that.mainMap = new Radar("radar-1", 3, 8, data.latitude, data.longitude, false);
miniMap = new Radar("minimap", 3, 6, data.latitude, data.longitude);
}
$('#city').text(mainloc);
$('#forecast-city').text(mainloc + ':');
2018-09-08 19:22:46 +00:00
$('#current-temp').text( dataMan.locations[0].temperature() ) ;
$('#conditions-icon').css('background-image', 'url("' + getCCicon(+data.iconCode, data.windSpeed) + '")');
2018-09-08 19:22:46 +00:00
}
}
var weatherMan = new WeatherManager();