Revision: 4131 http://vexi.svn.sourceforge.net/vexi/?rev=4131&view=rev Author: clrg Date: 2011-05-12 23:58:15 +0000 (Thu, 12 May 2011)
Log Message: ----------- Feature: track value types and return the same type Modified Paths: -------------- trunk/org.vexi-vexi.widgets/src_main/vexi/util/date.t trunk/org.vexi-vexi.widgets/src_test/test/widget/datefield.t Added Paths: ----------- trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/util/date.t Copied: trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/util/date.t (from rev 4128, trunk/org.vexi-vexi.widgets/src_main/vexi/util/date.t) =================================================================== --- trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/util/date.t (rev 0) +++ trunk/org.vexi-vexi.widgets/src_main/org/vexi/lib/util/date.t 2011-05-12 23:58:15 UTC (rev 4131) @@ -0,0 +1,309 @@ +<!-- Copyright 2011 - see COPYING for details [LGPL] --> + +<vexi xmlns:js="vexi://js" xmlns:meta="vexi://meta" xmlns="org.vexi.lib.util"> + <meta:doc> + <author>Charles Goodwin</author> + </meta:doc> + + <js:Object> + + thisobj.allowzeroday = false; + thisobj.date; + thisobj.day; + thisobj.month; + thisobj.monthdays; + thisobj.year; + thisobj.weekday; + //thisobj.format; + // TODO: thisobj.string ++= static.stringRead; + + thisobj.getDaysInMonth = static.getDaysInMonth; + thisobj.getMonthName = static.getMonthName; + thisobj.getWeekday = static.getWeekday; + thisobj.compareTo = static.compareTo; + thisobj.isDateInRange = static.isDateInRange; + thisobj.parseDate = static.parseDate; + thisobj.copy = static.copy; + + thisobj.date ++= static.dateRead; + thisobj.date ++= static.dateWrite; + thisobj.day ++= static.dayWrite; + thisobj.month ++= static.monthWrite; + thisobj.weekday ++= static.weekdayRead; + thisobj.monthname ++= static.monthnameRead; + + if (arguments[0]) { + switch (typeof(arguments[0])) { + case "string": + static.parseDate(arguments[0], arguments[1], thisobj); + break; + default: + break; + } + } + + </js:Object> + + /** creates a copy of the context date */ + static.copy = function() { + var d = new .date(); + d.year = this.year; + d.month = this.month; + d.day = this.day; + return d; + } + + /** date read trap */ + static.dateRead = function() { + if (!trapee.day or !trapee.month or !trapee.year) { + return null; + } + try { + return vexi.date(trapee.year, trapee.month-1, trapee.day); + } catch (e) { + return null; + } + } + + /** date write trap */ + static.dateWrite = function(v) { + try { + trapee.year = v ? v.getFullYear() : null; + trapee.month = v ? v.getMonth()+1 : null; + trapee.day = v ? v.getDate() : null; + } catch (e) { + throw "Unsupported date value: "+v+" (type: "+typeof(v)+")"; + } + return; + } + + /** constrain days */ + static.dayWrite = function(v) { + if (v != null) { + if (trapee.allowzeroday and v == 0) { + ; + } else if (trapee.allow_dashes and typeof(v)=="string" and v.charAt(0) == '-') { + ; + } + else { + if (typeof(v)=="string") { + v = vexi.string.parseInt(v); + } + // NOTE: the order of the monthday +=/-= and month ++/-- + // is important and dependent on days surplus / shortage + while (v>trapee.monthdays) { + v -= trapee.monthdays; + trapee.month++; + } + while (1>v) { + trapee.month--; + v += trapee.monthdays; + } + } + } + cascade = v; + } + + /** returns the number of days for month m in year y */ + static.getDaysInMonth = function(y, m) { return 32 - vexi.date(y, m-1, 32).getDate(); } + + /** get the name of a month */ + static.getMonthName = function(m) { + switch (m) { + case 1: return "January"; + case 2: return "February"; + case 3: return "March"; + case 4: return "April"; + case 5: return "May"; + case 6: return "June"; + case 7: return "July"; + case 8: return "August"; + case 9: return "September"; + case 10: return "October"; + case 11: return "November"; + case 12: return "December"; + } + return null; + } + + /** get day of the week for a given year, month, and day */ + static.getWeekday = function(y, m, d) { return vexi.date(y, m-1, d).getDay(); } + + /** + * tests if a date is in the given range + * + * returns: + * -1 : date is before specified date + * 0 : date is the same specified date + * 1 : date is after specified date + */ + static.compareTo = function(d) { + var dY = this.getFullYear(); + var dM = this.getMonth(); + var dD = this.getDate(); + var d2Y = d2.getFullYear(); + var d2M = d2.getMonth(); + var d2D = d2.getDate(); + if (d2Y>dY or (d2Y==dY and (d2M>dM or (d2M==dM and d2D>dD)))) { + // below range + return -1; + } + if (dY>d2Y or (dY==d2Y and (dM>d2M or (dM==d2M and dD>d2D)))) { + // above range + return 1; + } + // date is in range + return 0; + } + + /** + * tests if a date is in the given range + * + * returns: + * -1 : date is before range + * 0 : date is in range + * 1 : date is after range + */ + static.isDateInRange = function(d1, d2) { + var dY = this.getFullYear(); + var dM = this.getMonth(); + var dD = this.getDate(); + var d1Y = d1.getFullYear(); + var d1M = d1.getMonth(); + var d1D = d1.getDate(); + var d2Y = d2.getFullYear(); + var d2M = d2.getMonth(); + var d2D = d2.getDate(); + if (d1Y>dY or (d1Y==dY and (d1M>dM or (d1M==dM and d1D>dD)))) { + // below range + return -1; + } + if (dY>d2Y or (dY==d2Y and (dM>d2M or (dM==d2M and dD>d2D)))) { + // above range + return 1; + } + // date is in range + return 0; + } + + /** read the name of a month of a date */ + static.monthnameRead = function() { return static.getMonthName(trapee.month); } + + /** contrains month and set monthdays */ + static.monthWrite = function(v) { + if (v != null) { + //vexi.log.info("trapee.allow_dashes=" + trapee.allow_dashes + ", typeof(v)=" + typeof(v) + " v.charAt(0)=" + v.charAt(0)); + if (trapee.allow_dashes and typeof(v)=="string" and v.charAt(0) == '-') { + trapee.monthdays = null; + cascade = v; + return; + } + else if (typeof(v)=="string") { + v = vexi.string.parseInt(v); + } + while (v>12) { + trapee.year = trapee.year+1; + v -= 12; + } + while (1>v) { + trapee.year = trapee.year-1; + v += 12; + } + // establish month length + trapee.monthdays = static.getDaysInMonth(trapee.year, v); + } else { + trapee.monthdays = null; + } + cascade = v; + } + + /** contrains month and set monthdays */ + static.yearWrite = function(v) { + if (trapee.allow_dashes and typeof(v)=="string" and v.charAt(0) == '-') { + ; + } + else if (v != null and typeof(v)=="string") { + v = vexi.string.parseInt(v); + } + cascade = v; + } + + /** read the weekday of a date */ + static.weekdayRead = function() { return static.getWeekday(trapee.year, trapee.month, trapee.day); } + + /** allow input of 00xx years */ + var expandYear = function(ystr) { + if (2>=ystr.length) { + var d = vexi.date().getFullYear(); + // auto-fill year to this year + if (ystr=="") { + return d; + } else { + // auto-expand 2-digit years + ystr = vexi.string.parseInt(ystr, 10); + return (d+10>=ystr ? 2000 : 1900) + ystr; + } + } else return vexi.string.parseInt(ystr, 10); + } + + /** takes a string 'v' and parses it as a date for 'o' */ + static.parseDate = function(v, format, o) { + if (!o) { + o = new .date(); + } + // establish how many characters for each part + var mc = format.charAt(0); + var m1, m2, mn; + var m3 = format.length; + for (var i=1; m3>i; i++) { + mn = format.charAt(i); + if (mc!=mn) { + if (mc) { + if (!m1) m1 = i; + else if (!m2) m2 = i; + else break; + } + mc = mn; + } + } + // parse string into 3 number parts + var s0 = ""; + var s1 = ""; + var s2 = ""; + var i = 0; + var j = 0; + try { + for (var c = v.charCodeAt(i); c>57 or 48>c; c = v.charCodeAt(++i)) { /* skip non-numbers */ } + for (var c = v.charCodeAt(i); m1>j and c>47 and 58>c; c = v.charCodeAt(++i)) { s0 = s0 + v.charAt(i); j++; } + for (var c = v.charCodeAt(i); c>57 or 48>c; c = v.charCodeAt(++i)) { /* skip non-numbers */ } + for (var c = v.charCodeAt(i); m2>j and c>47 and 58>c; c = v.charCodeAt(++i)) { s1 = s1 + v.charAt(i); j++; } + for (var c = v.charCodeAt(i); c>57 or 48>c; c = v.charCodeAt(++i)) { /* skip non-numbers */ } + for (var c = v.charCodeAt(i); m3>j and c>47 and 58>c; c = v.charCodeAt(++i)) s2 = s2 + v.charAt(i); + } catch (e) { throw "unsupported text value for datefield: '"+v+"'"; } + var d = !s0 or !s1 or !s2 ? vexi.date() : null; + // assign values to widget + switch (format) { + case "MMDDYYYY": + s0 = s0 ? vexi.string.parseInt(s0, 10) : d.getMonth(); + s1 = s1 ? vexi.string.parseInt(s1, 10) : d.getDay(); + s2 = s2 ? expandYear(s2) : d.getFullYear(); + o.day = s1; o.month = s0; o.year = s2; + break; + case "YYYYMMDD": + s0 = s0 ? expandYear(s0) : d.getFullYear(); + s1 = s1 ? vexi.string.parseInt(s1, 10) : d.getMonth(); + s2 = s2 ? vexi.string.parseInt(s2, 10) : d.getDay(); + o.day = s2; o.month = s1; o.year = s0; + break; + case "DDMMYYYY": + default: + s0 = s0 ? vexi.string.parseInt(s0, 10) : d.getDay(); + s1 = s1 ? vexi.string.parseInt(s1, 10) : d.getMonth(); + s2 = s2 ? expandYear(s2) : d.getFullYear(); + o.day = s0; o.month = s1; o.year = s2; + break; + } + return o; + } + +</vexi> Modified: trunk/org.vexi-vexi.widgets/src_main/vexi/util/date.t =================================================================== --- trunk/org.vexi-vexi.widgets/src_main/vexi/util/date.t 2011-05-12 21:24:05 UTC (rev 4130) +++ trunk/org.vexi-vexi.widgets/src_main/vexi/util/date.t 2011-05-12 23:58:15 UTC (rev 4131) @@ -1,10 +1,10 @@ -<!-- Copyright 2009 - see COPYING for details [LGPL] --> +<!-- Copyright 2011 - see COPYING for details [LGPL] --> -<vexi xmlns:js="vexi://js" xmlns:meta="vexi://meta" xmlns="vexi.util"> +<vexi xmlns:meta="vexi://meta" xmlns="org.vexi.lib.util"> <meta:doc> <author>Charles Goodwin</author> <name>Date</name> - <desc>A template for working with dates</desc> + <desc>A date object to facilitate for working with dates</desc> <usage> This date 'object' differs from the JavaScript date standard in that months are treated the same as years and days - that @@ -58,303 +58,5 @@ </usage> </meta:doc> - <js:Object> - - thisobj.allowzeroday = false; - thisobj.date; - thisobj.day; - thisobj.month; - thisobj.monthdays; - thisobj.year; - thisobj.weekday; - - thisobj.getDaysInMonth = static.getDaysInMonth; - thisobj.getMonthName = static.getMonthName; - thisobj.getWeekday = static.getWeekday; - thisobj.compareTo = static.compareTo; - thisobj.isDateInRange = static.isDateInRange; - thisobj.parseDate = static.parseDate; - thisobj.copy = static.copy; - - thisobj.date ++= static.dateRead; - thisobj.date ++= static.dateWrite; - thisobj.day ++= static.dayWrite; - thisobj.month ++= static.monthWrite; - thisobj.weekday ++= static.weekdayRead; - thisobj.monthname ++= static.monthnameRead; - - if (arguments[0]) { - switch (typeof(arguments[0])) { - case "string": - static.parseDate(arguments[0], arguments[1], thisobj); - break; - default: - break; - } - } - - </js:Object> - - /** creates a copy of the context date */ - static.copy = function() { - var d = .date(); - d.year = this.year; - d.month = this.month; - d.day = this.day; - return d; - } - - /** date read trap */ - static.dateRead = function() { - if (!trapee.day or !trapee.month or !trapee.year) { - return null; - } - try { - return vexi.date(trapee.year, trapee.month-1, trapee.day); - } catch (e) { - return null; - } - } - - /** date write trap */ - static.dateWrite = function(v) { - try { - trapee.year = v ? v.getFullYear() : null; - trapee.month = v ? v.getMonth()+1 : null; - trapee.day = v ? v.getDate() : null; - } catch (e) { - throw "Unsupported date value: "+v+" (type: "+typeof(v)+")"; - } - return; - } - - /** constrain days */ - static.dayWrite = function(v) { - if (v != null) { - if (trapee.allowzeroday and v == 0) { - ; - } else if (trapee.allow_dashes and typeof(v)=="string" and v.charAt(0) == '-') { - ; - } - else { - if (typeof(v)=="string") { - v = vexi.string.parseInt(v); - } - // NOTE: the order of the monthday +=/-= and month ++/-- - // is important and dependent on days surplus / shortage - while (v>trapee.monthdays) { - v -= trapee.monthdays; - trapee.month++; - } - while (1>v) { - trapee.month--; - v += trapee.monthdays; - } - } - } - cascade = v; - } - - /** returns the number of days for month m in year y */ - static.getDaysInMonth = function(y, m) { return 32 - vexi.date(y, m-1, 32).getDate(); } - - /** get the name of a month */ - static.getMonthName = function(m) { - switch (m) { - case 1: return "January"; - case 2: return "February"; - case 3: return "March"; - case 4: return "April"; - case 5: return "May"; - case 6: return "June"; - case 7: return "July"; - case 8: return "August"; - case 9: return "September"; - case 10: return "October"; - case 11: return "November"; - case 12: return "December"; - } - return null; - } - - /** get day of the week for a given year, month, and day */ - static.getWeekday = function(y, m, d) { return vexi.date(y, m-1, d).getDay(); } - - /** - * tests if a date is in the given range - * - * returns: - * -1 : date is before specified date - * 0 : date is the same specified date - * 1 : date is after specified date - */ - static.compareTo = function(d) { - var dY = this.getFullYear(); - var dM = this.getMonth(); - var dD = this.getDate(); - var d2Y = d2.getFullYear(); - var d2M = d2.getMonth(); - var d2D = d2.getDate(); - if (d2Y>dY or (d2Y==dY and (d2M>dM or (d2M==dM and d2D>dD)))) { - // below range - return -1; - } - if (dY>d2Y or (dY==d2Y and (dM>d2M or (dM==d2M and dD>d2D)))) { - // above range - return 1; - } - // date is in range - return 0; - } - - /** - * tests if a date is in the given range - * - * returns: - * -1 : date is before range - * 0 : date is in range - * 1 : date is after range - */ - static.isDateInRange = function(d1, d2) { - var dY = this.getFullYear(); - var dM = this.getMonth(); - var dD = this.getDate(); - var d1Y = d1.getFullYear(); - var d1M = d1.getMonth(); - var d1D = d1.getDate(); - var d2Y = d2.getFullYear(); - var d2M = d2.getMonth(); - var d2D = d2.getDate(); - if (d1Y>dY or (d1Y==dY and (d1M>dM or (d1M==dM and d1D>dD)))) { - // below range - return -1; - } - if (dY>d2Y or (dY==d2Y and (dM>d2M or (dM==d2M and dD>d2D)))) { - // above range - return 1; - } - // date is in range - return 0; - } - - /** read the name of a month of a date */ - static.monthnameRead = function() { return static.getMonthName(trapee.month); } - - /** contrains month and set monthdays */ - static.monthWrite = function(v) { - if (v != null) { - //vexi.log.info("trapee.allow_dashes=" + trapee.allow_dashes + ", typeof(v)=" + typeof(v) + " v.charAt(0)=" + v.charAt(0)); - if (trapee.allow_dashes and typeof(v)=="string" and v.charAt(0) == '-') { - trapee.monthdays = null; - cascade = v; - return; - } - else if (typeof(v)=="string") { - v = vexi.string.parseInt(v); - } - while (v>12) { - trapee.year = trapee.year+1; - v -= 12; - } - while (1>v) { - trapee.year = trapee.year-1; - v += 12; - } - // establish month length - trapee.monthdays = static.getDaysInMonth(trapee.year, v); - } else { - trapee.monthdays = null; - } - cascade = v; - } - - /** contrains month and set monthdays */ - static.yearWrite = function(v) { - if (trapee.allow_dashes and typeof(v)=="string" and v.charAt(0) == '-') { - ; - } - else if (v != null and typeof(v)=="string") { - v = vexi.string.parseInt(v); - } - cascade = v; - } - - /** read the weekday of a date */ - static.weekdayRead = function() { return static.getWeekday(trapee.year, trapee.month, trapee.day); } - - /** allow input of 00xx years */ - var expandYear = function(ystr) { - if (2>=ystr.length) { - var d = vexi.date().getFullYear(); - // auto-fill year to this year - if (ystr=="") { - return d; - } else { - // auto-expand 2-digit years - ystr = vexi.string.parseInt(ystr, 10); - return (d+10>=ystr ? 2000 : 1900) + ystr; - } - } else return vexi.string.parseInt(ystr, 10); - } - - /** takes a string 'v' and parses it as a date for 'o' */ - static.parseDate = function(v, format, o) { - if (!o) { - o = new .date(); - } - // establish how many characters for each part - var mc = format.charAt(0); - var m1, m2, mn; - var m3 = format.length; - for (var i=1; m3>i; i++) { - mn = format.charAt(i); - if (mc!=mn) { - if (mc) { - if (!m1) m1 = i; - else if (!m2) m2 = i; - else break; - } - mc = mn; - } - } - // parse string into 3 number parts - var s0 = ""; - var s1 = ""; - var s2 = ""; - var i = 0; - var j = 0; - try { - for (var c = v.charCodeAt(i); c>57 or 48>c; c = v.charCodeAt(++i)) { /* skip non-numbers */ } - for (var c = v.charCodeAt(i); m1>j and c>47 and 58>c; c = v.charCodeAt(++i)) { s0 = s0 + v.charAt(i); j++; } - for (var c = v.charCodeAt(i); c>57 or 48>c; c = v.charCodeAt(++i)) { /* skip non-numbers */ } - for (var c = v.charCodeAt(i); m2>j and c>47 and 58>c; c = v.charCodeAt(++i)) { s1 = s1 + v.charAt(i); j++; } - for (var c = v.charCodeAt(i); c>57 or 48>c; c = v.charCodeAt(++i)) { /* skip non-numbers */ } - for (var c = v.charCodeAt(i); m3>j and c>47 and 58>c; c = v.charCodeAt(++i)) s2 = s2 + v.charAt(i); - } catch (e) { throw "unsupported text value for datefield: '"+v+"'"; } - var d = !s0 or !s1 or !s2 ? vexi.date() : null; - // assign values to widget - switch (format) { - case "MMDDYYYY": - s0 = s0 ? vexi.string.parseInt(s0, 10) : d.getMonth(); - s1 = s1 ? vexi.string.parseInt(s1, 10) : d.getDay(); - s2 = s2 ? expandYear(s2) : d.getFullYear(); - o.day = s1; o.month = s0; o.year = s2; - break; - case "YYYYMMDD": - s0 = s0 ? expandYear(s0) : d.getFullYear(); - s1 = s1 ? vexi.string.parseInt(s1, 10) : d.getMonth(); - s2 = s2 ? vexi.string.parseInt(s2, 10) : d.getDay(); - o.day = s2; o.month = s1; o.year = s0; - break; - case "DDMMYYYY": - default: - s0 = s0 ? vexi.string.parseInt(s0, 10) : d.getDay(); - s1 = s1 ? vexi.string.parseInt(s1, 10) : d.getMonth(); - s2 = s2 ? expandYear(s2) : d.getFullYear(); - o.day = s0; o.month = s1; o.year = s2; - break; - } - return o; - } - + <date /> </vexi> Modified: trunk/org.vexi-vexi.widgets/src_test/test/widget/datefield.t =================================================================== --- trunk/org.vexi-vexi.widgets/src_test/test/widget/datefield.t 2011-05-12 21:24:05 UTC (rev 4130) +++ trunk/org.vexi-vexi.widgets/src_test/test/widget/datefield.t 2011-05-12 23:58:15 UTC (rev 4131) @@ -1,4 +1,4 @@ -<vexi xmlns:meta="vexi://meta" xmlns:ui="vexi://ui" xmlns="vexi.widget"> +<vexi xmlns:meta="vexi://meta" xmlns:ui="vexi://ui" xmlns="vexi.widget" xmlns:util="vexi.util"> <meta:doc> <author>Charles Goodwin</author> </meta:doc> @@ -6,14 +6,40 @@ var vunit = vexi..vexi.test.vunit; /// Quick Suite - var suite = {}; - suite.testQ1 = function() { - var b = .datefield(vexi.box); + var suite = { name: "vexi.widget.datefield Tests" }; + + suite.test_format = function() { + var b = new .datefield(); + b.format = "YYYY-MM-DD"; + b.text = "2008-01-05"; + assert("2008-01-05", b.text); b.value = "2008-01-05"; assert("2008-01-05", b.value); - }; - suite.name = "vexi.widget.datefield Tests"; - + }; + + suite.test_value_date_put = function() { + var b = new .datefield(); + var d = new util.date(); + d.day = "05"; + d.month = "01"; + d.year = "2008"; + b.value = d; + var val = b.value; + assert("05", d.day); + assert("01", d.month); + assert("2008", d.year); + }; + + suite.test_value_write_on_enter = function() { + var b = new .datefield(); + b.format = "YYYY-MM-DD"; + b.value = "2008-01-05"; + var putval; + b.value ++= function(v) { cascade = v; putval = v; } + b.KeyPressed = "enter"; + assert("2008-01-05", putval); + }; + static.test = suite; <surface> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Achieve unprecedented app performance and reliability What every C/C++ and Fortran developer should know. Learn how Intel has extended the reach of its next-generation tools to help boost performance applications - inlcuding clusters. http://p.sf.net/sfu/intel-dev2devmay _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn