日本語
<html> <head> </head> <script language="javascript"> function PageLoad() { /*var myMenu = new MainMenu('menu','test.xml');*/ var myMenu = new MainMenu('menu'); var menuFile = new MenuItem('File','File',null,'F'); var menuSave = new MenuItem('Save','Save','document.execCommand) ("名前を付けて保存")','S'); var menuExit = new MenuItem('Exit','Exit','Exit()','X'); ,null,'E'); var menuView = new MenuItem('View','View',null,'V'); var menuProg = new MenuItem('Prog','プログラム スクリプトによって初期化されました','SelectOpt(this) )'); var menuConf = new MenuItem('Conf','設定ファイルによって初期化されました','SelectOpt(this)'); var menuChk = new MenuItem('Chk','テスト: 複数のオプション'); new MenuItem('Chk1','テスト: オプション 1','CheckOpt(this)'); var menuChk2 = new MenuItem('Chk1','テスト: オプション 2','CheckOpt(this)'); menuHelp = new MenuItem('Help','Help',null,'H'); var menuTopics = new MenuItem('Topics','Help Topic',null,'T'); ','ヘルプコンテンツ',null,'C'); var menuAbout = new MenuItem('About','About MenuItem','alert("MenuItem Ver1.00 by Akira(akira_cn@msn.com)")', 'A'); menuFile.addChild(menuExit); menuView.addChild(menuConf); menuChk.addChild(menuChk1); menuHelp.addChild(menuContent); myMenu.addItem(menuFile); addItem(menuEdit); myMenu.addItem(menuHelp); menuTopics.Disable(); .Check(); } 関数 contact() { アラート('お問い合わせ') } 関数 Exit() { self.opener = self; } 関数 SelectOpt(target) { var editBox1 = document.getElementById ('editBox1'); var editBox2 = document.getElementById('editBox2'); if (target.id == 'Conf') { editBox1.style.display = "none"; } else { editBox2.style.display = "none"; editBox1.style.display = "block"; } function CheckOpt(target) { target.Check(); style='padding:0 0 0 0;margin:0 0 0 0' onload='PageLoad()'> <textarea id="editBox1" style="width:100%;height:90%"> メインページを初期化しますプログラムメニューの例: <script> var myMenu = new MainMenu('menu') var menuFile = new MenuItem('File','File',null,'F'); var menuSave = new MenuItem('Save', ' Save','document.execCommand("SaveAs")','S'); var menuExit = new MenuItem('Exit','Exit','Exit()','X'); ( '編集','編集',null,'E'); var menuView = new MenuItem('View','View',null,'V'); var menuProg = new MenuItem('Prog','プログラム経由) script Initialize','SelectOpt(this)'); var menuConf = new MenuItem('Conf','構成ファイルによる初期化','SelectOpt(this)'); var menuChk1 = new MenuItem('Chk1','Test:複雑なオプション 1','CheckOpt(this)'); var menuChk2 = new MenuItem('Chk1','テスト: オプション 2','CheckOpt(this)'); var menuHelp = new MenuItem('Help','ヘルプ ',null,'H'); var menuTopics = new MenuItem('トピック','ヘルプ トピック',null,'T'); var menuContent = new MenuItem('コンテンツ','ヘルプ コンテンツ',null,' C '); var menuAbout = new MenuItem ('About','AboutMenuItem','alert('MenuItem Ver1.00 by Akira(akira_cn@msn.com)')','A'); ; myMenu.addItem(menuEdit); myMenu.addItem(menuHelp); menuView.addChild(menuConf); menuView.addLine(); // 行を分割します。 menuView.addChild(menuTopics); ; myMenu.Show(); menuTopics.Disable(); </script> </textarea> <textarea id=" editBox2" style="width:100%;height:90%;display:none"> 設定ファイルを使用してメイン メニューを初期化する例: <!--xml 設定ファイル。たとえば、test.xml として保存します-- > <?xml バージョン ="1.0" エンコーディング="gb2312" スタンドアロン="yes"?> <MainMenu> <MenuItem id="File" Text="File" Accelerator="F"> <MenuItem id="Save" テキスト="保存" アクセラレータ = "S" onClick="document.execCommand('SaveAs')"/> <MenuItem id="終了" Text="終了" アクセラレータ = "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="プログラム スクリプトを通じて初期化" Selected="true" onClick="SelectOpt(this)"/> <MenuItem id="Conf" Text="構成ファイルを通じて初期化" onClick="SelectOpt(this) "/> <MenuItem /> <MenuItem id="Chk1" Text="テスト: オプション 1 をチェック" Checked="true" onClick="CheckOpt(this)"/> <MenuItem id="Chk2" Text="テスト:オプション 2 をチェックします。 onClick ="CheckOpt(this)"/> </MenuItem> <MenuItem id="Help" Text="Help" Accelerator="H"> <MenuItem id="Topics" Text="ヘルプ トピック" アクセラレータ="T" 無効 ="true"/> <MenuItem id="Content" Text="ヘルプ コンテンツ" Accelerator="C" Disabled="true"/> <MenuItem/> <MenuItem id="概要" Text=" MenuItem について" Accelerator= "A" onClick="alert('MenuItem Ver1.00 by Akira(akira_cn@msn.com)')"/> </MenuItem> </MainMenu> <script> var myMenu = new MainMenu(' menu','test .xml'); //設定ファイル test.xml からメニュー情報を読み取ります </script> </textarea> </body> </html> <!---- ---------------------------------------------------- ---------- --------------- メニュー制御メインプログラム部 バージョン 1.00 アキラ (akira_cn@msn.com) --------------- - ------------------------------------------------- - ---> <script language="javascript"> function MenuItem(id, text, clickEvent,accelerator, subWidth) { this.id = null; this.text = null; this.clickEvent = clickEvent ; if (id == null) { this.id = "menu" + (new Date()).getTime(); } else this.id = id this.text = text; if (subWidth == null) .subWidth = 0; for (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; if (clickEvent == null) onclick = function(){}; else this.onclick = function(){eval(clickEvent);}; if (accelerator != null) //ショートカット キー { this.accelerator = accelerator } this.subMenuItems = new Array ( ); this.visible = true; this.disabled = false; this.selected = false; this.visible = false; "なし"; } MenuItem.prototype.Show = function() { this.visible = true; this._span.style.display = "ブロック" } 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'); this._span.appendChild(textObj); (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._span.style.paddingLeft = "18px"; MenuItem.prototype.Select = function() { varparent = 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; ._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;プロトタイプ.addChild = function(menuItem) { this.subMenuItems.push(menuItem); 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.id; ; if (this.text == null) { this._span.appendChild(document.createElement("hr")); this._span.style.height = "2px"; ; this._span.style.marginBottom = "2px"; } else { if (accelerator != null) { var site = text.indexOf(accelerator) if (site != -1) { var part1 = text.slice( 0, サイト); var パート 2 = text.slice(サイト + 1); var textNode2 = document.createTextNode("font"); .style.textDecoration = "下線"; ノード.innerText = アクセラレータ; 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 = "2px"; this._span.style.marginBottom = "2px"; = "12px"; this._span.style.paddingTop = "5px"; 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 = 関数(){this.EventHandler._span_Click(this,event);} } if (accelerator != null) { this.accelerator =accelerator.charCodeAt(0) } MenuItem.prototype.ShowSubmenu = function() { var pos = this .getObjPosition(); var subSpan = document.createElement("span"); subSpan.style.position = "absolute"; if(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.backgroundColor = "#FFFFFF"; subSpan.style.border = "#ACA899"; ; subSpan.className = "subMenu"; subSpan.owner = this.id; //this._span.onmouseout = function(){}; for (var i = 0; i < this.subMenuItems.length; i++) .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); MenuItem.prototype._span_Click; ) { if (!this.hasChild() || this.clickEvent) { this.hideSubmenu(true); this.onclick(); } else { if(this.hasChild()) { this.ShowSubmenu();親._showSub = true; } } if(event.stopPropagation)event.stopProgpagation(); elseevent.cancelBubble = true; } MenuItem.prototype._span_MouseOver = function(sender、event) (送信者); if(this.hasChild() && (this.parent._showSub || this.parentinstanceofMenuItem)) { this.ShowSubmenu() } } MenuItem.prototype._span_MouseOut = function(sender,event); ._cleanMenu(sender); } MenuItem.prototype._activeMenu = function(menuSpan) { menuSpan.style.backgroundColor = "#FFFFFF"; .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) { varparent = this.parent while (parent; != null) {parent._showSub = false;parent =parent.parent; } } this._cleanMenu(this._span); } } MenuItem.prototype isParentOf = function(owner) { varparent = this.parent; while(parent != null) { if (owner ==parent.id) return true; } // オブジェクトを取得します。座標 MenuItem.prototype.getObjPosition = function() { var obj = this._span; var objLeft = obj.offsetTop; var objParent = obj.offsetParent; { objLeft += objParent.offsetLeft; objParent = objParent.offsetParent; } MenuItem.Instances[this.id] = this; ( this); if (document.body.onkeydown == null) { document.body.onkeydown = function() { for (var i = 0; i < MenuItem.Instances.All.length; i++) { if (MenuItem.インスタンス .All[i].accelerator != null) { if (event.altKey &&event.keyCode == MenuItem.Instances.All[i].accelerator) { MenuItem.Instances.All[i]._span_Click.call(MenuItem) . Instances.All[i],MenuItem.Instances.All[i]._span,event); } } } } } if (document.body.onclick == null) { document.body.onclick = function() { .Instances.All[0].hideSubmenu(true); } } } MenuItem.Instances = new Array(); MenuItem.Instances.All = new Array() 関数 MainMenu(id,cfgSrc); ( ); this.parent = null; this.id = id; MainMenu.prototype._loadCfg = function(src) // 構成ファイルから読み取ります { var objXMLDoc = new ActiveXObject("Microsoft.XMLDOM"); async = false; objXMLDoc.load(src); if (objXMLDoc.documentElement == null) throw new TypeError("XML ドキュメントの解析に失敗しました! "); return objXMLDoc; } catch(e) {alert("XML ドキュメントの解析に失敗しました!"); throw new TypeError("XML ドキュメントの解析に失敗しました!"); } } MainMenu.prototype._applyCfg = function(element, menuItem) { var id = element.getAttribute("id"); var text = element.getAttribute("Text"); var Accelerator = element.getAttribute("Accelerator"); subMenuItem = new MenuItem(id,text,onClick,Accelerator); if(menuItem != null) menuItem.addChild(subMenuItem); else this.addItem(subMenuItem) for (var i = 0; i < element.childNodes.length ; i++) { if (element.childNodes[i].tagName == "MenuItem") { this._applyCfg(element.childNodes[i], subMenuItem) } } } MainMenu.prototype._setStatus = function(xmlDoc); cfgElements = xmlDoc.getElementsByTagName("MenuItem"); for (var i = 0; i < cfgElements.length; i++) { var element = cfgElements[i] var selected = element.getAttribute("チェック済み"); var selected = element.getAttribute("Visible"); var disabled = element.getAttribute("Disabled"); null) { 続行; } if (チェック済み == "true") { MenuItem.Instances[id].Check(); } if (選択済み == "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) } } null; } MainMenu.prototype.addItem = function(menuItem) { this.items.push(menuItem); 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); 項目を返します; } if (items[i].subMenuItems.length > 0) { this.find(id, items[i].subMenuItems) } } this; ._div = document.createElement("div"); this._div.id = this.id; this._div.style.height = "24px"; //ドキュメント.body.appendChild(this._div); if(cfgSrc != null) { var objXMLDoc = this._loadCfg(cfgSrc); var rootElement = objXMLDoc.documentElement if(rootElement.tagName != "MainMenu") {alert(" XML ドキュメントは正当なメイン メニュー設定ファイルではありません。 (ルート要素のタグ名は MainMenu ではありません)"); throw new TypeError("XML ドキュメントは正当なメイン メニュー構成ファイルではありません! (ルート要素のタグ名は MainMenu ではありません)"); } for ( var i = 0; i < rootElement .childNodes.length; i++) { if (rootElement.childNodes[i].tagName == "MenuItem") { this._applyCfg(rootElement.childNodes[i]) } } (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>