This shows you the differences between two versions of the page.
sample_code:unit_convert [2014/11/01 12:52] 106.77.65.188 [The code] |
sample_code:unit_convert [2014/11/01 20:55] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Converterlator ====== | ||
- | Calculator+Converter app. | ||
- | ===== The code ===== | ||
- | <code javascript unit_convert.js> | ||
- | // | ||
- | var unitval = []; | ||
- | unitval [" | ||
- | unitval [" | ||
- | unitval [" | ||
- | unitval [" | ||
- | |||
- | unitval [" | ||
- | unitval [" | ||
- | unitval [" | ||
- | |||
- | // | ||
- | var sum = ""; | ||
- | var π = 3.14; | ||
- | |||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | lay = app.CreateLayout( " | ||
- | lay.SetBackColor( "# | ||
- | |||
- | // | ||
- | keys = [ 7, | ||
- | |||
- | // | ||
- | abt = app.CreateButton( " | ||
- | abt.SetOnTouch( abt_OnTouch ); | ||
- | lay.AddChild( abt ); | ||
- | |||
- | // | ||
- | clrs = app.CreateSpinner( " | ||
- | clrs.SetOnTouch( clrs_OnTouch ); | ||
- | lay.AddChild( clrs ); | ||
- | |||
- | // | ||
- | txtSum = app.CreateText( "", | ||
- | txtSum.SetTextSize( 42 ); | ||
- | txtSum.SetBackColor( "# | ||
- | txtSum.SetMargins( 0, | ||
- | lay.AddChild( txtSum ); | ||
- | |||
- | // | ||
- | lay1st = app.CreateLayout( " | ||
- | for( i=0; | ||
- | lay.AddChild( lay1st ); | ||
- | |||
- | // | ||
- | lay2nd = app.CreateLayout( " | ||
- | for( i=4; | ||
- | lay.AddChild( lay2nd ); | ||
- | |||
- | // | ||
- | lay3rd = app.CreateLayout( " | ||
- | for( i=8; | ||
- | lay.AddChild( lay3rd ); | ||
- | |||
- | // | ||
- | lay4th = app.CreateLayout( " | ||
- | for( i=12; | ||
- | lay.AddChild( lay4th ); | ||
- | |||
- | // | ||
- | lay5th = app.CreateLayout( " | ||
- | for( i=16; | ||
- | lay.AddChild( lay5th ); | ||
- | |||
- | // | ||
- | cnvrtr = app.CreateButton( " | ||
- | cnvrtr.SetOnTouch( cnvrtr_OnTouch ); | ||
- | cnvrtr.SetMargins( 0, | ||
- | lay.AddChild( cnvrtr ); | ||
- | |||
- | // | ||
- | app.SetDebugEnabled( false ); | ||
- | |||
- | app.EnableBackKey( false ); | ||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | function OnBack() | ||
- | { | ||
- | var yesno = app.CreateYesNoDialog( " | ||
- | yesno.SetOnTouch( yesno_OnTouch ); | ||
- | } | ||
- | function yesno_OnTouch( result ) | ||
- | { | ||
- | if( result == " | ||
- | } | ||
- | function abt_OnTouch() | ||
- | { | ||
- | // | ||
- | dlg = app.CreateDialog( " | ||
- | |||
- | // | ||
- | laydlg = app.CreateLayout( " | ||
- | dlg.AddLayout( laydlg ); | ||
- | |||
- | vrsn = app.CreateText( " | ||
- | vrsn.SetTextSize(24); | ||
- | vrsn.SetMargins( 0, | ||
- | laydlg.AddChild(vrsn); | ||
- | |||
- | nm = app.CreateText( " | ||
- | nm.SetTextSize(20); | ||
- | nm.SetPadding(0.05, | ||
- | laydlg.AddChild(nm); | ||
- | |||
- | dlg.Show(); | ||
- | } | ||
- | |||
- | function clrs_OnTouch() | ||
- | { | ||
- | if(clrs.GetText() == " | ||
- | { | ||
- | lay.SetBackColor("# | ||
- | } | ||
- | else if( clrs.GetText() == " | ||
- | { | ||
- | lay.SetBackColor("# | ||
- | } | ||
- | else if( clrs.GetText() == " | ||
- | { | ||
- | lay.SetBackColor( "# | ||
- | } | ||
- | else if( clrs.GetText() == " | ||
- | { | ||
- | lay.SetBackColor( "# | ||
- | } | ||
- | else if( clrs.GetText() == " | ||
- | { | ||
- | lay.SetBackColor( "# | ||
- | } | ||
- | else if( clrs.GetText() == " | ||
- | { | ||
- | lay.SetBackColor( "# | ||
- | } | ||
- | else | ||
- | { | ||
- | lay.SetBackColor( "# | ||
- | } | ||
- | } | ||
- | |||
- | // | ||
- | function AddButton( lay, | ||
- | { | ||
- | if( name==" | ||
- | btn = app.CreateButton( name, | ||
- | btn.SetOnTouch( btns_OnTouch ); | ||
- | lay.AddChild( btn ); | ||
- | } | ||
- | |||
- | // | ||
- | function btns_OnTouch() | ||
- | { | ||
- | app.Vibrate( " | ||
- | |||
- | // | ||
- | btn = app.GetLastButton(); | ||
- | var txt = btn.GetText(); | ||
- | |||
- | // | ||
- | if( txt==" | ||
- | |||
- | // | ||
- | else if( txt==" | ||
- | |||
- | else if( txt==" | ||
- | { | ||
- | |||
- | sum = Math.sqrt( sum ); | ||
- | } | ||
- | |||
- | // | ||
- | else sum += txt; | ||
- | |||
- | // | ||
- | txtSum.SetText( sum ); | ||
- | } | ||
- | |||
- | // | ||
- | function CalcResult() | ||
- | { | ||
- | try{ | ||
- | // | ||
- | sum = eval( sum ).toFixed(4); | ||
- | } | ||
- | catch(e) { sum = " | ||
- | } | ||
- | |||
- | function cnvrtr_OnTouch() | ||
- | { | ||
- | // | ||
- | txtSum.SetVisibility( " | ||
- | lay1st.SetVisibility( " | ||
- | lay2nd.SetVisibility( " | ||
- | lay3rd.SetVisibility( " | ||
- | lay4th.SetVisibility( " | ||
- | lay5th.SetVisibility( " | ||
- | cnvrtr.SetVisibility( " | ||
- | |||
- | // | ||
- | qnt = app.CreateSpinner( " | ||
- | qnt.SetOnTouch( qnt_OnTouch ); | ||
- | qnt.SetMargins( 0, | ||
- | lay.AddChild( qnt ); | ||
- | |||
- | // | ||
- | lay1 = app.CreateLayout( " | ||
- | lay.AddChild( lay1 ); | ||
- | |||
- | // | ||
- | lay2 = app.CreateLayout( " | ||
- | lay.AddChild( lay2 ); | ||
- | |||
- | / | ||
- | to be same because if anyone is edited, | ||
- | same result. So, | ||
- | them, | ||
- | |||
- | // | ||
- | unt1 = app.CreateSpinner( " | ||
- | unt1.SetOnTouch( unt1_OnTouch ); | ||
- | lay1.AddChild( unt1 ); | ||
- | |||
- | // | ||
- | edt1 = app.CreateTextEdit( " | ||
- | edt1.SetOnChange( unt1_OnTouch ); | ||
- | lay1.AddChild( edt1 ); | ||
- | |||
- | // | ||
- | unt2 = app.CreateSpinner( " | ||
- | unt2.SetOnTouch( unt2_OnTouch ); | ||
- | lay2.AddChild( unt2 ); | ||
- | |||
- | // | ||
- | edt2 = app.CreateTextEdit( " | ||
- | edt2.SetOnChange( unt2_OnTouch ); | ||
- | lay2.AddChild( edt2 ); | ||
- | |||
- | // | ||
- | clc = app.CreateButton( " | ||
- | clc.SetOnTouch( clc_OnTouch ); | ||
- | clc.SetMargins( 0, | ||
- | lay.AddChild( clc ); | ||
- | |||
- | } | ||
- | |||
- | function qnt_OnTouch() | ||
- | { | ||
- | if( qnt.GetText() == " | ||
- | { | ||
- | unt1.SetList( " | ||
- | unt2.SetList( " | ||
- | } | ||
- | } | ||
- | |||
- | function unt1_OnTouch() | ||
- | { | ||
- | // | ||
- | var a = unitval[unt1.GetText()]; | ||
- | var b = unitval[unt2.GetText()]; | ||
- | qty = edt1.GetText(); | ||
- | res = qty*a/ | ||
- | edt2.SetText( res ); | ||
- | |||
- | } | ||
- | |||
- | function unt2_OnTouch() | ||
- | { | ||
- | // | ||
- | var a = unitval[unt1.GetText()]; | ||
- | var b = unitval[unt2.GetText()]; | ||
- | qty = edt2.GetText(); | ||
- | res = qty*b/ | ||
- | edt1.SetText( res ); | ||
- | |||
- | } | ||
- | |||
- | function isNumber(n) | ||
- | { | ||
- | return !isNaN(parseFloat(n)) && | ||
- | } | ||
- | |||
- | function clc_OnTouch() | ||
- | { | ||
- | // | ||
- | qnt.SetVisibility( " | ||
- | lay1.SetVisibility( " | ||
- | lay2.SetVisibility( " | ||
- | clc.SetVisibility( " | ||
- | |||
- | // | ||
- | txtSum = app.CreateText( "", | ||
- | txtSum.SetTextSize( 42 ); | ||
- | txtSum.SetBackColor( "# | ||
- | txtSum.SetMargins( 0, | ||
- | lay.AddChild( txtSum ); | ||
- | |||
- | // | ||
- | lay1st = app.CreateLayout( " | ||
- | for( i=0; | ||
- | lay.AddChild( lay1st ); | ||
- | |||
- | // | ||
- | lay2nd = app.CreateLayout( " | ||
- | for( i=4; | ||
- | lay.AddChild( lay2nd ); | ||
- | |||
- | // | ||
- | lay3rd = app.CreateLayout( " | ||
- | for( i=8; | ||
- | lay.AddChild( lay3rd ); | ||
- | |||
- | // | ||
- | lay4th = app.CreateLayout( " | ||
- | for( i=12; | ||
- | lay.AddChild( lay4th ); | ||
- | |||
- | // | ||
- | lay5th = app.CreateLayout( " | ||
- | for( i=16; | ||
- | lay.AddChild( lay5th ); | ||
- | |||
- | // | ||
- | cnvrtr = app.CreateButton( " | ||
- | cnvrtr.SetOnTouch( cnvrtr_OnTouch ); | ||
- | cnvrtr.SetMargins( 0, | ||
- | lay.AddChild( cnvrtr ); | ||
- | |||
- | } | ||
- | |||
- | |||
- | |||
- | </ | ||
- | ===== Note ===== | ||
- | This app is in no way the sort that anyone might wish to use in real life. | ||
- | |||
- | It is just a very simplistic demo of the type of approach that might be taken. | ||