2 * (c) 2005-2009 Richard Cowin (http://openrico.org)
3 * (c) 2005-2009 Matt Brown (http://dowdybrown.com)
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the
11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12 * either express or implied. See the License for the specific language governing permissions
13 * and limitations under the License.
16 Rico.KeywordSearch = function(id,options) {
17 this.initialize(id,options);
20 Rico.KeywordSearch.prototype = {
22 * @class Implements a pop-up keyword search control.
25 * @param id unique identifier
26 * @param options object may contain any of the following:<dl>
27 * <dt>showColorCode</dt><dd> show hex color code as user hovers over color grid? default=false</dd>
28 * <dt>cellsPerRow </dt><dd> number of colors per row in the grid? default=18</dd>
29 * <dt>palette </dt><dd> array of 6 digit hex values, default=216 "web safe" colors</dd>
32 initialize: function(id,options) {
34 Rico.extend(this, new Rico.Window(Rico.getPhraseById("keywordTitle"),options));
35 Rico.addClass(this.content,'ricoKeywordSearch');
36 Rico.extend(this.options, {
44 this.searchField=Rico.createFormField(this.contentDiv,'input','text',this.id+'_search');
45 this.searchField.style.display="block";
46 this.searchField.style.width=this.options.width;
47 Rico.eventBind(this.searchField,'keyup',Rico.eventHandle(this,'filterKeypress'),false);
48 this.selectList=Rico.createFormField(this.contentDiv,'select',null,this.id+'_list');
49 this.selectList.size=this.options.listLength;
50 this.selectList.style.display="block";
51 this.selectList.style.width=this.options.width;
52 Rico.eventBind(this.selectList,'change',Rico.eventHandle(this,'listClick'),false);
54 * alias for closePopup
57 this.close=this.closePopup;
61 open: function(currentVal,column) {
63 this.grid=this.column.liveGrid;
64 this.searchField.value='';
65 this.selectList.options.length=0;
67 this.searchField.focus();
68 this.selectValuesRequest('');
71 selectValuesRequest: function(filter) {
72 var colnum=this.column.index;
74 Rico.extend(options, this.grid.buffer.ajaxOptions);
75 options.parameters = {id: this.grid.tableId, offset: '0', page_size: this.options.maxSuggest, edit: colnum};
76 options.parameters[this.grid.actionId]="query";
77 if (filter!='' && filter!='*') {
78 if (filter.indexOf('*')==-1) filter='*'+filter+'*';
79 options.parameters['f[1][op]']="LIKE";
80 options.parameters['f[1][len]']=1;
81 options.parameters['f[1][0]']=filter;
84 options.onComplete = function(request) { self.selectValuesUpdate(request); };
85 new Rico.ajaxRequest(this.grid.buffer.dataSource, options);
88 selectValuesUpdate: function(request) {
89 var response = request.responseXML.getElementsByTagName("ajax-response");
90 Rico.log("selectValuesUpdate: "+request.status);
91 if (response == null || response.length != 1) return;
93 var error = response.getElementsByTagName('error');
94 if (error.length > 0) {
95 var errmsg=Rico.getContentAsString(error[0],this.grid.buffer.isEncoded);
96 Rico.log("Data provider returned an error:\n"+errmsg);
97 alert(Rico.getPhraseById("requestError",errmsg));
100 this.selectList.options.length=0;
101 response=response.getElementsByTagName('response')[0];
102 var rowsElement = response.getElementsByTagName('rows')[0];
103 var rows = this.grid.buffer.dom2jstable(rowsElement);
104 Rico.log("selectValuesUpdate: id="+this.selectList.id+' rows='+rows.length);
105 for (var i=0; i<rows.length; i++) {
106 if (rows[i].length>0) {
108 var c1=(rows[i].length>1) ? rows[i][1] : c0;
109 Rico.addSelectOption(this.selectList,c0,c1);
114 filterKeypress: function(e) {
115 var txtbox=Rico.eventElement(e);
116 if (typeof this.lastKeyFilter != 'string') this.lastKeyFilter='';
117 if (this.lastKeyFilter==txtbox.value) return;
119 Rico.log("filterKeypress: "+this.index+' '+v);
120 this.lastKeyFilter=v;
121 this.selectValuesRequest(v);
124 listClick: function(e) {
125 var elem=Rico.eventElement(e);
126 if (elem.tagName.toLowerCase() != 'select') return;
127 if (this.returnValue) {
128 var opt=elem.options[elem.selectedIndex];
129 this.returnValue(opt.value,opt.innerHTML);