|
From: <ma...@us...> - 2012-05-06 12:19:58
|
Revision: 792
http://openautomation.svn.sourceforge.net/openautomation/?rev=792&view=rev
Author: mayerch
Date: 2012-05-06 12:19:51 +0000 (Sun, 06 May 2012)
Log Message:
-----------
New feature: allow full JavaScript formulas for a mapping
Modified Paths:
--------------
CometVisu/trunk/visu/lib/templateengine.js
CometVisu/trunk/visu/visu_config_demo.xml
Modified: CometVisu/trunk/visu/lib/templateengine.js
===================================================================
--- CometVisu/trunk/visu/lib/templateengine.js 2012-05-06 11:12:01 UTC (rev 791)
+++ CometVisu/trunk/visu/lib/templateengine.js 2012-05-06 12:19:51 UTC (rev 792)
@@ -106,15 +106,23 @@
function map( value, element ) {
var map = element.data('mapping');
- if( map && mappings[map] && (mappings[map][value] || mappings[map]['range']) ) {
- if( mappings[map]['range'] ) value = parseFloat( value );
- if( mappings[map][value] ) return mappings[map][value];
-
- var range = mappings[map]['range'];
- for( var min in range ) {
- if( min > value ) continue;
- if( range[min][0] < value ) continue; // check max
- return range[min][1];
+ if( map && mappings[map] )
+ {
+ var m = mappings[map];
+
+ if( m.formula ) {
+ return m.formula( value );
+ } else if( m[value] ) {
+ return m[value];
+ } else if( m['range'] ) {
+ var valueFloat = parseFloat( value );
+
+ var range = m['range'];
+ for( var min in range ) {
+ if( min > valueFloat ) continue;
+ if( range[min][0] < valueFloat ) continue; // check max
+ return range[min][1];
+ }
}
}
return value;
@@ -230,18 +238,27 @@
// then the mappings
$( 'meta > mappings mapping', xml ).each( function(i){
- var name = $(this).attr('name');
+ var $this = $(this);
+ var name = $this.attr('name');
mappings[ name ] = {};
- $(this).find('entry').each( function(){
- if( $(this).attr('value') )
- {
- mappings[ name ][ $(this).attr('value') ] = $(this).text();
- } else {
- if( ! mappings[ name ][ 'range' ] ) mappings[ name ][ 'range' ] = {};
- mappings[ name ][ 'range' ][ parseFloat($(this).attr('range_min')) ] =
- [ parseFloat( $(this).attr('range_max') ), $(this).text() ];
- }
- });
+ var formula = $this.find('formula');
+ if( formula.length > 0 )
+ {
+ eval( 'var func = function(x){' + formula.text() + '; return y;}' );
+ mappings[ name ][ 'formula' ] = func;
+ } else {
+ $this.find('entry').each( function(){
+ var $localThis = $(this);
+ if( $localThis.attr('value') )
+ {
+ mappings[ name ][ $localThis.attr('value') ] = $localThis.text();
+ } else {
+ if( ! mappings[ name ][ 'range' ] ) mappings[ name ][ 'range' ] = {};
+ mappings[ name ][ 'range' ][ parseFloat($localThis.attr('range_min')) ] =
+ [ parseFloat( $localThis.attr('range_max') ), $localThis.text() ];
+ }
+ });
+ }
});
// then the stylings
Modified: CometVisu/trunk/visu/visu_config_demo.xml
===================================================================
--- CometVisu/trunk/visu/visu_config_demo.xml 2012-05-06 11:12:01 UTC (rev 791)
+++ CometVisu/trunk/visu/visu_config_demo.xml 2012-05-06 12:19:51 UTC (rev 792)
@@ -47,6 +47,9 @@
<entry value="4">Sat</entry>
<entry value="5">Kino</entry>
</mapping>
+ <mapping name="One1000th">
+ <formula>y = x/1000;</formula>
+ </mapping>
</mappings>
<stylings>
<styling name="Red_Green">
@@ -365,6 +368,11 @@
<label>Format "%+.2f":</label>
<address transform="DPT:9">12/7/9</address>
</info>
+ <line/>
+ <info format="%.4f" mapping="One1000th">
+ <label>Format "%.4f + Formel":</label>
+ <address transform="DPT:9">12/7/9</address>
+ </info>
</page>
<page name="Stylings Test" align="center">
<switch mapping="On_Off" styling="Green_Red">
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|