Revision: 4253 http://vexi.svn.sourceforge.net/vexi/?rev=4253&view=rev Author: clrg Date: 2011-09-22 23:28:13 +0000 (Thu, 22 Sep 2011) Log Message: ----------- Tidy up of lib.widget.numfield
Modified Paths: -------------- trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/widget/numfield.t Modified: trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/widget/numfield.t =================================================================== --- trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/widget/numfield.t 2011-09-22 21:21:38 UTC (rev 4252) +++ trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/widget/numfield.t 2011-09-22 23:28:13 UTC (rev 4253) @@ -1,4 +1,4 @@ -<!-- Copyright 2009 - see COPYING for details [LGPL] --> +<!-- Copyright 2011 - see COPYING for details [LGPL] --> <vexi xmlns:ui="vexi://ui" xmlns:meta="vexi://meta" xmlns="org.vexi.lib" xmlns:sync="vexi.util.sync"> <meta:doc> @@ -9,101 +9,100 @@ <role.tooltipable /> <text.field /> <ui:box> + thisbox.v_edit; // abstract thisbox.enabled = true; thisbox.minvalue; thisbox.maxvalue; - thisbox.scale; // number of decimal places, if positive. If negative, number of trailing 0's + thisbox.value; // value and the v_edit.text are synchronized - // value and the v_edit.text are synchronized - thisbox.value; + thisbox.scale; // if positive, number of decimal places + // if negative, number of trailing 0s - thisbox.validateText = static.validateTextFunc; KeyPressed ++= static.keypressEvent; scale ++= static.scale; maxvalue ++= static.limitvalueWrite; minvalue ++= static.limitvalueWrite; + focused ++= static.focusWrite; + value ++= static.valueWrite; + v_edit ++= static.editWrite; + const Decimal = vexi.js.Decimal; - thisbox.validateNumber = function(d){ - if(d==null) return null; + /** validate a number against the parameters of this numfield */ + thisbox.validateNumber = function(d) { + if (d==null) return null; var d0 = d; - - - if(maxvalue!=null){ - if(0>Decimal.compare(maxvalue, d)){ - d = maxvalue; - } - } - - if(minvalue!=null){ - if(0 > Decimal.compare(d, minvalue)){ - d = minvalue; - } - } - - if(scale!=null){ + + if (maxvalue!=null) { + if (0 > Decimal.compare(maxvalue, d)) { + d = maxvalue; + } + } + + if (minvalue!=null) { + if (0 > Decimal.compare(d, minvalue)) { + d = minvalue; + } + } + + if (scale!=null) { d = d.round(scale); } return d; - }; - - - thisbox.focused ++= function(v){ - if(!v){ - // trigger value setting - v_edit.text = v_edit.text; - } - cascade = v; - }; + } - thisbox.value ++= function(v){ - cascade = validateNumber(Decimal.cast(v)); - }; - - - thisbox.text2decimal = function(v){ + /** convert from a string to a decimal */ + thisbox.text2decimal = function(v) { var d = Decimal.tryParseString(v); - if(d!=null){ + if (d!=null) { d = validateNumber(d); } return d; - }; + } - var decimal2text= function(d){ - if(d==null) return ""; - return d+""; - }; - - thisbox.v_edit ++= function(v){ - cascade = v; - sync..syncTransform(thisbox, v, "value", "text", decimal2text, text2decimal); - thisbox[trapname] --= callee; - }; - - </ui:box> - - var Decimal = vexi.js.Decimal; + const Decimal = vexi.js.Decimal; + + var decimal2text= function(d) { + if (d==null) return ""; + return d+""; + } + + /** set up sync between edit.text and numfield.value */ + static.editWrite = function(v) { + cascade = v; + sync..syncTransform(trapee, v, "value", "text", decimal2text, trapee.text2decimal); + trapee[trapname] --= callee; + } + + /** when focus changes, self put text in case text content is updated */ + static.focusWrite = function(v) { + if (!v) { + // trigger value setting + trapee.v_edit.text = trapee.v_edit.text; + } + cascade = v; + } + /** fire action or filter key in case of password fields */ static.keypressEvent = function(v) { var t = trapee; var e = t.v_edit; var txt0 = e.text; - - switch(v){ + switch (v) { // Handle '-' specially. Toggles sign. case '-': { var cpos = e.getCursorCharIndex(); - if(txt0.length==0 or txt0.charAt(0)!='-'){ + if (txt0.length==0 or txt0.charAt(0)!='-') { e.text = '-'+txt0; cpos++; - }else{ + } else { e.text = txt0.substring(1); if(cpos!=0) cpos--; } @@ -143,7 +142,6 @@ cascade = v; } } - /** constrain value to min */ static.limitvalueWrite = function(v) { @@ -160,4 +158,9 @@ trapee.value = trapee.value; } + /** ensure value is valid */ + static.valueWrite = function(v) { + cascade = trapee.validateNumber(Decimal.cast(v)); + } + </vexi> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense. http://p.sf.net/sfu/splunk-d2dcopy1 _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn