Français
<html> <head> </head> <script language="javascript"> function PageLoad() { /*var myMenu = new MainMenu('menu','test.xml'); var monMenu = new MainMenu('menu'); var menuFile = new MenuItem('File','File',null,'F'); var menuSave = new MenuItem('Save','Save','document.execCommand ("Enregistrer sous")','S'); var menuExit = new MenuItem('Exit','Exit','Exit()','X'); var menuEdit = new MenuItem('Edit','Edit'); ,null,'E'); var menuView = new MenuItem('View','View',null,'V'); var menuProg = new MenuItem('Prog','Initialisé via le script du programme','SelectOpt(this )'); var menuConf = new MenuItem('Conf','Initialisé via le fichier de configuration','SelectOpt(this)'); var menuChk = new MenuItem('Chk','Test : options multiples'); new MenuItem('Chk1','Test : Vérifier l'option 1','CheckOpt(this)'); var menuChk2 = new MenuItem('Chk1','Test : Vérifier l'option 2','CheckOpt(this)'); menuHelp = new MenuItem('Aide','Help',null,'H'); var menuTopics = new MenuItem('Sujets','Help Topic',null,'T'); var menuContent = new MenuItem('Content ','Contenu de l'aide',null,'C'); var menuAbout = new MenuItem('About','About MenuItem','alert("MenuItem Ver1.00 by Akira(akira_cn@msn.com)")', 'A'); menuFile.addChild (menuSave); menuFile.addChild (menuExit); menuView.addChild (menuProg); menuView.addChild (menuConf); menuChk.addChild(menuChk1); menuChk.addChild(menuHelp.addChild(menuTopics); menuHelp.addChild(menuContent); menuHelp.addChild(menuAbout); addItem(menuEdit); monMenu.addItem(menuView); monMenu.addItem(menuHelp); monMenu.Disable(); menuContent.Disable(); .Check(); } function contact() { alert('Contactez-nous'); } function Exit() { self.opener = self.close( } function SelectOpt(target) { var editBox1 = document.getElementById ('editBox1'); var editBox2 = document.getElementById('editBox2'); if (target.id == 'Conf') { editBox1.style.display = "aucun"; } else { editBox2.style.display = "none"; editBox1.style.display = "block"; } target.Select(); } function CheckOpt(target) { target.Check( } </script> <body style='padding:0 0 0 0;margin:0 0 0 0' onload='PageLoad()'> <textarea id="editBox1" style="width:100%;height:90%"> Initialiser la page principale via un exemple de menu de programme : <script> var myMenu = new MainMenu('menu'); var menuFile = new MenuItem('File','File',null,'F'); 'Enregistrer','document.execCommand("SaveAs")','S'); var menuExit = new MenuItem('Exit','Exit','Exit()','X'); ( 'Edit','Edit',null,'E'); var menuView = new MenuItem('View','View',null,'V'); var menuProg = new MenuItem('Prog','via programme); script Initialize','SelectOpt(this)'); var menuConf = new MenuItem('Conf','Initialiser via le fichier de configuration','SelectOpt(this)'); var menuChk1 = new MenuItem('Chk1','Test: Option complexe 1','CheckOpt(this)'); var menuChk2 = new MenuItem('Chk1','Test : Vérifier l'option 2','CheckOpt(this)'); Aide ',null,'H'); var menuTopics = new MenuItem('Sujets','Sujet d'aide',null,'T'); var menuContent = new MenuItem('Content','Contenu d'aide',null,' C '); var menuAbout = new MenuItem ('About','AboutMenuItem','alert('MenuItem Ver1.00 par Akira(akira_cn@msn.com)')','A'); ; monMenu.addItem(menuEdit); monMenu.addItem(menuView); monMenu.addItem(menuHelp); menuFile.addChild(menuSave); menuView.addLine(); // Lignes séparées menuView.addChild(menuChk1); menuHelp.addChild(menuTopics); ; monMenu.Show(); menuEdit.Disable(); menuTopics.Disable(); menuProg.Select(); editBox2" style="width:100%;height:90%;display:none"> Exemple d'initialisation du menu principal via un fichier de configuration : <!--fichier de configuration xml, par exemple, enregistrez-le sous test.xml-- > <?xml version ="1.0" encoding="gb2312" standalone="yes"?> <MainMenu> <MenuItem id="File" Text="File" Accelerator="F"> <MenuItem id="Save" Text ="Save" Accelerator ="S" onClick="document.execCommand('SaveAs')"/> <MenuItem id="Exit" Text="Exit" Accelerator="X" onClick="Exit()"/> < /MenuItem> <MenuItem id="Edit" Text="Edit" Accelerator="E" Disabled="true"> </MenuItem> <MenuItem id="View" Text="View" Accelerator="V"> <MenuItem id="Prog" Text="Initialisé via le script du programme" Selected="true" onClick="SelectOpt(this)"/> <MenuItem id="Conf" Text="Initialisé via le fichier de configuration" onClick="SelectOpt(this) "/> <MenuItem /> <MenuItem id="Chk1" Text="Test : Vérifier l'option 1" Checked="true" onClick="CheckOpt(this)"/> <MenuItem id="Chk2" Text="Test : Cochez l'option 2" onClick ="CheckOpt(this)"/> </MenuItem> <MenuItem id="Help" Text="Aide" Accelerator="H"> <MenuItem id="Sujets" Text="Sujets d'aide" Accélérateur ="T" Désactivé ="true"/> <MenuItem id="Content" Text="Contenu de l'aide" Accelerator="C" Disabled="true"/> <MenuItem/> <MenuItem id="À propos" Text=" À propos de MenuItem" Accelerator= "A" onClick="alert('MenuItem Ver1.00 by Akira(akira_cn@msn.com)')"/> </MenuItem> </MainMenu> <script> var myMenu = new MainMenu(' menu','test .xml'); //Lire les informations de menu à partir du fichier de configuration test.xml myMenu.Show(); </script> </textarea> </body> </html> <!---- ---- ----------------------------------------------- ---- --------------- Partie principale du programme de contrôle des menus Version 1.00 Akira (akira_cn@msn.com) --------------- - ------------------------------------------------- - ---> <script langage="javascript"> function MenuItem(id, text, clickEvent, accelerator, subWidth) { this.id = null; this.text = null this.accelerator = null; if (id == null) { this.id = "menu" + (new Date()).getTime(); else this.id = id = text; .subWidth = 0; pour (var i = 0; this.text != null && i < this.text.length; i++) { this.subWidth = this.subWidth - 0 + (text.charCodeAt(i) > 127 ? 20 : 10); } //this.subWidth = this.text != null ? this.text.length * 16 : 96;//100; else this.subWidth = subWidth; onclick = function(){}; else this.onclick = function(){eval(clickEvent);}; if (accélérateur != null) //Touche de raccourci { this.accelerator = accélérateur } this.subMenuItems = new Array ( ); this.visible = true; this.disabled = false; this.checked = false; this.selected = false; "aucun"; } MenuItem.prototype.Show = function() { this.visible = true; this._span.style.display = "block" } MenuItem.prototype.Enable = function() { this.disabled = false ; this._span.EventHandler = this; this._span.onmouseover = function(){this.EventHandler._span_MouseOver(this,event);} this._span.onmouseout = function(){this.EventHandler._span_MouseOut(this,event) );} this._span.onclick = function(){this.EventHandler._span_Click(this,event);} this._span.style.color = "#000000"; } MenuItem.prototype.Disable = function() { this .disabled = true ; this._span.onmouseover = function(){} this._span.onmouseout = function(){} this._span.onclick = function(){} this._span.style.color = "#ACA899" ; } MenuItem.prototype.Check = function() { if (!this.checked) { var textObj = document.createElement('b'); textObj.innerText = '√'; (var i = 0; i < this._span.childNodes.length - 1; i++) { this._span.appendChild(this._span.removeChild(this._span.childNodes[i]) } this.checked = true ; this._span.style.paddingLeft = "6px"; } else { this._span.removeChild(this._span.childNodes[0]); this.checked = false; } } MenuItem.prototype.Select = function() { var parent = this.parent; for (var i = 0; i < parent.subMenuItems.length; i++) { parent.subMenuItems[i]._unselect(); liObj = document.createElement("li"); this._span.appendChild(liObj); for (var i = 0; i < this._span.childNodes.length - 1; i++) { liObj.appendChild(this._span. RemoveChild(this._span.childNodes[i])); } this.selected = true; //this.subWidth = this.subWidth - 0 + 16; this._span.style.paddingLeft = "2px" } MenuItem.prototype; ._unselect = function() { if (this.selected) { var liObj = this._span.removeChild(this._span.childNodes[0]); for (var i = 0; i < liObj.childNodes.length; i++) { this._span.appendChild(liObj.childNodes[i]); } this.selected = false; this._span.style.paddingLeft = "18px"; //this.subWidth = this.subWidth - 16 } } MenuItem. prototype.addChild = function(menuItem) { this.subMenuItems.push(menuItem.parent = this; } MenuItem.prototype.addLine = function() { this.addChild(new MenuItem()); hasChild = function() { return this.subMenuItems.length > 0; } this._span = document.createElement("span"); this._span.id = this._span.style.cursor = "default" ; if (this.text == null) { this._span.appendChild(document.createElement("hr")); this._span.style.height = "2px"; this._span.style.marginTop = "2px" ; this._span.style.marginBottom = "2px"; } else { if (accélérateur != null) { var site = text.indexOf(accélérateur); if (site != -1) { var part1 = text.slice( 0, site); var part2 = text.slice(site + 1); var textNode1 = document.createTextNode(part1); var textNode2 = document.createTextNode(part2); .style.textDecoration = "underline"; node.innerText = accelerator; this._span.appendChild(textNode1); this._span.appendChild(node); this._span.appendChild(textNode2); createTextNode(text + "("); var node = document.createElement("font"); node.style.textDecoration = "underline"; node.innerText = accelerator; this._span.appendChild(textNode); this._span. appendChild(node); this._span.appendChild(document.createTextNode(")")); this.subWidth = this.subWidth - 0 + 48; } } else { var textNode = document.createTextNode(text); .appendChild(textNode); } this._span.style.height = "20px"; this._span.style.marginTop = "2px"; this._span.style.marginBottom = "2px"; = "12px"; this._span.style.paddingTop = "5px"; this._span.style.paddingLeft = "18px"; this._span.style.paddingRight = "10px"; ._span.onmouseover = function(){this.EventHandler._span_MouseOver(this,event);} this._span.onmouseout = function(){this.EventHandler._span_MouseOut(this,event);} this._span.onclick = function (){this.EventHandler._span_Click(this,event);} } if (accélérateur != null) { this.accelerator = accelerator.charCodeAt(0); } MenuItem.prototype.ShowSubmenu = function() { var pos = this .getObjPosition(); var subSpan = document.createElement("span"); subSpan.style.position = "absolute"; if(this.parent instanceof MainMenu) { subSpan.style.left = subSpan.style .top = pos[1] + this._span.clientHeight; } else { subSpan.style.left = pos[0] + this._span.clientWidth + 1; subSpan.style.top = pos[1]; style.paddingLeft = "0px"; subSpan.style.width = 0; subSpan.style.backgroundColor = "#FFFFFF"; subSpan.style.border = "solid 1px #ACA899"; ; subSpan.className = "subMenu"; subSpan.owner = this.id; //this._span.onmouseout = function(){}; for (var i = 0; i < this.subMenuItems.length; i++) { this .subMenuItems[i]._span.style.marginTop = 0; this.subMenuItems[i]._span.style.marginBottom = 0; this.subMenuItems[i]._span.style.width = "100%" ; .subWidth < this.subMenuItems[i].subWidth) { this.subWidth = this.subMenuItems[i].subWidth; } subSpan.appendChild(this.subMenuItems[i]._span } subSpan.style.width = this. subWidth > 96 ? this.subWidth : 96; //this._span.appendChild(subSpan); document.body.appendChild(subSpan); document.body.focus(); ) { if (!this.hasChild() || this.clickEvent) { this.hideSubmenu(true); this.onclick(); } else { if(this.hasChild()) { this.ShowSubmenu(); parent._showSub = true; } } if (event.stopPropagation) event.stopProgpagation(); else event.cancelBubble = true; } MenuItem.prototype._span_MouseOver = function(expéditeur, événement) { this.hideSubmenu(); (expéditeur); if(this.hasChild() && (this.parent._showSub || this.parent instanceof MenuItem)) { this.ShowSubmenu(); } } MenuItem.prototype._span_MouseOut = function(sender, event) { this ._cleanMenu(sender); //this.hideSubmenu(); } MenuItem.prototype._activeMenu = function(menuSpan) { menuSpan.style.backgroundColor = "#316AC5"; .prototype._cleanMenu = function(menuSpan) { if (menuSpan.parentNode.className == "subMenu") menuSpan.style.backgroundColor = "#FFFFFF" else menuSpan.style.backgroundColor = "#F1EEE5"; color = "#000000"; } MenuItem.prototype.hideSubmenu = function(bHideAll) { var subMenus = document.getElementsByTagName("span"); for (var i = subMenus.length - 1; i >= 0; i-- ) { if (subMenus[i].className == "subMenu" && (bHideAll || !this.isParentOf(subMenus[i].owner))) { if (bHideAll) { var parent = this.parent while (parent; != null) { parent._showSub = false; parent = parent.parent; } } this._cleanMenu(this._span); subMenus[i].parentNode.removeChild(subMenus[i]); isParentOf = function(owner) { var parent = this.parent; while(parent != null) { if (owner == parent.id) return true; parent.parent } return false } //Obtenir l'objet; coordonnées MenuItem.prototype.getObjPosition = function() { var obj = this._span; var objLeft = obj.offsetLeft; var objTop = var objParent = obj.offsetParent; { objLeft += objParent.offsetLeft; objTop += objParent.offsetTop; objParent = objParent.offsetParent; } return([objLeft,objTop]); ( this); if (document.body.onkeydown == null) { document.body.onkeydown = function() { for (var i = 0; i < MenuItem.Instances.All.length; i++) { if (MenuItem. Instances .All[i].accelerator != null) { if (event.altKey && event.keyCode == MenuItem.Instances.All[i].accelerator) { MenuItem.Instances.All[i]._span_Click.call(MenuItem } } } } } if (document.body.onclick == null) { document.body.onclick = function() { MenuItem. .Instances.All[0].hideSubmenu(true); } } } MenuItem.Instances = new Array(); MenuItem.Instances.All = new Array(); ( ); this.parent = null; this.id = id; MainMenu.prototype._loadCfg = function(src) //Lire à partir du fichier de configuration { try { var objXMLDoc = new ActiveXObject("Microsoft.XMLDOM"); async = false; objXMLDoc.load(src); if (objXMLDoc.documentElement == null) throw new TypeError("Échec de l'analyse du document XML ! "); return objXMLDoc; } catch(e) { alert("Échec de l'analyse du document XML!"); throw new TypeError("Échec de l'analyse du document XML!"); } } MainMenu.prototype._applyCfg = function(element, menuItem) { var id = element.getAttribute("id"); var text = element.getAttribute("Texte"); var onClick = element.getAttribute("onClick"); var Accelerator = element.getAttribute("Accélérateur"); subMenuItem = new MenuItem(id,text,onClick,Accelerator); if(menuItem != null) menuItem.addChild(subMenuItem); else this.addItem(subMenuItem); ; i++) { if (element.childNodes[i].tagName == "MenuItem") { this._applyCfg(element.childNodes[i], subMenuItem); } } } MainMenu.prototype._setStatus = function(xmlDoc) { var cfgElements = xmlDoc.getElementsByTagName("MenuItem"); for (var i = 0; i < cfgElements.length; i++) { var element = cfgElements[i]; var id = element.getAttribute("var vérifié =" element.getAttribute("Vérifié"); var selected = element.getAttribute("Selected"); var visible = element.getAttribute("Visible"); null) { continuer ; } if (checked == "true") { MenuItem.Instances[id].Check( } if (selected == "true") { MenuItem.Instances[id].Select(); if (visible == "false") { MenuItem.Instances[id].Hide(); if (disabled == "true") { MenuItem.Instances[id].Disable(); menuItem = function(id) { return this._find(id, this.items); } MainMenu.prototype._find = function(id, items) { for (var i = 0; i < items.length; i++) { if (items[i].id == id) { return items[i]; } if (items[i].subMenuItems.length > 0) { this._find(id, items[i].subMenuItems } } return); null; } MainMenu.prototype.addItem = function(menuItem) { this.items.push(menuItem.parent = this; } MainMenu.prototype.removeItem = function(id) { return this._delete(id, this. items); } MainMenu.prototype._delete = function(id, items) { for (var i = 0; i < items.length; i++) { if (items[i].id == id) { var item = items [i]; items.splice(i); return item; } if (items[i].subMenuItems.length > 0) { this.find(id, items[i].subMenuItems); ._div = document.createElement("div"); this._div.id = this.id; this._div.style.height = "24px"; this._div.style.backgroundColor = "#F1EEE5" ; .body.appendChild(this._div); if(cfgSrc != null) { var objXMLDoc = this._loadCfg(cfgSrc); var rootElement = objXMLDoc.documentElement; if(rootElement.tagName != "MainMenu") { alert(" Le document XML n'est pas un fichier de configuration de menu principal légal ! (Le nom de balise de l'élément racine n'est pas MainMenu)"); throw new TypeError("Le document XML n'est pas un fichier de configuration légal du menu principal ! (Le nom de balise de l'élément racine n'est pas MainMenu)"); } for ( var i = 0; i < rootElement .childNodes.length; i++) { if (rootElement.childNodes[i].tagName == "MenuItem") { this._applyCfg(rootElement.childNodes[i] } } this._setStatus); (objXMLDoc); } MainMenu.prototype .Show = function() { document.body.appendChild(this._div); for (var i = 0; i < document.body.childNodes.length; i++) { if(document. body.childNodes[i].id != this.id) { document.body.appendChild(document.body.removeChild(document.body.childNodes[i]) } } for (var i = 0; i < this .items.length; i++) { this._div.appendChild(this.items[i]._span } } } </script>