2014-05-06 15:14:30 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* events . js *
* script for event specific functions and setup *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * L i c e n s e
Copyright 2014 Kasper D . Fischer < kasper . fischer @ rub . de >
This program is free software : you can redistribute it and / or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation , either version 3 of the License , or ( at your option )
any later version .
This program is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
for more details .
You should have received a copy of the GNU General Public License along
with this program . If not , see http : //www.gnu.org/licenses/.
$Id$
* /
2014-05-06 13:26:47 +02:00
/* do reverse geolocation lookup */
function getGeolocation ( id , lat , lng ) {
if ( ! geolocationTable [ id ] ) {
// $.getJSON( "//nominatim.openstreetmap.org/reverse", { lat: lat, lon: lng, zoom: 10, format: "json" } )
$ . getJSON ( "//open.mapquestapi.com/nominatim/v1/reverse.php" , { lat : lat , lon : lng , zoom : 10 , format : "json" } )
. done ( function ( json ) {
var city = json . address [ "city" ] ;
var country = json . address [ "country" ] ;
var countryCode = json . address [ "country_code" ] . toUpperCase ( ) ;
geolocationTable [ id ] = city ;
( country != "Deutschland" ) ? geolocationTable [ id ] = geolocationTable [ id ] + " (" + countryCode + ")" : null ;
if ( city ) {
$ ( "#eventstable a.toggle[eventid=" + id + "]" ) . text ( geolocationTable [ id ] ) ;
var sort = [ [ 0 , 1 ] , [ 1 , 1 ] , [ 2 , 1 ] ] ;
$ ( "#eventstable" ) . trigger ( "update" , [ true ] ) ;
$ ( "#eventstable" ) . trigger ( "updateCache" ) ;
$ ( "#eventstable" ) . trigger ( "sorton" , [ sort ] ) ;
} else {
console . log ( "Nominatim did not provide a city tag for " + lat + " / " + lng ) ;
} ;
} )
. fail ( function ( jqxhr , textStatus , error ) {
var err = textStatus + ", " + error ;
console . log ( "Request Failed: " + err ) ;
} ) ;
} ;
} ;
/* Load events using ajax */
function ajaxLoadEvents ( stime , etime ) {
var mapBounds = map . getBounds ( ) ;
var N = mapBounds . getNorth ( ) ;
var E = mapBounds . getEast ( ) ;
var S = mapBounds . getSouth ( ) ;
var W = mapBounds . getWest ( ) ;
var d = 0.1 ;
if ( ! stime ) {
var stime = new Date ( ) ;
stime . setDate ( stime . getDate ( ) - 180 ) ;
} ;
if ( ! etime ) {
var etime = new Date ( ) ;
etime . setDate ( etime . getDate ( ) + 1 ) ;
} ;
var url = "https://ariadne.geophysik.ruhr-uni-bochum.de/fdsnws/event/1/query"
var request _data = {
starttime : sprintf ( "%d-%02d-%02d" , stime . getFullYear ( ) , stime . getMonth ( ) + 1 , stime . getDate ( ) ) ,
endtime : sprintf ( "%d-%02d-%02d" , etime . getFullYear ( ) , etime . getMonth ( ) + 1 , etime . getDate ( ) ) ,
minlat : S - d ,
maxlat : N + d ,
minlon : W - d ,
maxlon : E + d ,
minmag : minMag - 0.1 ,
} ;
$ . ajax ( {
type : "GET" ,
url : url ,
data : request _data ,
dataType : "xml" ,
success : function ( xml ) {
$ ( xml ) . find ( 'event' ) . each ( function ( ) {
var id = $ ( this ) . attr ( 'publicID' ) . split ( '/' ) [ 2 ] ;
var mag = $ ( this ) . find ( 'magnitude > mag > value' ) . text ( ) ;
var otime = $ ( this ) . find ( 'origin > time > value' ) . text ( ) ;
var lng = $ ( this ) . find ( 'origin > longitude > value' ) . text ( ) ;
var lat = $ ( this ) . find ( 'origin > latitude > value' ) . text ( ) ;
var mag = $ ( this ) . find ( 'magnitude > mag > value' ) . text ( ) ;
var evaluationMode = $ ( this ) . find ( 'evaluationMode' ) . text ( ) ;
var evaluationStatus = $ ( this ) . find ( 'evaluationStatus' ) . text ( ) ;
var type = $ ( this ) . find ( 'type' ) . last ( ) . text ( ) ;
var location = getLocation ( Number ( lat ) , Number ( lng ) ) [ 0 ] ;
( location ) ? null : location = $ ( this ) . find ( 'description > text' ) . text ( ) ;
// create table row: Date, Time, Mag, Location
if ( ! eventTable [ id ] && ( type == 'earthquake' || type == 'induced or triggered event' || type == 'outside of network interest' ) && evaluationMode != 'automatic' && evaluationStatus != 'preliminary' && Number ( mag ) + 0.05 >= minMag ) {
var row = '<tr class="tablesorter-hasChildRow">'
+ '<td class="utctime-date">' + otime . split ( '.' ) [ 0 ] + 'Z</td>'
+ '<td class="utctime-time">' + otime . split ( '.' ) [ 0 ] + 'Z</td>'
+ sprintf ( '<td class="ar">%.1f</td>' , Number ( mag ) )
+ '<td><a href="#" class="toggle" eventid="' + id + '">' + location + '</a><a class="map-link" href="#" eventid="' + id + '">Karte</a></td>'
+ '</tr>' ;
row += '<tr class="tablesorter-childRow">'
+ '<td colspan="4" eventid="' + id + '">not implemented</td></tr>' ;
var added = $ ( '#eventstable tbody' ) . append ( row ) ;
added . find ( '.tablesorter-childRow td' ) . hide ( ) ;
$ ( '#eventstable' ) . find ( 'td.utctime-date' ) . each ( function ( ) {
$ . localtime . formatObject ( $ ( this ) , "dd.MM.yyyy" ) ;
$ ( this ) . removeClass ( 'utctime-date' ) ;
$ ( this ) . addClass ( 'localtime-date' ) ;
} ) ;
$ ( '#eventstable' ) . find ( 'td.utctime-time' ) . each ( function ( ) {
$ . localtime . formatObject ( $ ( this ) , "HH:mm" ) ;
$ ( this ) . removeClass ( 'utctime-time' ) ;
$ ( this ) . addClass ( 'localtime-time' ) ;
} ) ;
// create marker
var marker = addEventMarker ( id , Number ( lat ) , Number ( lng ) , Number ( mag ) ) ;
var text = sprintf ( '<h3 eventid="%s">%s</h3>' , id , location )
+ sprintf ( '<p>Ereignis: %s</br>' , id )
+ sprintf ( 'Ort: %.4f °N, %.4f °O </br>' , Number ( lat ) , Number ( lng ) )
+ sprintf ( 'Zeit: <span class="utctime">%sZ</span></p>' , otime . split ( '.' ) [ 0 ] , otime . split ( '.' ) [ 0 ] ) ;
marker . bindPopup ( text ) ;
// try to get better location with reverse geolocation lookup (nominatim), check cache first
( geolocationTable [ id ] ) ? $ ( "#eventstable a.toggle[eventid=" + id + "]" ) . text ( geolocationTable [ id ] ) : getGeolocation ( id , lat , lng ) ;
} ;
} ) ;
} ,
complete : function ( ) {
initStationTable ( ) ;
var sort = [ [ 0 , 1 ] , [ 1 , 1 ] , [ 2 , 1 ] ] ;
$ ( "#eventstable" ) . trigger ( "update" , [ true ] ) ;
$ ( "#eventstable" ) . trigger ( "updateCache" ) ;
$ ( "#eventstable" ) . trigger ( "sorton" , [ sort ] ) ;
initMapLink ( ) ;
} ,
error : function ( jqxhr , textStatus , error ) {
var err = textStatus + ", " + error ;
console . log ( "Request Failed: " + err ) ;
}
} ) ;
} ;
2014-05-06 15:14:30 +02:00
/ * a d d r o w t o t a b l e
obsolete * /
2014-05-06 13:26:47 +02:00
function addEventRow ( id , props ) {
$ ( '#eventstable' ) . tablesorter ( {
sortList : "[[0,0], [1,1]], [2,1]" ,
resort : true ,
showProcessing : true ,
pager _size : 35
} ) ;
var html = '<tr class="tablesorter-hasChildRow">'
+ '<td class="utctime-date">' + props . date + 'T' + props . time . split ( '.' ) [ 0 ] + 'Z</td>'
+ '<td class="utctime-time">' + props . date + 'T' + props . time . split ( '.' ) [ 0 ] + 'Z</td>'
+ '<td class="ar">' + props . mag + '</td>'
+ '<td><a href="#" class="toggle">' + props . location + '</a><a class="map-link" href="#" eventid="' + id + '">Karte</a></td>'
+ '</tr>'
+ '<tr class="tablesorter-childRow">'
+ '<td colspan="4" eventid="' + id + '">'
+ "<pre>ID " + id + "\n\n"
+ "Origin\n"
+ "Date " + props . date + "\n"
+ "Time " + props . time + "\n"
+ "Latitude " + props . lat + " deg +/- " + props . lat _err + " km\n"
+ "Longitude " + props . lon + " deg +/- " + props . lon _err + " km\n"
+ "Depth " + props . depth + " km +/- " + props . depth _err + " km\n"
+ "Residual RMS " + props . rms + " s\n"
+ "Azimuthal gap " + props . gap + " deg\n\n"
+ props . no _phases + " Phase arrivals:\n"
+ "sta net dist azi phase time res wt sta\n" ;
for ( i = 0 ; i < props . no _phases ; i ++ ) {
html += props . phases [ i ] ;
( i < props . no _phases - 1 ) ? html += "\n" : null ;
} ;
html += "</pre></td></tr>\n" ;
var added = $ ( '#eventstable tbody' ) . append ( html ) ;
added . find ( '.tablesorter-childRow td' ) . hide ( ) ;
$ ( '#eventstable' ) . find ( 'td.utctime-date' ) . each ( function ( ) {
$ . localtime . formatObject ( $ ( this ) , "dd. MM. yyyy" ) ;
$ ( this ) . removeClass ( 'utctime-date' ) ;
$ ( this ) . addClass ( 'localtime-date' ) ;
} ) ;
$ ( '#eventstable' ) . find ( 'td.utctime-time' ) . each ( function ( ) {
$ . localtime . formatObject ( $ ( this ) , "HH:mm" ) ;
$ ( this ) . removeClass ( 'utctime-time' ) ;
$ ( this ) . addClass ( 'localtime-time' ) ;
} ) ;
// force resorting
$ ( "#eventstable" ) . trigger ( "update" , [ true ] ) ;
} ;
2014-05-06 15:14:30 +02:00
/ * t o g g l e s v i s i b i l i t y o f f i l t e r e d m a r k e r s
* only events in the event list are shown * /
function toggleFilteredMarkers ( ) {
// show all shown events in map
$ ( "#eventstable > tbody > tr:not(.filtered) > td > a.map-link" ) . each ( function ( ) {
if ( $ ( this ) . attr ( "eventid" ) ) {
eventTable [ $ ( this ) . attr ( "eventid" ) ] . setStyle ( { opacity : 1 , strokeOpacity : 1 , fillOpacity : eventMarkerOpacity } ) ;
} ;
} ) ;
// hide filtered events in map
$ ( "#eventstable > tbody > tr.filtered > td > a.map-link" ) . each ( function ( ) {
if ( $ ( this ) . attr ( "eventid" ) ) {
eventTable [ $ ( this ) . attr ( "eventid" ) ] . setStyle ( { opacity : 0 , strokeOpacity : 0 , fillOpacity : 0 } ) ;
} ;
} ) ;
highlightFirstEvent ( ) ;
} ;
/ * H i g h l i g h t t h e f i r s t e v e n t o f t h e e v e n t l i s t o n t h e m a p i f n o
* other event is selected * /
function highlightFirstEvent ( ) {
var highlightStyle = {
color : 'red' ,
fillColor : '#f03' ,
} ;
var normalStyle = {
fillColor : "#FFF500" ,
color : "#FFF500"
} ;
$ ( "#eventstable a.map-link" ) . each ( function ( ) {
if ( $ ( this ) . attr ( "eventid" ) ) {
eventTable [ $ ( this ) . attr ( "eventid" ) ] . setStyle ( normalStyle ) ;
$ ( this ) . removeClass ( 'first' ) ;
$ ( this ) . text ( 'Karte' ) ;
} ;
} ) ;
$ ( "#eventstable > tbody > tr:not(.filtered)" ) . first ( ) . find ( "a.map-link" ) . each ( function ( ) {
if ( $ ( this ) . attr ( "eventid" ) ) {
eventTable [ $ ( this ) . attr ( "eventid" ) ] . setStyle ( highlightStyle ) ;
$ ( this ) . addClass ( 'first' ) ;
$ ( this ) . text ( 'Karte (rot)' ) ;
} ;
} ) ;
} ;
function highlightEvent ( id ) {
var highlightStyle = {
color : 'red' ,
fillColor : '#f03' ,
} ;
var normalStyle = {
fillColor : "#FFF500" ,
color : "#FFF500"
} ;
$ ( "#eventstable > tbody > tr:not(.filtered)" ) . find ( "a.map-link" ) . each ( function ( ) {
if ( $ ( this ) . attr ( "eventid" ) ) {
if ( $ ( this ) . attr ( "eventid" ) == id ) {
eventTable [ $ ( this ) . attr ( "eventid" ) ] . setStyle ( highlightStyle ) ;
$ ( this ) . addClass ( 'first' ) ;
$ ( this ) . text ( 'Karte (rot)' ) ;
} else {
eventTable [ $ ( this ) . attr ( "eventid" ) ] . setStyle ( normalStyle ) ;
$ ( this ) . removeClass ( 'first' ) ;
$ ( this ) . text ( 'Karte' ) ;
}
} ;
} ) ;
} ;
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* document ready *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2014-05-06 13:26:47 +02:00
$ ( document ) . ready ( function ( ) {
// tablesorter for event list
$ ( "#eventstable" ) . tablesorter (
{
theme : 'blue' ,
dateFormat : "ddmmyyyy" ,
headers : {
0 : { sorter : "shortDate" }
} ,
cssChildRow : "tablesorter-childRow" , // this is the default setting
widgets : [ "uitheme" , "zebra" , "filter" , "pager" ] , // initialize zebra and filter widgets, "scroller"
widgetOptions : {
// possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
pager _output : '# {startRow} - {endRow} ({totalRows}) | Seite {page} ({totalPages})' ,
pager _removeRows : false ,
pager _size : 35 ,
filter _childRows : true ,
filter _cssFilter : 'tablesorter-filter' ,
filter _startsWith : false ,
filter _ignoreCase : true ,
scroller _height : $ ( 'div.map' ) . height ( ) - 250 ,
scroller _barWidth : 10 ,
scroller _jumpToHeader : false ,
sortList : "[[0,1], [1,1], [2,1]]" ,
resort : true ,
showProcessing : true ,
}
} ) ;
// hide child rows
$ ( '#eventstable > tbody > tr.tablesorter-childRow td' ) . hide ( ) ;
// update map after filtering
$ ( '#eventstable' ) . bind ( 'filterEnd' , function ( ) {
toggleFilteredMarkers ( ) ;
} ) ;
// highlight first event
$ ( '#eventstable' ) . bind ( 'sortEnd' , function ( ) {
highlightFirstEvent ( ) ;
} ) ;
// show / hide event info
$ ( '#eventstable' ) . delegate ( '.toggle' , 'click' , function ( ) {
// toggle visibility of selected row
$ ( this ) . closest ( 'tr' ) . nextUntil ( 'tr.tablesorter-hasChildRow' ) . find ( 'td' ) . toggle ( 'slow' ) ;
// mark currently selected row and remove class selected from all other rows
// hide other rows
$ ( this ) . closest ( 'tr' ) . nextUntil ( 'tr.tablesorter-hasChildRow' ) . find ( 'td' ) . addClass ( 'selected-now' ) ;
$ ( this ) . closest ( 'tbody' ) . find ( 'td.selected' ) . each ( function ( ) {
if ( ! $ ( this ) . hasClass ( 'selected-now' ) ) {
$ ( this ) . hide ( 'slow' ) ;
$ ( this ) . removeClass ( 'selected' ) ;
} ;
} ) ;
$ ( this ) . closest ( 'tr' ) . nextUntil ( 'tr.tablesorter-hasChildRow' ) . find ( 'td' ) . each ( function ( ) {
$ ( this ) . removeClass ( 'selected-now' ) ;
var selected = $ ( this ) . hasClass ( 'selected' ) ;
if ( selected ) {
$ ( this ) . removeClass ( 'selected' ) ;
} else {
$ ( this ) . addClass ( 'selected' ) ;
} ;
} ) ;
return false ;
} ) ;
} ) ;