Changeset 3919


Ignore:
Timestamp:
Oct 28, 2010, 11:01:38 AM (13 years ago)
Author:
Gregory Vincic
Message:

Tidying javascript, also fixing initTable so it does'nt do anything if there is no node of the given id.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/client/servlet/www/static/js/script.js

    r3905 r3919  
    1 /*
    2  * Add var TOGGLE = 0; to a global script
    3  */
    4 
     1//  Add var TOGGLE = 0; to a global script
    52var CONTEXT = "/proteios/app?action=";
    63
    7 function empty() { return true; }
     4function empty()
     5{
     6    return true;
     7}
     8
    89function rightGo(action)
    910{
    10   if(parent.right)
    11   {
    12     parent.right.location = CONTEXT + action;
    13   }
    14   else
    15   {
    16     goToLocation(CONTEXT + action);
    17   }
     11    if(parent.right)
     12    {
     13        parent.right.location = CONTEXT + action;
     14    }
     15    else
     16    {
     17        goToLocation(CONTEXT + action);
     18    }
    1819}
    1920
    2021function popupGo(action)
    2122{
    22   window.open(CONTEXT + action ,'popup','width=500,height=500');
     23    window.open(CONTEXT + action ,'popup','width=500,height=500');
    2324}
    2425
    2526function goToLocation(location)
    2627{
    27   if(TOGGLE == 0)
    28   {
    29       window.location = location;
    30   }
    31   TOGGLE = 0;
    32 }
    33 
    34 
    35 function getScrollXY() {
    36   var scrOfX = 0, scrOfY = 0;
    37   if( typeof( window.pageYOffset ) == 'number' ) {
    38     //Netscape compliant
    39     scrOfY = window.pageYOffset;
    40     scrOfX = window.pageXOffset;
    41   } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    42     //DOM compliant
    43     scrOfY = document.body.scrollTop;
    44     scrOfX = document.body.scrollLeft;
    45   } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    46     //IE6 standards compliant mode
    47     scrOfY = document.documentElement.scrollTop;
    48     scrOfX = document.documentElement.scrollLeft;
    49   }
    50   return [ scrOfX, scrOfY ];
    51 }
    52 
    53 function getXYSize() {
    54   var myWidth = 0, myHeight = 0;
    55   if( typeof( window.innerWidth ) == 'number' ) {
    56     //Non-IE
    57     myWidth = window.innerWidth;
    58     myHeight = window.innerHeight;
    59   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    60     //IE 6+ in 'standards compliant mode'
    61     myWidth = document.documentElement.clientWidth;
    62     myHeight = document.documentElement.clientHeight;
    63   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    64     //IE 4 compatible
    65     myWidth = document.body.clientWidth;
    66     myHeight = document.body.clientHeight;
    67   }
    68   return [ myWidth, myHeight ];
    69 }
    70 
     28    if(TOGGLE == 0)
     29    {
     30        window.location = location;
     31    }
     32    TOGGLE = 0;
     33}
     34
     35function getScrollXY()
     36{
     37    var scrOfX = 0, scrOfY = 0;
     38    if( typeof( window.pageYOffset ) == 'number' )
     39    {
     40        //Netscape compliant
     41        scrOfY = window.pageYOffset;
     42        scrOfX = window.pageXOffset;
     43    }
     44    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
     45    {
     46        //DOM compliant
     47        scrOfY = document.body.scrollTop;
     48        scrOfX = document.body.scrollLeft;
     49    }
     50    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
     51    {
     52        //IE6 standards compliant mode
     53        scrOfY = document.documentElement.scrollTop;
     54        scrOfX = document.documentElement.scrollLeft;
     55    }
     56    return [ scrOfX, scrOfY ];
     57}
     58
     59function getXYSize()
     60{
     61    var myWidth = 0, myHeight = 0;
     62    if( typeof( window.innerWidth ) == 'number' ) {
     63        // Non-IE
     64        myWidth = window.innerWidth;
     65        myHeight = window.innerHeight;
     66    }
     67    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
     68    {
     69        // IE 6+ in 'standards compliant mode'
     70        myWidth = document.documentElement.clientWidth;
     71        myHeight = document.documentElement.clientHeight;
     72    }
     73    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
     74    {
     75        // IE 4 compatible
     76        myWidth = document.body.clientWidth;
     77        myHeight = document.body.clientHeight;
     78    }
     79    return [ myWidth, myHeight ];
     80}
     81
     82// Returns node by the given id, or null if it does not exist
    7183function getElement(id)
    7284{
    73   var div;
    74   if(document.all)
    75   {
    76     div = document.all[id];
    77   }
    78   else
    79   {
    80     div = document.getElementById(id);
    81   }
    82   return div;
     85  return document.all ? document.all[id] : document.getElementById(id);
    8386}
    8487
    8588function submitForm(formId)
    8689{
    87   getElement(formId).submit();
    88 }
    89 
     90    getElement(formId).submit();
     91}
    9092
    9193function addHiliteEvents(item)
    9294{
    93   item.onmouseover = function() { this.className = "hover " + this.className;}
    94   item.onmouseout = function() { this.className = this.className.slice(6);}
    95 }
    96 
     95    item.onmouseover = function()
     96    {
     97        this.className = "hover " + this.className;
     98    };
     99    item.onmouseout = function()
     100    {
     101        this.className = this.className.slice(6);
     102    };
     103}
    97104
    98105function submitFormAs(formId, action)
    99106{
    100   if(TOGGLE == 0)
    101   {
    102     getElement(formId + "action").value = action;
    103     getElement(formId).submit();
    104   }
    105   TOGGLE = 0;
    106 }
    107 
    108 /* Toggles visibility of a div */
     107    if(TOGGLE == 0)
     108    {
     109        getElement(formId + "action").value = action;
     110        getElement(formId).submit();
     111    }
     112    TOGGLE = 0;
     113}
     114
     115// Toggles visibility of a div
    109116function toggle(id)
    110117{
    111   var div = getElement(id);
    112   if(div.style.display == "block")
    113   {
     118    var div = getElement(id);
     119    if(div.style.display == "block")
     120    {
     121        div.style.display = "none";
     122    }
     123    else
     124    {
     125        div.style.display = "block";
     126    }
     127}
     128
     129// Toggles visibility of a fieldset
     130function toggleFieldset(id)
     131{
     132    var node = getElement(id);
     133    var icon = getElement("btn" + id);
     134    if(node.style.display == "block")
     135    {
     136        node.style.display = "none";
     137        icon.src = "static/img/plus.gif";
     138    }
     139    else
     140    {
     141        node.style.display = "block";
     142        icon.src = "static/img/minus.gif";
     143    }
     144}
     145
     146var visiblePopup = 0;
     147// popup, toggles the visibility and positions a div so it's visible
     148function popup(id)
     149{
     150    var div = getElement(id);
     151    var xy = getScrollXY();
     152    var newTop = (xy[1]+50) + "px";
     153    div.style.top = newTop;
     154    toggle(id);
     155    visiblePopup=id;
     156}
     157
     158// TODO there might be a situation where more popups have been shown, this method should close them all
     159function closeAllVisiblePopups()
     160{
     161    hide(visiblePopup);
     162}
     163
     164// Sets display:none for the given elementId
     165function hide(id)
     166{
     167    var div = getElement(id);
    114168    div.style.display = "none";
    115   }
    116   else
    117   {
     169}
     170
     171function closePopup(id)
     172{
     173    hide(id);
     174}
     175
     176// Sets display:block for the given elementId
     177function show(id)
     178{
     179    var div = getElement(id);
    118180    div.style.display = "block";
    119   }
    120 }
    121 
    122 /* Toggles visibility of a fieldset */
    123 function toggleFieldset(id)
    124 {
    125   var node = getElement(id);
    126   var icon = getElement("btn" + id);
    127   if(node.style.display == "block")
    128   {
    129     node.style.display = "none";
    130     icon.src = "static/img/plus.gif";
    131   }
    132   else
    133   {
    134     node.style.display = "block";
    135     icon.src = "static/img/minus.gif";
    136   }
    137 }
    138 
    139 var visiblePopup = 0;
    140 /* popup, toggles the visibility and positions a div so it's visible */
    141 function popup(id)
    142 {
    143   var div = getElement(id);
    144   var xy = getScrollXY();
    145   var newTop = (xy[1]+50) + "px";
    146   div.style.top = newTop;
    147   toggle(id);
    148   visiblePopup=id;
    149 }
    150 
    151 /* TODO there might be a situation where more popups have been shown, this method should close them all */
    152 function closeAllVisiblePopups()
    153 {
    154   hide(visiblePopup);
    155 }
    156 
    157 /* Sets display:none for the given elementId */
    158 function hide(id)
    159 {
    160  var div = getElement(id);
    161  div.style.display = "none";
    162 }
    163 
    164 function closePopup(id)
    165 {
    166   hide(id);
    167 }
    168 
    169 
    170 /* Sets display:block for the given elementId */
    171 function show(id)
    172 {
    173    var div = getElement(id);
    174    div.style.display = "block";
    175 }
    176 
     181}
    177182
    178183var LASTTABID = 0;
    179184function showTab(id)
    180185{
    181   var prefix = "contentOf";
    182   if(LASTTABID != 0)
    183   {
    184     var last = getElement(LASTTABID);
    185     last.className = "tab";
    186     hide(prefix + LASTTABID);
    187   }
    188   show(prefix + id);
    189   var tab = getElement(id);
    190   tab.className = "hover tab here";
    191   LASTTABID = id;
    192 }
    193 
    194 
    195 /* Initiates a table with correct class and eventhandlers*/
     186    var prefix = "contentOf";
     187    if(LASTTABID != 0)
     188    {
     189        var last = getElement(LASTTABID);
     190        last.className = "tab";
     191        hide(prefix + LASTTABID);
     192    }
     193    show(prefix + id);
     194    var tab = getElement(id);
     195    tab.className = "hover tab here";
     196    LASTTABID = id;
     197}
     198
     199// Initiates a table with correct class and eventhandlers
    196200function initTable(id)
    197201{
    198   var table = getElement(id);
    199   var tableRows = table.rows;
    200   for(var i = 0; i<tableRows.length;i++)
    201   {
    202     var row = tableRows[i];
    203     addHiliteEvents(row);
    204   }
    205 }
    206 
    207 
    208 /* Binds hover events to all LI elements  */
     202    var table = getElement(id);
     203    if(table != null)
     204    {
     205        var tableRows = table.rows;
     206        for(var i = 0; i<tableRows.length;i++)
     207        {
     208            var row = tableRows[i];
     209            addHiliteEvents(row);
     210        }
     211    }
     212}
     213
     214// Binds hover events to all LI elements
    209215function initUl(id) {
    210   var ul = getElement(id);
    211   var elements = ul.getElementsByTagName("LI");
    212   for(var i = 0; i< elements.length-1;i++)
    213   {
    214     if(elements[i].nodeName == "LI")
    215     {
    216         var li = elements[i];
    217       addHiliteEvents(li);
    218     }
    219   }
    220 }
    221 
     216    var ul = getElement(id);
     217    var elements = ul.getElementsByTagName("LI");
     218    for(var i = 0; i< elements.length-1;i++)
     219    {
     220        if(elements[i].nodeName == "LI")
     221        {
     222            var li = elements[i];
     223            addHiliteEvents(li);
     224        }
     225    }
     226}
    222227
    223228function setNameField(select)
    224229{
    225   var form = select.form;
    226   var option = select.options[select.selectedIndex];
    227   var value = option.text;
    228   var elements = form.getElementsByTagName("input");
    229   for(var i = 0; i < elements.length;i++)
    230   {
    231       var input = elements[i];
    232     if(input.id == "annotationName")
    233     {
    234       if(option.value == "0")
    235       {
    236         input.value = "";
    237         input.disabled = !(input.disabled);
    238       }
    239       else
    240       {
    241         input.value = option.text;
    242         input.disabled = "disabled";
    243       }
    244     }
    245   }
     230    var form = select.form;
     231    var option = select.options[select.selectedIndex];
     232    var value = option.text;
     233    var elements = form.getElementsByTagName("input");
     234    for(var i = 0; i < elements.length;i++)
     235    {
     236        var input = elements[i];
     237        if(input.id == "annotationName")
     238        {
     239            if(option.value == "0")
     240            {
     241                input.value = "";
     242                input.disabled = !(input.disabled);
     243            }
     244            else
     245            {
     246                input.value = option.text;
     247                input.disabled = "disabled";
     248            }
     249        }
     250    }
    246251}
    247252
    248253function checkAllRowsInTable(formId)
    249254{
    250   var form = getElement(formId);
    251   var elements = form.getElementsByTagName("input");
    252   for(var i = 0; i < elements.length;i++)
    253   {
    254       var input = elements[i];
    255     if(input.id == "itemId")
    256     {
    257       input.checked = !input.checked;
    258     }
    259   }
     255    var form = getElement(formId);
     256    var elements = form.getElementsByTagName("input");
     257    for(var i = 0; i < elements.length;i++)
     258    {
     259        var input = elements[i];
     260        if(input.id == "itemId")
     261        {
     262            input.checked = !input.checked;
     263        }
     264    }
    260265}
    261266
    262267/*
    263   Add a field to a form. If it already exists the value is changed.
     268    Add a field to a form. If it already exists the value is changed.
    264269*/
    265270function addField(formId, name, value)
    266271{
    267   var field = null;
    268     var form = getElement(formId);
    269   for(var i = 0; i < form.elements.length; i++)
    270   {
    271     var e = form.elements[i];
    272     if(e.name == name)
    273     {
    274       field = e;
    275       break;
    276     }
    277   }
    278   if(field == null)
    279   {
    280   /* Create new element */
    281     field = document.createElement("input");
    282       field.type = "hidden";
    283       field.name = name;
    284       form.appendChild(field);
    285     }
     272    var field = null;
     273    var form = getElement(formId);
     274    for(var i = 0; i < form.elements.length; i++)
     275    {
     276        var e = form.elements[i];
     277        if(e.name == name)
     278        {
     279            field = e;
     280            break;
     281        }
     282    }
     283    if(field == null)
     284    {
     285        // Create new element
     286        field = document.createElement("input");
     287        field.type = "hidden";
     288        field.name = name;
     289        form.appendChild(field);
     290    }
    286291    field.value = value;
    287292}
     
    289294function addShadeUnshade(obj)
    290295{
    291   obj.shade = function() {
    292     this.className = "shade " + this.className;
    293     }
    294 
    295     obj.unshade = function() {
    296     this.className = this.className.slice(6);
    297     }
    298 }
    299 
    300 /* http://blog.firetree.net/2005/07/04/javascript-find-position/ */
    301   function findPosX(obj)
    302   {
     296    obj.shade = function()
     297    {
     298        this.className = "shade " + this.className;
     299    };
     300
     301    obj.unshade = function()
     302    {
     303        this.className = this.className.slice(6);
     304    };
     305}
     306
     307// http://blog.firetree.net/2005/07/04/javascript-find-position/
     308function findPosX(obj)
     309{
    303310    var curleft = 0;
    304311    if(obj.offsetParent)
    305         while(1)
    306         {
    307           curleft += obj.offsetLeft;
    308           if(!obj.offsetParent)
     312    while(1)
     313    {
     314        curleft += obj.offsetLeft;
     315        if(!obj.offsetParent)
     316        {
    309317            break;
    310           obj = obj.offsetParent;
    311         }
     318        }
     319        obj = obj.offsetParent;
     320    }
    312321    else if(obj.x)
     322    {
    313323        curleft += obj.x;
     324    }
     325
    314326    return curleft;
    315   }
    316 
    317   function findPosY(obj)
    318   {
     327}
     328
     329function findPosY(obj)
     330{
    319331    var curtop = 0;
    320332    if(obj.offsetParent)
    321         while(1)
    322         {
    323           curtop += obj.offsetTop;
    324           if(!obj.offsetParent)
     333    while(1)
     334    {
     335        curtop += obj.offsetTop;
     336        if(!obj.offsetParent)
     337        {
    325338            break;
    326           obj = obj.offsetParent;
    327         }
     339        }
     340        obj = obj.offsetParent;
     341    }
    328342    else if(obj.y)
     343    {
    329344        curtop += obj.y;
     345    }
     346
    330347    return curtop;
    331   }
    332 
    333 
    334  /* http://bytes.com/forum/thread763579.html */
    335  function resize_all_iframes()
    336  {
     348}
     349
     350// http://bytes.com/forum/thread763579.html
     351function resize_all_iframes()
     352{
    337353    var f_list = document.getElementsByTagName("IFRAME");
    338354    if(f_list.length > 0)
    339355    {
    340      for (var i = 0, f; f = f_list[i]; i++)
    341      {
    342        resize_iframe(f.id);
    343      }
    344     }
    345  }
    346 
    347  /* http://guymal.com/mycode/100_percent_iframe/ */
     356        for (var i = 0, f; f = f_list[i]; i++)
     357        {
     358            resize_iframe(f.id);
     359        }
     360    }
     361}
     362
     363// http://guymal.com/mycode/100_percent_iframe/
    348364function resize_iframe(frameId)
    349365{
    350366
    351   var height=window.innerWidth;//Firefox
    352   if (document.body.clientHeight)
    353   {
    354     height=document.body.clientHeight;//IE
    355   }
    356   //resize the iframe according to the size of the
    357   //window (all these should be on the same line)
    358   var iframe = getElement(frameId);
    359   var y = findPosY(iframe);
    360   iframe.style.height=parseInt(height-y-8)+"px";
    361 }
    362 
    363 /* http://www.dynamicdrive.com/dynamicindex5/texttool.htm */
     367    var height=window.innerWidth;//Firefox
     368    if (document.body.clientHeight)
     369    {
     370        height=document.body.clientHeight;//IE
     371    }
     372    // resize the iframe according to the size of the
     373    // window (all these should be on the same line)
     374    var iframe = getElement(frameId);
     375    var y = findPosY(iframe);
     376    iframe.style.height=parseInt(height-y-8)+"px";
     377}
     378
     379// http://www.dynamicdrive.com/dynamicindex5/texttool.htm
    364380function showtip(current,e,text)
    365381{
    366    if (document.all || document.getElementById)
    367    {
    368       thetitle = text.split('<br>');
    369       if (thetitle.length > 1)
    370       {
    371          thetitles = '';
    372          for (i=0; i < thetitle.length; i++)
    373          {
    374             thetitles += thetitle[i];
    375          }
    376          current.title = thetitles;
    377       }
    378       else
    379       {
    380          current.title = text;
    381       }
    382    }
    383    else if (document.layers)
    384    {
    385       document.tooltip.document.write('<layer bgColor="white" style="border:1px solid black;font-size:12px;">'+text+'</layer>');
    386       document.tooltip.document.close();
    387       document.tooltip.left = e.pageX + 5;
    388       document.tooltip.top = e.pageY + 5;
    389       document.tooltip.visibility = "show";
    390    }
     382    if (document.all || document.getElementById)
     383    {
     384        thetitle = text.split('<br>');
     385        if (thetitle.length > 1)
     386        {
     387            thetitles = '';
     388            for (i=0; i < thetitle.length; i++)
     389            {
     390                thetitles += thetitle[i];
     391            }
     392            current.title = thetitles;
     393        }
     394        else
     395        {
     396            current.title = text;
     397        }
     398    }
     399    else if (document.layers)
     400    {
     401        document.tooltip.document.write('<layer bgColor="white" style="border:1px solid black;font-size:12px;">'+text+'</layer>');
     402        document.tooltip.document.close();
     403        document.tooltip.left = e.pageX + 5;
     404        document.tooltip.top = e.pageY + 5;
     405        document.tooltip.visibility = "show";
     406    }
    391407}
    392408
    393409function hidetip()
    394410{
    395    if (document.layers)
    396    {
    397       document.tooltip.visibility = "hidden";
    398    }
    399 }
    400 
    401 // this will resize all iframes every
    402 // time you change the size of the window.
     411    if (document.layers)
     412    {
     413        document.tooltip.visibility = "hidden";
     414    }
     415}
     416
     417// this will resize all iframes every time you change the size of the window.
    403418window.onresize=resize_all_iframes;
    404419
    405 
    406420var body;
    407 window.onload =function() {
    408   body = getElement("rightFrameBody");
    409   addShadeUnshade(body);
    410   var elements = document.getElementsByTagName("LI");
    411   for(var i = 0; i < elements.length;i++)
    412   {
    413     addHiliteEvents(elements[i]);
    414   }
    415 }
    416 
    417 
    418 
    419 
     421window.onload =function()
     422{
     423    body = getElement("rightFrameBody");
     424    addShadeUnshade(body);
     425    var elements = document.getElementsByTagName("LI");
     426    for(var i = 0; i < elements.length;i++)
     427    {
     428        addHiliteEvents(elements[i]);
     429    }
     430};
     431
Note: See TracChangeset for help on using the changeset viewer.