]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/menu.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / menu.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36
37
38 var menuStack = new Array();
39 var hiddenElmStack = new Array();
40 var currentMenu = null;
41 var closeMenusDelay = null;
42 var openMenusDelay = null;
43 var delayTime = 75;                                     // ms for menu open delay
44
45
46 function eraseTimeout(tId) {
47         //if (tId != null)
48         window.clearTimeout(tId);
49         return null;
50 }
51
52 function tbButtonMouseOverOrig(id){
53         closeMenusDelay = eraseTimeout(closeMenusDelay);
54         var menuName = id.replace(/Handle/i,'Menu');
55         var menu = getLayer(menuName);
56         //if (menu) menu.className = 'tbButtonMouseOverUp';
57         if (currentMenu){
58                 closeAllMenus();
59         }
60         popupMenu(id, menu);
61 }
62 function tbButtonMouseOver(id,top,left,leftOffset){
63         closeMenusDelay = eraseTimeout(closeMenusDelay);
64         if (openMenusDelay == null){
65                 openMenusDelay = window.setTimeout("showMenu('"+id+"','"+top+"','"+left+"','"+leftOffset+"')", delayTime);
66         }
67 }
68 function showMenu(id,top,left,leftOffset){
69         openMenusDelay = eraseTimeout(openMenusDelay);
70         var menuName = id.replace(/Handle/i,'Menu');
71         var menu = getLayer(menuName);
72         //if (menu) menu.className = 'tbButtonMouseOverUp';
73         if (currentMenu){
74                 closeAllMenus();
75         }
76         popupMenu(id, menu, top,left,leftOffset);
77 }
78
79 function showSubMenu(id){
80         closeMenusDelay = eraseTimeout(closeMenusDelay);
81         var menuName = id.replace(/Handle/i,'Menu');
82         var menu = getLayer(menuName);
83 //      if (currentMenu){
84 //              closeMenus();
85 //      }
86 //      popupMenu(id, menu);
87         popupSubMenu(id, menu);
88 }
89
90 function popupMenu(handleID, menu, top, left, leftOffset){
91         var bw = checkBrowserWidth();
92         var menuName = handleID.replace(/Handle/i,'Menu');
93         var menuWidth = 120;
94         var imgWidth = document.getElementById(handleID).width;
95         if (menu){
96                 var menuHandle = getLayer(handleID);
97                 var p=menuHandle;
98                 if (left == "") {
99                 var left = 0;
100                 while(p&&p.tagName.toUpperCase()!='BODY'){
101                         left+=p.offsetLeft;
102                         p=p.offsetParent;
103                 }
104                 left+=parseInt(leftOffset);
105                         
106                 }
107                 if (left+menuWidth>bw) {
108                         left = left-menuWidth+imgWidth;
109                 }
110                 setMenuVisible(menu, left, top, false);
111         }
112 }
113
114
115 function popupSubMenu(handleID, menu){
116         if (menu){
117                 var menuHandle = getLayer(handleID);
118                 var p=menuHandle;
119                 //var top = p.offsetHeight, left = 0;
120                 var top = 0, left = p.offsetWidth;
121                 while(p&&p.tagName.toUpperCase()!='BODY'){
122                         top+=p.offsetTop;
123                         left+=p.offsetLeft;
124                         p=p.offsetParent;
125                 }
126                 if (is.ie && is.mac){
127                         top -= 3;
128                         left -= 10;
129                 }
130 /*
131                 if (menu.isSubMenu){
132                         try{
133                                 if (blnNetscape6){
134                                         left+=(getLayer(menu.parentid).offsetWidth - 4);
135                                 }else{
136                                         left+=(getLayer(menu.parentid).clientWidth - 8);
137                                 }
138                         }catch(e){
139                         }
140                 }else{
141                         top += menuItem.offsetHeight;
142                 }
143 */
144                 //menu.x = left;
145                 //menu.y = top;
146                 setMenuVisible(menu, left, top, true);
147                 //fixWidth(paneID, menu);
148         }
149 }
150
151 function closeMenusOrig(){
152         if (currentMenu){
153                 setMenuVisibility(currentMenu, false);
154 //              currentMenu = null;
155         }
156 }
157
158 function closeSubMenus(handle){
159         closeMenusDelay = eraseTimeout(closeMenusDelay);
160         if (menuStack.length > 0){
161                 for (var i = menuStack.length-1; i >=0; i--){
162                         var menu = menuStack[menuStack.length-1];
163                         if (menu.id == handle.getAttribute('parentid')){
164                                 currentMenu = menu;
165                                 break;
166                         }else{
167                                 closeMenu(menu);
168                                 //menuStack.pop();
169                                 menuPop();
170                         }
171                 }
172         }
173 }
174
175 function closeMenu(menu){
176         setMenuVisibility(menu, false);
177 }
178
179 function closeMenusOrig(){
180         if (menuStack.length > 0){
181                 for (var i = menuStack.length-1; i >=0; i--){
182                         //var menu = menuStack.pop();
183                         var menu = menuPop();
184                         closeMenu(menu);
185                 }
186         }
187         currentMenu = null;
188 }
189
190 function closeMenus(){
191         if (closeMenusDelay == null){
192                 closeMenusDelay = window.setTimeout("closeAllMenus()", delayTime);
193         }
194 }
195
196 function closeAllMenus(){
197         closeMenusDelay = eraseTimeout(closeMenusDelay);
198         if (menuStack.length > 0){
199                 for (var i = menuStack.length-1; i >=0; i--){
200                         //var menu = menuStack.pop();
201                         var menu = menuPop();
202                         closeMenu(menu);
203                 }
204         }
205         currentMenu = null;
206 }
207
208 function setMenuVisible(menu, x, y, isSubMenu){
209 /*
210    var id = menu.id;
211    var left=0;
212    var top=0;
213    var menuItem = menu.getMenuItemElm();
214
215    if (menuItem && menu){
216       if (menu.isTopMenu){
217          menuItem.className = 'tbButtonMouseDown';
218       }
219    }
220 */
221         if (menu){
222                 //menu.x = left;
223                 //menu.y = top;
224                 if (isSubMenu){
225                         if (menu.getAttribute('parentid') == currentMenu.getAttribute('parentid')){
226                                 //menuStack.pop();
227                                 menuPop();
228                                 setMenuVisibility(currentMenu, false);
229                         }
230                 }else{
231                         //menuStack.pop();
232                         menuPop();
233                         setMenuVisibility(currentMenu, false);
234                 }
235                 currentMenu = menu;
236                 //menuStack.push(menu);
237                 menuPush(menu);
238                 setMenuVisibility(menu, true, x, y);
239         }
240 }
241
242 function getLayer(layerid){
243 /*
244         if (document.layers && layerid){
245                 if (document.layers[layerid]) return document.layers[layerid];
246         }
247         if (document.links && layerid){
248                 if (document.links[layerid]) return document.links[layerid];
249         }
250         if (document.all && layerid){
251                 if (document.all(layerid)) return document.all(layerid);
252         }
253 */
254         return document.getElementById(layerid);
255 }
256
257 function setMenuVisibility(menu, on, x, y){
258         var parent = menu;
259         if (menu){
260 /*
261                 menu.visible = on;
262                 setLayer(menu.id, !menu.visible, menu.x, menu.y);
263                 setLayer(menu.id, !menu.visible, 0, 0);
264                 menu.visible = on;
265 */
266                 setLayer(menu.id, !on, x, y);
267                 if (is.ie){
268                         if (!on){
269                                 if (!menu.getAttribute('parentid')){
270                                         showElement("SELECT");
271                                 }
272                         }else{
273                                 hideElement("SELECT", x, y, menu.offsetWidth, menu.offsetHeight);
274                         }
275                 }
276 /*
277                 setLayer(menu.id, !menu.visible, 0, 0);
278                 var menuWidth, menuHeight;
279                 var menuLayer = getLayer(menu.id);
280                 if (menuLayer){
281                         if (blnIE55){
282                                 menuWidth = menuLayer.clientWidth;
283                                 menuHeight = menuLayer.clientHeight;
284                         }else{
285                                 menuWidth = menuLayer.offsetWidth;
286                                 menuHeight = menuLayer.offsetHeight;
287                         }
288                         if (menu.x+menuWidth > clientWindowWidth){
289                                 menu.x = clientWindowWidth - menuWidth - 25;
290                                 if (menu.x < 10){
291                                         menu.x = 10;
292                                 }
293                         }
294                         if (menu.y+menuHeight > clientWindowHeight){
295                                 menu.y = clientWindowHeight - menuHeight - 25;
296                                 if (menu.y < 10){
297                                         menu.y = 10;
298                                 }
299                         }
300                         setLayer(menu.id, !menu.visible, menu.x, menu.y);
301                 }
302 */
303         }
304 /*
305         var parentid = menu.parentid;
306         while (parentid){
307                 parent = getMenu(menu.paneID, parentid);
308                 if (parent){
309                         parent.visible = on;
310                         setLayer(parent.id, !parent.visible, parent.x, parent.y);
311                         parentid = parent.parentid;
312                         if (on == false) currentMenu = parent;
313                 }else{
314                         parentid = null;
315                 }
316         }
317         }
318         return parent;
319 */
320 }
321
322 function menuPop(){
323         if (is.ie && (is.mac || !is.ie5_5up)){
324                 var menu = menuStack[menuStack.length-1];
325                 var newMenuStack = new Array();
326                 for (var i = 0; i < menuStack.length-1; i++){
327                         newMenuStack[newMenuStack.length] = menuStack[i];
328                 }
329                 menuStack = newMenuStack;
330                 return menu;
331         }else{
332                 return menuStack.pop();
333         }
334 }
335
336 function menuPush(menu){
337         if (is.ie && (is.mac || !is.ie5_5up)){
338                 menuStack[menuStack.length] = menu;
339         }else{
340                 menuStack.push(menu);
341         }
342 }
343
344 function checkBrowserWidth(){
345         var     windowWidth;
346         if (is.ie){
347                 windowWidth = document.body.clientWidth;
348         }else{
349                 // 17px for scrollbar width
350                 windowWidth = window.innerWidth - 16;
351         }
352         if (windowWidth >= 1000){
353                 showSB('sbContent',true,'sb');
354         }else{
355                 showSB('sbContent',false,'sb');
356         }
357         return windowWidth;
358 }
359
360 function showSB(id, hideit, imgIdPrefix){
361         setLayer(id, !hideit, -1, -1);
362         setLayer(imgIdPrefix+'On', !hideit, -1, -1);
363         setLayer(imgIdPrefix+'Off', hideit, -1, -1);
364 }
365
366 function setLayer(id, hidden, x, y){
367         var layer = getLayer(id);
368         setLayerElm(layer, hidden, x, y);
369 }
370
371 function setLayerElm(layer, hideit, x, y){
372         if (layer && layer.style){
373                 if (hideit){
374                         layer.style.visibility='hidden';
375                         //layer.style.display='none';
376                 }else{
377                         layer.style.display='block';
378                         layer.style.visibility='visible';
379                 }
380                 if (x >=0 && x != ""){
381             //alert(layer.id+': '+x+', '+y+'\n'+layer.offsetLeft+', '+layer.offsetTop);
382                         //layer.style.left=x;
383                         //layer.style.top=y;
384                         layer.style.left = x+'px';
385                 }
386                 if (y >= 0 && y != "") {
387                         layer.style.top = y+'px';
388                 }
389         }
390 }
391
392 function hiliteItem(menuItem,changeClass){
393         closeMenusDelay = eraseTimeout(closeMenusDelay);
394         if (changeClass=='yes') {
395                 if (menuItem.getAttribute('avid') == 'false'){
396                         menuItem.className = 'menuItemHiliteX';
397                 }else{
398                         menuItem.className = 'menuItemHilite';
399                 }
400         }
401 }
402 function unhiliteItem(menuItem){
403         closeMenusDelay = eraseTimeout(closeMenusDelay);
404         if (menuItem.getAttribute('avid') == 'false'){
405                 menuItem.className = 'menuItemX';
406         }else{
407                 menuItem.className = 'menuItem';
408         }
409 }
410
411 function showElement(elmID){
412         for (i = 0; i < document.getElementsByTagName(elmID).length; i++)       {
413                 obj = document.getElementsByTagName(elmID)[i];
414                 if (! obj || ! obj.offsetParent)
415                         continue;
416                 obj.style.visibility = "";
417         }
418 }
419 function showElementNew(elmID){
420         if (hiddenElmStack.length > 0){
421                 for (var i = hiddenElmStack.length-1; i >=0; i--){
422                         var obj = hiddenElmStack[hiddenElmStack.length-1];
423                         obj.style.visibility = "";;
424                         hiddenElmStack.pop();
425                 }
426         }
427 }
428
429 function hideElement(elmID,x,y,w,h){
430         for (i = 0; i < document.getElementsByTagName(elmID).length; i++){
431                 obj = document.getElementsByTagName(elmID)[i];
432                 if (! obj || ! obj.offsetParent)
433                         continue;
434
435                 // Find the element's offsetTop and offsetLeft relative to the BODY tag.
436                 objLeft   = obj.offsetLeft;
437                 objTop    = obj.offsetTop;
438                 objParent = obj.offsetParent;
439                 while (objParent.tagName.toUpperCase() != "BODY"){
440                         objLeft  += objParent.offsetLeft;
441                         objTop   += objParent.offsetTop;
442                         if(objParent.offsetParent == null)
443                                 break;
444                         else
445                                 objParent = objParent.offsetParent;
446                 }
447                 // Adjust the element's offsetTop relative to the dropdown menu
448                 objTop = objTop - y;
449
450                 if (x > (objLeft + obj.offsetWidth) || objLeft > (x + w))
451                         ;
452                 else if (objTop > h)
453                         ;
454                 else if ((y + h) <= 80)
455                         ;
456                 else {
457                         obj.style.visibility = "hidden";
458                         //hiddenElmStack.push(obj);
459                 }
460         }
461 }
462
463 function Is (){
464     // convert all characters to lowercase to simplify testing
465     var agt = navigator.userAgent.toLowerCase();
466
467     // *** BROWSER VERSION ***
468     // Note: On IE5, these return 4, so use is.ie5up to detect IE5.
469     this.major = parseInt(navigator.appVersion);
470     this.minor = parseFloat(navigator.appVersion);
471
472     // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
473     // If you want to allow spoofing, take out the tests for opera and webtv.
474     this.nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
475                 && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
476                 && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
477     this.nav2 = (this.nav && (this.major == 2));
478     this.nav3 = (this.nav && (this.major == 3));
479     this.nav4 = (this.nav && (this.major == 4));
480     this.nav4up = (this.nav && (this.major >= 4));
481     this.navonly      = (this.nav && ((agt.indexOf(";nav") != -1) ||
482                           (agt.indexOf("; nav") != -1)) );
483     this.nav6 = (this.nav && (this.major == 5));
484     this.nav6up = (this.nav && (this.major >= 5));
485     this.gecko = (agt.indexOf('gecko') != -1);
486
487     this.nav7 = (this.gecko && (this.major >= 5) && (agt.indexOf('netscape/7')!=-1));
488     this.moz1 = false;
489     this.moz1up = false;
490     this.moz1_1 = false;
491     this.moz1_1up = false;
492     if (this.nav6up){
493 //    if (this.nav){
494        myRegEx = new RegExp("rv:\\d*.\\d*.\\d*");
495        //myFind = myRegEx.exec("; rv:9.10.5)");
496        myFind = myRegEx.exec(agt);
497            if(myFind!=null){
498          var strVersion = myFind.toString();
499          strVersion = strVersion.replace(/rv:/,'');
500          var arrVersion = strVersion.split('.');
501          var major = parseInt(arrVersion[0]);
502          var minor = parseInt(arrVersion[1]);
503          if (arrVersion[2]) var revision = parseInt(arrVersion[2]);
504          this.moz1 = ((major == 1) && (minor == 0));
505          this.moz1up = ((major == 1) && (minor >= 0));
506          this.moz1_1 = ((major == 1) && (minor == 1));
507          this.moz1_1up = ((major == 1) && (minor >= 1));
508           }
509     }
510
511     this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
512     this.ie3    = (this.ie && (this.major < 4));
513     this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
514     this.ie4up  = (this.ie  && (this.major >= 4));
515     this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
516     this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
517     this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
518
519     this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
520     this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
521     this.ie6up  = (this.ie  && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);
522
523         this.mac    = (agt.indexOf("mac") != -1);
524 }
525
526 function runPageLoadItems (){
527         var myVar;
528         checkBrowserWidth();
529 }
530 var is = new Is();
531
532 if (is.ie) {
533         document.write('<style type="text/css">');
534         document.write('body {font-size: x-small;}');
535         document.write ('</style>');
536 }
537