1 |
/* |
2 |
WebPAC AJAX class |
3 |
*/ |
4 |
|
5 |
var WebPAC = { }; |
6 |
|
7 |
/* |
8 |
autocompleter which isn't (a suggest box) |
9 |
- don't just on first option on appear |
10 |
- removed fade-in/out effect |
11 |
- added configurable width (content width? centered?) |
12 |
- add icon which means suggest |
13 |
|
14 |
hopefully, this implementation won't break (much) when I update |
15 |
prototype next time :-) |
16 |
*/ |
17 |
|
18 |
|
19 |
WebPAC.Suggest = Class.create(); |
20 |
Logger.debug('Created WebPAC.Suggest Class'); |
21 |
Object.extend(Object.extend(WebPAC.Suggest.prototype, Ajax.Autocompleter.prototype), { |
22 |
initialize: function(element, update, url, options) { |
23 |
this.baseInitialize(element, update, options); |
24 |
this.options.asynchronous = true; |
25 |
this.options.onComplete = this.onComplete.bind(this); |
26 |
this.options.defaultParams = this.options.parameters || null; |
27 |
this.url = url; |
28 |
this.ignoreReturn = true; |
29 |
|
30 |
Logger.debug('WebPAC.Sugget initialize '+element+','+update+','+url); |
31 |
|
32 |
this.options.onShow = function(element, update) { |
33 |
if(!update.style.position || update.style.position=='absolute') { |
34 |
update.style.position = 'absolute'; |
35 |
Position.clone(element, update, {setHeight: false, offsetTop: element.offsetHeight}); |
36 |
} |
37 |
new Element.show(update); |
38 |
Logger.debug('WebPAC.Suggest.onShow'); |
39 |
}; |
40 |
|
41 |
this.options.onHide = function(element, update) { |
42 |
new Element.hide(update); |
43 |
Logger.debug('WebPAC.Suggest.onHide'); |
44 |
}; |
45 |
}, |
46 |
|
47 |
getUpdatedChoices: function() { |
48 |
var entry = encodeURIComponent(this.options.paramName) + '=' + |
49 |
encodeURIComponent(this.getToken()); |
50 |
|
51 |
this.options.parameters = this.options.callback ? |
52 |
this.options.callback(this.element, entry) : entry; |
53 |
|
54 |
if(this.options.defaultParams) |
55 |
this.options.parameters += '&' + this.options.defaultParams; |
56 |
|
57 |
var form = $('results_form'); |
58 |
var inputs = form.getElementsByTagName('input'); |
59 |
var name = '_database'; |
60 |
|
61 |
var matchingInputs = new Array(); |
62 |
for (var i = 0; i < inputs.length; i++) { |
63 |
var input = inputs[i]; |
64 |
if (input.name != name || !input.checked) continue; |
65 |
Logger.debug('keeping '+name+'='+input.value); |
66 |
matchingInputs.push(name+'='+input.value); |
67 |
} |
68 |
|
69 |
// construct arguments for url |
70 |
var args = ''; |
71 |
var pos = this.url.indexOf('?'); |
72 |
if (pos == -1) { |
73 |
args += '?'; |
74 |
} else { |
75 |
args += '&'; |
76 |
} |
77 |
args += matchingInputs.join('&'); |
78 |
|
79 |
Logger.debug('WebPAC.Suggest called '+this.url+args); |
80 |
new Ajax.Request(this.url+args, this.options); |
81 |
}, |
82 |
|
83 |
onComplete: function(request) { |
84 |
Logger.debug('WebPAC.Suggest onComplete called'); |
85 |
this.updateChoices(request.responseText); |
86 |
}, |
87 |
|
88 |
markPrevious: function() { |
89 |
if(this.index > 0) this.index-- |
90 |
// else this.index = this.entryCount-1; |
91 |
}, |
92 |
|
93 |
markNext: function() { |
94 |
if(this.index < this.entryCount-1) this.index++ |
95 |
// else this.index = 0; |
96 |
} |
97 |
|
98 |
}); |
99 |
|
100 |
/* |
101 |
WebPAC.Updater |
102 |
*/ |
103 |
|
104 |
WebPAC.Updater = Class.create(); |
105 |
Logger.debug('Created WebPAC.Updater Class'); |
106 |
Object.extend(WebPAC.Updater.prototype, { |
107 |
initialize: function(container, url, options) { |
108 |
Logger.debug('WebPAC.Updater initialize '+container+','+url+','+options); |
109 |
if (url.indexOf('/ajax') == -1) { |
110 |
var pos = url.indexOf('?'); |
111 |
if (pos == -1) { |
112 |
url += '/ajax'; |
113 |
} else { |
114 |
url = url.substring(0,pos) + '/ajax' + url.substring(pos); |
115 |
} |
116 |
Logger.debug('added /ajax to '+url+' pos:'+pos); |
117 |
} |
118 |
Logger.info('using AJAX to update "'+container+'" from '+url); |
119 |
this.updater = new Ajax.Updater(container, url, options); |
120 |
} |
121 |
}); |
122 |
|