Author: pyaschenko
Date: 2008-03-12 12:15:56 -0400 (Wed, 12 Mar 2008)
New Revision: 6763
Modified:
trunk/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js
Log:
RF-1774
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-12
16:10:44 UTC (rev 6762)
+++
trunk/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js 2008-03-12
16:15:56 UTC (rev 6763)
@@ -1,4 +1,25 @@
-if (!window.RichFaces) window.RichFaces = {};
+if (!window.Richfaces) window.Richfaces = {};
+if (!Richfaces.Selection) Richfaces.Selection = {};
+Richfaces.Selection.getStart = function(element)
+{
+ if (element.createTextRange) {
+ var r = document.selection.createRange().duplicate();
+ r.moveEnd('character', element.value.length);
+ if (r.text == '') return element.value.length;
+ return element.value.lastIndexOf(r.text);
+ } else return element.selectionStart;
+}
+
+
+Richfaces.Selection.getEnd = function(element)
+{
+ if (element.createTextRange) {
+ var r = document.selection.createRange().duplicate();
+ r.moveStart('character', -element.value.length);
+ return r.text.length;
+ } else return element.selectionEnd;
+}
+
var Suggestion = {};
Suggestion.Base = function() {
};
@@ -16,6 +37,8 @@
this.keyEvent = false;
this.oldValue = this.element.value;
this.skipHover = false;
+ this.selectedItems = [];
+ this.selectedItemsCache = [];
options.selection = update + "_selection";
@@ -585,17 +608,6 @@
} else
value = Element.collectTextNodesIgnoreClass(selectedElement,
'informal');
- /*var lastTokenPos = this.findLastToken();
- if (lastTokenPos != -1) {
- var newValue = this.element.value.substr(0, lastTokenPos + 1);
- var whitespace = this.element.value.substr(lastTokenPos
- + 1).match(/^\s+/);
- if (whitespace)
- newValue += whitespace[0];
- this.element.value = newValue + value;
- } else {
- this.element.value = value;
- }*/
this.insertValue(value);
this.oldValue = this.element.value;
this.element.focus();
@@ -664,22 +676,22 @@
LOG.debug("Observer event occurs");
this.changed = false;
var oldValue = this.element.value;
-
+
if (this.oldValue!=oldValue)
{
- this.endPosition = 0;
this.startPosition = 0;
- var tokens = this.options.tokens.join('');
- while (this.oldValue[this.endPosition]==oldValue[this.endPosition])
+ this.endPosition = oldValue.length-1;
+ if (this.options.tokens.length!=0)
{
- if (tokens.indexOf(oldValue[this.endPosition])!=-1) this.startPosition =
this.endPosition+1;
- this.endPosition++;
+ var tokens = this.options.tokens.join('');
+ this.startPosition = this.endPosition =
Richfaces.Selection.getStart(this.element);
+
+ while (this.endPosition<oldValue.length &&
tokens.indexOf(oldValue[this.endPosition])==-1) this.endPosition++;
+
+ while (this.startPosition>0 &&
tokens.indexOf(oldValue[this.startPosition-1])==-1) this.startPosition--;
}
- if (oldValue.length>this.oldValue.length &&
this.startPosition!=this.endPosition &&
tokens.indexOf(oldValue[this.endPosition])!=-1) this.startPosition = ++this.endPosition;
-
- while (oldValue[this.startPosition]==' ') this.startPosition++;
- while (this.endPosition<oldValue.length &&
tokens.indexOf(oldValue[this.endPosition])==-1) this.endPosition++;
- //alert(this.startPosition + ","+ this.endPosition+":
"+oldValue.substring(this.startPosition, this.endPosition));
+
+ this.updateItems(oldValue);
}
if (this.getToken().length >= this.options.minChars) {
@@ -695,54 +707,69 @@
this.oldValue = oldValue;
this.observerHandle = null;
},
-
- /*getToken: function() {
- var tokenPos = this.findLastToken();
- if (tokenPos != -1)
- var ret = this.element.value.substr(tokenPos
- + 1).replace(/^\s+/, '').replace(/\s+$/, '');
- else
- var ret = this.element.value;
-
- return /\n/.test(ret) ? '' : ret;
- },*/
- insertValue: function(value)
+ updateItems:function (newValue)
{
- //if (this.startPosition && this.endPosition)
- //{
- var startStr = this.element.value.substr(0,this.startPosition);
- var endStr = this.element.value.substr(this.endPosition);
- var whitespace = this.element.value.substring(this.startPosition,
this.endPosition).match(/\s+$/);
- if (whitespace)
- endStr = whitespace[0] + endStr;
- this.element.value = startStr + value + endStr;
- //}
- /*else
+ var value = newValue.replace(/^\s+/, '').replace(/\s+$/, '');
+ var itm="";
+ this.selectedItems = [];
+ var newItemsCache = [];
+ if (this.options.tokens.length!=0)
{
- this.element.value = value;
- }*/
+ var re = new
RegExp('\\s*[\\'+this.options.tokens.join("|\\")+']\\s*');
+ var items = value.split(re);
+ for (var i=0;i<items.length;i++)
+ {
+ itm = this.selectedItemsCache[items[i]];
+ if (!itm) itm = {text:items[i], object: undefined};
+ this.selectedItems.push(itm);
+ newItemsCache[itm.text] = itm;
+ }
+ }
+ else
+ {
+ itm = this.selectedItemsCache[value];
+ if (!itm) itm = {text:items[i], object: undefined};
+ this.selectedItems.push(itm);
+ newItemsCache[itm.text] = itm;
+ }
+ this.selectedItemsCache = newItemsCache;
},
+ insertValue: function(value)
+ {
+ var startStr = this.element.value.substr(0,this.startPosition);
+ var endStr = this.element.value.substr(this.endPosition);
+ var str = this.element.value.substring(this.startPosition, this.endPosition);
+ var whitespace = str.match(/^\s+/);
+ if (whitespace) startStr += whitespace[0];
+ whitespace = str.match(/\s+$/);
+ if (whitespace) endStr = whitespace[0] + endStr;
+ this.element.value = startStr + value + endStr;
+
+ var index = 0;
+ if (this.options.tokens.length!=0)
+ {
+ // search index for update item's object
+ var tokens = this.options.tokens.join('');
+ var p = 0;
+ while (p<this.startPosition)
+ {
+ if (tokens.indexOf(this.element.value[p])!=-1) index++;
+ p++;
+ }
+ }
+
+ // if object null we don't need to request data for it in future
+ var itm = {text:value, object:this.fetchValues[this.index]}
+ this.selectedItemsCache[value] = itm;
+ this.selectedItems[index] = itm;
+ },
+
getToken: function() {
- //if (this.startPosition && this.endPosition)
- var ret = this.element.value.substring(this.startPosition,
this.endPosition).replace(/^\s+/, '').replace(/\s+$/, '');
- /*else
- var ret = this.element.value;*/
-
+ var ret = this.element.value.substring(this.startPosition,
this.endPosition).replace(/^\s+/, '').replace(/\s+$/, '');
return /\n/.test(ret) ? '' : ret;
- }/*,
-
- findLastToken: function() {
- var lastTokenPos = -1;
-
- for (var i = 0; i < this.options.tokens.length; i++) {
- var thisTokenPos = this.element.value.lastIndexOf(this.options.tokens[i]);
- if (thisTokenPos > lastTokenPos)
- lastTokenPos = thisTokenPos;
- }
- return lastTokenPos;
- }*/
+ }
}
@@ -769,7 +796,8 @@
getUpdatedChoices: function(event) {
this.options.parameters[this.options.param] = this.getToken();
-
+ // TODO add string list parameter for the values that need object from server
+
if (this.onsubmitFunction && ! this.onsubmitFunction()) {
return;
}
@@ -787,6 +815,7 @@
}
this.updateChoices();
this.fetchValues = data;
+ // TODO add update item objects from data
LOG.debug("Choices updated");
if (this.options.onajaxcomplete) {
this.options.onajaxcomplete(request, event);
Show replies by date