User:Iritscen/TestSpace.js: Difference between revisions

From OniGalore
Jump to navigation Jump to search
(now that I am in my test space, I can spam it up with alerts)
(oops, can't continue outside a loop)
Line 5: Line 5:
  *  Maintainers: [[User:Iritscen]]
  *  Maintainers: [[User:Iritscen]]
  */
  */
function sortSortableTables()
function sortSortableTables()
{
{
   // Iterate over all <div> elements
   // Iterate over all <div> elements
   var divs = document.getElementsByTagName("div");
   var divs = document.getElementsByTagName("div");
   if (!divs) continue;
   if (!divs) return;
   alert("Checking for sortable table.");
   alert("Checking for sortable table.");
   for (var i = 0; i < divs.length; i++)
   for (var i = 0; i < divs.length; i++)

Revision as of 23:38, 12 November 2015

/* Put test JS here! */
/***** autosort_sortable ********
 * Auto-sorts sortable tables by specified columns (why is this not built-in?!)
 *
 *  Maintainers: [[User:Iritscen]]
 */

function sortSortableTables()
{
   // Iterate over all <div> elements
   var divs = document.getElementsByTagName("div");
   if (!divs) return;
   alert("Checking for sortable table.");
   for (var i = 0; i < divs.length; i++)
   {
      var theDiv = divs[i];
      var tables = theDiv.getElementsByTagName("table");
      if (!tables) continue;
      alert("Found a table.");
      for (var j = 0; j < tables.length; j++)
      {
         var theTable = tables[j];
         // If we found a sortable table...
         if (hasClass(theTable, "sortable"))
         {
            alert("Found a sortable one.");
            // ...Look for sort button
            var allTHs = theTable.getElementsByTagName("th");
            if (!allTHs) continue;
            for (var k = 0; k < allTHs.length; k++)
            {
               if (hasClass(allTHs[k], "headersort"))
               {
                  alert("Found the arrow.");
                  allTHs[k].trigger(click); // use jQuery's trigger() to send click event to this arrow
               }
            }
         }
      }
   }
}

addOnloadHook(sortSortableTables);