Fixes to some Rico 3 .net examples. Also added 2 new .net examples. Added Dojo 1...
[infodrom/rico3] / minsrc / ricoSearch.js
1 /*
2  *  (c) 2005-2009 Richard Cowin (http://openrico.org)
3  *  (c) 2005-2009 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   },
42
43   atLoad : function() {
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);
53     /**
54      * alias for closePopup
55      * @function
56      */
57     this.close=this.closePopup;
58     this.close();
59   },
60   
61   open: function(currentVal,column) {
62     this.column=column;
63     this.grid=this.column.liveGrid;
64     this.searchField.value='';
65     this.selectList.options.length=0;
66     this.openPopup();
67     this.searchField.focus();
68     this.selectValuesRequest('');
69   },
70
71   selectValuesRequest: function(filter) {
72     var colnum=this.column.index;
73     var options={};
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;
82     }
83     var self=this;
84     options.onComplete = function(request) { self.selectValuesUpdate(request); };
85     new Rico.ajaxRequest(this.grid.buffer.dataSource, options);
86   },
87
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;
92     response=response[0];
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));
98       return null;
99     }
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) {
107         var c0=rows[i][0];
108         var c1=(rows[i].length>1) ? rows[i][1] : c0;
109         Rico.addSelectOption(this.selectList,c0,c1);
110       }
111     }
112   },
113
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;
118     var v=txtbox.value;
119     Rico.log("filterKeypress: "+this.index+' '+v);
120     this.lastKeyFilter=v;
121     this.selectValuesRequest(v);
122   },
123   
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);
130     }
131     this.close();
132   }
133
134 };