4
Copyright DHTMLX LTD. http://www.dhtmlx.com
5
You allowed to use this component or parts of it under GPL terms
6
To use it on other terms or get Professional edition of the component please contact us at sales@dhtmlx.com
10
* @desc: context menu object
12
* @param: width - menu width
13
* @param: height - menu height
14
* @param: gfxPath - path to gfx (must be specified in https mode)
15
* @param: httpsdummy - dummy html page, which will be used for invisible iframes (necessary only for https mode)
18
function dhtmlXContextMenuObject(width,height,gfxPath,httpsdummy){
19
this.menu=new dhtmlXMenuBarObject(document.body,width,height,"",1,gfxPath,httpsdummy);
20
this.menu.setMenuMode("popup");
22
this.menu.contextMenu=this;
23
this.menu.enableWindowOpenMode(false);
24
this.menu.setOnClickHandler(this._innerOnClick);
25
this.aframes=new Array();
26
this.registerFrame(window);
31
dhtmlXContextMenuObject.prototype.registerFrame=function(awin){
32
this.aframes[this.aframes.length]=awin;
38
* @desc: set onClick context menu handler
40
* @param: func - user defined function
43
dhtmlXContextMenuObject.prototype.setContextMenuHandler=function(func){
44
if (typeof(func)=="function") this.onClickHandler=func; else this.onClickHandler=eval(func);
47
* @desc: open menu at specified position
49
* @param: x - x position
50
* @param: y - y position
51
* @param: id - context id
52
* @param: smartPosition - enable/disable correction of position
55
dhtmlXContextMenuObject.prototype.openAt=function(x,y,id,smartPosition){
56
this.espc=convertStringToBoolean(smartPosition);
59
f.clientX=parseInt(x);
60
f.clientY=parseInt(y);
62
start.contextMenuId=id;
63
start.contextMenu=this;
64
start.a=this._contextStart;
65
start.a(document.body,f);
71
* @desc: disable/enable context menu
73
* @param: mode - true/false as disable/enable
76
dhtmlXContextMenuObject.prototype.disableMenu=function(mode){
77
this._dsbd=convertStringToBoolean(mode);
81
* @desc: set onShow context menu handler
83
* @param: func - user defined function
86
dhtmlXContextMenuObject.prototype.setOnShowMenuHandler=function(func){
87
if (typeof(func)=="function") this.onShowHandler=func; else this.onShowHandler=eval(func);
92
* @desc: set zIndex value for menu object
93
* @param: left - (integer) left position shift
94
* @param: top - (integer) top position shift
98
dhtmlXContextMenuObject.prototype.setContextMenuPosition=function(left,top){
99
this._msTop=parseInt(top);
100
this._msLeft=parseInt(left);
104
* @desc: default menu onclick precessor
106
* @param: id - clicked node id
109
dhtmlXContextMenuObject.prototype._innerOnClick=function(id){
111
var that=document.body.contextMenu;
112
if (that.contextZone.ownerDocument){
113
if (that.contextZone.ownerDocument.body.onclick) that.contextZone.ownerDocument.body.onclick();
114
} else if (document.body.onclick) document.body.onclick();
115
if (that.onClickHandler) return that.onClickHandler(id,that.zoneId,that.contextZone);
120
* @desc: set context zones
122
* @param: htmlObject - id of html tag or html object
123
* @param: zoneId - id of zone
126
dhtmlXContextMenuObject.prototype.setContextZone=function(htmlObject,zoneId){
127
if (typeof(htmlObject)!="object")
128
htmlObject=document.getElementById(htmlObject);
130
if (!htmlObject.contextMenu)
131
htmlObject.contextOnclick=htmlObject.onmouseup;
132
htmlObject.selfobj = this;//nb
133
htmlObject[_isMacOS?"oncontextmenu":"onmouseup"]= function(e){ this.selfobj._contextStart(this,e); };//nb
134
// htmlObject.onmousedown= this._contextStart;
135
htmlObject.contextMenu=this;
136
htmlObject.contextMenuId=zoneId;
139
* @desc: open context menu
141
* @param: e - event object
144
dhtmlXContextMenuObject.prototype._contextStart=function(obj,e){
145
if ((_isIE)&&(window.event))
146
event.srcElement.oncontextmenu = function(){ event.cancelBubble=true; return false; };
147
if (e && e.preventDefault) e.preventDefault();
150
if(!this.contextMenu)//nb
151
this.contextMenu = this;
152
var win = obj.ownerDocument?obj.ownerDocument.parentWindow:window;
154
win = obj.ownerDocument.defaultView;
157
var a=this.contextMenu.menu.topNod;
158
a.style.position="absolute";
163
if (document.body.onclick) document.body.onclick();
165
if ((!_isMacOS)&&((!e)||(e.button!=2)))
167
if (obj.contextOnclick) obj.contextOnclick();
171
if (this.contextMenu._dsbd) return true;
172
if (this.contextMenu.onShowHandler){
173
var z=this.contextMenu.onShowHandler(obj.contextMenuId);
174
if ((typeof(z)=="boolean")&&(!z)) return;
177
this.contextMenu.menu.showBar();
180
var winScreenTop = window.screenTop;
181
if ((!winScreenTop)&&(winScreenTop!=0)){
182
winScreenTop = window.screenY+window.outerHeight-window.innerHeight;//-(window.statusbar.visible?20:0);
184
//winScreenTop = top.screenY+top.outerHeight-top.innerHeight;//-(window.statusbar.visible?20:0);
186
//alert(window.screenY+"+"+window.outerHeight+"-"+window.innerHeight)
188
var winScreenLeft = window.screenLeft;
189
if((!winScreenLeft)&&(winScreenLeft!=0)){
190
winScreenLeft = window.screenX+window.outerWidth-window.innerWidth-4;
192
//alert(window.pageXOffset)
193
//winScreenLeft = top.screenX+top.outerWidth-top.innerWidth-4;
195
var dEl0=window.document.documentElement;
196
var dEl1=window.document.body;
197
if(e.screenY-winScreenTop+a.offsetHeight-(dEl0.scrollTop||dEl1.scrollTop) > (dEl0.clientHeight||dEl1.clientHeight)){//-e.offsetY
198
var verCor = a.offsetHeight
200
var verCor = ((this.contextMenu._msTop*(-1))||0);
202
var corrector = new Array((dEl0.scrollLeft||dEl1.scrollLeft)+(this.contextMenu._msLeft||0),verCor-(dEl0.scrollTop||dEl1.scrollTop));
205
// a.style.left=e.clientX*1+document.body.scrollLeft;
206
// a.style.top=e.clientY*1+document.body.scrollTop;
207
// this.contextMenu.menu.showBar();
208
// alert(a.style.left);
212
if ((!e.screenY)&&(e.clientX))
214
a.style.top = e.clientY+"px";
215
a.style.left = e.clientX+"px";
218
a.style.top = e.screenY-winScreenTop-corrector[1]+"px";//-e.offsetY
219
a.style.left = e.screenX-winScreenLeft+corrector[0]+"px";//-e.offsetX
222
//nb: (to work correctly in frames in mozilla)
224
a.style.top = e.clientY-corrector[1]+"px";
225
a.style.left = e.clientX+corrector[0]+"px";
227
this.contextMenu.correctMenuPosition(a);
230
a.ieFix.style.top=a.style.top;
231
a.ieFix.style.left=a.style.left;
234
win.document.body.oncontextmenu=new Function("document.body.oncontextmenu=new Function('if (document.body.onclick) document.body.onclick(); return false;'); return false; ");
236
for (var i=0; i<this.contextMenu.aframes.length; i++){
237
if (this.contextMenu.aframes[i].document)
238
this.contextMenu.aframes[i].document.body.selfobj = this;
239
this.contextMenu.aframes[i].document.body.onclick=function(e){this.selfobj.contextMenu._contextEnd(e)};
242
document.body.contextMenu=this.contextMenu;
243
this.contextMenu.contextZone=obj;
244
this.contextMenu.zoneId=obj.contextMenuId;//this.contextMenu.zoneId=this.contextMenuId;
249
* @desc: correct context menu position
251
* @param: panel - html menu object
252
* @param: panel - recurse flag
255
dhtmlXContextMenuObject.prototype.correctMenuPosition=function(panel,mode){
256
var xs=document.body.offsetWidth+document.body.scrollLeft;
257
var ys=document.body.offsetHeight-15+document.body.scrollTop;
258
//now check is panel real visible
259
if ((panel.offsetWidth+parseInt(panel.style.left))>xs)
261
var z=parseInt(panel.style.left)-panel.offsetWidth;
262
if ((z<0)||(this.espc))
263
z=xs-panel.offsetWidth;
265
panel.style.left=z+"px";
266
if (panel.ieFix) panel.ieFix.style.left=z+"px";
269
if ((panel.offsetHeight+parseInt(panel.style.top))>ys)
271
var z=parseInt(panel.style.top) - panel.offsetHeight;
272
if ((z<0)||(this.espc))
273
z=ys-panel.offsetHeight;
275
panel.style.top=z+"px";
276
if (panel.ieFix) panel.ieFix.style.top=z+"px";
279
if (!mode) this.correctMenuPosition(panel,1);
282
* @desc: correct context menu position
286
dhtmlXMenuBarObject.prototype.correctMenuPosition=dhtmlXContextMenuObject.prototype.correctMenuPosition;
290
* @desc: close context menu
292
* @param: e - event object
295
dhtmlXContextMenuObject.prototype._contextEnd=function(e){
297
menu._closePanel(menu);
298
menu.lastOpenedPanel="";
299
menu.lastSelectedItem=0;
301
for (var i=0; i<this.aframes.length; i++)
302
if (this.aframes[i].document){
303
this.aframes[i].document.body.onclick=null;
304
this.aframes[i].document.body.oncontextmenu=null;
309
//(c)dhtmlx ltd. www.dhtmlx.com