contexmenu dynamic
This commit is contained in:
@ -10,6 +10,7 @@
|
||||
-webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175);
|
||||
box-shadow: 0 6px 12px rgba(0,0,0,.175);
|
||||
background-clip: padding-box;
|
||||
overflow: scroll;
|
||||
}
|
||||
.b-m-split {
|
||||
height: 6px;
|
||||
|
File diff suppressed because one or more lines are too long
@ -12,8 +12,12 @@ LEA.cmroot = 1;
|
||||
var gTemplet = $("<div/>").addClass("b-m-mpanel").attr("unselectable", "on").css("display", "none");
|
||||
var iTemplet = $("<div/>").addClass("b-m-item").attr("unselectable", "on");
|
||||
var sTemplet = $("<div/>").addClass("b-m-split");
|
||||
var $body = $("body");
|
||||
|
||||
var itemsCache = {}; // idx => items
|
||||
|
||||
//build group item, which has sub items
|
||||
var buildGroup = function(obj) {
|
||||
var buildGroup = function(obj) { // this = $("")对象, obj=item
|
||||
groups[obj.alias] = this;
|
||||
this.gidx = obj.alias;
|
||||
this.id = obj.alias;
|
||||
@ -21,7 +25,7 @@ LEA.cmroot = 1;
|
||||
this.disable = obj.disable;
|
||||
this.className = "b-m-idisable";
|
||||
}
|
||||
$(this).width(obj.width).click(function(){}).mousedown(returnfalse).appendTo($("body"));
|
||||
$(this).width(obj.width).click(function(){}).mousedown(returnfalse).appendTo($body);
|
||||
|
||||
obj = null;
|
||||
return this;
|
||||
@ -59,42 +63,47 @@ LEA.cmroot = 1;
|
||||
//add new items
|
||||
var addItems = function(gidx, items, parentAlias) {
|
||||
var tmp = null;
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
if (items[i].type == "splitLine") {
|
||||
//split line
|
||||
var len = items.length;
|
||||
for (var i = 0; i < len; i++) {
|
||||
var item = items[i];
|
||||
if (item.type == "splitLine") {
|
||||
tmp = sTemplet.clone()[0];
|
||||
} else {
|
||||
// life, alias可以不需要, 从text取, 但必须唯一
|
||||
if(!items[i].alias) {
|
||||
if(!item.alias) {
|
||||
if(parentAlias) {
|
||||
items[i].alias = parentAlias + "." + items[i].text; // 移动.Hadoop
|
||||
item.alias = parentAlias + "." + item.text; // 移动.Hadoop
|
||||
} else {
|
||||
items[i].alias = items[i].text;
|
||||
item.alias = item.text;
|
||||
}
|
||||
// log(items[i].alias);
|
||||
// log(item.alias);
|
||||
}
|
||||
items[i].gidx = gidx;
|
||||
if (items[i].type == "group" && !items[i].action) {
|
||||
item.gidx = gidx;
|
||||
if (item.type == "group" && !item.action) {
|
||||
//group
|
||||
buildGroup.apply(gTemplet.clone()[0], [items[i]]);
|
||||
arguments.callee(items[i].alias, items[i].items, items[i].alias); // life 传上级的alias, 避免重复
|
||||
items[i].type = "arrow";
|
||||
tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
|
||||
buildGroup.apply(gTemplet.clone()[0], [item]);
|
||||
itemsCache[item.alias] = item.items;
|
||||
// 递归调用, 可以动态生成?
|
||||
// arguments.callee(item.alias, item.items, item.alias); // life 传上级的alias, 避免重复
|
||||
item.type = "arrow";
|
||||
tmp = buildItem.apply(iTemplet.clone()[0], [item]);
|
||||
} else {
|
||||
// 如果group有action还是可以点击的 life
|
||||
if(items[i].type == "group") {
|
||||
if(item.type == "group") {
|
||||
//group
|
||||
buildGroup.apply(gTemplet.clone()[0], [items[i]]);
|
||||
arguments.callee(items[i].alias, items[i].items, items[i].alias); // life 传上级的alias, 避免重复
|
||||
items[i].type = "arrow";
|
||||
tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
|
||||
buildGroup.apply(gTemplet.clone()[0], [item]);
|
||||
itemsCache[item.alias] = item.items;
|
||||
// 递归调用
|
||||
// arguments.callee(item.alias, item.items, item.alias); // life 传上级的alias, 避免重复
|
||||
item.type = "arrow";
|
||||
tmp = buildItem.apply(iTemplet.clone()[0], [item]);
|
||||
} else {
|
||||
//normal item
|
||||
items[i].type = "ibody";
|
||||
tmp = buildItem.apply(iTemplet.clone()[0], [items[i]]);
|
||||
item.type = "ibody";
|
||||
tmp = buildItem.apply(iTemplet.clone()[0], [item]);
|
||||
}
|
||||
//
|
||||
var thisItem = items[i];
|
||||
var thisItem = item;
|
||||
|
||||
// 点击item
|
||||
// 用闭包来存储变量
|
||||
@ -115,16 +124,16 @@ LEA.cmroot = 1;
|
||||
});
|
||||
|
||||
}(thisItem, tmp));
|
||||
|
||||
|
||||
} //end if
|
||||
$(tmp).bind("contextmenu", returnfalse).hover(overItem, outItem);
|
||||
}
|
||||
groups[gidx].appendChild(tmp);
|
||||
tmp = items[i] = items[i].items = null;
|
||||
tmp = item = item.items = null;
|
||||
} //end for
|
||||
gidx = items = null;
|
||||
};
|
||||
// hover
|
||||
var overItem = function(e) {
|
||||
//menu item is disabled
|
||||
if (this.disable)
|
||||
@ -134,11 +143,12 @@ LEA.cmroot = 1;
|
||||
if (this.group) {
|
||||
var pos = $(this).offset();
|
||||
var width = $(this).outerWidth();
|
||||
showMenuGroup.apply(groups[this.idx], [pos, width]);
|
||||
showMenuGroup.apply(groups[this.idx], [pos, width, this]);
|
||||
}
|
||||
this.className = "b-m-ifocus";
|
||||
return false;
|
||||
};
|
||||
// hover out
|
||||
//menu loses focus
|
||||
var outItem = function(e) {
|
||||
//disabled item
|
||||
@ -150,15 +160,26 @@ LEA.cmroot = 1;
|
||||
} //Endif
|
||||
return false;
|
||||
};
|
||||
//show menu group at specified position
|
||||
var showMenuGroup = function(pos, width) {
|
||||
var bwidth = $("body").width();
|
||||
var bheight = document.documentElement.clientHeight;
|
||||
|
||||
// 显示group, 这里可以动态生成
|
||||
// show menu group at specified position
|
||||
var showMenuGroup = function(pos, width, t) {
|
||||
var $this = $(this); // dom 对象
|
||||
// 没有东西, 那么生成之, 动态生成 life [ok]
|
||||
if($this.html() == "") {
|
||||
addItems(t.idx, itemsCache[t.idx], t.idx);
|
||||
}
|
||||
var bwidth = $body.width();
|
||||
// var bheight = $body.height();
|
||||
var bheight = document.documentElement.clientHeight-10;
|
||||
bheight = bheight < 0 ? 100 : bheight;
|
||||
var mwidth = $(this).outerWidth();
|
||||
var mheight = $(this).outerHeight()-10;
|
||||
mheight = mheight < 0 ? 100 : mheight;
|
||||
var mwidth = $(this).outerWidth();
|
||||
var mheight = $(this).outerHeight();
|
||||
pos.left = (pos.left + width + mwidth > bwidth) ? (pos.left - mwidth < 0 ? 0 : pos.left - mwidth) : pos.left + width;
|
||||
pos.top = (pos.top + mheight > bheight) ? (pos.top - mheight + (width > 0 ? 25 : 0) < 0 ? 0 : pos.top - mheight + (width > 0 ? 25 : 0)) : pos.top;
|
||||
$(this).css(pos).show();
|
||||
$(this).css(pos).show().css("max-height", bheight);
|
||||
showGroups.push(this.gidx);
|
||||
};
|
||||
//to hide menu
|
||||
@ -217,27 +238,13 @@ LEA.cmroot = 1;
|
||||
root = buildGroup.apply(gTemplet.clone()[0], [option]);
|
||||
root.applyrule = applyRule;
|
||||
root.showMenu = showMenu;
|
||||
// 这里很费时
|
||||
addItems(option.alias, option.items);
|
||||
}
|
||||
else {
|
||||
root = $root[0];
|
||||
}
|
||||
|
||||
/*
|
||||
var me = $(this).each(function() {
|
||||
return $(this).bind('contextmenu', function(e) {
|
||||
var bShowContext = (option.onContextMenu && $.isFunction(option.onContextMenu)) ? option.onContextMenu.call(this, e) : true;
|
||||
if (bShowContext) {
|
||||
if (option.onShow && $.isFunction(option.onShow)) {
|
||||
option.onShow.call(this, root);
|
||||
}
|
||||
root.showMenu(e, this);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
function onShowMenu(e) {
|
||||
var bShowContext = (option.onContextMenu && $.isFunction(option.onContextMenu)) ? option.onContextMenu.call(this, e) : true;
|
||||
if (bShowContext) {
|
||||
@ -261,8 +268,10 @@ LEA.cmroot = 1;
|
||||
if (option.rule) {
|
||||
applyRule(option.rule);
|
||||
}
|
||||
/*
|
||||
gTemplet = iTemplet = sTemplet = itemTpl = buildGroup = buildItem = null;
|
||||
addItems = overItem = outItem = null;
|
||||
*/
|
||||
//CollectGarbage();
|
||||
|
||||
var out = {
|
||||
@ -270,8 +279,6 @@ LEA.cmroot = 1;
|
||||
me.unbind("contextmenu");
|
||||
},
|
||||
showMenu: function(e, target) {
|
||||
log(e);
|
||||
log(target);
|
||||
onShowMenu.call(target, e);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user