From d7ad06b90ccc182a7e0da76a59ebd1a4521492f3 Mon Sep 17 00:00:00 2001 From: Erwan de Ferrieres Date: Mon, 14 Mar 2011 11:15:24 +0000 Subject: [PATCH] Adding a masked input capability to the date fields, use mask=Y and the field type will be taken for it. jQuery pPlugin is from http://digitalbush.com/projects/masked-input-plugin/ and under a MIT license git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1081333 13f79535-47bb-0310-9956-ffa450edef68 --- framework/common/widget/CommonScreens.xml | 1 + .../widget/example/FormWidgetExampleForms.xml | 2 +- .../jquery/plugins/jquery.maskedinput-1.2.2.min.js | 7 +++++++ framework/widget/dtd/widget-form.xsd | 9 +++++++++ .../org/ofbiz/widget/form/MacroFormRenderer.java | 13 +++++++++++++ .../src/org/ofbiz/widget/form/ModelFormField.java | 6 ++++++ framework/widget/templates/csvFormMacroLibrary.ftl | 2 +- framework/widget/templates/foFormMacroLibrary.ftl | 2 +- framework/widget/templates/htmlFormMacroLibrary.ftl | 7 ++++++- 9 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 framework/images/webapp/images/jquery/plugins/jquery.maskedinput-1.2.2.min.js diff --git a/framework/common/widget/CommonScreens.xml b/framework/common/widget/CommonScreens.xml index 86f2561446..face9fd829 100644 --- a/framework/common/widget/CommonScreens.xml +++ b/framework/common/widget/CommonScreens.xml @@ -109,6 +109,7 @@ under the License. + diff --git a/framework/example/widget/example/FormWidgetExampleForms.xml b/framework/example/widget/example/FormWidgetExampleForms.xml index 68f147d408..8f63bb401a 100644 --- a/framework/example/widget/example/FormWidgetExampleForms.xml +++ b/framework/example/widget/example/FormWidgetExampleForms.xml @@ -31,7 +31,7 @@ under the License. - + diff --git a/framework/images/webapp/images/jquery/plugins/jquery.maskedinput-1.2.2.min.js b/framework/images/webapp/images/jquery/plugins/jquery.maskedinput-1.2.2.min.js new file mode 100644 index 0000000000..b55421b95a --- /dev/null +++ b/framework/images/webapp/images/jquery/plugins/jquery.maskedinput-1.2.2.min.js @@ -0,0 +1,7 @@ +/* + Masked Input plugin for jQuery + Copyright (c) 2007-2009 Josh Bush (digitalbush.com) + Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) + Version: 1.2.2 (03/09/2009 22:39:06) +*/ +(function(a){var c=(a.browser.msie?"paste":"input")+".mask";var b=(window.orientation!=undefined);a.mask={definitions:{"9":"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"}};a.fn.extend({caret:function(e,f){if(this.length==0){return}if(typeof e=="number"){f=(typeof f=="number")?f:e;return this.each(function(){if(this.setSelectionRange){this.focus();this.setSelectionRange(e,f)}else{if(this.createTextRange){var g=this.createTextRange();g.collapse(true);g.moveEnd("character",f);g.moveStart("character",e);g.select()}}})}else{if(this[0].setSelectionRange){e=this[0].selectionStart;f=this[0].selectionEnd}else{if(document.selection&&document.selection.createRange){var d=document.selection.createRange();e=0-d.duplicate().moveStart("character",-100000);f=e+d.text.length}}return{begin:e,end:f}}},unmask:function(){return this.trigger("unmask")},mask:function(j,d){if(!j&&this.length>0){var f=a(this[0]);var g=f.data("tests");return a.map(f.data("buffer"),function(l,m){return g[m]?l:null}).join("")}d=a.extend({placeholder:"_",completed:null},d);var k=a.mask.definitions;var g=[];var e=j.length;var i=null;var h=j.length;a.each(j.split(""),function(m,l){if(l=="?"){h--;e=m}else{if(k[l]){g.push(new RegExp(k[l]));if(i==null){i=g.length-1}}else{g.push(null)}}});return this.each(function(){var r=a(this);var m=a.map(j.split(""),function(x,y){if(x!="?"){return k[x]?d.placeholder:x}});var n=false;var q=r.val();r.data("buffer",m).data("tests",g);function v(x){while(++x<=h&&!g[x]){}return x}function t(x){while(!g[x]&&--x>=0){}for(var y=x;y16&&z<32)||(z>32&&z<41));if((x.begin-x.end)!=0&&(!n||z==8||z==46)){w(x.begin,x.end)}if(z==8||z==46||(b&&z==127)){t(x.begin+(z==46?0:-1));return false}else{if(z==27){r.val(q);r.caret(0,p());return false}}}function o(B){if(n){n=false;return(B.keyCode==8)?false:null}B=B||window.event;var C=B.charCode||B.keyCode||B.which;var z=a(this).caret();if(B.ctrlKey||B.altKey||B.metaKey){return true}else{if((C>=32&&C<=125)||C>186){var x=v(z.begin-1);if(xz.length){break}}else{if(m[B]==z[x]&&B!=e){x++;C=B}}}if(!y&&C+1=e){s();if(!y){r.val(r.val().substring(0,C+1))}}}return(e?B:i)}if(!r.attr("readonly")){r.one("unmask",function(){r.unbind(".mask").removeData("buffer").removeData("tests")}).bind("focus.mask",function(){q=r.val();var x=p();s();setTimeout(function(){if(x==j.length){r.caret(0,x)}else{r.caret(x)}},0)}).bind("blur.mask",function(){p();if(r.val()!=q){r.change()}}).bind("keydown.mask",l).bind("keypress.mask",o).bind(c,function(){setTimeout(function(){r.caret(p(true))},0)})}p()})}})})(jQuery); \ No newline at end of file diff --git a/framework/widget/dtd/widget-form.xsd b/framework/widget/dtd/widget-form.xsd index a3f28cda6c..360b507e66 100644 --- a/framework/widget/dtd/widget-form.xsd +++ b/framework/widget/dtd/widget-form.xsd @@ -656,6 +656,15 @@ under the License. + + Will use the type value if set to yes, add a masked input + + + + + + + diff --git a/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java b/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java index 061b395539..19650f84b6 100644 --- a/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java +++ b/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java @@ -474,6 +474,7 @@ public class MacroFormRenderer implements FormStringRenderer { String className = ""; String alert = "false"; String name = ""; + String formattedMask = ""; String event = modelFormField.getEvent(); String action = modelFormField.getAction(context); if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) { @@ -610,6 +611,16 @@ public class MacroFormRenderer implements FormStringRenderer { ampmName = UtilHttp.makeCompositeParam(paramName, "ampm"); } } + String mask = dateTimeField.getMask(); + if ("Y".equals(mask)) { + if ("date".equals(dateTimeField.getType())) { + formattedMask = "9999-99-99"; + } else if ("time".equals(dateTimeField.getType())) { + formattedMask = "99:99:99"; + } else if ("timestamp".equals(dateTimeField.getType())) { + formattedMask = "9999-99-99 99:99:99.999"; + } + } StringWriter sr = new StringWriter(); sr.append("<@renderDateTimeField "); sr.append("name=\""); @@ -676,6 +687,8 @@ public class MacroFormRenderer implements FormStringRenderer { sr.append(compositeType); sr.append("\" formName=\""); sr.append(formName); + sr.append("\" mask=\""); + sr.append(formattedMask); sr.append("\" />"); executeMacro(writer, sr.toString()); this.addAsterisks(writer, context, modelFormField); diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java index 8dbd509df2..dbfafa83f7 100644 --- a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java +++ b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java @@ -3074,6 +3074,7 @@ public class ModelFormField { protected String inputMethod; protected String clock; protected String step; + protected String mask; protected DateTimeField() { super(); @@ -3093,6 +3094,7 @@ public class ModelFormField { type = element.getAttribute("type"); inputMethod = element.getAttribute("input-method"); clock = element.getAttribute("clock"); + mask = element.getAttribute("mask"); if (UtilValidate.isNotEmpty(element.getAttribute("step"))) { this.setStep(element.getAttribute("step")); } @@ -3126,6 +3128,10 @@ public class ModelFormField { return this.clock; } + public String getMask() { + return this.mask; + } + public String getStep() { return this.step; } diff --git a/framework/widget/templates/csvFormMacroLibrary.ftl b/framework/widget/templates/csvFormMacroLibrary.ftl index 2a752b4d2c..a807d0ef66 100644 --- a/framework/widget/templates/csvFormMacroLibrary.ftl +++ b/framework/widget/templates/csvFormMacroLibrary.ftl @@ -28,7 +28,7 @@ under the License. <#macro renderTextareaField name className alert cols rows id readonly value visualEditorEnable language buttons><@renderField value /> -<#macro renderDateTimeField name className alert title value size maxlength step timeValues id dateType shortDateInput timeDropdownParamName defaultDateTimeString localizedIconTitle timeDropdown timeHourName classString hour1 hour2 timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType formName><@renderField value /> +<#macro renderDateTimeField name className alert title value size maxlength step timeValues id dateType shortDateInput timeDropdownParamName defaultDateTimeString localizedIconTitle timeDropdown timeHourName classString hour1 hour2 timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType formName mask><@renderField value /> <#macro renderDropDownField name className alert id multiple formName otherFieldName event action size firstInList currentValue explicitDescription allowEmpty options fieldName otherFieldName otherValue otherFieldSize dDFCurrent ajaxEnabled noCurrentSelectedKey ajaxOptions frequency minChars choices autoSelect partialSearch partialChars ignoreCase fullSearch> <#if currentValue?has_content && firstInList?has_content> diff --git a/framework/widget/templates/foFormMacroLibrary.ftl b/framework/widget/templates/foFormMacroLibrary.ftl index d642254478..38b43d5927 100644 --- a/framework/widget/templates/foFormMacroLibrary.ftl +++ b/framework/widget/templates/foFormMacroLibrary.ftl @@ -52,7 +52,7 @@ under the License. <#macro renderTextareaField name className alert cols rows id readonly value visualEditorEnable language buttons><@makeBlock className value /> -<#macro renderDateTimeField name className alert title value size maxlength step timeValues id event action dateType shortDateInput timeDropdownParamName defaultDateTimeString localizedIconTitle timeDropdown timeHourName classString hour1 hour2 timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType formName><@makeBlock className value /> +<#macro renderDateTimeField name className alert title value size maxlength step timeValues id event action dateType shortDateInput timeDropdownParamName defaultDateTimeString localizedIconTitle timeDropdown timeHourName classString hour1 hour2 timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType formName mask><@makeBlock className value /> <#macro renderDropDownField name className alert id multiple formName otherFieldName event action size firstInList currentValue explicitDescription allowEmpty options fieldName otherFieldName otherValue otherFieldSize dDFCurrent ajaxEnabled noCurrentSelectedKey ajaxOptions frequency minChars choices autoSelect partialSearch partialChars ignoreCase fullSearch> <#if currentValue?has_content && firstInList?has_content> diff --git a/framework/widget/templates/htmlFormMacroLibrary.ftl b/framework/widget/templates/htmlFormMacroLibrary.ftl index f360b8f60a..dbba13bb3c 100644 --- a/framework/widget/templates/htmlFormMacroLibrary.ftl +++ b/framework/widget/templates/htmlFormMacroLibrary.ftl @@ -95,8 +95,13 @@ under the License. -<#macro renderDateTimeField name className alert title value size maxlength id dateType shortDateInput timeDropdownParamName defaultDateTimeString localizedIconTitle timeDropdown timeHourName classString hour1 hour2 timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType formName event="" action="" step="" timeValues=""> +<#macro renderDateTimeField name className alert title value size maxlength id dateType shortDateInput timeDropdownParamName defaultDateTimeString localizedIconTitle timeDropdown timeHourName classString hour1 hour2 timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType formName mask="" event="" action="" step="" timeValues=""> + <#if mask?has_content> + + ${event}="${action}" <@renderClass className alert /><#rt/> <#if title?has_content> title="${title}" <#if value?has_content> value="${value}"