From a77ea70bed0725ac8709df308e443a319bc7b031 Mon Sep 17 00:00:00 2001 From: Musachy Barroso Date: Fri, 23 Mar 2007 00:51:54 +0000 Subject: [PATCH] WW-1607 Add ajax notify topics to autocompleter git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@521551 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/webapp/ajax/autocompleter/index.jsp | 2 +- .../dojo/components/Autocompleter.java | 33 +++++++++ .../dojo/views/jsp/ui/AutocompleterTag.java | 24 ++++++ .../struts2/static/dojo/struts/widget/Bind.js | 4 +- .../static/dojo/struts/widget/BindDiv.js | 4 +- .../static/dojo/struts/widget/ComboBox.js | 73 +++++++++++++++++-- .../resources/template/ajax/autocompleter.ftl | 12 +++ .../dojo/views/jsp/ui/AutocompleterTest.java | 2 + .../dojo/views/jsp/ui/Autocompleter-1.txt | 1 + .../dojo/views/jsp/ui/Autocompleter-2.txt | 1 + 10 files changed, 145 insertions(+), 11 deletions(-) diff --git a/apps/showcase/src/main/webapp/ajax/autocompleter/index.jsp b/apps/showcase/src/main/webapp/ajax/autocompleter/index.jsp index c9d3732b8..71b990afa 100644 --- a/apps/showcase/src/main/webapp/ajax/autocompleter/index.jsp +++ b/apps/showcase/src/main/webapp/ajax/autocompleter/index.jsp @@ -72,7 +72,7 @@ Disabled combobox (disabled="true") Link two autocompleter elements. When the selected value in 'Autocompleter 1' changes, the available values in 'Autocompleter 2' will change also.
-

Autocompleter 1

+

Autocompleter 1

Autocompleter 2 diff --git a/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/Autocompleter.java b/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/Autocompleter.java index f5006d7d0..76a081aa2 100644 --- a/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/Autocompleter.java +++ b/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/Autocompleter.java @@ -71,6 +71,10 @@ public class Autocompleter extends ComboBox { protected String iconPath; protected String keyName; protected String dataFieldName; + protected String beforeNotifyTopics; + protected String afterNotifyTopics; + protected String errorNotifyTopics; + protected String valueNotifyTopics; public Autocompleter(ValueStack stack, HttpServletRequest request, HttpServletResponse response) { @@ -144,6 +148,15 @@ public class Autocompleter extends ComboBox { String keyNameExpr = "%{" + keyName + "}"; addParameter("key", findString(keyNameExpr)); + + if (beforeNotifyTopics != null) + addParameter("beforeNotifyTopics", findString(beforeNotifyTopics)); + if (afterNotifyTopics != null) + addParameter("afterNotifyTopics", findString(afterNotifyTopics)); + if (errorNotifyTopics != null) + addParameter("errorNotifyTopics", findString(errorNotifyTopics)); + if (valueNotifyTopics != null) + addParameter("valueNotifyTopics", findString(valueNotifyTopics)); } @Override @@ -292,4 +305,24 @@ public class Autocompleter extends ComboBox { public void setValue(String arg0) { super.setValue(arg0); } + + @StrutsTagAttribute(description="Comma delimmited list of topics that will published after the request(if the request succeeds)") + public void setAfterNotifyTopics(String afterNotifyTopics) { + this.afterNotifyTopics = afterNotifyTopics; + } + + @StrutsTagAttribute(description="Comma delimmited list of topics that will published before the request") + public void setBeforeNotifyTopics(String beforeNotifyTopics) { + this.beforeNotifyTopics = beforeNotifyTopics; + } + + @StrutsTagAttribute(description="Comma delimmited list of topics that will published after the request(if the request fails)") + public void setErrorNotifyTopics(String errorNotifyTopics) { + this.errorNotifyTopics = errorNotifyTopics; + } + + @StrutsTagAttribute(description="Comma delimmited list of topics that will published when a value is selected") + public void setValueNotifyTopics(String valueNotifyTopics) { + this.valueNotifyTopics = valueNotifyTopics; + } } diff --git a/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTag.java b/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTag.java index 95890cc0d..144055308 100644 --- a/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTag.java +++ b/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTag.java @@ -55,6 +55,10 @@ public class AutocompleterTag extends ComboBoxTag { protected String iconPath; protected String keyName; protected String dataFieldName; + protected String beforeNotifyTopics; + protected String afterNotifyTopics; + protected String errorNotifyTopics; + protected String valueNotifyTopics; public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { return new Autocompleter(stack, req, res); @@ -84,6 +88,10 @@ public class AutocompleterTag extends ComboBoxTag { autocompleter.setIconPath(iconPath); autocompleter.setKeyName(keyName); autocompleter.setDataFieldName(dataFieldName); + autocompleter.setAfterNotifyTopics(afterNotifyTopics); + autocompleter.setBeforeNotifyTopics(beforeNotifyTopics); + autocompleter.setErrorNotifyTopics(errorNotifyTopics); + autocompleter.setValueNotifyTopics(valueNotifyTopics); } public void setAutoComplete(String autoComplete) { @@ -169,4 +177,20 @@ public class AutocompleterTag extends ComboBoxTag { public void setDataFieldName(String dataFieldName) { this.dataFieldName = dataFieldName; } + + public void setAfterNotifyTopics(String afterNotifyTopics) { + this.afterNotifyTopics = afterNotifyTopics; + } + + public void setBeforeNotifyTopics(String beforeNotifyTopics) { + this.beforeNotifyTopics = beforeNotifyTopics; + } + + public void setErrorNotifyTopics(String errorNotifyTopics) { + this.errorNotifyTopics = errorNotifyTopics; + } + + public void setValueNotifyTopics(String valueNotifyTopics) { + this.valueNotifyTopics = valueNotifyTopics; + } } diff --git a/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/Bind.js b/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/Bind.js index 8923a6df2..e947089c2 100644 --- a/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/Bind.js +++ b/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/Bind.js @@ -183,12 +183,12 @@ dojo.widget.defineWidget( this.notifyTo(topicsArray, data, type, e); }, - notifyTo : function(topicsArray, data, e) { + notifyTo : function(topicsArray, data, type, e) { var self = this; if(topicsArray) { dojo.lang.forEach(topicsArray, function(topic) { try { - dojo.event.topic.publish(topic, data, e); + dojo.event.topic.publish(topic, data, type, e); } catch(ex){ self.log(ex); } diff --git a/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/BindDiv.js b/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/BindDiv.js index 2749b0e86..0dda0f5d3 100644 --- a/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/BindDiv.js +++ b/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/BindDiv.js @@ -117,12 +117,12 @@ dojo.widget.defineWidget( this.notifyTo(topicsArray, data, type, e); }, - notifyTo : function(topicsArray, data, e) { + notifyTo : function(topicsArray, data, type, e) { var self = this; if(topicsArray) { dojo.lang.forEach(topicsArray, function(topic) { try { - dojo.event.topic.publish(topic, data, e); + dojo.event.topic.publish(topic, data, type, e); } catch(ex){ self.log(ex); } diff --git a/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/ComboBox.js b/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/ComboBox.js index 5674a9994..ee861e556 100644 --- a/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/ComboBox.js +++ b/plugins/dojo/src/main/resources/org/apache/struts2/static/dojo/struts/widget/ComboBox.js @@ -59,8 +59,9 @@ struts.widget.ComboBoxDataProvider = function(/*Array*/ dataPairs, /*Number*/ li url: url, formNode: dojo.byId(this.formId), formFilter: window[this.formFilter], - load: dojo.lang.hitch(this, function(type, data, evt) { - //show indicator + handler: dojo.lang.hitch(this, function(type, data, evt) { + this.firstRequest = false; + //hide indicator dojo.html.hide(this.cbox.indicator); //if notifyTopics is published on the first request (onload) @@ -92,7 +93,6 @@ struts.widget.ComboBoxDataProvider = function(/*Array*/ dataPairs, /*Number*/ li data = arrData; } this.setData(data); - this.firstRequest = false; }), mimetype: "text/json" }); @@ -215,7 +215,14 @@ dojo.widget.defineWidget( listenTopics : "", notifyTopics : "", notifyTopicsArray : null, - + beforeNotifyTopics : "", + beforeNotifyTopicsArray : null, + afterNotifyTopics : "", + afterNotifyTopicsArray : null, + errorNotifyTopics : "", + errorNotifyTopicsArray : null, + valueNotifyTopics : "", + valueNotifyTopicsArray : null, indicator : "", formId : "", @@ -234,7 +241,7 @@ dojo.widget.defineWidget( //dojo has "stringstart" which is invalid searchType: "STARTSTRING", - dataFieldName : "" , + dataFieldName : "", keyName: "", templateCssPath: dojo.uri.dojoUri("struts/ComboBox.css"), //from Dojo's ComboBox @@ -341,9 +348,30 @@ dojo.widget.defineWidget( } } + //notify topics if(!dojo.string.isBlank(this.notifyTopics)) { this.notifyTopicsArray = this.notifyTopics.split(","); } + + //before topics + if(!dojo.string.isBlank(this.beforeNotifyTopics)) { + this.beforeNotifyTopicsArray = this.beforeNotifyTopics.split(","); + } + + //after topics + if(!dojo.string.isBlank(this.afterNotifyTopics)) { + this.afterNotifyTopicsArray = this.afterNotifyTopics.split(","); + } + + //error topics + if(!dojo.string.isBlank(this.errorNotifyTopics)) { + this.errorNotifyTopicsArray = this.errorNotifyTopics.split(","); + } + + //value topics + if(!dojo.string.isBlank(this.valueNotifyTopics)) { + this.valueNotifyTopicsArray = this.valueNotifyTopics.split(","); + } //better name this.comboBoxSelectionValue.name = dojo.string.isBlank(this.keyName) ? this.name + "Key" : this.keyName; @@ -376,11 +404,44 @@ dojo.widget.defineWidget( notify : function(data, type, e) { if(this.notifyTopicsArray) { + var self = this; dojo.lang.forEach(this.notifyTopicsArray, function(topic) { try { dojo.event.topic.publish(topic, data, type, e); } catch(ex) { - dojo.debug(ex); + self.log(ex); + } + }); + } + + //before, after and error topics + var topicsArray = null; + switch(type) { + case "before": + topicsArray = this.beforeNotifyTopicsArray; + break; + case "load": + topicsArray = this.afterNotifyTopicsArray; + break; + case "error": + topicsArray = this.errorNotifyTopicsArray; + break; + case "valuechanged": + topicsArray = this.valueNotifyTopicsArray; + break; + } + + this.notifyTo(topicsArray, data, type, e); + }, + + notifyTo : function(topicsArray, data, type, e) { + var self = this; + if(topicsArray) { + dojo.lang.forEach(topicsArray, function(topic) { + try { + dojo.event.topic.publish(topic, data, type, e); + } catch(ex){ + self.log(ex); } }); } diff --git a/plugins/dojo/src/main/resources/template/ajax/autocompleter.ftl b/plugins/dojo/src/main/resources/template/ajax/autocompleter.ftl index 62a6d16ed..ac74c1629 100644 --- a/plugins/dojo/src/main/resources/template/ajax/autocompleter.ftl +++ b/plugins/dojo/src/main/resources/template/ajax/autocompleter.ftl @@ -71,6 +71,18 @@ <#if parameters.notifyTopics?if_exists != ""> notifyTopics="${parameters.notifyTopics?html}"<#rt/> +<#if parameters.beforeNotifyTopics?if_exists != ""> + beforeNotifyTopics="${parameters.beforeNotifyTopics?html}"<#rt/> + +<#if parameters.afterNotifyTopics?if_exists != ""> + afterNotifyTopics="${parameters.afterNotifyTopics?html}"<#rt/> + +<#if parameters.errorNotifyTopics?if_exists != ""> + errorNotifyTopics="${parameters.errorNotifyTopics?html}"<#rt/> + +<#if parameters.valueNotifyTopics?if_exists != ""> + valueNotifyTopics="${parameters.valueNotifyTopics?html}"<#rt/> + <#if parameters.indicator?if_exists != ""> indicator="${parameters.indicator?html}"<#rt/> diff --git a/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTest.java b/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTest.java index ec3e438f5..f5e24e346 100644 --- a/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTest.java +++ b/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/AutocompleterTest.java @@ -47,6 +47,7 @@ public class AutocompleterTest extends AbstractUITagTest { tag.setIconPath("i"); tag.setTemplateCssPath("j"); tag.setDataFieldName("k"); + tag.setValueNotifyTopics("l"); tag.doStartTag(); tag.doEndTag(); @@ -68,6 +69,7 @@ public class AutocompleterTest extends AbstractUITagTest { tag.setName("f"); tag.setIconPath("i"); tag.setTemplateCssPath("j"); + tag.setValueNotifyTopics("k"); tag.doStartTag(); tag.doEndTag(); diff --git a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-1.txt b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-1.txt index 125235b24..0a44ccfc6 100644 --- a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-1.txt +++ b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-1.txt @@ -11,6 +11,7 @@ name="f" keyName="i" initialValue="g" + valueNotifyTopics="l" indicator="h" loadOnType="true" loadMinimum="3" diff --git a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-2.txt b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-2.txt index b6bbb7e95..c780b4e4c 100644 --- a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-2.txt +++ b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/Autocompleter-2.txt @@ -9,6 +9,7 @@ dropdownHeight="10" name="f" keyName="fKey" + valueNotifyTopics="k" visibleDownArrow="true" buttonSrc="i" templateCssPath="j"