ComboBox_wev8.js
5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.widget.ComboBox");
dojo.require("dojo.widget.*");
dojo.require("dojo.event.*");
dojo.widget.incrementalComboBoxDataProvider = function(url, limit, timeout){
this.searchUrl = url;
this.inFlight = false;
this.activeRequest = null;
this.allowCache = false;
this.cache = {};
this.addToCache = function(keyword, data){
if(this.allowCache){
this.cache[keyword] = data;
}
}
this.startSearch = function(searchStr, type, ignoreLimit){
if(this.inFlight){
// FIXME: implement backoff!
}
var tss = encodeURIComponent(searchStr);
var realUrl = dojo.string.paramString(this.searchUrl, {"searchString": tss});
var _this = this;
var request = dojo.io.bind({
url: realUrl,
method: "get",
mimetype: "text/javascript",
load: function(type, data, evt){
_this.inFlight = false;
_this.addToCache(searchStr, data);
_this.provideSearchResults(data);
}
});
this.inFlight = true;
}
}
dojo.widget.ComboBoxDataProvider = function(dataPairs, limit, timeout){
// NOTE: this data provider is designed as a naive reference
// implementation, and as such it is written more for readability than
// speed. A deployable data provider would implement lookups, search
// caching (and invalidation), and a significantly less naive data
// structure for storage of items.
this.data = [];
this.searchTimeout = 500;
this.searchLimit = 30;
this.searchType = "STARTSTRING"; // may also be "STARTWORD" or "SUBSTRING"
this.caseSensitive = false;
// for caching optimizations
this._lastSearch = "";
this._lastSearchResults = null;
this.startSearch = function(searchStr, type, ignoreLimit){
// FIXME: need to add timeout handling here!!
this._preformSearch(searchStr, type, ignoreLimit);
}
this._preformSearch = function(searchStr, type, ignoreLimit){
//
// NOTE: this search is LINEAR, which means that it exhibits perhaps
// the worst possible speed charachteristics of any search type. It's
// written this way to outline the responsibilities and interfaces for
// a search.
//
var st = type||this.searchType;
// FIXME: this is just an example search, which means that we implement
// only a linear search without any of the attendant (useful!) optimizations
var ret = [];
if(!this.caseSensitive){
searchStr = searchStr.toLowerCase();
}
for(var x=0; x<this.data.length; x++){
if((!ignoreLimit)&&(ret.length >= this.searchLimit)){
break;
}
// FIXME: we should avoid copies if possible!
var dataLabel = new String((!this.caseSensitive) ? this.data[x][0].toLowerCase() : this.data[x][0]);
if(dataLabel.length < searchStr.length){
// this won't ever be a good search, will it? What if we start
// to support regex search?
continue;
}
if(st == "STARTSTRING"){
// jum.debug(dataLabel.substr(0, searchStr.length))
// jum.debug(searchStr);
if(searchStr == dataLabel.substr(0, searchStr.length)){
ret.push(this.data[x]);
}
}else if(st == "SUBSTRING"){
// this one is a gimmie
if(dataLabel.indexOf(searchStr) >= 0){
ret.push(this.data[x]);
}
}else if(st == "STARTWORD"){
// do a substring search and then attempt to determine if the
// preceeding char was the beginning of the string or a
// whitespace char.
var idx = dataLabel.indexOf(searchStr);
if(idx == 0){
// implicit match
ret.push(this.data[x]);
}
if(idx <= 0){
// if we didn't match or implicily matched, march onward
continue;
}
// otherwise, we have to go figure out if the match was at the
// start of a word...
// this code is taken almost directy from nWidgets
var matches = false;
while(idx!=-1){
// make sure the match either starts whole string, or
// follows a space, or follows some punctuation
if(" ,/(".indexOf(dataLabel.charAt(idx-1)) != -1){
// FIXME: what about tab chars?
matches = true; break;
}
idx = dataLabel.indexOf(searchStr, tti+1);
}
if(!matches){
continue;
}else{
ret.push(this.data[x]);
}
}
}
this.provideSearchResults(ret);
}
this.provideSearchResults = function(resultsDataPairs){
}
this.addData = function(pairs){
// FIXME: incredibly naive and slow!
this.data = this.data.concat(pairs);
}
this.setData = function(pdata){
// populate this.data and initialize lookup structures
this.data = pdata;
}
if(dataPairs){
this.setData(dataPairs);
}
}
dojo.widget.ComboBox = function(){
dojo.widget.Widget.call(this);
}
dojo.inherits(dojo.widget.ComboBox, dojo.widget.Widget);
dojo.widget.ComboBox.defaults = {
widgetType: "ComboBox",
isContainer: false,
forceValidOption: false,
searchType: "stringstart",
dataProvider: null,
startSearch: function(searchString){},
openResultList: function(results){},
clearResultList: function(){},
hideResultList: function(){},
selectNextResult: function(){},
selectPrevResult: function(){},
setSelectedResult: function(){}
};
dojo.lang.extend(dojo.widget.ComboBox, dojo.widget.ComboBox.defaults);
dojo.widget.DomComboBox = function(){
dojo.widget.ComboBox.call(this);
dojo.widget.DomWidget.call(this, true);
}
dojo.inherits(dojo.widget.DomComboBox, dojo.widget.DomWidget);
dojo.widget.tags.addParseTreeHandler("dojo:combobox");
// render-specific includes
dojo.requireAfterIf("html", "dojo.widget.html.ComboBox");