-
-
Save dz0ny/d0bafed3497b0268b6f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Vreme local search | |
(function() { | |
function Vreme() { | |
this.init(this); | |
} | |
Vreme.prototype = { | |
init: function(self){ | |
self.osvezi(self); | |
$('#dodaj').bind('click', function() {self.dodajKraj(self)}); | |
}, | |
_request: function (query, callback) { | |
var url = "http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(query)+"&format=json&callback=" | |
var data = this._getCache(url); | |
var self = this; | |
if(data){ | |
callback(data); | |
} | |
else{ | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function () { | |
if (this.readyState == 4) { | |
if (this.status == 200) { | |
var data = JSON.parse(this.responseText); | |
self._setCache(url, data); | |
callback(data); | |
} | |
} | |
}; | |
xhr.open("GET", url); | |
xhr.send(); | |
} | |
}, | |
_getCache: function (hash) { | |
if (sessionStorage [murmurhash3_32_gc(hash, 1)]) { | |
return JSON.parse(sessionStorage [murmurhash3_32_gc(hash, 1)]); | |
} else { | |
return false; | |
} | |
}, | |
_delCache: function (hash, callback) { | |
delete sessionStorage[murmurhash3_32_gc(hash, 1)]; | |
}, | |
_setCache: function (hash, data) { | |
hash = murmurhash3_32_gc(hash, 1); | |
sessionStorage [hash] = JSON.stringify(data); | |
setTimeout(function () { | |
delete sessionStorage [hash]; | |
}, 1000 *60 *10); | |
}, | |
getVreme: function(callback, self){ | |
var query = 'select two_day_history_url, metData.domain_altitude, metData.rh, metData.t, metData.tsValid_issued, metData.domain_meteosiId, metData.domain_longTitle, metData.domain_lat, metData.domain_lon from xml where url in (select title from atom where url="http://spreadsheets.google.com/feeds/list/0AvY_vCMQloRXdE5HajQxUGF5ZEZYUjhKNG9EeVl2bFE/od6/public/basic")' | |
self._request(query, function (json) { | |
self.addKraj(json, self, function() { | |
self.lokacije.sort(function(a, b) { | |
return a.oddaljenost-b.oddaljenost; | |
}); | |
callback(self); | |
}); | |
}); | |
}, | |
addKraj: function(json, self, callback){ | |
if (json.query.count) { | |
if ( Array.isArray(json.query.results.data) ) { | |
//več krajev v json | |
json.query.results.data.forEach(function(kraj) { | |
self.parseKraj(kraj, self); | |
}); | |
} | |
}; | |
callback(self); | |
}, | |
parseKraj: function(kraj, self){ | |
if (kraj.metData) { | |
var x = 69.1 * (kraj.metData.domain_lat - self.lat); | |
var y = 69.1 * (kraj.metData.domain_lon - self.lon) * Math.cos(kraj.metData.domain_lat/57.3); | |
var oddaljenost = Math.round( Math.sqrt(x * x + y * y)*1.609344*10)/10; | |
self.lokacije.push({ | |
ime: kraj.metData.domain_longTitle.replace(/\s\(.*\)/img,""), | |
lat: kraj.metData.domain_lat, | |
lon: kraj.metData.domain_lon, | |
id: kraj.metData.domain_meteosiId, | |
temp: kraj.metData.t, | |
pocutje:self.dewPoint( kraj.metData.t, kraj.metData.rh ), | |
visina: kraj.metData.domain_altitude, | |
zgodovina: kraj.two_day_history_url, | |
posodobljeno: kraj.metData.tsValid_issued, | |
oddaljenost: oddaljenost | |
}); | |
}; | |
}, | |
dewPoint: function(T, RH){ | |
var dew_numer = 237.7*(Math.log(RH/100)+((17.27*T)/(T+237.7))); | |
var dew_denom = 17.27-Math.log(RH/100)-((17.27*T)/(T+237.7)); | |
return Math.round( dew_numer/dew_denom *10)/10; | |
}, | |
getLocation: function (self, callback) { | |
if (sessionStorage["lat"] && sessionStorage["lon"]) { | |
self.lat = sessionStorage["lat"]; | |
self.lon = sessionStorage["lon"]; | |
callback(true); | |
}else{ | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(function (position) { | |
sessionStorage["lat"] = self.lat = position.coords.latitude; | |
sessionStorage["lon"] = self.lon = position.coords.longitude; | |
callback(true); | |
},callback(false)) | |
}else{ | |
callback(false); | |
} | |
} | |
}, | |
posodobi: function(self){ | |
if(self.lokacije.length){ | |
var lokacija = self.lokacije[0]; | |
$("#temperatura").attr("class", "center"); | |
$("#kraj").attr("title", "Nadmorska višina "+lokacija.visina+"m"); | |
$("#kraj").text(lokacija.ime); | |
$("#temperatura").text(lokacija.temp + " °C"); | |
document.title = lokacija.ime + " " + lokacija.temp + "°C"; | |
//http://shrey27.wordpress.com/2010/09/30/facebook-smileys-utf-8-codes/ | |
if (lokacija.pocutje > 10 && lokacija.pocutje < 22) { | |
$("#pocutje").text("☺"); | |
}else{ | |
$("#pocutje").text("☹"); | |
} | |
} | |
}, | |
osvezi: function(self){ | |
clearTimeout(self.refresh); | |
self.lokacije = []; | |
self.getLocation(self, function(gotLocation) { | |
if (gotLocation) { | |
self.getVreme(function(event) { | |
self.refresh = setTimeout(function() {self.osvezi(self)},1000*60*30) | |
self.posodobi(self); | |
}, self); | |
} | |
}); | |
}, | |
dodajKraj: function(){ | |
} | |
} | |
//SamoUNIČENJE (tm) | |
return new Vreme(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment