2 * (c) 2005-2011 Richard Cowin (http://openrico.org)
3 * (c) 2005-2011 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, {
42 Rico.onLoad(function() { self.atLoad(); })
46 this.searchField=Rico.createFormField(this.contentDiv,'input','text',this.id+'_search');
47 this.searchField.style.display="block";
48 this.searchField.style.width=this.options.width;
49 Rico.eventBind(this.searchField,'keyup',Rico.eventHandle(this,'filterKeypress'),false);
50 this.selectList=Rico.createFormField(this.contentDiv,'select',null,this.id+'_list');
51 this.selectList.size=this.options.listLength;
52 this.selectList.style.display="block";
53 this.selectList.style.width=this.options.width;
54 Rico.eventBind(this.selectList,'change',Rico.eventHandle(this,'listClick'),false);
56 * alias for closePopup
59 this.close=this.closePopup;
63 open: function(currentVal,column) {
65 this.grid=this.column.liveGrid;
66 this.searchField.value='';
67 this.selectList.options.length=0;
69 this.searchField.focus();
70 this.selectValuesRequest('');
73 selectValuesRequest: function(filter) {
74 var colnum=this.column.index;
76 Rico.extend(options, this.grid.buffer.ajaxOptions);
77 options.parameters = {id: this.grid.tableId, offset: '0', page_size: this.options.maxSuggest, edit: colnum};
78 options.parameters[this.grid.actionId]="query";
79 if (filter!='' && filter!='*') {
80 if (filter.indexOf('*')==-1) filter='*'+filter+'*';
81 options.parameters['f[1][op]']="LIKE";
82 options.parameters['f[1][len]']=1;
83 options.parameters['f[1][0]']=filter;
86 options.onComplete = function(request) { self.selectValuesUpdate(request); };
87 new Rico.ajaxRequest(this.grid.buffer.dataSource, options);
90 selectValuesUpdate: function(request) {
91 var response = request.responseXML.getElementsByTagName("ajax-response");
92 Rico.log("selectValuesUpdate: "+request.status);
93 if (response == null || response.length != 1) return;
95 var error = response.getElementsByTagName('error');
96 if (error.length > 0) {
97 var errmsg=Rico.getContentAsString(error[0],this.grid.buffer.isEncoded);
98 Rico.log("Data provider returned an error:\n"+errmsg);
99 alert(Rico.getPhraseById("requestError",errmsg));
102 this.selectList.options.length=0;
103 response=response.getElementsByTagName('response')[0];
104 var rowsElement = response.getElementsByTagName('rows')[0];
105 var rows = this.grid.buffer.dom2jstable(rowsElement);
106 Rico.log("selectValuesUpdate: id="+this.selectList.id+' rows='+rows.length);
107 for (var i=0; i<rows.length; i++) {
108 if (rows[i].length>0) {
110 var c1=(rows[i].length>1) ? rows[i][1] : c0;
111 Rico.addSelectOption(this.selectList,c0,c1);
116 filterKeypress: function(e) {
117 var txtbox=Rico.eventElement(e);
118 if (typeof this.lastKeyFilter != 'string') this.lastKeyFilter='';
119 if (this.lastKeyFilter==txtbox.value) return;
121 Rico.log("filterKeypress: "+this.index+' '+v);
122 this.lastKeyFilter=v;
123 this.selectValuesRequest(v);
126 listClick: function(e) {
127 var elem=Rico.eventElement(e);
128 if (elem.tagName.toLowerCase() != 'select') return;
129 if (this.returnValue) {
130 var opt=elem.options[elem.selectedIndex];
131 this.returnValue(opt.value,opt.innerHTML);