Jump to content

User:Bob/script/addlimenu.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* a brilliant function from http://spudly.shuoink.com/2007/12/01/the-ultimate-getelementsby-objectgetelementswhere/ */
function getElementsWhere(node, comparison) {
   var found = new Array();
   var all = node.getElementsByTagName("*");
   for( var i = 0; i < all.length; i++ ) {
      if( eval( "all[i]." + comparison ) ) {
         found.push( all[i] );
      }
   }
   return found;
};

function addLiMenu(tabText, position, parent) {
    if (parent) {
      var tabs = parent;
    } else {
      var pCactions = document.getElementById('p-cactions');
      var tabs = pCactions.getElementsByTagName('ul')[0];
    }

    var na, mn;
    var li;

    var tabId  = 'ca-'+tabText;
    var menuId = 'ca-'+tabText+'-menu';
    var href = 'javascript:toggleMenu("'+menuId+'");';

    na = document.createElement("a");
    na.appendChild(document.createTextNode(tabText+' ▼'));
    na.href = href;
    mn = document.createElement("ul");
    mn.id = menuId;
    li = document.createElement("li");
    li.appendChild(na);
    li.appendChild(mn);
    li.id = tabId;
    li.className = 'tabmenu';

    if (position) {
        tabs.insertBefore(li, position);
    } else {
        tabs.appendChild(li);
    }

    return li;  // the li.id is useful
}

function toggleMenu(id) {
  var el = document.getElementById(id);
  el.parentNode.blur();
  el.style.display = (el.style.display == 'block') ? 'none' : 'block';
}

function closeMenu(menus) {
  var m=0;
  while(menus[m]) {
    document.getElementById(menus[m].id).style.display = 'none';
    m++;
  }
}

function getElementUrl(id) {
    if (id) {
       return id.firstChild.getAttribute('href', 2);
    }
}

function getElementUrlText(id) {
    if (id) {
       return id.firstChild.innerHTML;
    }
}

function getTheDate() {
  var months = new Array("January", "February", "March", 
      "April", "May", "June", "July", "August", "September", 
      "October", "November", "December");
  var d = new Date();
  var curr_month = d.getMonth();
  var curr_year = d.getFullYear();
  return (months[curr_month] + curr_year);
}

$(function(){
  var menus = getElementsWhere(document,"id.match(/ca-.+-menu/)");
  document.onmouseup = function(){closeMenu(menus);};

});