Fixes based on recent testing. Key changes: EntryType DT (datetime) has been replaced...
[infodrom/rico3] / minsrc / ricoSearch.js
1 /*
2  *  (c) 2005-2011 Richard Cowin (http://openrico.org)
3  *  (c) 2005-2011 Matt Brown (http://dowdybrown.com)
4  *
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
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
14  */
15
16 Rico.KeywordSearch = function(id,options) {
17   this.initialize(id,options);
18 };
19
20 Rico.KeywordSearch.prototype = {
21 /**
22  * @class Implements a pop-up keyword search control.
23  * @extends Rico.Popup
24  * @constructs
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>
30  *</dl>
31  */
32   initialize: function(id,options) {
33     this.id=id;
34     Rico.extend(this, new Rico.Window(Rico.getPhraseById("keywordTitle"),options));
35     Rico.addClass(this.content,'ricoKeywordSearch');
36     Rico.extend(this.options, {
37       listLength : 10,
38       maxSuggest : 20,
39       width: '12em'
40     });
41     var self=this;
42     Rico.onLoad(function() { self.atLoad(); })
43   },
44
45   atLoad : function() {
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);
55     /**
56      * alias for closePopup
57      * @function
58      */
59     this.close=this.closePopup;
60     this.close();
61   },
62   
63   open: function(currentVal,column) {
64     this.column=column;
65     this.grid=this.column.liveGrid;
66     this.searchField.value='';
67     this.selectList.options.length=0;
68     this.openPopup();
69     this.searchField.focus();
70     this.selectValuesRequest('');
71   },
72
73   selectValuesRequest: function(filter) {
74     var colnum=this.column.index;
75     var options={};
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;
84     }
85     var self=this;
86     options.onComplete = function(request) { self.selectValuesUpdate(request); };
87     new Rico.ajaxRequest(this.grid.buffer.dataSource, options);
88   },
89
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;
94     response=response[0];
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));
100       return null;
101     }
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) {
109         var c0=rows[i][0];
110         var c1=(rows[i].length>1) ? rows[i][1] : c0;
111         Rico.addSelectOption(this.selectList,c0,c1);
112       }
113     }
114   },
115
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;
120     var v=txtbox.value;
121     Rico.log("filterKeypress: "+this.index+' '+v);
122     this.lastKeyFilter=v;
123     this.selectValuesRequest(v);
124   },
125   
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);
132     }
133     this.close();
134   }
135
136 };