jquery - jsTree 上下文菜单翻译

标签 jquery jstree

我需要翻译jsTree中的上下文菜单(当你单击右键时会显示),目前我创建了新文件jstree.contextmenu.pl.js并复制粘贴 jquery.jstree.js 中的一些代码并做出了自己的改变。它有效,但我不确定它是不是最好的选择。

    $.jstree.plugin("contextmenu", {
    __init : function () {
        this.get_container()
            .delegate("a", "contextmenu.jstree", $.proxy(function (e) {
                    e.preventDefault();
                    if(!$(e.currentTarget).hasClass("jstree-loading")) {
                        this.show_contextmenu(e.currentTarget, e.pageX, e.pageY);
                    }
                }, this))
            .delegate("a", "click.jstree", $.proxy(function (e) {
                    if(this.data.contextmenu) {
                        $.vakata.context.hide();
                    }
                }, this))
            .bind("destroy.jstree", $.proxy(function () {
                    // TODO: move this to descruct method
                    if(this.data.contextmenu) {
                        $.vakata.context.hide();
                    }
                }, this));
        $(document).bind("context_hide.vakata", $.proxy(function () { this.data.contextmenu = false; }, this));
    },
    defaults : { 
        select_node : false, // requires UI plugin
        show_at_node : true,
        items : { // Could be a function that should return an object like this one
            "create" : {
                "separator_before"  : false,
                "separator_after"   : true,
                "label"             : "Utwórz nowy",
                "action"            : function (obj) { this.create(obj); }
            },
            "rename" : {
                "separator_before"  : false,
                "separator_after"   : false,
                "label"             : "Zmień nazwę",
                "action"            : function (obj) { this.rename(obj); }
            },
            "remove" : {
                "separator_before"  : false,
                "icon"              : false,
                "separator_after"   : false,
                "label"             : "Usuń",
                "action"            : function (obj) { if(this.is_selected(obj)) { this.remove(); } else { this.remove(obj); } }
            },
            "ccp" : {
                "separator_before"  : true,
                "icon"              : false,
                "separator_after"   : false,
                "label"             : "Edytuj",
                "action"            : false,
                "submenu" : { 
                    "cut" : {
                        "separator_before"  : false,
                        "separator_after"   : false,
                        "label"             : "Wytnij",
                        "action"            : function (obj) { this.cut(obj); }
                    },
                    "copy" : {
                        "separator_before"  : false,
                        "icon"              : false,
                        "separator_after"   : false,
                        "label"             : "Kopiuj",
                        "action"            : function (obj) { this.copy(obj); }
                    },
                    "paste" : {
                        "separator_before"  : false,
                        "icon"              : false,
                        "separator_after"   : false,
                        "label"             : "Wklej",
                        "action"            : function (obj) { this.paste(obj); }
                    }
                }
            }
        }
    },
    _fn : {
        show_contextmenu : function (obj, x, y) {
            obj = this._get_node(obj);
            var s = this.get_settings().contextmenu,
                a = obj.children("a:visible:eq(0)"),
                o = false,
                i = false;
            if(s.select_node && this.data.ui && !this.is_selected(obj)) {
                this.deselect_all();
                this.select_node(obj, true);
            }
            if(s.show_at_node || typeof x === "undefined" || typeof y === "undefined") {
                o = a.offset();
                x = o.left;
                y = o.top + this.data.core.li_height;
            }
            i = obj.data("jstree") && obj.data("jstree").contextmenu ? obj.data("jstree").contextmenu : s.items;
            if($.isFunction(i)) { i = i.call(this, obj); }
            this.data.contextmenu = true;
            $.vakata.context.show(i, a, x, y, this, obj, this._get_settings().core.rtl);
            if(this.data.themes) { $.vakata.context.cnt.attr("class", "jstree-" + this.data.themes.theme + "-context"); }
        }
    }
});

怎样做才是正确的?

最佳答案

由于项目可以是一个函数,因此现在可以通过以下方式完成它们的翻译:

$('#jstreediv').jstree({
  //...
    'contextmenu': {
        'items': function(n) {
            var tmp = $.jstree.defaults.contextmenu.items();
            tmp.create.label = 'Créer';
            tmp.rename.label = 'Renommer';
            tmp.remove.label = 'Supprimer';
            return tmp;
        }
    }
});

关于jquery - jsTree 上下文菜单翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16957291/

相关文章:

javascript - 如何使用状态插件管理选定的叶子

css - jsTree 节点图标有什么规范?

javascript - 将 UTC 日期时间转换为本地日期时间

Javascript:如果 hasClass 然后 addClass

javascript - 尽管与 img 具有相同的 css 属性,ul 仍不会居中

javascript - 始终触发 jstree 事件

java - 在 java 中表示 XPath 列表的最佳方式

javascript - 我希望我的 bootstrap 4 导航栏在悬停时展开

javascript - 单击后加载 iframe

javascript - jQuery 中的上下文菜单项