0

我希望它像 Windows 中的 explorer.exe 一样工作,只有当我们剪切或复制了粘贴项目才会出现的元素时。这有时有效,但也许我缺少一些东西。如果有一种改进它的方法总是我很高兴知道你对它的想法。

           jQuery.noConflict();

        jQuery(document).ready(function ($) {
        // This is for the context menu to bind with operations on the right clicked node
                function customMenu(node) {
            // The default set of all items

        var control;

            var items = {

                      createItem: { 
                    label: "Create",
                    action: function (node) {   return {createItem: this.create(node) }; }
                },

                        renameItem: { 
                    label: "Rename",
                    action: function (node) {  return {renameItem: this.rename(node) }; }
                },

                    deleteItem: { 
                    label: "Delete",
                    action: function (node) {     return {deleteItem: this.remove(node) }; },
                    "separator_after": true

                },
                        copyItem: { 
                                label: "Copy",
                                action: function (node) {  $(node).addClass("copy");  return {copyItem: this.copy(node)  }; }
                },
                                cutItem: { 
                                label: "Cut",
                                action: function (node) { $(node).addClass("cut");  return {cutItem: this.cut(node)  }; }
                },
                        pasteItem: { 
                                label: "Paste",
                                action: function (node) {  $(node).addClass("paste");    return {pasteItem: this.paste(node) }; }
                }


            };


            // We go over all the selected items as the context menu only takes action on the one that is right clicked
                $.jstree._reference("#{$id_arr[$k]}").get_selected(false, true).each(function(index,element){   

                        if ( $(element).attr("id") != $(node).attr("id") )
                        {
                            // Let's deselect all nodes that are unrelated to the context menu -- selected but are not the one right clicked

                            $("#{$id_arr[$k]}").jstree("deselect_node", '#'+$(element).attr("id") );  
                        }
                    });



            // Let's loop through all elements and try to find if the paste operation was done already      
            $("#{$id_arr[$k]}").find("li").each(function(index,element){        

             if($(node).hasClass("paste") )
             { 
                //We are asumming that the current event is pasted so remove the Paste from the context menu
                 control =0; 
                // and from any node that has the class for cut or copy.. there is no other way as dnd, move and copy, cut are a move operation for jstree
                    if( $(element).hasClass("copy") ) $(element).removeClass("copy");   
                    if ( $(element).hasClass("cut") ) $(element).removeClass("cut");
             }  
           if( ($(element).hasClass("copy") || $(element).hasClass("cut") )  )
           { 
            control =1;
           }

           //we already pasted the node
           if( $(element).hasClass("paste") )
           { 
           $(this).removeClass("paste");
             }

            });


               switch (control)
               {
                case 1:
                                    switch ($(node).attr("rel"))
                                                {

                                                        case "drive":
                                                            delete items.renameItem;
                                                            delete items.deleteItem;
                                                            delete items.cutItem;
                                                            delete items.copyItem;
                                                        break;

                                                }
                    break;


                case 0:
                                                switch ($(node).attr("rel"))
                                                        {

                                                                case "drive":
                                                                    delete items.renameItem;
                                                                    delete items.deleteItem;
                                                                    delete items.cutItem;
                                                                    delete items.copyItem;
                                                                delete items.pasteItem;
                                                                break;

                                                                case "default":
                                                              delete items.pasteItem;
                                                                break;

                                                        }   
                break;

                default:                switch ($(node).attr("rel"))
                                                        {

                                                                case "drive":
                                                                    delete items.renameItem;
                                                                    delete items.deleteItem;
                                                                    delete items.cutItem;
                                                                    delete items.copyItem;
                                                                delete items.pasteItem;
                                                                break;

                                                                case "default":
                                                              delete items.pasteItem;
                                                                break;

                                                        }   

                break;

                    }



            return items;

        }
4

1 回答 1

0

显然这样做了,但它可以适用于所有情况吗?如果剪切或粘贴是当前节点,那么我不想在其上显示粘贴选项,而仅在其他节点上显示。希望任何人都可以看一下,或者它可以帮助试图实现无需查看绑定的相同功能:

                <div id="$id_arr[$k]" class="jstree_container"></div>
        </div>
        </li>

        <!-- JavaScript neccessary for this tree : {$value} -->
        <script type="text/javascript" >
           jQuery.noConflict();

        jQuery(function ($) {

        // This is for the context menu to bind with operations on the right clicked node
                function customMenu(node) {
            // The default set of all items

        var control;

            var items = {

                      createItem: { 
                    label: "Create",
                    action: function (node) {   return {createItem: this.create(node) }; }
                },

                        renameItem: { 
                    label: "Rename",
                    action: function (node) {  return {renameItem: this.rename(node) }; }
                },

                    deleteItem: { 
                    label: "Delete",
                    action: function (node) {     return {deleteItem: this.remove(node) }; },
                    "separator_after": true

                },
                        copyItem: { 
                                label: "Copy",
                                action: function (node) {  $(node).addClass("copy");  return {copyItem: this.copy(node)  }; }
                },
                                cutItem: { 
                                label: "Cut",
                                action: function (node) { $(node).addClass("cut");  return {cutItem: this.cut(node)  }; }
                },
                        pasteItem: { 
                                label: "Paste",
                                action: function (node) {  $(node).addClass("paste");    return {pasteItem: this.paste(node) }; }
                }


            };


            // We go over all the selected items as the context menu only takes action on the one that is right clicked
                $.jstree._reference("#{$id_arr[$k]}").get_selected(false, true).each(function(index,element)
                {   

                        if ( $(element).attr("id") != $(node).attr("id") )
                        {
                            // Let's deselect all nodes that are unrelated to the context menu -- selected but are not the one right clicked

                            $("#{$id_arr[$k]}").jstree("deselect_node", '#'+$(element).attr("id") );  
                        }
                    });




        //if any previous click has the class for copy or cut
             $("#{$id_arr[$k]}").find("li").each(function(index,element)        
             {  
                        if ($(element) != $(node) )
                    {   
                        if(  $(element).hasClass("copy") || $(element).hasClass("cut") ) control=1;
                    }
                    else if(  $(node).hasClass("cut") || $(node).hasClass("copy"))
                        {
                        control=0;
                        }
            });


            //only remove the class for cut or copy if the current operation is to paste
        if($(node).hasClass("paste") )
        {   
            control=0;   
            // Let's loop through all elements and try to find if the paste operation was done already      
            $("#{$id_arr[$k]}").find("li").each(function(index,element)
            {       
                if( $(element).hasClass("copy") ) $(this).removeClass("copy");  
                    if ( $(element).hasClass("cut") ) $(this).removeClass("cut"); 
                  if ( $(element).hasClass("paste") ) $(this).removeClass("paste");  
                });
        }


               switch (control)
               {
                //Remove the paste item from the context menu
                case 0:
                                switch ($(node).attr("rel"))
                                    {

                                            case "drive":
                                                delete items.renameItem;
                                                delete items.deleteItem;
                                                delete items.cutItem;
                                                delete items.copyItem;
                                            delete items.pasteItem;
                                            break;

                                            case "default":
                                          delete items.pasteItem;
                                            break;

                                    }   
                break;

               //Remove the paste item from the context menu only on the node that has either copy or cut added class
                        case 1:
                        if( $(node).hasClass("cut") || $(node).hasClass("copy") )
                        {
                                                switch ($(node).attr("rel"))
                                                {

                                                                case "drive":
                                                                    delete items.renameItem;
                                                                    delete items.deleteItem;
                                                                    delete items.cutItem;
                                                                    delete items.copyItem;
                                                                delete items.pasteItem;
                                                                break;

                                                                case "default":
                                                              delete items.pasteItem;
                                                                break;


                                                }
                                        }   
                                            else //Re-enable it on the clicked node that does not have the cut or copy class
                                                {
                                    switch ($(node).attr("rel"))
                                                {

                                                        case "drive":
                                                            delete items.renameItem;
                                                            delete items.deleteItem;
                                                            delete items.cutItem;
                                                            delete items.copyItem;
                                                        break;

                                                }       
                                            }   

                    break;

                //initial state don't show the paste option on any node
                default:                switch ($(node).attr("rel"))
                                                        {

                                                                case "drive":
                                                                    delete items.renameItem;
                                                                    delete items.deleteItem;
                                                                    delete items.cutItem;
                                                                    delete items.copyItem;
                                                                    delete items.pasteItem;
                                                                break;

                                                                case "default":
                                                              delete items.pasteItem;
                                                                break;

                                                        }   

                break;

                    }

            return items;

        }




        $("#{$id_arr[$k]}").jstree({


                // List of active plugins used
                "plugins" : [ "themes","json_data", "ui", "crrm" , "hotkeys" , "types" , "dnd", "contextmenu"],  

                "contextmenu" : { "items" : customMenu  , "select_node": true},
于 2012-06-17T16:52:52.803 回答