[9] | 1 | (function($) {
|
---|
| 2 | function returnfalse() { return false; };
|
---|
| 3 | $.fn.contextmenu = function(option) {
|
---|
| 4 | option = $.extend({ alias: "cmroot", width: 150 }, option);
|
---|
| 5 | var ruleName = null, target = null,
|
---|
| 6 | groups = {}, mitems = {}, actions = {}, showGroups = [],
|
---|
| 7 | itemTpl = "<div class='b-m-$[type]' unselectable=on><nobr unselectable=on><img src='$[icon]' align='absmiddle'/><span unselectable=on>$[text]</span></nobr></div>";
|
---|
| 8 | var gTemplet = $("<div/>").addClass("b-m-mpanel").attr("unselectable", "on").css("display", "none");
|
---|
| 9 | var iTemplet = $("<div/>").addClass("b-m-item").attr("unselectable", "on");
|
---|
| 10 | var sTemplet = $("<div/>").addClass("b-m-split");
|
---|
| 11 | //build group item, which has sub items
|
---|
| 12 | var buildGroup = function(obj) {
|
---|
| 13 | groups[obj.alias] = this;
|
---|
| 14 | this.gidx = obj.alias;
|
---|
| 15 | this.id = obj.alias;
|
---|
| 16 | if (obj.disable) {
|
---|
| 17 | this.disable = obj.disable;
|
---|
| 18 | this.className = "b-m-idisable";
|
---|
| 19 | }
|
---|
| 20 | $(this).width(obj.width).click(returnfalse).mousedown(returnfalse).appendTo($("body"));
|
---|
| 21 | obj = null;
|
---|
| 22 | return this;
|
---|
| 23 | };
|
---|
| 24 | var buildItem = function(obj) {
|
---|
| 25 | var T = this;
|
---|
| 26 | T.title = obj.text;
|
---|
| 27 | T.idx = obj.alias;
|
---|
| 28 | T.gidx = obj.gidx;
|
---|
| 29 | T.data = obj;
|
---|
| 30 | T.innerHTML = itemTpl.replace(/\$\[([^\]]+)\]/g, function() {
|
---|
| 31 | return obj[arguments[1]];
|
---|
| 32 | });
|
---|
| 33 | if (obj.disable) {
|
---|
| 34 | T.disable = obj.disable;
|
---|
| 35 | T.className = "b-m-idisable";
|
---|
| 36 | }
|
---|
| 37 | obj.items && (T.group = true);
|
---|
| 38 | obj.action && (actions[obj.alias] = obj.action);
|
---|
| 39 | mitems[obj.alias] = T;
|
---|
| 40 | T = obj = null;
|
---|
| 41 | return this;
|
---|
| 42 | };
|
---|
| 43 | //add new items
|
---|
| 44 | var addItems = function(gidx, items) {
|
---|
| 45 | var tmp = null;
|
---|
| 46 | for (var i = 0; i < items.length; i++) {
|
---|
| 47 | if (items[i].type == "splitLine") {
|
---|
| 48 | //split line
|
---|
| 49 | tmp = sTemplet.clone()[0];
|
---|
| 50 | } else {
|
---|
| 51 | items[i].gidx = gidx;
|
---|
| 52 | if (items[i].type == "group") {
|
---|
| 53 | //group
|
---|
| 54 | buildGroup.apply(gTemplet.clone()[0], [items[i]]);
|
---|
| 55 | arguments.callee(items[i].alias, items[i].items);
|
---|
| 56 | items[i].type = "arrow";
|
---|
| 57 | tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
|
---|
| 58 | } else {
|
---|
| 59 | //normal item
|
---|
| 60 | items[i].type = "ibody";
|
---|
| 61 | tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
|
---|
| 62 | $(tmp).click(function(e) {
|
---|
| 63 | if (!this.disable) {
|
---|
| 64 | if ($.isFunction(actions[this.idx])) {
|
---|
| 65 | actions[this.idx].call(this, target);
|
---|
| 66 | }
|
---|
| 67 | hideMenuPane();
|
---|
| 68 | }
|
---|
| 69 | return false;
|
---|
| 70 | });
|
---|
| 71 |
|
---|
| 72 | } //end if
|
---|
| 73 | $(tmp).bind("contextmenu", returnfalse).hover(overItem, outItem);
|
---|
| 74 | }
|
---|
| 75 | groups[gidx].appendChild(tmp);
|
---|
| 76 | tmp = items[i] = items[i].items = null;
|
---|
| 77 | } //end for
|
---|
| 78 | gidx = items = null;
|
---|
| 79 | };
|
---|
| 80 | var overItem = function(e) {
|
---|
| 81 | //menu item is disabled
|
---|
| 82 | if (this.disable)
|
---|
| 83 | return false;
|
---|
| 84 | hideMenuPane.call(groups[this.gidx]);
|
---|
| 85 | //has sub items
|
---|
| 86 | if (this.group) {
|
---|
| 87 | var pos = $(this).offset();
|
---|
| 88 | var width = $(this).outerWidth();
|
---|
| 89 | showMenuGroup.apply(groups[this.idx], [pos, width]);
|
---|
| 90 | }
|
---|
| 91 | this.className = "b-m-ifocus";
|
---|
| 92 | return false;
|
---|
| 93 | };
|
---|
| 94 | //menu loses focus
|
---|
| 95 | var outItem = function(e) {
|
---|
| 96 | //disabled item
|
---|
| 97 | if (this.disable )
|
---|
| 98 | return false;
|
---|
| 99 | if (!this.group) {
|
---|
| 100 | //normal item
|
---|
| 101 | this.className = "b-m-item";
|
---|
| 102 | } //Endif
|
---|
| 103 | return false;
|
---|
| 104 | };
|
---|
| 105 | //show menu group at specified position
|
---|
| 106 | var showMenuGroup = function(pos, width) {
|
---|
| 107 | var bwidth = $("body").width();
|
---|
| 108 | var bheight = document.documentElement.clientHeight;
|
---|
| 109 | var mwidth = $(this).outerWidth();
|
---|
| 110 | var mheight = $(this).outerHeight();
|
---|
| 111 | pos.left = (pos.left + width + mwidth > bwidth) ? (pos.left - mwidth < 0 ? 0 : pos.left - mwidth) : pos.left + width;
|
---|
| 112 | pos.top = (pos.top + mheight > bheight) ? (pos.top - mheight + (width > 0 ? 25 : 0) < 0 ? 0 : pos.top - mheight + (width > 0 ? 25 : 0)) : pos.top;
|
---|
| 113 | $(this).css(pos).show();
|
---|
| 114 | showGroups.push(this.gidx);
|
---|
| 115 | };
|
---|
| 116 | //to hide menu
|
---|
| 117 | var hideMenuPane = function() {
|
---|
| 118 | var alias = null;
|
---|
| 119 | for (var i = showGroups.length - 1; i >= 0; i--) {
|
---|
| 120 | if (showGroups[i] == this.gidx)
|
---|
| 121 | break;
|
---|
| 122 | alias = showGroups.pop();
|
---|
| 123 | groups[alias].style.display = "none";
|
---|
| 124 | mitems[alias] && (mitems[alias].className = "b-m-item");
|
---|
| 125 | } //Endfor
|
---|
| 126 | //CollectGarbage();
|
---|
| 127 | };
|
---|
| 128 | function applyRule(rule) {
|
---|
| 129 | if (ruleName && ruleName == rule.name)
|
---|
| 130 | return false;
|
---|
| 131 | for (var i in mitems)
|
---|
| 132 | disable(i, !rule.disable);
|
---|
| 133 | for (var i = 0; i < rule.items.length; i++)
|
---|
| 134 | disable(rule.items[i], rule.disable);
|
---|
| 135 | ruleName = rule.name;
|
---|
| 136 | };
|
---|
| 137 | function disable(alias, disabled) {
|
---|
| 138 | var item = mitems[alias];
|
---|
| 139 | item.className = (item.disable = item.lastChild.disabled = disabled) ? "b-m-idisable" : "b-m-item";
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | /* to show menu */
|
---|
| 143 | function showMenu(e, menutarget) {
|
---|
| 144 | target = menutarget;
|
---|
| 145 | showMenuGroup.call(groups.cmroot, { left: e.pageX, top: e.pageY }, 0);
|
---|
| 146 | $(document).one('mousedown', hideMenuPane);
|
---|
| 147 | }
|
---|
| 148 | var $root = $("#" + option.alias);
|
---|
| 149 | var root = null;
|
---|
| 150 | if ($root.length == 0) {
|
---|
| 151 | root = buildGroup.apply(gTemplet.clone()[0], [option]);
|
---|
| 152 | root.applyrule = applyRule;
|
---|
| 153 | root.showMenu = showMenu;
|
---|
| 154 | addItems(option.alias, option.items);
|
---|
| 155 | }
|
---|
| 156 | else {
|
---|
| 157 | root = $root[0];
|
---|
| 158 | }
|
---|
| 159 | var me = $(this).each(function() {
|
---|
| 160 | return $(this).bind('contextmenu', function(e) {
|
---|
| 161 | var bShowContext = (option.onContextMenu && $.isFunction(option.onContextMenu)) ? option.onContextMenu.call(this, e) : true;
|
---|
| 162 | if (bShowContext) {
|
---|
| 163 | if (option.onShow && $.isFunction(option.onShow)) {
|
---|
| 164 | option.onShow.call(this, root);
|
---|
| 165 | }
|
---|
| 166 | root.showMenu(e, this);
|
---|
| 167 | }
|
---|
| 168 | return false;
|
---|
| 169 | });
|
---|
| 170 | });
|
---|
| 171 | //to apply rule
|
---|
| 172 | if (option.rule) {
|
---|
| 173 | applyRule(option.rule);
|
---|
| 174 | }
|
---|
| 175 | gTemplet = iTemplet = sTemplet = itemTpl = buildGroup = buildItem = null;
|
---|
| 176 | addItems = overItem = outItem = null;
|
---|
| 177 | //CollectGarbage();
|
---|
| 178 | return me;
|
---|
| 179 | }
|
---|
| 180 | })(jQuery); |
---|