2010-10-03 21:51:02 +02:00
/ * *
* Javascript functions for the frontend
*
* @ author Florian Ziegler < fz @ f10 - home . de >
* @ author Justin Otherguy < justin @ justinotherguy . org >
* @ author Steffen Vogel < info @ steffenvogel . de >
* @ copyright Copyright ( c ) 2010 , The volkszaehler . org project
* @ package default
* @ license http : //opensource.org/licenses/gpl-license.php GNU Public License
* /
/ *
* This file is part of volkzaehler . org
*
* volkzaehler . org 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 any later version .
*
* volkzaehler . org 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
* volkszaehler . org . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* Initialize the WUI ( Web User Interface )
* /
2010-10-30 10:42:08 +02:00
vz . wui . init = function ( ) {
2010-10-04 03:29:12 +02:00
// initialize dropdown accordion
2010-10-03 21:51:02 +02:00
$ ( '#accordion h3' ) . click ( function ( ) {
$ ( this ) . next ( ) . toggle ( 'fast' ) ;
return false ;
} ) . next ( ) . hide ( ) ;
2010-12-10 22:13:19 +01:00
// buttons
2010-11-07 22:20:24 +01:00
$ ( 'button, input[type=button],[type=image]' ) . button ( ) ;
2010-12-10 22:13:19 +01:00
$ ( 'button[name=options-save]' ) . click ( function ( ) { vz . options . save ( ) ; } ) ;
$ ( '#permalink' ) . click ( function ( ) { // TODO add uuids
var u = window . location . protocol + '//' +
window . location . host +
window . location . pathname +
'?from=' + vz . options . plot . xaxis . min +
'&to=' + vz . options . plot . xaxis . max ;
window . location = u ;
} ) ;
$ ( 'button[name=entity-add]' ) . click ( function ( ) { $ ( '#entity-add' ) . dialog ( 'open' ) ; } ) ;
$ ( '#entity-subscribe input[type=button]' ) . click ( function ( ) {
try {
vz . uuids . add ( $ ( '#entity-subscribe input[type=text]' ) . val ( ) ) ;
$ ( '#entity-subscribe input[type=text]' ) . val ( '' ) ;
$ ( '#entity-add' ) . dialog ( 'close' ) ;
vz . entities . loadDetails ( ) ;
}
catch ( exception ) {
vz . wui . dialogs . exception ( exception ) ;
}
} ) ;
2010-11-07 22:20:24 +01:00
// bind plot actions
$ ( '#controls button' ) . click ( this . handleControls ) ;
$ ( '#controls' ) . buttonset ( ) ;
2010-10-03 21:51:02 +02:00
2010-10-06 06:34:36 +02:00
// tuple resolution
2010-12-10 22:13:19 +01:00
vz . options . tuples = Math . round ( $ ( '#flot' ) . width ( ) / 4 ) ;
2010-11-07 23:25:14 +01:00
$ ( '#tuples' ) . val ( vz . options . tuples ) . change ( function ( ) {
2010-10-03 21:51:02 +02:00
vz . options . tuples = $ ( this ) . val ( ) ;
2010-10-08 22:35:17 +02:00
vz . entities . loadData ( ) ;
2010-10-03 21:51:02 +02:00
} ) ;
2010-10-30 10:42:08 +02:00
2010-12-10 22:13:19 +01:00
// backend address
2010-12-10 17:20:50 +01:00
$ ( '#backend-url' )
2010-10-30 10:42:08 +02:00
. val ( vz . options . backendUrl )
. change ( function ( ) {
vz . options . backendUrl = $ ( this ) . val ( ) ;
} ) ;
2010-12-10 22:13:19 +01:00
// auto refresh
2010-12-10 22:36:10 +01:00
if ( vz . options . refresh ) {
$ ( '#refresh' ) . attr ( 'checked' , true ) ;
vz . wui . interval = window . setInterval ( vz . wui . refresh , vz . options . refreshInterval ) ;
}
$ ( '#refresh' ) . change ( function ( ) {
if ( $ ( this ) . attr ( 'checked' ) ) {
vz . options . refresh = true ;
vz . wui . interval = window . setInterval ( vz . wui . refresh , vz . options . refreshInterval ) ;
}
else {
vz . options . refresh = false ;
window . clearInterval ( vz . wui . interval ) ;
}
} ) ;
2010-10-03 21:51:02 +02:00
2010-10-06 06:34:36 +02:00
// plot rendering
2010-12-10 22:13:19 +01:00
$ ( '#render-lines' ) . attr ( 'checked' , ( vz . options . render == 'lines' ) ) ;
$ ( '#render-points' ) . attr ( 'checked' , ( vz . options . render == 'points' ) ) ;
$ ( 'input[name=render][type=radio]' ) . change ( function ( ) {
if ( $ ( this ) . attr ( 'checked' ) ) {
vz . options . render = $ ( this ) . val ( ) ;
2010-10-08 22:35:17 +02:00
vz . drawPlot ( ) ;
2010-12-10 22:13:19 +01:00
}
} ) ;
2010-10-04 21:10:58 +02:00
} ;
2010-10-03 21:51:02 +02:00
2010-10-06 06:34:36 +02:00
/ * *
* Initialize dialogs
* /
2010-10-30 10:42:08 +02:00
vz . wui . dialogs . init = function ( ) {
2010-10-09 16:13:03 +02:00
// initialize dialogs
2010-12-10 17:20:50 +01:00
$ ( '#entity-add.dialog' ) . dialog ( {
2010-10-09 16:13:03 +02:00
autoOpen : false ,
2010-12-10 17:20:50 +01:00
title : 'Kanal hinzufügen' ,
2010-12-10 22:13:19 +01:00
width : 530 ,
2010-10-09 16:13:03 +02:00
resizable : false
} ) ;
2010-12-10 17:20:50 +01:00
$ ( '#entity-add.dialog > div' ) . tabs ( ) ;
2010-10-06 06:34:36 +02:00
} ;
2010-10-05 13:51:58 +02:00
/ * *
* Bind events to handle plot zooming & panning
* /
2010-10-30 10:42:08 +02:00
vz . wui . initEvents = function ( ) {
2010-10-05 13:29:51 +02:00
$ ( '#plot' )
. bind ( "plotselected" , function ( event , ranges ) {
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min = ranges . xaxis . from ;
vz . options . plot . xaxis . max = ranges . xaxis . to ;
2010-10-05 13:29:51 +02:00
vz . options . plot . yaxis . min = 0 ;
vz . options . plot . yaxis . max = null ; // autoscaling
2010-10-08 22:35:17 +02:00
vz . entities . loadData ( ) ;
2010-10-05 13:29:51 +02:00
} )
/ * . b i n d ( ' p l o t p a n ' , f u n c t i o n ( e v e n t , p l o t ) {
2010-10-05 13:51:58 +02:00
var axes = plot . getAxes ( ) ;
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min = axes . xaxis . min ;
vz . options . plot . xaxis . max = axes . xaxis . max ;
2010-10-05 13:29:51 +02:00
vz . options . plot . yaxis . min = axes . yaxis . min ;
vz . options . plot . yaxis . max = axes . yaxis . max ;
} ) * /
/ * . b i n d ( ' m o u s e u p ' , f u n c t i o n ( e v e n t ) {
2010-10-08 22:35:17 +02:00
vz . entities . loadData ( ) ;
2010-10-05 13:29:51 +02:00
} ) * /
. bind ( 'plotzoom' , function ( event , plot ) {
2010-10-05 13:51:58 +02:00
var axes = plot . getAxes ( ) ;
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min = axes . xaxis . min ;
vz . options . plot . xaxis . max = axes . xaxis . max ;
2010-10-05 13:29:51 +02:00
vz . options . plot . yaxis . min = axes . yaxis . min ;
vz . options . plot . yaxis . max = axes . yaxis . max ;
2010-10-08 22:35:17 +02:00
vz . entities . loadData ( ) ;
2010-10-05 13:29:51 +02:00
} ) ;
} ;
2010-10-03 21:51:02 +02:00
/ * *
* Refresh plot with new data
* /
2010-10-30 10:42:08 +02:00
vz . wui . refresh = function ( ) {
2010-12-10 22:13:19 +01:00
var delta = vz . options . plot . xaxis . max - vz . options . plot . xaxis . min ;
vz . options . plot . xaxis . max = new Date ( ) . getTime ( ) ; // move plot
vz . options . plot . xaxis . min = vz . options . plot . xaxis . max - delta ; // move plot
vz . entities . loadData ( ) ;
2010-10-04 21:10:58 +02:00
} ;
2010-10-03 21:51:02 +02:00
/ * *
* Move & zoom in the plotting area
* /
2010-10-30 10:42:08 +02:00
vz . wui . handleControls = function ( ) {
2010-10-08 22:35:17 +02:00
var delta = vz . options . plot . xaxis . max - vz . options . plot . xaxis . min ;
var middle = vz . options . plot . xaxis . min + delta / 2 ;
2010-11-01 17:43:57 +01:00
switch ( $ ( this ) . val ( ) ) {
2010-12-10 17:20:50 +01:00
case 'move-last' :
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . max = new Date ( ) . getTime ( ) ;
vz . options . plot . xaxis . min = new Date ( ) . getTime ( ) - delta ;
2010-10-03 21:51:02 +02:00
break ;
2010-12-10 17:20:50 +01:00
case 'move-back' :
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min -= delta ;
vz . options . plot . xaxis . max -= delta ;
2010-10-03 21:51:02 +02:00
break ;
2010-12-10 17:20:50 +01:00
case 'move-forward' :
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min += delta ;
vz . options . plot . xaxis . max += delta ;
2010-10-03 21:51:02 +02:00
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-reset' :
2010-10-28 01:31:44 +02:00
vz . options . plot . xaxis . min = middle - vz . options . defaultInterval / 2 ;
vz . options . plot . xaxis . max = middle + vz . options . defaultInterval / 2 ;
2010-10-03 21:51:02 +02:00
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-in' :
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min += delta / 4 ;
vz . options . plot . xaxis . max -= delta / 4 ;
2010-10-03 21:51:02 +02:00
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-out' :
2010-10-08 22:35:17 +02:00
vz . options . plot . xaxis . min -= delta ;
vz . options . plot . xaxis . max += delta ;
2010-10-03 21:51:02 +02:00
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-hour' :
2010-11-01 17:43:57 +01:00
hour = 60 * 60 * 1000 ;
vz . options . plot . xaxis . min = middle - hour / 2 ;
vz . options . plot . xaxis . max = middle + hour / 2 ;
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-day' :
2010-11-01 17:43:57 +01:00
var day = 24 * 60 * 60 * 1000 ;
vz . options . plot . xaxis . min = middle - day / 2 ;
vz . options . plot . xaxis . max = middle + day / 2 ;
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-week' :
2010-11-01 17:43:57 +01:00
var week = 7 * 24 * 60 * 60 * 1000 ;
vz . options . plot . xaxis . min = middle - week / 2 ;
vz . options . plot . xaxis . max = middle + week / 2 ;
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-month' :
2010-11-01 17:43:57 +01:00
var month = 30 * 24 * 60 * 60 * 1000 ;
vz . options . plot . xaxis . min = middle - month / 2 ;
vz . options . plot . xaxis . max = middle + month / 2 ;
break ;
2010-12-10 17:20:50 +01:00
case 'zoom-year' :
2010-12-10 15:34:54 +01:00
var year = 365 * 24 * 60 * 60 * 1000 ;
2010-11-01 17:43:57 +01:00
vz . options . plot . xaxis . min = middle - year / 2 ;
vz . options . plot . xaxis . max = middle + year / 2 ;
break ;
2010-10-03 21:51:02 +02:00
}
2010-11-07 23:57:55 +01:00
// reenable autoscaling for yaxis
vz . options . plot . yaxis . min = 0 ;
vz . options . plot . yaxis . max = null ;
2010-10-08 23:01:41 +02:00
// we dont want to zoom/pan into the future
2010-11-07 23:57:55 +01:00
if ( vz . options . plot . xaxis . max > new Date ( ) . getTime ( ) ) {
delta = vz . options . plot . xaxis . max - vz . options . plot . xaxis . min ;
2010-10-08 23:01:41 +02:00
vz . options . plot . xaxis . max = new Date ( ) . getTime ( ) ;
vz . options . plot . xaxis . min = new Date ( ) . getTime ( ) - delta ;
}
2010-10-08 22:35:17 +02:00
vz . entities . loadData ( ) ;
2010-10-04 21:10:58 +02:00
} ;
/ * *
* Get all entity information from backend
* /
2010-10-08 22:35:17 +02:00
vz . entities . loadDetails = function ( ) {
2010-10-28 01:31:44 +02:00
this . clear ( ) ;
2010-12-10 17:20:50 +01:00
$ ( '#entity-list tbody' ) . empty ( ) ;
2010-10-04 21:10:58 +02:00
vz . uuids . each ( function ( index , value ) {
2010-10-08 13:27:44 +02:00
vz . load ( 'entity' , value , { } , waitAsync ( function ( json ) {
2010-10-04 21:10:58 +02:00
vz . entities . push ( new Entity ( json . entity ) ) ;
} , vz . entities . show , 'information' ) ) ;
} ) ;
} ;
/ * *
* Create nested entity list
* /
vz . entities . show = function ( ) {
var i = 0 ;
2010-10-28 01:31:44 +02:00
2010-10-08 22:35:17 +02:00
vz . entities . each ( function ( entity , parent ) {
2010-10-06 06:34:36 +02:00
entity . color = vz . options . plot . colors [ i ++ % vz . options . plot . colors . length ] ;
2010-10-08 22:35:17 +02:00
entity . active = ( entity . active ) ? entity . active : true ;
2010-10-28 01:31:44 +02:00
2010-10-06 06:34:36 +02:00
var row = $ ( '<tr>' )
. addClass ( ( parent ) ? 'child-of-entity-' + parent . uuid : '' )
. attr ( 'id' , 'entity-' + entity . uuid )
. append ( $ ( '<td>' )
2010-10-28 01:31:44 +02:00
. addClass ( 'visibility' )
2010-10-06 06:34:36 +02:00
. css ( 'background-color' , entity . color )
. append ( $ ( '<input>' )
. attr ( 'type' , 'checkbox' )
. attr ( 'checked' , entity . active )
. bind ( 'change' , entity , function ( event ) {
2010-10-28 01:31:44 +02:00
var state = $ ( this ) . attr ( 'checked' ) ;
event . data . active = state ;
if ( entity . type == 'group' ) {
entity . children . each ( function ( entity ) {
$ ( '#entity-' + entity . uuid + ' input[type=checkbox]' ) . attr ( 'checked' , state ) ;
entity . active = state ;
} ) ;
}
2010-10-08 22:35:17 +02:00
vz . drawPlot ( ) ;
2010-10-06 06:34:36 +02:00
} )
2010-10-04 21:10:58 +02:00
)
2010-10-06 06:34:36 +02:00
)
2010-10-28 01:31:44 +02:00
. append ( $ ( '<td>' ) . addClass ( 'expander' ) )
2010-10-06 06:34:36 +02:00
. append ( $ ( '<td>' )
. append ( $ ( '<span>' )
. text ( entity . title )
. addClass ( 'indicator' )
. addClass ( ( entity . type == 'group' ) ? 'group' : 'channel' )
2010-10-04 21:10:58 +02:00
)
2010-10-06 06:34:36 +02:00
)
2010-10-28 01:31:44 +02:00
. append ( $ ( '<td>' ) . text ( entity . type ) ) // channel type
. append ( $ ( '<td>' ) . addClass ( 'min' ) ) // min
. append ( $ ( '<td>' ) . addClass ( 'max' ) ) // max
. append ( $ ( '<td>' ) . addClass ( 'average' ) ) // avg
2011-01-07 15:42:51 +01:00
. append ( $ ( '<td>' ) . addClass ( 'last' ) ) // last
2010-10-28 01:31:44 +02:00
. append ( $ ( '<td>' ) // operations
2010-10-06 06:34:36 +02:00
. addClass ( 'ops' )
2010-10-28 01:31:44 +02:00
. append ( $ ( '<input>' )
2010-10-04 21:10:58 +02:00
. attr ( 'type' , 'image' )
2010-10-06 06:34:36 +02:00
. attr ( 'src' , 'images/information.png' )
. attr ( 'alt' , 'details' )
. bind ( 'click' , entity , function ( event ) { event . data . showDetails ( ) ; } )
2010-10-28 01:31:44 +02:00
)
2010-10-06 06:34:36 +02:00
) ;
if ( parent == null ) {
$ ( 'td.ops' , row ) . prepend ( $ ( '<input>' )
. attr ( 'type' , 'image' )
. attr ( 'src' , 'images/delete.png' )
. attr ( 'alt' , 'delete' )
. bind ( 'click' , entity , function ( event ) {
vz . uuids . remove ( event . data . uuid ) ;
2010-10-09 01:43:50 +02:00
vz . entities . loadDetails ( ) ;
2010-10-06 06:34:36 +02:00
} )
) ;
}
2010-10-04 21:10:58 +02:00
2010-12-10 17:20:50 +01:00
$ ( '#entity-list tbody' ) . append ( row ) ;
2010-10-04 21:10:58 +02:00
} ) ;
// http://ludo.cubicphuse.nl/jquery-plugins/treeTable/doc/index.html
2010-12-10 17:20:50 +01:00
$ ( '#entity-list table' ) . treeTable ( {
2010-10-04 21:10:58 +02:00
treeColumn : 2 ,
clickableNodeNames : true
} ) ;
2010-12-10 22:36:10 +01:00
2010-10-04 21:10:58 +02:00
// load data and show plot
2010-10-08 22:35:17 +02:00
vz . entities . loadData ( ) ;
2010-10-04 21:10:58 +02:00
} ;
2010-10-03 21:51:02 +02:00
2010-10-06 06:34:36 +02:00
/ * *
* Overwritten each iterator for entity array
* /
vz . entities . each = function ( cb ) {
2010-10-28 01:31:44 +02:00
for ( var i = 0 ; i < this . length ; i ++ ) {
this [ i ] . each ( cb ) ;
2010-10-06 06:34:36 +02:00
}
}
2010-10-03 21:51:02 +02:00
/ * *
2010-10-04 22:37:29 +02:00
* Load json data from the backend
2010-10-03 21:51:02 +02:00
* /
2010-10-08 22:35:17 +02:00
vz . entities . loadData = function ( ) {
2010-12-10 17:20:50 +01:00
$ ( '#overlay' ) . html ( '<img src="images/loading.gif" alt="loading..." /><p>loading...</p>' ) ;
2010-10-28 01:31:44 +02:00
this . each ( function ( entity , parent ) {
if ( entity . active && entity . type != 'group' ) { // TODO add group data aggregation
2010-10-19 19:57:45 +02:00
vz . load ( 'data' , entity . uuid ,
{
2010-11-08 00:26:05 +01:00
from : Math . floor ( vz . options . plot . xaxis . min ) , // TODO fetch enough data to fill the holes on the beginning and the end of the plot
2010-10-19 19:57:45 +02:00
to : Math . ceil ( vz . options . plot . xaxis . max ) ,
tuples : vz . options . tuples
} ,
waitAsync ( function ( json ) {
entity . data = json . data ;
2010-10-28 01:31:44 +02:00
// update entity table
// TODO add units
2011-01-08 01:43:16 +01:00
if ( entity . data . min ) {
2010-10-28 01:31:44 +02:00
$ ( '#entity-' + entity . uuid + ' .min' )
. text ( entity . data . min . value )
2010-12-10 22:36:10 +01:00
. attr ( 'title' , $ . plot . formatDate ( new Date ( entity . data . min . timestamp ) , '%d. %b %h:%M:%S' , vz . options . plot . xaxis . monthNames ) ) ;
2011-01-08 01:43:16 +01:00
}
if ( entity . data . max ) {
2010-10-28 01:31:44 +02:00
$ ( '#entity-' + entity . uuid + ' .max' )
. text ( entity . data . max . value )
2011-01-08 01:43:16 +01:00
. attr ( 'title' , $ . plot . formatDate ( new Date ( entity . data . max . timestamp ) , '%d. %b %h:%M:%S' , vz . options . plot . xaxis . monthNames ) ) ;
}
if ( entity . data . average ) {
2011-01-09 21:42:27 +01:00
$ ( '#entity-' + entity . uuid + ' .average' ) . text ( Math . round ( entity . data . average / vz . options . rounding ) * vz . options . rounding ) ;
// rounding: Math.round rounds to whole numbers; to round to one decimal (e.g. 15.2) we multiply by 10 (resp. divide through 0.1), round and reverse the multiplication again; therefore "vz.options.rounding" needs to be set to 0.1 in that case
2011-01-08 01:43:16 +01:00
}
if ( entity . data . last ) {
2011-01-09 21:42:27 +01:00
$ ( '#entity-' + entity . uuid + ' .last' ) . text ( Math . round ( entity . data . last / vz . options . rounding ) * vz . options . rounding ) ;
2010-10-28 01:31:44 +02:00
}
2010-10-19 19:57:45 +02:00
} , vz . drawPlot , 'data' )
) ;
2010-10-06 06:34:36 +02:00
}
2010-10-03 21:51:02 +02:00
} ) ;
2010-10-04 21:10:58 +02:00
} ;
2010-10-03 21:51:02 +02:00
2010-12-10 22:13:19 +01:00
vz . wui . updateHeadline = function ( ) {
2010-12-10 22:36:10 +01:00
var from = $ . plot . formatDate ( new Date ( vz . options . plot . xaxis . min + vz . options . timezoneOffset ) , '%d. %b %h:%M:%S' , vz . options . plot . xaxis . monthNames ) ;
var to = $ . plot . formatDate ( new Date ( vz . options . plot . xaxis . max + vz . options . timezoneOffset ) , '%d. %b %h:%M:%S' , vz . options . plot . xaxis . monthNames ) ;
2010-12-10 22:13:19 +01:00
$ ( '#title' ) . text ( from + ' - ' + to ) ;
}
2010-10-04 22:37:29 +02:00
/ * *
* Draws plot to container
* /
2010-10-08 22:35:17 +02:00
vz . drawPlot = function ( ) {
2010-12-10 22:13:19 +01:00
vz . wui . updateHeadline ( ) ;
2010-10-08 22:35:17 +02:00
var data = new Array ;
vz . entities . each ( function ( entity , parent ) {
if ( entity . active && entity . data && entity . data . count > 0 ) {
data . push ( {
data : entity . data . tuples ,
color : entity . color
} ) ;
}
} ) ;
2010-11-07 22:20:24 +01:00
if ( data . length == 0 ) {
2010-12-10 17:20:50 +01:00
$ ( '#overlay' ) . html ( '<img src="images/empty.png" alt="no data..." /><p>nothing to plot...</p>' ) ;
2010-12-10 15:34:54 +01:00
data . push ( { } ) ; // add empty dataset to show axes
2010-10-08 22:35:17 +02:00
}
else {
2010-11-07 22:20:24 +01:00
$ ( '#overlay' ) . empty ( ) ;
2010-10-08 22:35:17 +02:00
}
2010-11-07 22:20:24 +01:00
2010-12-10 22:13:19 +01:00
vz . options . plot . series . lines . show = ( vz . options . render == 'lines' ) ;
vz . options . plot . series . points . show = ( vz . options . render == 'points' ) ;
2010-11-07 22:20:24 +01:00
vz . plot = $ . plot ( $ ( '#flot' ) , data , vz . options . plot ) ;
2010-10-04 21:10:58 +02:00
} ;
2010-10-08 13:27:44 +02:00
2010-10-28 01:31:44 +02:00
/ * *
* Universal helper for backend ajax requests with error handling
* /
2010-10-08 13:27:44 +02:00
vz . load = function ( context , identifier , data , success ) {
$ . ajax ( {
success : success ,
2010-12-10 22:13:19 +01:00
url : this . options . backendUrl + '/' + context + '/' + identifier + '.json' ,
2010-10-08 13:27:44 +02:00
dataType : 'json' ,
data : data ,
error : function ( xhr ) {
json = JSON . parse ( xhr . responseText ) ;
2010-12-10 22:13:19 +01:00
vz . wui . dialogs . error ( xhr . statusText , json . exception . message , xhr . status ) ; // TODO or throw exception?
2010-10-28 01:31:44 +02:00
}
} ) ;
} ;
2010-12-10 22:13:19 +01:00
/ * *
* Parse URL GET parameters
* /
vz . parseUrlVars = function ( ) {
var vars = $ . getUrlVars ( ) ;
for ( var key in vars ) {
if ( vars . hasOwnProperty ( key ) ) {
switch ( key ) {
case 'uuid' : // add optional uuid from url
try {
vz . uuids . add ( vars [ key ] ) ;
} catch ( exception ) {
vz . wui . dialogs . exception ( exception ) ;
}
break ;
case 'from' :
vz . options . plot . xaxis . min = parseInt ( vars [ key ] ) ;
break ;
case 'to' :
vz . options . plot . xaxis . max = parseInt ( vars [ key ] ) ;
break ;
case 'debug' :
$ . getScript ( 'javascripts/firebug-lite.js' ) ;
break ;
}
}
}
} ;
2010-10-28 01:31:44 +02:00
/ * *
* Load definitions from backend
* /
vz . definitions . load = function ( ) {
$ . ajax ( {
cache : true ,
dataType : 'json' ,
url : vz . options . backendUrl + '/capabilities/definition/entity.json' ,
success : function ( json ) {
vz . definitions . entity = json . definition . entity
}
} ) ;
$ . ajax ( {
cache : true ,
dataType : 'json' ,
url : vz . options . backendUrl + '/capabilities/definition/property.json' ,
success : function ( json ) {
vz . definitions . property = json . definition . property
2010-10-08 13:27:44 +02:00
}
} ) ;
} ;
2010-10-19 19:57:45 +02:00
2010-10-28 01:31:44 +02:00
vz . definitions . get = function ( section , iname ) {
for ( var i in vz . definitions [ section ] ) {
alert ( vz . definitions [ section ] [ i ] . name ) ;
if ( vz . definitions [ section ] [ i ] . name == iname ) {
return definition ;
}
}
}
2010-10-19 19:57:45 +02:00
/ *
* Error & Exception handling
* /
2010-10-30 10:42:08 +02:00
vz . wui . dialogs . error = function ( error , description , code ) {
2010-10-19 19:57:45 +02:00
if ( typeof code != undefined ) {
error = code + ': ' + error ;
}
$ ( '<div>' )
. append ( $ ( '<span>' ) . text ( description ) )
. dialog ( {
title : error ,
2010-10-28 01:31:44 +02:00
width : 450 ,
dialogClass : 'ui-error' ,
resizable : false ,
modal : true ,
buttons : {
Ok : function ( ) {
$ ( this ) . dialog ( "close" ) ;
}
}
2010-10-19 19:57:45 +02:00
} ) ;
} ;
2010-10-30 10:42:08 +02:00
vz . wui . dialogs . exception = function ( exception ) {
this . error ( exception . type , exception . message , exception . code ) ;
2010-10-19 19:57:45 +02:00
} ;