-
Notifications
You must be signed in to change notification settings - Fork 2
/
datalist.js
executable file
·187 lines (180 loc) · 7.39 KB
/
datalist.js
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
/* DataList & DataListLocal are created by Christian Pelczarski
* and is freely distributable under the terms of an MIT-style license.
/*--------------------------------------------------------------------------*/
var DataListLocal = Class.create();
Object.extend(Object.extend(DataListLocal.prototype, Autocompleter.Local.prototype), {
initialize: function(element, update, array, options,styles) {
this.baseInitialize(element, update, options);
this.options.array = array;
this.el = this.element.next();
this.styles = styles || { width: 200 };
if(!this.options.noButton){
this.options.dropLimit = this.options.dropLimit || 150;
if( this.options.disableButton || this.options.array.values().length > this.options.dropLimit ){
var min_chars = this.options.partialChars;
$(this.el).observe('click',function(){
var msg = "This list is too long to display.\nType directly into the field\n(minimum of " + min_chars + " characters)\nfor more focused results";
this.element.focus();
alert(msg);
}.bind(this) );
}else{
$(this.el).observe('click',DataListHelper.trigger.bindAsEventListener(this) );
}
}
// Override onShow
this.options.onShow = DataListHelper.show.bind(this);
if( array instanceof Hash ){
this.options.selector = function(){
var ret = [];
var partial = []; // Inside matches
var entry = this.getToken();
var count = 0;
var opt = this.options;
var element = this.element;
if(this.options.array.toQueryString() == '=' || this.options.array.toQueryString().empty()){
this.options.array.unset('');
this.options.array.set( '', 'nothing in dropdown');
this.styles.width = '140px';
this.styles.height = '25px';
}
this.options.array.each( function(pair){
var elem = pair.value;
var inside = '<span class="inside" style="display:none;">' + pair.key + '</span><span class="outside">';
// Long lists will not display unless we have this if
// condition here; by checking if the element is empty we know
// the dropdown button was pushed
if(element.value.empty()){
ret.push('<li>' + inside + elem + '</span></li>');
}else{
var foundPos = opt.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase()) : elem.indexOf(entry);
while (foundPos != -1) {
if (foundPos == 0 && elem.length != entry.length) {
ret.push('<li>' + inside + '<strong>' + elem.substr(0, entry.length) + "</strong>" +
elem.substr(entry.length) + "</span></li>");
break;
} else if (entry.length >= opt.partialChars &&
opt.partialSearch && foundPos != -1) {
if (opt.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
partial.push("<li>" + inside + elem.substr(0, foundPos) + "<strong>" +
elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(
foundPos + entry.length) + "</span></li>");
break;
}
}
foundPos = opt.ignoreCase ?
elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) :
elem.indexOf(entry, foundPos + 1);
}
}
});
if (partial.length)
ret = ret.concat(partial.slice(0, this.options.choices - ret.length))
return "<ul>" + ret.join('') + "</ul>";
}.bind(this);
}
}
});
var DataList = Class.create();
Object.extend(Object.extend(DataList.prototype, Ajax.Autocompleter.prototype), {
initialize: function(element, update, url, options,styles) {
this.baseInitialize(element, update, options);
this.options.asynchronous = true;
this.options.onComplete = this.onComplete.bind(this);
this.options.defaultParams = this.options.parameters || null;
this.url = url;
this.el = this.element.next();
this.iefix = true;
this.styles = styles || { width:200 };
if(!this.options.noButton){
if( this.options.disableButton ){
$(this.el).observe('click',function(){
var msg = "This list is too long to display.\nType directly into the field\n(minimum of 3 characters)\nfor more focused results";
alert(msg)
} );
}else{
$(this.el).observe('click',DataListHelper.trigger.bindAsEventListener(this) );
}
}
// Override onShow
this.options.onShow = DataListHelper.show.bind(this);
}
});
var DataListHelper = {
show : function(){
if(!this.update.style.position || this.update.style.position=='absolute') {
this.update.style.position = 'absolute';
this.update.scrollTop = 0;
if(this.element.value.empty()){// if element is blank then drop down button was hit
if( typeof this.savedValue != 'undefined' ){
this.styles.overflow = 'auto';
}else{
this.styles.overflow = 'visible';
}
}else{
this.styles.height = '';
this.styles.width = this.styles.width || '';
}
this.update.setStyle(this.styles);
var clone_opts = {
setHeight: false,
setWidth: false,
setTop: true,
setLeft: true,
offsetTop: this.element.offsetHeight,
offsetLeft: this.styles.offsetLeft || 0
}
this.update.clonePosition(this.element,clone_opts );
}
Effect.Appear(this.update,{duration:0.15});
},
trigger : function(){
// This "if" is to protect against going from select box to select box
var dvalue = this.update.getStyle('display');
if(dvalue == 'none'){
this.savedValue = this.element.value;
this.element.value = '';
setTimeout( function(){
this.activate();
this.element.value = this.savedValue || '';
}.bind(this),200 );// this must remain at 200 for best results
this.shroud = DataListHelper.shroud.bindAsEventListener(this);
document.observe('mousedown',this.shroud);
setTimeout( function() {
delete this.savedValue;
}.bind(this),1000 );
}
},
shroud : function(event){
var a = this.update.clicked(event);
if(!a && !$(this.el).clicked(event)){
this.onBlur();
document.stopObserving('mousedown',this.shroud );
}
},
parse: function(obj,a,b){
var a = a || 'inside';
var b = b || 'outside';
var c = Element.collectTextNodesIgnoreClass( obj,b );
var d = Element.collectTextNodesIgnoreClass( obj,a );
a = this.esc(a);
b = this.esc(b);
c = this.esc(c);
d = this.esc(d);
var json = 'var e = { #{a}: "#{c}", #{b}: "#{d}" };'.interpolate( { 'a': a,'b': b,'c': c,'d': d } );
eval(json);
return e;
},
esc: function(str){
return str.replace(/"/g,'\\"');
},
scroll: function(event){
event.stop();// Stop the default behavior of the mousewheel
// Here is the code that will scroll the div with the mousewheel
var delta = event.memo.delta;
var up = delta > 0 ? true : false;
var abs = Math.abs(delta);
var t = this.cumulativeScrollOffset()[1];
var incr = 20 * abs;
this.scrollTop = up ? t - incr : t + incr;
}
};