AideJavascriptXml
From GeoGebraWiki
Vincent MAILLE propose ce script pour afficher ou non les axes
<script language="JavaScript">
function AxisVisible(applet,a){
// applet is the applet name
// a is a boolean
// example: AxisVisible(window.document.figure,true);
var applet = window.document.figure;
if (a==true) {
ap= new String('"true"');
av= new String('"false"');
} else {
av= new String('"true"');
ap= new String('"false"');
}
ch=new String(applet.getXML());
i=ch.indexOf('evSettings axes='+av);
if (i>-1){
ch=ch.substr(0,i)+'evSettings axes='+ap+ch.substring(i+16+av.length,ch.length);
}
applet.setXML(ch);
}
</script>
appelé par (figure est le nom donné à l'applet):
<form method="get" name="formul">
<input style="border: medium outset; width:150px;" name="Btn" value="Afficher" onclick='javascript:AxisVisible(window.document.figure,true)' type="button">
<input style="border: medium outset; width:150px;" name="Btn" value="Cacher" onclick='javascript:AxisVisible(window.document.figure,false)' type="button">
</form>
Philippe PACLET propose une fonction getProperty(applet, objname, property, attributes)
qui permet d'inspecter les valeurs des attributs des propriétés des 'elements' de la figure, 'en temps réel'. Elle se base sur une lecture du code xml de la figure effectuée à l'aide de techniques d' 'expression régulières'.
Par exemple, elle peut servir à tester la visibilité d'un objet, comme dans:
function isVisible(objname) {
applet = document.ggbapplet;
// propriété et attributs à inspecter property = "show"; attributes = ["object", "label"];
ans = getProperty(applet, objname, property, options);
if(ans[0] == "undefined")
alert(objname + " n'est pas défini");
else
if(ans[1][1] == "false")
alert(objname + ' est invisible');
else
alert(objname + ' est visible');
}
// function getProperty(applet, objname, property, attributes)
// args:
// - applet mis en jeu
// - objname: nom de l'objet
// - property: nom de la propriété
// - array des attributs qu'on veut inspecter; attention à respecter l'ordre
// valeur de retour: une array (array[0] = type ou "undefined" ou "Property undefined";
// array[1] = [attributes[0], valeur de attributes[0]]
// array[2] = [attributes[1], valeur de attributes[1]]
// etc...
// Philippe Paclet 22/02/07
function getProperty(applet, objname, property, attributes) {
xml = new String(applet.getXML()); cr_regexp = /\r|\n/g; newxml = xml.replace(cr_regexp, "");
answers = new Array(); answers[0] = "undefined";
re_str = '<element type="([^"]*?)" label="' + objname + '">(.*?)<' + property + ' ';
for(i = 0; i < attributes.length; i++)
{
re_str = re_str + '\\s*' + attributes[i] + '="([^"]*?)"';
}
regexp = new RegExp(re_str, "i");
if(matched = newxml.match(regexp))
{
el_re = /<\/element/;
if(!el_re.test(matched[0]))
{
answers[0] = matched[1]; //type
for(i = 0; i < attributes.length; i++)
{
answers[i+1] = [attributes[i], matched[i+3]];
}
}
else
{
answers[0] = "Property undefined";
}
}
return answers;
}
