2009-10-05 23 views
1

我有一個web應用程序(did)允許使用zeroClipboard,使用Dijit.Menu,通過右鍵單擊複製文本。問題在於Flash 10需要用戶點擊實際的Flash小程序才能使其發生。使用ZeroClipboard與Dijit.Menu提供複製功能

我曾嘗試使用ZeroClipboard.glue()方法將swf粘貼到菜單項,但沒有成功。我只是想知道是否有人必須處理這個問題。

謝謝。

回答

0

我想出了一個解決方案,並不漂亮,因爲當鼠標懸停在它上面時,菜單標題將消失,但至少可以工作。我沒有廣泛測試,但我希望它不會泄漏閃存對象。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>ZeroClipboard Menu Demo</title> 
    <style type="text/css"> 
     @import "http://o.aolcdn.com/dojo/1.0.0/dijit/themes/tundra/tundra.css"; 
     @import "http://o.aolcdn.com/dojo/1.0.0/dojo/resources/dojo.css"; 
    </style> 
    <script type="text/javascript" src="js/ZeroClipboard.js"></script> 
    <script type="text/javascript" src="js/dojo/dojo.js" 
      djConfig="parseOnLoad:true, isDebug:false, preventBackButtonFix: true"></script> 
    <script type="text/javascript"> 
     dojo.require("dojo.parser"); 
     dojo.require("dijit.Menu"); 
     ZeroClipboard.setMoviePath('flash/ZeroClipboard.swf'); 
    </script> 
</head> 
<body class="tundra"> 


<div id="MenuTarget">Right Click Here</div> 

<script type="text/javascript"> 
    var menu = new dijit.Menu(); 

    var menuItem1 = new dijit.MenuItem({ 
     id: "tester", 
     label: "This Does Nothing", 
     parentMenu: menu 
    }); 

    menu.addChild(menuItem1); 
    menu.bindDomNode(dojo.byId("MenuTarget")); 

    var menuItem2 = new dijit.MenuItem({ 
     id: "tester2", 
     label: "Copy Some Text", 
     parentMenu: menu 
    }); 

    attachCopyEvent(menuItem2, "Some Text"); 
    menu.addChild(menuItem2); 

    /* 
    attaches ZeroClipboard object to specified menu item 
    menuItem: diji.menuItem to attach to 
    copyText: text to copy to clipboard 
    */ 
    function attachCopyEvent(menuItem, copyText) { 
     var myId = menuItem.domNode.id; 
     var origText = menuItem.label; //Grab the original menu caption 
     var clip; //keep the clip object external/public to the connecdt functions 

     //Replace the text of the menuItem with the flash object when the mouse hovers over it 
     //We use the connect method of the menuitem so that the connects are destroyed when the object is destroyed 
     menuItem.connect(menuItem, "_onHover", function() { 
      var myLabelDiv = dojo.byId(myId + "_text"); //The label for menuItems is always the id of the menu item with '_text' appended 
      clip = new ZeroClipboard.Client(); 
      //Destroy the flash object when copy is complted to save RAM 
      clip.addEventListener('onComplete', function() { 
       clip.destroy(); 
      }); 
      //Grab the size of the div and tell zeroclipboard to generate embed html of neccesary size 
      clip.setText(copyText); 
      myLabelDiv.innerHTML = clip.getHTML(myLabelDiv.clientWidth - 5, myLabelDiv.clientHeight - 6); 
     }); 

     menuItem.connect(menuItem, "_onUnhover", function() { 
      var myLabelDiv = dojo.byId(myId + "_text"); //The label for menuItems is always the id of the menu item with '_text' appended 
      myLabelDiv.innerHTML = origText; 
      clip.destroy(); 
     }); 
    } 

</script> 

</body> 
</html>