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
| var AWLMIXINS = {
DependentSelect : Class.create(),
linkDependentSelect : function(element, dependentSelect, href)
{
element = $(element);
var select = $(dependentSelect).referer;
var selectedIndex;
var successHandler = function(transport)
{
var reply = transport.responseJSON;
select.show(reply.content);
select.store(selectedIndex, reply.content);
Tapestry.processScriptInReply(reply);
};
// Otherwise, assume it's just an ordinary link.
var handler = function(event)
{
// look for the selected value to build the request Ajax URL
var selectedValue;
for(i=0 ; i<element.options.length ; i++){
if(element.options[i].selected == true){
selectedIndex = i;
selectedValue = element.options[i].value;
break;
}
}
if(select.get(selectedIndex) != null){
select.showCache(selectedIndex);
}else{
var dynHref = href.replace("\$value\$", selectedValue);
new Ajax.Request(dynHref, { onSuccess : successHandler });
}
return false;
};
element.onchange = handler;
},
linkSelect: function (spec)
{
new AWLMIXINS.DependentSelect(spec);
AWLMIXINS.linkDependentSelect.apply(null, spec);
}
}
AWLMIXINS.DependentSelect.prototype = {
initialize: function(spec)
{
this.select = $(spec[1]);
this.select.referer = this;
this.updateFunc = Tapestry.ElementEffect[$(spec[3])] || Tapestry.ElementEffect.highlight;
},
// redefine the show
show : function(content) {
Element.update(this.select, content);
this.updateFunc.call(this, this.select);
},
showCache : function(key) {
Element.update(this.select, this.store[key]);
this.updateFunc.call(this, this.select);
},
// Used to handle a simple cache mechanism
store : function(key, content) {
this.store[key] = content;
},
// Used to verify if the index has already been requested
get : function(key) {
if(this.store != null){
return this.store[key];
}
return null;
}
}; |
Partager