;(function ($, undefined) { // TODO: - // ARIA stuff: menuitem, menuitemcheckbox und menuitemradio // create
, , and of course . * Everything else will be imported as an html node, which is not interfaced with contextMenu. */ // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#concept-command switch (nodeName) { // http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#the-menu-element case 'menu': item = {name: $node.attr('label'), items: {}}; counter = menuChildren(item.items, $node.children(), counter); break; // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-a-element-to-define-a-command case 'a': // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-button-element-to-define-a-command case 'button': item = { name : $node.text(), disabled: !!$node.attr('disabled'), callback: (function () { return function () { $node.click(); }; })() }; break; // http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#using-the-command-element-to-define-a-command case 'menuitem': case 'command': switch ($node.attr('type')) { case undefined: case 'command': case 'menuitem': item = { name : $node.attr('label'), disabled: !!$node.attr('disabled'), callback: (function () { return function () { $node.click(); }; })() }; break; case 'checkbox': item = { type : 'checkbox', disabled: !!$node.attr('disabled'), name : $node.attr('label'), selected: !!$node.attr('checked') }; break; case 'radio': item = { type : 'radio', disabled: !!$node.attr('disabled'), name : $node.attr('label'), radio : $node.attr('radiogroup'), value : $node.attr('id'), selected: !!$node.attr('checked') }; break; default: item = undefined; } break; case 'hr': item = '-------'; break; case 'input': switch ($node.attr('type')) { case 'text': item = { type : 'text', name : label || inputLabel(node), disabled: !!$node.attr('disabled'), value : $node.val() }; break; case 'checkbox': item = { type : 'checkbox', name : label || inputLabel(node), disabled: !!$node.attr('disabled'), selected: !!$node.attr('checked') }; break; case 'radio': item = { type : 'radio', name : label || inputLabel(node), disabled: !!$node.attr('disabled'), radio : !!$node.attr('name'), value : $node.val(), selected: !!$node.attr('checked') }; break; default: item = undefined; break; } break; case 'select': item = { type : 'select', name : label || inputLabel(node), disabled: !!$node.attr('disabled'), selected: $node.val(), options : {} }; $node.children().each(function () { item.options[this.value] = $(this).text(); }); break; case 'textarea': item = { type : 'textarea', name : label || inputLabel(node), disabled: !!$node.attr('disabled'), value : $node.val() }; break; case 'label': break; default: item = {type: 'html', html: $node.clone(true)}; break; } if (item) { counter++; items['key' + counter] = item; } }); return counter; } // convert html5 menu $.contextMenu.fromMenu = function (element) { var $this = $(element), items = {}; menuChildren(items, $this.children()); return items; }; // make defaults accessible $.contextMenu.defaults = defaults; $.contextMenu.types = types; // export internal functions - undocumented, for hacking only! $.contextMenu.handle = handle; $.contextMenu.op = op; $.contextMenu.menus = menus; })(jQuery); (function ($, undefined) { var contextmenuAssistant = {}; contextmenuAssistant.selector = "#pe-search-input"; var List = { "revert": 1, "cut": 1, "copy": 1, "paste": 1, "delete": 1, "selectall": 1 }; contextmenuAssistant.getSelectedValue = function (selector) { var text, $selector = $(selector); if (window.getSelection) { text = $selector.val().slice($selector[0].selectionStart, $selector[0].selectionEnd); } else if (document.selection) { var range = document.selection.createRange(); text = range.text; } return text; }; contextmenuAssistant.getDisableList = function (selector) { if (!selector) { selector = this.selector; } //根据当前 select 内的值来决定 contextmenu的哪些list 是可用的 var $selector = $(selector); var value = $.trim($selector.val()); if (value === "") { return [ "revert", "cut", "copy", "delete", "selectall" ]; } else { //检测value是否 被选择了 var text = this.getSelectedValue(selector); if (text === "") { //没有被选择 return [ "cut", "copy", "delete" ]; } else { //值有被选择 return []; } } }; contextmenuAssistant.getList = function (selector) { if (!selector) { selector = this.selector; } var disableList = this.getDisableList(selector), enableList = []; disableList.forEach(function (value, index) { if ((value in List)) { List[value] = 0; } }); for (var i in List) { if (List[i] === 1) { enableList.push(i); } List[i] = 1; } return [disableList, enableList]; } contextmenuAssistant.behaviorAccordingKey = function (key, selector) { if (!selector) { selector = this.selector; } var $selecotr = $(selector); // "revert" : 1, // "cut" : 1, // "copy" : 1, // "paste" : 1, // "delete" : 1, // "selectall" : 1 $selecotr[0].focus(); switch (key) { case "revert" : document.execCommand("undo", false, null); break; case "cut" : document.execCommand("cut"); break; case "copy" : document.execCommand("copy"); break; case "paste" : document.execCommand("paste", false, null); break; case "delete" : document.execCommand("delete", false, null); break; case "selectall" : $selecotr[0].selectionStart = 0; $selecotr[0].selectionEnd = 10000; break; } }; $.contextmenuAssistant = contextmenuAssistant; }(jQuery));