Author: pyaschenko
Date: 2008-03-14 14:24:30 -0400 (Fri, 14 Mar 2008)
New Revision: 6832
Modified:
trunk/ui/suggestionbox/src/main/config/component/suggestionbox.xml
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java
trunk/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js
Log:
RF-1774
Modified: trunk/ui/suggestionbox/src/main/config/component/suggestionbox.xml
===================================================================
--- trunk/ui/suggestionbox/src/main/config/component/suggestionbox.xml 2008-03-14 18:19:50
UTC (rev 6831)
+++ trunk/ui/suggestionbox/src/main/config/component/suggestionbox.xml 2008-03-14 18:24:30
UTC (rev 6832)
@@ -171,6 +171,14 @@
update value of target element
</description>
</property>
+ <property>
+ <name>onobjectchange</name>
+ <classname>java.lang.String</classname>
+ <description>
+ JavaScript code for call when selected objects are changed
+ </description>
+ </property>
+
<!--
<property>
<name>popup</name>
Modified:
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java
===================================================================
---
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java 2008-03-14
18:19:50 UTC (rev 6831)
+++
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java 2008-03-14
18:24:30 UTC (rev 6832)
@@ -368,6 +368,15 @@
options.put("onselect", function);
}
+ String onobjectchange = (String) attributes.get("onobjectchange");
+ if (null != onobjectchange) {
+ JSFunctionDefinition function = new JSFunctionDefinition(
+ "suggestion","event");
+ function.addToBody(onobjectchange);
+
+ options.put("onobjectchange", function);
+
+ }
if (component.getValueBinding("fetchValue") != null
|| attributes.get("fetchValue") != null) {
options.put("select",
attributes.get("selectValueClass"));
Modified:
trunk/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js
===================================================================
---
trunk/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js 2008-03-14
18:19:50 UTC (rev 6831)
+++
trunk/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js 2008-03-14
18:24:30 UTC (rev 6832)
@@ -38,7 +38,7 @@
this.oldValue = this.element.value;
this.skipHover = false;
this.selectedItems = [];
- this.selectedItemsCache = [];
+ this.selectedItemsCache = {};
options.selection = update + "_selection";
@@ -579,12 +579,8 @@
var input = $(this.options.selection);
input.value = this.index;
var value="";
- if ( this.fetchValues && (value=this.fetchValues[this.index]) )
- {
- $(this.content+"_hiddenFetchValue").value = value;
- }
- this.updateElement(this.getCurrentEntry());
+ this.updateElement(this.getCurrentEntry(), event);
if (this.options.onselect) {
this.options.onselect(this, event);
}
@@ -594,7 +590,7 @@
input.value = "";
},
- updateElement: function(selectedElement) {
+ updateElement: function(selectedElement, event) {
if (this.options.updateElement) {
this.options.updateElement(selectedElement);
return;
@@ -608,7 +604,7 @@
} else
value = Element.collectTextNodesIgnoreClass(selectedElement,
'informal');
- this.insertValue(value);
+ this.insertValue(value, event);
this.oldValue = this.element.value;
this.element.focus();
@@ -710,10 +706,12 @@
updateItems:function (newValue)
{
+ this.isSelectedItemsUpdated = false;
+ var oldSelectedItems = this.selectedItems;
var value = newValue.replace(/^\s+/, '').replace(/\s+$/, '');
var itm="";
this.selectedItems = [];
- var newItemsCache = [];
+ var newItemsCache = {};
if (this.options.tokens.length!=0)
{
var re = new
RegExp('\\s*[\\'+this.options.tokens.join("|\\")+']\\s*');
@@ -734,14 +732,34 @@
newItemsCache[itm.text] = itm;
}
this.selectedItemsCache = newItemsCache;
+ // check for changes
+ if (this.selectedItems.length!=oldSelectedItems.length) this.isSelectedItemsUpdated
= true;
+ else
+ {
+ for (var i=0;i<this.selectedItems.length;i++)
+ {
+ if (this.selectedItems[0].text!=oldSelectedItems.text ||
this.selectedItems[0].object!=oldSelectedItems.object)
+ {
+ this.isSelectedItemsUpdated = true;
+ break;
+ }
+ }
+ }
},
updateSelectedItems: function (items)
{
for (var i=0;i<this.selectedItems.length; i++)
{
- var obj = items[this.selectedItems.text];
- if (obj) this.selectedItems.object = obj;
+ if (!this.selectedItems.object)
+ {
+ var obj = items[this.selectedItems.text];
+ if (obj)
+ {
+ this.selectedItems.object = obj;
+ this.isSelectedItemsUpdated = true;
+ }
+ }
}
},
@@ -753,7 +771,7 @@
{
for (var i=0;i<this.selectedItems.length; i++)
{
- if (!this.selectedItems[i].object) list.push(this.selectedItems[i].text);
+ if (!this.selectedItems[i].object && this.selectedItems[i].text.length==0)
list.push(this.selectedItems[i].text);
}
result = list.join(this.options.tokens[0]);
}
@@ -762,7 +780,7 @@
return result;
},
- insertValue: function(value)
+ insertValue: function(value, event)
{
var startStr = this.element.value.substr(0,this.startPosition);
var endStr = this.element.value.substr(this.endPosition);
@@ -788,14 +806,29 @@
// if object null we don't need to request data for it in future
var itm = {text:value, object:this.fetchValues[this.index]}
+ var flag = (this.selectedItems[index].text != value ||
this.selectedItems[index].object==null ? true : false);
this.selectedItemsCache[value] = itm;
this.selectedItems[index] = itm;
+ if (flag)
+ {
+ //call user listner
+ this.callOnObjectChangeListener(event);
+ }
},
getToken: function() {
var ret = this.element.value.substring(this.startPosition,
this.endPosition).replace(/^\s+/, '').replace(/\s+$/, '');
return /\n/.test(ret) ? '' : ret;
+ },
+
+ callOnObjectChangeListener: function(event)
+ {
+ if (this.options.onobjectchange)
+ {
+ this.options.onobjectchange(this, event);
+ }
}
+
}
@@ -822,7 +855,6 @@
getUpdatedChoices: function(event) {
this.options.parameters[this.options.param] = this.getToken();
- // TODO-done: add string list parameter for the values that need object from server
this.options.parameters[this.options.param + "request"] =
this.getItemListForUpdate();
if (this.onsubmitFunction && ! this.onsubmitFunction()) {
return;
@@ -841,9 +873,14 @@
}
this.updateChoices();
if (data) {
- this.fetchValues = data.suggestionObjects; // use data.suggestionObjects
- // TODO-done: add update item objects from data
- this.updateSelectedItems(data.requestedObjects); // use data.requestedObjects
+ this.fetchValues = data.suggestionObjects;
+ this.updateSelectedItems(data.requestedObjects);
+ if (this.isSelectedItemsUpdated)
+ {
+ //call user listner
+ this.isSelectedItemsUpdated = false;
+ this.callOnObjectChangeListener(event);
+ }
LOG.debug("Choices updated");
}