Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / Siman / WebContent / jvs / popup.js
1 //
2 //  Displays and hide pop-up menus.
3 //  Pop-ups are displayed when left-clicking on an edition icon of a link.
4 //  The HTML corresponding tags are supposed being the following:
5 //
6 //      <a href="popupname"><img src="{path}icon.ed..." .../></a>
7 //
8 //  Any other HTML construct will be ignored.
9 //
10 //  Pop-ups can be used on a web page including java applets.
11 //  If a pop-up hides an applet, this latter must be hidden when displaying the pop-up - as a limitation,
12 //  the applet is supposed entirely hidden by the pop-up.
13 //  By convention:
14 //  - such pop-up must be named "toolpop" (<div id="toolpop"...></div><a href="toolpop">...</a>),
15 //  - the  applet must be named "perform" (<applet id="perform"...></applet>).
16
17     var _iconName         = "icon.ed";
18     var _appletName       = "perform";
19     var _hiddingPopup     = "toolpop";
20
21     var _replaceContext   = false;       // replace the system context menu
22     var _mouseOverContext = false;       // is the mouse over the context menu
23     var _divContext       = null;
24     var _appletContext    = null;        // Applet hidden by the popup
25     
26     InitContext();
27
28     function InitContext() {
29 //  ----------------------
30       document.onmousedown = ContextMouseDown;
31       document.onclick     = ContextShow;
32     }
33
34 //  Call from the onMouseDown event, passing the event if standards compliant
35     function ContextMouseDown(event) {
36 //  --------------------------------
37       if (_mouseOverContext) return;
38       
39       var leftbutton = 0;
40         
41    // IE is evil and doesn't pass the event object
42       if (event == null) {
43         event      = window.event;
44         leftbutton = 1;
45       }
46    // We assume we have a standards compliant browser, but check if we have IE
47       var target = event.target != null ? event.target : event.srcElement;
48         
49    // Only show the context menu if the right mouse button is pressed
50    // and a hyperlink on an edit icon has been clicked
51           if (_divContext != null) ContextHide();   // In case of another context menu previously shown
52       if (target.tagName.toLowerCase() == 'img') {
53         if (target.src.indexOf(_iconName) < 0) return;
54         target = target.parentNode;
55       }
56       if (event.button == leftbutton && target.tagName.toLowerCase() == 'a') {
57         _replaceContext = true;
58         if (target.getAttribute("href") == _hiddingPopup) _appletContext = document.getElementById(_appletName);
59       }
60     }
61
62     function ContextHide() {
63 //  -----------------------
64       _divContext.style.display = 'none';
65           if (_appletContext != null) {
66           _appletContext.style.display = 'inline';
67           _appletContext = null;
68           }
69     }
70
71 //  Call from the onContextMenu event, passing the event
72 //  If this function returns false, the browser's context menu will not show up
73     function ContextShow(event) {
74 //  ---------------------------
75       if (_mouseOverContext) return true;
76       
77    // IE is evil and doesn't pass the event object
78       if (event == null) event = window.event;
79
80    // we assume we have a standards compliant browser, but check if we have IE
81       var target = event.target != null ? event.target : event.srcElement;
82       target = target.parentNode;                          // Gets the hyperlink
83       if (_replaceContext) {
84         _divContext = $(target.attributes[0].nodeValue);   // Popup involved (made of document index prefixed by "popup")
85
86                 // document.body.scrollTop does not work in IE
87                 var scrollTop  = document.scrollTop ? document.scrollTop : document.documentElement.scrollTop;
88                 var scrollLeft = document.scrollLeft ? document.scrollLeft : document.documentElement.scrollLeft;
89
90                 // hide the menu first to avoid an "up-then-over" visual effect
91                 _divContext.style.display = 'none';
92                 _divContext.style.left = event.clientX + scrollLeft + 'px';
93                 _divContext.style.top = event.clientY + scrollTop + 'px';
94                 _divContext.style.display = 'block';
95
96                 _replaceContext = false;
97             if (_appletContext != null) _appletContext.style.display = 'none';
98
99                 return false;
100       }
101       return true;
102     }
103
104 //  For making easier on the eyes and fingers
105     function $(id) {
106 //  --------------
107       return document.getElementById(id);
108     }