silverstripe-framework/javascript/DropdownField_WithAdd.js
Hayden Smith 4a5d9b03f8 Moved Sapphire module to open source path
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@39001 467b73ca-7a2a-4603-9d3b-597d59a354a9
2007-07-19 10:40:28 +00:00

111 lines
2.6 KiB
JavaScript
Executable File

DropdownField_WithAdd = Class.create();
DropdownField_WithAdd.applyTo('div.dropdownfield_withadd');
DropdownField_WithAdd.prototype = {
initialize: function() {
WithAdd_Link.applyToChildren(this, '.editlink');
WithAdd_Link.applyToChildren(this, '.link');
},
getAddLink: function(){
return this.getElementsByTagName('a')[0];
},
getUseExistingLink: function(){
return this.getElementsByTagName('a')[1];
},
toggleLinks: function(){
var addlink = this.getAddLink();
var useExistinglink = this.getUseExistingLink();
if(addlink.style.display == "none"){
addlink.style.display = "inline";
useExistinglink.style.display = "none";
}else{
addlink.style.display = "none";
useExistinglink.style.display = "inline";
}
}
}
WithAdd_Link = Class.create();
WithAdd_Link.prototype = {
onclick: function(){
this.toggleSiblingDropdown();
var ownerField = this.getOwnerField();
ownerField.toggleLinks();
var relatedDataEditor = this.getRelatedDataEditor();
if(relatedDataEditor){
if(this.className.match(/addlink/))
relatedDataEditor.status = 'adding';
else
relatedDataEditor.status = 'useexisting';
relatedDataEditor.linkOnClick();
}
return false;
},
findSiblingDropdown: function(){
return this.parentNode.getElementsByTagName('select')[0];
},
toggleSiblingDropdown: function(){
var dropdown = this.findSiblingDropdown();
if(dropdown.disabled)
dropdown.disabled = false;
else
dropdown.disabled = true;
},
getOwnerField: function(){
var f = this.parentNode;
while(f && !f.className.match(/dropdownfield_withadd/)) f=f.parentNode;
return f;
},
getRelatedDataEditor: function(){
var f = this.parentNode;
while(f && !f.className.match(/RelatedDataEditor/)) {
f=f.parentNode;
if(f.tagName.toLowerCase() == 'form') break;
}
return f;
}
}
WithAdd_Link = Class.create();
WithAdd_Link.prototype = {
initialize: function() {
this.checkDisplay();
},
onclick: function(){
var relatedDataEditor = this.getRelatedDataEditor();
if(relatedDataEditor){
relatedDataEditor.linkOnClick();
}
return false;
},
getRelatedDataEditor: function(){
var f = this.parentNode;
while(f && !f.className.match(/RelatedDataEditor/)) {
f=f.parentNode;
if(f.tagName.toLowerCase() == 'form') break;
}
return f;
},
checkDisplay: function(){
var dropdown = this.getdropdownField();
if(dropdown.value==0)
this.style.display = 'none';
else
this.style.display = 'inline';
},
getdropdownField: function(){
var f = this.parentNode;
var dropdown = f.getElementsByTagName('select')[0];
return dropdown;
}
}