Author: pyaschenko
Date: 2010-07-09 10:10:13 -0400 (Fri, 09 Jul 2010)
New Revision: 17780
Modified:
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/ComboBox.js
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/SelectBase.js
Log:
https://jira.jboss.org/browse/RF-8875
Modified:
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/ComboBox.js
===================================================================
---
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/ComboBox.js 2010-07-09
13:35:41 UTC (rev 17779)
+++
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/ComboBox.js 2010-07-09
14:10:13 UTC (rev 17780)
@@ -10,6 +10,10 @@
var getItems = function (key) {
var newCache = [];
+
+ if (key.length < this.key.length) {
+ return newCache;
+ }
if (this.cache[key]) {
newCache = this.cache[key];
@@ -34,10 +38,15 @@
return newCache;
};
+
+ var isCached = function (key) {
+ return this.cache[key];
+ }
$.extend(rf.utils.Cache.prototype, (function () {
return {
- getItems: getItems
+ getItems: getItems,
+ isCached: isCached
};
})());
@@ -55,8 +64,9 @@
$p.attachToDom.call(this, componentId);
this.componentId = componentId;
this.options = $.extend(this.options, defaultOptions, options);
- //this.currentValue = rf.getDomElement(this.fieldId).value;
+ this.inputValue = rf.getDomElement(this.fieldId).value;
this.index = -1;
+ this.isFirstAjax = true;
bindEventHandlers.call(this);
updateItemsList.call(this, "");
};
@@ -72,14 +82,25 @@
var defaultOptions = {
selectedItemClass:'cb_select',
autoFill:true,
- minChars:0,
- selectFirst:false
+ minChars:1,
+ selectFirst:true,
+ ajaxMode:true
};
var ID = {
SELECT:'List',
ITEMS:'Items'
};
+
+ var REGEXP_TRIM = /^[\n\s]*(.*)[\n\s]*$/;
+
+ var getData = function (nodeList) {
+ var data = [];
+ nodeList.each(function () {;
+ data.push($(this).text().replace(REGEXP_TRIM, "$1"));
+ });
+ return data;
+ }
var bindEventHandlers = function () {
rf.Event.bind(rf.getDomElement(this.componentId+ID.ITEMS).parentNode,
"mouseover"+this.namespace, onMouseOver, this);
@@ -105,14 +126,6 @@
});
};
- var getData = function (nodeList) {
- var data = [];
- nodeList.each(function () {
- data.push($(this).text());
- });
- return data;
- };
-
var scrollToSelectedItem = function() {
var offset = 0;
this.items.slice(0, this.index).each(function() {
@@ -132,19 +145,34 @@
var autoFill = function (inputValue, value) {
if( this.options.autoFill) {
var field = rf.getDomElement(this.fieldId);
+ var start = rf.Selection.getStart(field);
field.value = inputValue + value.substring(inputValue.length);
- rf.Selection.set(field, inputValue.length, field.value.length);
+ rf.Selection.set(field, start, field.value.length);
}
};
var callAjax = function(event) {
+
+ var _this = this;
+ var ajaxSuccess = function () {
+ updateItemsList.call(_this, _this.inputValue);
+ if (_this.options.selectFirst) {
+ _this.selectItem(0);
+ }
+ }
+
+ var ajaxError = function () {
+ alert("error");
+ }
+
+ this.isFirstAjax = false;
//caution: JSF submits inputs with empty names causing "WARNING: Parameters:
Invalid chunk ignored." in Tomcat log
var params = {};
params[this.componentId + ".ajax"] = "1";
- rf.ajax(this.componentId, event, {parameters: params});
+ rf.ajax(this.componentId, event, {parameters: params, error: ajaxError,
complete:ajaxSuccess});
};
-
+
// Add new properties and methods
$.extend(rf.ui.ComboBox.prototype, (function () {
return {
@@ -189,7 +217,7 @@
item.addClass(this.options.selectedItemClass);
scrollToSelectedItem.call(this);
- !noAutoFill && autoFill.call(this, this.newValue, item.text());
+ !noAutoFill && autoFill.call(this, this.inputValue, getData(item)[0]);
},
selectPrevItem: function () {
@@ -205,19 +233,33 @@
},
onBeforeShow: function (event) {
- callAjax.call(this, event);
},
changeValue: function (event, value) {
+ this.selectItem();
+
+ if (typeof value == "undefined") {
+ // called from show method, not actually value changed
+ if (this.items.length==0 &&
this.inputValue.length>=this.options.minChars && this.isFirstAjax) {
+ this.options.ajaxMode && callAjax.call(this, event);
+ }
+ return;
+ }
+
// TODO: ajax call here if needed
-
- this.selectItem();
+ if (( value.indexOf(this.cache.key)!=0 || this.inputValue.length==0) &&
+ value.length>=this.options.minChars) {
+ this.inputValue = value;
+ this.options.ajaxMode && callAjax.call(this, event);
+ return;
+ }
+
var newItems = this.cache.getItems(value);
this.items = $(newItems);
//TODO: works only with simple markup, not with <tr>
$(rf.getDomElement(this.componentId+ID.ITEMS)).empty().append(newItems);
this.index = -1;
- this.newValue = value;
+ this.inputValue = value;
if (this.options.selectFirst) {
this.selectItem(0, false, event.which == rf.KEYS.BACKSPACE);
}
Modified:
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/SelectBase.js
===================================================================
---
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/SelectBase.js 2010-07-09
13:35:41 UTC (rev 17779)
+++
root/ui-sandbox/inputs/trunk/combobox/src/main/resources/META-INF/resources/org.richfaces/SelectBase.js 2010-07-09
14:10:13 UTC (rev 17780)
@@ -108,7 +108,7 @@
if (this.isVisible) {
this.hide(event);
} else {
- this.show(event);
+ onShow.call(this, event);
//rf.getDomElement(this.fieldId).focus();
}
};
@@ -135,17 +135,21 @@
var onChange = function (event) {
var value = this.getInputValue();
var flag = value != this.currentValue;
+ //TODO: is it needed to chesk keys?
if (event.which == rf.KEYS.LEFT || event.which == rf.KEYS.RIGHT || flag) {
if (flag) {
this.changeValue(event, value);
this.currentValue = value;
- if (!this.isVisible) {
- this.show();
- }
+ onShow.call(this, event, true);
}
}
}
+ var onShow = function (event, noChangeValue) {
+ !noChangeValue && this.changeValue(event);
+ this.show(event);
+ }
+
/*var checkOnBlur = function (event) {
var e = $(rf.getDomElement(this.options.buttonId));
@@ -165,7 +169,7 @@
if (this.isVisible) {
this.selectNextItem();
} else {
- this.show();
+ onShow.call(this, event);
}
break;
case rf.KEYS.PAGEUP:
@@ -209,22 +213,26 @@
return {
name:"SelectBase",
show: function (event) {
- if (this.onBeforeShow(event)!=false) {
- $(rf.getDomElement(this.selectId)).setPosition({id: this.fieldId},
{type:"DROPDOWN", offset:[0,20]}).show();
- this.isVisible = true;
- this.scrollElements = rf.Event.bindScrollEventHandlers(this.selectId, this.hide,
this, this.namespace);
- if (this.onShow) {
- this.onShow(event);
- }
+ if (!this.isVisible) {
+ if (this.onBeforeShow(event)!=false) {
+ $(rf.getDomElement(this.selectId)).setPosition({id: this.fieldId},
{type:"DROPDOWN", offset:[0,20]}).show();
+ this.isVisible = true;
+ this.scrollElements = rf.Event.bindScrollEventHandlers(this.selectId, this.hide,
this, this.namespace);
+ if (this.onShow) {
+ this.onShow(event);
+ }
+ }
}
},
hide: function (event) {
- rf.Event.unbindScrollEventHandlers(this.scrollElements, this);
- $(rf.getDomElement(this.selectId)).hide();
- this.isVisible = false;
- if (this.onHide) {
- this.onHide(event);
- }
+ if (this.isVisible) {
+ rf.Event.unbindScrollEventHandlers(this.scrollElements, this);
+ $(rf.getDomElement(this.selectId)).hide();
+ this.isVisible = false;
+ if (this.onHide) {
+ this.onHide(event);
+ }
+ }
},
destroy: function () {
//TODO: add all unbind