/*
* Based on:
* Ext JS Library 1.1.1
* Copyright(c) 2006-2007, Ext JS, LLC.
*
* Originally Released Under LGPL - original licence link has changed is not relivant.
*
* Fork - LGPL
* <script type="text/javascript">
*/
/**
* @class Roo.form.ComboBox
* @extends Roo.form.TriggerField
* A combobox control with support for autocomplete, remote-loading, paging and many other features.
* @constructor
* Create a new ComboBox.
* @param {Object} config Configuration options
*/
Roo.form.Select = function(config){
Roo.form.Select.superclass.constructor.call(this, config);
};
Roo.extend(Roo.form.Select , Roo.form.ComboBox, {
/**
* @cfg {String/HTMLElement/Element} transform The id, DOM node or element of an existing select to convert to a ComboBox
*/
/**
* @cfg {Boolean} lazyRender True to prevent the ComboBox from rendering until requested (should always be used when
* rendering into an Roo.Editor, defaults to false)
*/
/**
* @cfg {Boolean/Object} autoCreate A DomHelper element spec, or true for a default element spec (defaults to:
* {tag: "input", type: "text", size: "24", autocomplete: "off"})
*/
/**
* @cfg {Roo.data.Store} store The data store to which this combo is bound (defaults to undefined)
*/
/**
* @cfg {String} title If supplied, a header element is created containing this text and added into the top of
* the dropdown list (defaults to undefined, with no header element)
*/
/**
* @cfg {String/Roo.Template} tpl The template to use to render the output
*/
// private
defaultAutoCreate : {tag: "select" },
/**
* @cfg {Number} listWidth The width in pixels of the dropdown list (defaults to the width of the ComboBox field)
*/
listWidth: undefined,
/**
* @cfg {String} displayField The underlying data field name to bind to this CombBox (defaults to undefined if
* mode = 'remote' or 'text' if mode = 'local')
*/
displayField: undefined,
/**
* @cfg {String} valueField The underlying data value name to bind to this CombBox (defaults to undefined if
* mode = 'remote' or 'value' if mode = 'local').
* Note: use of a valueField requires the user make a selection
* in order for a value to be mapped.
*/
valueField: undefined,
/**
* @cfg {String} hiddenName If specified, a hidden form field with this name is dynamically generated to store the
* field's data value (defaults to the underlying DOM element's name)
*/
hiddenName: undefined,
/**
* @cfg {String} listClass CSS class to apply to the dropdown list element (defaults to '')
*/
listClass: '',
/**
* @cfg {String} selectedClass CSS class to apply to the selected item in the dropdown list (defaults to 'x-combo-selected')
*/
selectedClass: 'x-combo-selected',
/**
* @cfg {String} triggerClass An additional CSS class used to style the trigger button. The trigger will always get the
* class 'x-form-trigger' and triggerClass will be <b>appended</b> if specified (defaults to 'x-form-arrow-trigger'
* which displays a downward arrow icon).
*/
triggerClass : 'x-form-arrow-trigger',
/**
* @cfg {Boolean/String} shadow True or "sides" for the default effect, "frame" for 4-way shadow, and "drop" for bottom-right
*/
shadow:'sides',
/**
* @cfg {String} listAlign A valid anchor position value. See {@link Roo.Element#alignTo} for details on supported
* anchor positions (defaults to 'tl-bl')
*/
listAlign: 'tl-bl?',
/**
* @cfg {Number} maxHeight The maximum height in pixels of the dropdown list before scrollbars are shown (defaults to 300)
*/
maxHeight: 300,
/**
* @cfg {String} triggerAction The action to execute when the trigger field is activated. Use 'all' to run the
* query specified by the allQuery config option (defaults to 'query')
*/
triggerAction: 'query',
/**
* @cfg {Number} minChars The minimum number of characters the user must type before autocomplete and typeahead activate
* (defaults to 4, does not apply if editable = false)
*/
minChars : 4,
/**
* @cfg {Boolean} typeAhead True to populate and autoselect the remainder of the text being typed after a configurable
* delay (typeAheadDelay) if it matches a known value (defaults to false)
*/
typeAhead: false,
/**
* @cfg {Number} queryDelay The length of time in milliseconds to delay between the start of typing and sending the
* query to filter the dropdown list (defaults to 500 if mode = 'remote' or 10 if mode = 'local')
*/
queryDelay: 500,
/**
* @cfg {Number} pageSize If greater than 0, a paging toolbar is displayed in the footer of the dropdown list and the
* filter queries will execute with page start and limit parameters. Only applies when mode = 'remote' (defaults to 0)
*/
pageSize: 0,
/**
* @cfg {Boolean} selectOnFocus True to select any existing text in the field immediately on focus. Only applies
* when editable = true (defaults to false)
*/
selectOnFocus:false,
/**
* @cfg {String} queryParam Name of the query as it will be passed on the querystring (defaults to 'query')
*/
queryParam: 'query',
/**
* @cfg {String} loadingText The text to display in the dropdown list while data is loading. Only applies
* when mode = 'remote' (defaults to 'Loading...')
*/
loadingText: 'Loading...',
/**
* @cfg {Boolean} resizable True to add a resize handle to the bottom of the dropdown list (defaults to false)
*/
resizable: false,
/**
* @cfg {Number} handleHeight The height in pixels of the dropdown list resize handle if resizable = true (defaults to 8)
*/
handleHeight : 8,
/**
* @cfg {Boolean} editable False to prevent the user from typing text directly into the field, just like a
* traditional select (defaults to true)
*/
editable: true,
/**
* @cfg {String} allQuery The text query to send to the server to return all records for the list with no filtering (defaults to '')
*/
allQuery: '',
/**
* @cfg {String} mode Set to 'local' if the ComboBox loads local data (defaults to 'remote' which loads from the server)
*/
mode: 'remote',
/**
* @cfg {Number} minListWidth The minimum width of the dropdown list in pixels (defaults to 70, will be ignored if
* listWidth has a higher value)
*/
minListWidth : 70,
/**
* @cfg {Boolean} forceSelection True to restrict the selected value to one of the values in the list, false to
* allow the user to set arbitrary text into the field (defaults to false)
*/
forceSelection:false,
/**
* @cfg {Number} typeAheadDelay The length of time in milliseconds to wait until the typeahead text is displayed
* if typeAhead = true (defaults to 250)
*/
typeAheadDelay : 250,
/**
* @cfg {String} valueNotFoundText When using a name/value combo, if the value passed to setValue is not found in
* the store, valueNotFoundText will be displayed as the field text if defined (defaults to undefined)
*/
valueNotFoundText : undefined,
/**
* @cfg {String} defaultValue The value displayed after loading the store.
*/
defaultValue: '',
/**
* @cfg {Boolean} blockFocus Prevents all focus calls, so it can work with things like HTML edtor bar
*/
blockFocus : false,
/**
* @cfg {Boolean} disableClear Disable showing of clear button.
*/
disableClear : false,
/**
* @cfg {Boolean} alwaysQuery Disable caching of results, and always send query
*/
alwaysQuery : false,
//private
addicon : false,
editicon: false,
// element that contains real text value.. (when hidden is used..)
// private
onRender : function(ct, position){
Roo.form.Field.prototype.onRender.call(this, ct, position);
if(this.store){
this.store.on('beforeload', this.onBeforeLoad, this);
this.store.on('load', this.onLoad, this);
this.store.on('loadexception', this.onLoadException, this);
this.store.load({});
}
},
// private
initEvents : function(){
//Roo.form.ComboBox.superclass.initEvents.call(this);
},
onDestroy : function(){
if(this.store){
this.store.un('beforeload', this.onBeforeLoad, this);
this.store.un('load', this.onLoad, this);
this.store.un('loadexception', this.onLoadException, this);
}
//Roo.form.ComboBox.superclass.onDestroy.call(this);
},
// private
fireKey : function(e){
if(e.isNavKeyPress() && !this.list.isVisible()){
this.fireEvent("specialkey", this, e);
}
},
// private
onResize: function(w, h){
return;
},
/**
* Allow or prevent the user from directly editing the field text. If false is passed,
* the user will only be able to select from the items defined in the dropdown list. This method
* is the runtime equivalent of setting the 'editable' config option at config time.
* @param {Boolean} value True to allow the user to directly edit the field text
*/
setEditable : function(value){
},
// private
onBeforeLoad : function(){
Roo.log("Select before load");
return;
this.innerList.update(this.loadingText ?
'<div class="loading-indicator">'+this.loadingText+'</div>' : '');
//this.restrictHeight();
this.selectedIndex = -1;
},
// private
onLoad : function(){
var dom = this.el.dom;
dom.innerHTML = '';
var od = dom.ownerDocument;
if (this.emptyText) {
var op = od.createElement('option');
op.setAttribute('value', '');
op.innerHTML = String.format('{0}', this.emptyText);
dom.appendChild(op);
}
if(this.store.getCount() > 0){
var vf = this.valueField;
var df = this.displayField;
this.store.data.each(function(r) {
// which colmsn to use... testing - cdoe / title..
var op = od.createElement('option');
op.setAttribute('value', r.data[vf]);
op.innerHTML = String.format('{0}', r.data[df]);
dom.appendChild(op);
});
if (typeof(this.defaultValue != 'undefined')) {
this.setValue(this.defaultValue);
}
}else{
//this.onEmptyResults();
}
//this.el.focus();
},
// private
onLoadException : function()
{
dom.innerHTML = '';
Roo.log("Select on load exception");
return;
this.collapse();
Roo.log(this.store.reader.jsonData);
if (this.store && typeof(this.store.reader.jsonData.errorMsg) != 'undefined') {
Roo.MessageBox.alert("Error loading",this.store.reader.jsonData.errorMsg);
}
},
// private
onTypeAhead : function(){
},
// private
onSelect : function(record, index){
Roo.log('on select?');
return;
if(this.fireEvent('beforeselect', this, record, index) !== false){
this.setFromData(index > -1 ? record.data : false);
this.collapse();
this.fireEvent('select', this, record, index);
}
},
/**
* Returns the currently selected field value or empty string if no value is set.
* @return {String} value The selected value
*/
getValue : function(){
var dom = this.el.dom;
this.value = dom.options[dom.selectedIndex].value;
return this.value;
},
/**
* Clears any text/value currently set in the field
*/
clearValue : function(){
this.value = '';
this.el.dom.selectedIndex = this.emptyText ? 0 : -1;
},
/**
* Sets the specified value into the field. If the value finds a match, the corresponding record text
* will be displayed in the field. If the value does not match the data value of an existing item,
* and the valueNotFoundText config option is defined, it will be displayed as the default field text.
* Otherwise the field will be blank (although the value will still be set).
* @param {String} value The value to match
*/
setValue : function(v){
var d = this.el.dom;
for (var i =0; i < d.options.length;i++) {
if (v == d.options[i].value) {
d.selectedIndex = i;
this.value = v;
return;
}
}
this.clearValue();
},
/**
* @property {Object} the last set data for the element
*/
lastData : false,
/**
* Sets the value of the field based on a object which is related to the record format for the store.
* @param {Object} value the value to set as. or false on reset?
*/
setFromData : function(o){
Roo.log('setfrom data?');
},
// private
reset : function(){
this.clearValue();
},
// private
findRecord : function(prop, value){
return false;
var record;
if(this.store.getCount() > 0){
this.store.each(function(r){
if(r.data[prop] == value){
record = r;
return false;
}
return true;
});
}
return record;
},
getName: function()
{
// returns hidden if it's set..
if (!this.rendered) {return ''};
return !this.hiddenName && this.el.dom.name ? this.el.dom.name : (this.hiddenName || '');
},
// private
onEmptyResults : function(){
Roo.log('empty results');
//this.collapse();
},
/**
* Returns true if the dropdown list is expanded, else false.
*/
isExpanded : function(){
return false;
},
/**
* Select an item in the dropdown list by its data value. This function does NOT cause the select event to fire.
* The store must be loaded and the list expanded for this function to work, otherwise use setValue.
* @param {String} value The data value of the item to select
* @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
* selected item if it is not currently in view (defaults to true)
* @return {Boolean} True if the value matched an item in the list, else false
*/
selectByValue : function(v, scrollIntoView){
Roo.log('select By Value');
return false;
if(v !== undefined && v !== null){
var r = this.findRecord(this.valueField || this.displayField, v);
if(r){
this.select(this.store.indexOf(r), scrollIntoView);
return true;
}
}
return false;
},
/**
* Select an item in the dropdown list by its numeric index in the list. This function does NOT cause the select event to fire.
* The store must be loaded and the list expanded for this function to work, otherwise use setValue.
* @param {Number} index The zero-based index of the list item to select
* @param {Boolean} scrollIntoView False to prevent the dropdown list from autoscrolling to display the
* selected item if it is not currently in view (defaults to true)
*/
select : function(index, scrollIntoView){
Roo.log('select ');
return ;
this.selectedIndex = index;
this.view.select(index);
if(scrollIntoView !== false){
var el = this.view.getNode(index);
if(el){
this.innerList.scrollChildIntoView(el, false);
}
}
},
// private
validateBlur : function(){
return;
},
// private
initQuery : function(){
this.doQuery(this.getRawValue());
},
// private
doForce : function(){
if(this.el.dom.value.length > 0){
this.el.dom.value =
this.lastSelectionText === undefined ? '' : this.lastSelectionText;
}
},
/**
* Execute a query to filter the dropdown list. Fires the beforequery event prior to performing the
* query allowing the query action to be canceled if needed.
* @param {String} query The SQL query to execute
* @param {Boolean} forceAll True to force the query to execute even if there are currently fewer characters
* in the field than the minimum specified by the minChars config option. It also clears any filter previously
* saved in the current store (defaults to false)
*/
doQuery : function(q, forceAll){
Roo.log('doQuery?');
if(q === undefined || q === null){
q = '';
}
var qe = {
query: q,
forceAll: forceAll,
combo: this,
cancel:false
};
if(this.fireEvent('beforequery', qe)===false || qe.cancel){
return false;
}
q = qe.query;
forceAll = qe.forceAll;
if(forceAll === true || (q.length >= this.minChars)){
if(this.lastQuery != q || this.alwaysQuery){
this.lastQuery = q;
if(this.mode == 'local'){
this.selectedIndex = -1;
if(forceAll){
this.store.clearFilter();
}else{
this.store.filter(this.displayField, q);
}
this.onLoad();
}else{
this.store.baseParams[this.queryParam] = q;
this.store.load({
params: this.getParams(q)
});
this.expand();
}
}else{
this.selectedIndex = -1;
this.onLoad();
}
}
},
// private
getParams : function(q){
var p = {};
//p[this.queryParam] = q;
if(this.pageSize){
p.start = 0;
p.limit = this.pageSize;
}
return p;
},
/**
* Hides the dropdown list if it is currently expanded. Fires the 'collapse' event on completion.
*/
collapse : function(){
},
// private
collapseIf : function(e){
},
/**
* Expands the dropdown list if it is currently hidden. Fires the 'expand' event on completion.
*/
expand : function(){
} ,
// private
/**
* @cfg {Boolean} grow
* @hide
*/
/**
* @cfg {Number} growMin
* @hide
*/
/**
* @cfg {Number} growMax
* @hide
*/
/**
* @hide
* @method autoSize
*/
setWidth : function()
{
},
getResizeEl : function(){
return this.el;
}
});