Difference between revisions of "MediaWiki:Common.js"

From Basin Wiki
Jump to navigation Jump to search
(Added countdown timer code, at request of Antitrust)
 
(47 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* Any JavaScript here will be loaded for all users on every page load. */
/** Collapsible tables *********************************************************
+
/* This code displays a clock or a countdown timer. Adapted from w3schools.com example. */
*
+
(function($) {
*  Description: Allows tables to be collapsed, showing only the header. See
+
 
*   http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
+
    function cdTimer() {
*  Maintainers: [[en:User:R. Koot]]
+
        var timerElem = $('#CountdownTimer');
*/
+
 
+
        function checkTime(i) {
var autoCollapse = 2;
+
            if (i < 10) {
var collapseCaption = 'hide';
+
                i = "0" + i;
var expandCaption = 'show';
+
            }
+
            return i;
function collapseTable( tableIndex ) {
+
        }
var Button = document.getElementById( 'collapseButton' + tableIndex );
+
 
var Table = document.getElementById( 'collapsibleTable' + tableIndex );
+
        if (timerElem.length > 0) {
+
            var cdoffset = Number(document.getElementById('cdoffset').innerHTML);
if ( !Table || !Button ) {
+
            var today = new Date();
return false;
+
            var h = today.getUTCHours();
}
+
            var m = today.getUTCMinutes();
+
            var s = today.getUTCSeconds();
var Rows = Table.rows;
+
 
+
            var s1 = (s !== 0) ? 60 - s : s;
if ( Button.firstChild.data == collapseCaption ) {
+
            var m1 = (m !== 0) ? 60 - m : m;
for ( var i = 1; i < Rows.length; i++ ) {
+
            var h1 = (24 - (h - cdoffset)) % 24;
Rows[i].style.display = 'none';
+
 
}
+
            // add a zero in front of numbers<10
Button.firstChild.data = expandCaption;
+
            h1 = checkTime(h1);
} else {
+
            m1 = checkTime(m1);
for ( var i = 1; i < Rows.length; i++ ) {
+
            s1 = checkTime(s1);
Rows[i].style.display = Rows[0].style.display;
+
            timerElem.text(h1 + ":" + m1 + ":" + s1);
}
+
            t = setTimeout(function() {
Button.firstChild.data = collapseCaption;
+
                cdTimer();
}
+
            }, 1000);
}
+
 
+
        }
function createCollapseButtons() {
+
    }
var tableIndex = 0;
+
 
var NavigationBoxes = new Object();
+
    function sTime() {
var Tables = document.getElementsByTagName( 'table' );
+
        var timeElem = $('#ServerTime');
+
 
for ( var i = 0; i < Tables.length; i++ ) {
+
        function checkTime(i) {
if ( hasClass( Tables[i], 'collapsible' ) ) {
+
            if (i < 10) {
+
                i = "0" + i;
/* only add button and increment count if there is a header row to work with */
+
            }
var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
+
            return i;
if ( !HeaderRow ) {
+
        }
continue;
+
 
}
+
        if (timeElem.length > 0) {
var Header = HeaderRow.getElementsByTagName( 'th' )[0];
+
            var soffset = Number(document.getElementById('soffset').innerHTML);
if ( !Header ) {
+
            var today = new Date();
continue;
+
            var h = today.getUTCHours();
}
+
            var m = today.getUTCMinutes();
+
            var s = today.getUTCSeconds();
NavigationBoxes[tableIndex] = Tables[i];
+
 
Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
+
            var h2 = (h + soffset) % 24;
 
var Button = document.createElement( 'span' );
 
var ButtonLink = document.createElement( 'a' );
 
var ButtonText = document.createTextNode( collapseCaption );
 
 
Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
 
 
ButtonLink.style.color = Header.style.color;
 
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
 
ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
 
ButtonLink.appendChild( ButtonText );
 
 
Button.appendChild( document.createTextNode( '[' ) );
 
Button.appendChild( ButtonLink );
 
Button.appendChild( document.createTextNode( ']' ) );
 
 
Header.insertBefore( Button, Header.childNodes[0] );
 
tableIndex++;
 
}
 
}
 
 
for ( var i = 0;  i < tableIndex; i++ ) {
 
if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
 
collapseTable( i );
 
} else if ( hasClass( NavigationBoxes[i], 'innercollapse' ) ) {
 
var element = NavigationBoxes[i];
 
while ( element = element.parentNode ) {
 
if ( hasClass( element, 'outercollapse' ) ) {
 
collapseTable( i );
 
break;
 
}
 
}
 
}
 
}
 
}
 
 
addOnloadHook( createCollapseButtons );
 
 
/** Test if an element has a certain class **************************************
 
*
 
* Description: Uses regular expressions and caching for better performance.
 
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
 
*/
 
 
var hasClass = ( function() {
 
var reCache = {};
 
return function( element, className ) {
 
return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
 
};
 
})();
 
  
/* This code displays a clock or a countdown timer. Adapted from w3schools.com example. */
+
            // add a zero in front of numbers<10
function cdTimer() {
+
            h2 = checkTime(h2);
        var timerElem = $('#CountdownTimer');
+
            m2 = checkTime(m);
        var cdoffset = document.getElementById('cdoffset').innerHTML;
+
            s2 = checkTime(s);
 +
            timeElem.text(h2 + ":" + m2 + ":" + s2);
 +
            t = setTimeout(function() {
 +
                sTime();
 +
            }, 1000);
 +
        }
 +
    }
  
        function checkTime(i) {
+
    $(document).ready(cdTimer);
                if (i < 10) {
+
    $(document).ready(sTime);
                        i = "0" + i;
 
                }
 
                return i;
 
        }
 
  
        if (timerElem.length > 0) {
+
})(jQuery);
                var today = new Date();
 
                var h = today.getUTCHours();
 
                var m = today.getUTCMinutes();
 
                var s = today.getUTCSeconds();
 
  
                var s1 = (s !==0) ? 60 - s : s;
 
                var m1 = (m !==0) ? 60 - m : m;
 
                var h1 = (24 - (h - cdoffset)) % 24;
 
  
                // add a zero in front of numbers<10
+
function dbfood()
                m1 = checkTime(m1);
+
{
                s1 = checkTime(s1);
+
    if(document.getElementById('intfood'))
                timerElem.text(h1 + ":" + m1 + ":" + s1);
+
    {
                t = setTimeout('cdTimer()', 1000);
+
        var drop1string = document.getElementById("dropdown1").innerHTML;
        }
+
        var drop1array = drop1string.split("@-@");
}
+
        var drop1list = " ";
 +
        var drop2list = " ";
  
addonloadHook(cdTimer);
+
        var drop3string = document.getElementById("dropdown2").innerHTML;
 +
        var drop3array = drop3string.split("@-@");
 +
        var drop3list = " ";
 +
        var rngarray = document.getElementById("rngbase").innerHTML.split("@@");
 +
       
 +
        for (var m=0;m<drop1array.length;m++)
 +
        {
 +
            var x = drop1array[m].split("@@");
 +
            drop1list = drop1list + '<option value="' + x[0] + '">' + x[1] + '</option>';
 +
            drop2list = drop1list;
 +
        }
  
function sTime() {
+
        for (var n=0;n<drop3array.length;n++)
        var timeElem = $('#ServerTime');
+
        {
        var soffset = document.getElementById('soffset').innerHTML;
+
            var y = drop3array[n].split("@@");
 +
            drop3list = drop3list + '<option value="' + y[0] + '">' + y[1] + '</option>';
 +
        }
  
        function checkTime(i) {
+
        document.getElementById('intfood').innerHTML = '<input id="gw2textinput" class="gw2textinput" placeholder="Search..."><br /><select id="drop1" class="gw2drop1">' + drop1list + '</select><br /><select id="drop2" class="gw2drop2">' + drop2list + '</select><br /><select id="drop3" class="gw2drop3">' + drop3list + '</select><br /><input class="gw2level1" value="1"><input class="gw2level2" value="80"><input id="food" type="checkbox" checked="checked" class="gw2check1" /><input id="notfood" type="checkbox" class="gw2check2" /><div class="gw2submit"></div>';
                if (i < 10) {
+
        document.getElementById('rngtext').innerHTML = rngarray[Math.floor((Math.random()*rngarray.length))];
                        i = "0" + i;
+
       
                }
+
        var ua = navigator.userAgent,
                return i;
+
        event = (ua.match(/iPad/i)) ? "touchstart" : "click";
        }
+
       
 +
$(".gw2submit").live(event, function() { 
 +
    var database = document.getElementById("database").innerHTML;
 +
    var databasearray = database.split("@@");
 +
    var level = [Number($(".gw2level1").val()),Number($(".gw2level2").val())];
 +
    var drop1 = document.getElementById("drop1").options[document.getElementById("drop1").selectedIndex].value;
 +
    var drop2 = document.getElementById("drop2").options[document.getElementById("drop2").selectedIndex].value;
 +
    var drop3 = document.getElementById("drop3").options[document.getElementById("drop3").selectedIndex].value;
 +
    var food;
 +
    var notfood;
 +
    var result = " ";
 +
    var newfield = " ";
 +
    var textinput = $(".gw2textinput").val().substring(0, 50).trim();
 +
    var patt1 = /ascending/g;
 +
    var patt2 = /wiki/g;
 +
    var wlinks = [];
 +
    var newliner;
 +
    var resultnumber = 0;
 +
    var databasearraysorting = [];
 +
   
 +
    for (var i=0;i<=1;i++)
 +
        {
 +
          if (level[i]<1)
 +
          {
 +
          level[i] = 1;
 +
          }
 +
          if (level[i]>80)
 +
          {
 +
          level[i] = 80;
 +
          }
 +
          if (level[0]>level[1])
 +
          {
 +
          level = level.reverse();
 +
          }
 +
        }
 +
   
 +
    if (document.getElementById("food").checked === true)
 +
          {
 +
          food = 1;
 +
          }
 +
          else
 +
          {
 +
          food = 0;
 +
          }
 +
          if (document.getElementById("notfood").checked === true)
 +
          {
 +
          notfood = 2;
 +
          }
 +
          else
 +
          {
 +
          notfood = 0;
 +
          }
 +
    var foodtype = food + notfood;
  
        if (timeElem.length > 0) {
+
    for (var h=0;h<databasearray.length;h++)
                var today = new Date();
+
    {
                var h = today.getUTCHours();
+
        databasearraysorting[h] = [databasearray[h],databasearray[h].split("@-@")[5]];
                var m = today.getUTCMinutes();
+
    }
                var s = today.getUTCSeconds();
 
  
                var h2 = (h + soffset) % 24;
+
    databasearraysorting.sort(function(a, b)
 +
        {
 +
        return b[1] - a[1];
 +
        });
  
                // add a zero in front of numbers<10
+
    for (var g=0;g<databasearray.length;g++)
                h2 = checkTime(h2);
+
    {
                m2 = checkTime(m);
+
        databasearray[g] = databasearraysorting[g][0];
                s2 = checkTime(s);
+
    }
                timeElem.text(h2 + ":" + m2 + ":" + s2);
+
   
                t = setTimeout('sTime()', 1000);
+
    if (patt1.test(textinput) === true)
        }
+
        {
 +
        databasearray = databasearray.reverse();
 +
        textinput = textinput.replace(patt1,'').trim();
 +
        }
 +
   
 +
    if (patt2.test(textinput) === true)
 +
        {
 +
        textinput = textinput.replace(patt2,'').trim();
 +
        newfield = document.getElementById("wtable").innerHTML;
 +
        }
 +
   
 +
    for (var k=0;k<databasearray.length;k++)
 +
    {
 +
        var temparray = databasearray[k].split("@-@");
 +
        if (Number(temparray[5]) >= level[0] && Number(temparray[5]) <= level[1])
 +
        {
 +
            if (foodtype == 3 || Number(temparray[6]) == foodtype)
 +
            {
 +
            if (databasearray[k].search(drop1) >=0 && databasearray[k].search(drop2) >=0 && databasearray[k].search(drop3) >=0 && databasearray[k].toLowerCase().search(textinput.toLowerCase()) >=0)
 +
            {
 +
                if (newfield === ' ')
 +
                {
 +
                result= result + '<table class="gw2tth"><tr><td colspan="2" class="gw2tt"><a style="color: #5498ff; text-decoration: none;" href="http://wiki.guildwars2.com/wiki/' + temparray[0] + '">' + temparray[0] + '</a></td></tr><tr><td valign="top" class="gw2tc1">Nourishment (' + temparray[1] + ')<br /><br />Required Level: ' + temparray[5] + '</td><td valign="top">' + temparray[2] + ((temparray[3] === " ")?temparray[3]:'<br />' + temparray[3]) + '<br />' + temparray[4] + '</td></tr></table>';
 +
                resultnumber = resultnumber + 1;
 +
                }
 +
                else
 +
                {
 +
                result = result + '<br />|-<br />|' + temparray[5] + '||' + temparray[1] + '||{{item icon|' + temparray[0] + '|large=y}} || ' + temparray[2] + ((temparray[3] === " ")?temparray[3]:'&lt;br /&gt;' + temparray[3]) + ((temparray[4].search('10') >=0)?'':'&lt;br /&gt;' + temparray[4]);
 +
                resultnumber = resultnumber + 1;
 +
                }
 +
            }
 +
            }
 +
        }
 +
    }
 +
   
 +
    if (newfield != ' ')
 +
    {
 +
    result = result.replace(/\./g,'');
 +
    wlinks = document.getElementById("wlinks").innerHTML.split("@-@");
 +
        for (var m=0;m<wlinks.length;m++)
 +
        {
 +
        result = result.split(wlinks[m].split("@@")[0]).join(wlinks[m].split("@@")[1]);
 +
        }
 +
    }
 +
    document.getElementById('result').innerHTML = ((newfield === ' ')?result:newfield + result + '<br />|}');
 +
    document.getElementById('resultnumber').innerHTML = resultnumber + ' out of ' + databasearray.length;
 +
});
 +
       
 +
    }
 
}
 
}
 
+
window.document.body.onload = dbfood;
addonloadHook(sTime);
 

Latest revision as of 21:23, 2 July 2013

/* Any JavaScript here will be loaded for all users on every page load. */
/* This code displays a clock or a countdown timer. Adapted from w3schools.com example. */
(function($) {

    function cdTimer() {
        var timerElem = $('#CountdownTimer');

        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }

        if (timerElem.length > 0) {
            var cdoffset = Number(document.getElementById('cdoffset').innerHTML);
            var today = new Date();
            var h = today.getUTCHours();
            var m = today.getUTCMinutes();
            var s = today.getUTCSeconds();

            var s1 = (s !== 0) ? 60 - s : s;
            var m1 = (m !== 0) ? 60 - m : m;
            var h1 = (24 - (h - cdoffset)) % 24;

            // add a zero in front of numbers<10
            h1 = checkTime(h1);
            m1 = checkTime(m1);
            s1 = checkTime(s1);
            timerElem.text(h1 + ":" + m1 + ":" + s1);
            t = setTimeout(function() {
                cdTimer();
            }, 1000);

        }
    }

    function sTime() {
        var timeElem = $('#ServerTime');

        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }

        if (timeElem.length > 0) {
            var soffset = Number(document.getElementById('soffset').innerHTML);
            var today = new Date();
            var h = today.getUTCHours();
            var m = today.getUTCMinutes();
            var s = today.getUTCSeconds();

            var h2 = (h + soffset) % 24;

            // add a zero in front of numbers<10
            h2 = checkTime(h2);
            m2 = checkTime(m);
            s2 = checkTime(s);
            timeElem.text(h2 + ":" + m2 + ":" + s2);
            t = setTimeout(function() {
                sTime();
            }, 1000);
        }
    }

    $(document).ready(cdTimer);
    $(document).ready(sTime);

})(jQuery);


function dbfood()
{
    if(document.getElementById('intfood'))
    {
        var drop1string = document.getElementById("dropdown1").innerHTML;
        var drop1array = drop1string.split("@-@");
        var drop1list = " ";
        var drop2list = " ";

        var drop3string = document.getElementById("dropdown2").innerHTML;
        var drop3array = drop3string.split("@-@");
        var drop3list = " ";
        var rngarray = document.getElementById("rngbase").innerHTML.split("@@");
        
        for (var m=0;m<drop1array.length;m++)
        {
            var x = drop1array[m].split("@@");
            drop1list = drop1list + '<option value="' + x[0] + '">' + x[1] + '</option>';
            drop2list = drop1list;
        }

        for (var n=0;n<drop3array.length;n++)
        {
            var y = drop3array[n].split("@@");
            drop3list = drop3list + '<option value="' + y[0] + '">' + y[1] + '</option>';
        }

        document.getElementById('intfood').innerHTML = '<input id="gw2textinput" class="gw2textinput" placeholder="Search..."><br /><select id="drop1" class="gw2drop1">' + drop1list + '</select><br /><select id="drop2" class="gw2drop2">' + drop2list + '</select><br /><select id="drop3" class="gw2drop3">' + drop3list + '</select><br /><input class="gw2level1" value="1"><input class="gw2level2" value="80"><input id="food" type="checkbox" checked="checked" class="gw2check1" /><input id="notfood" type="checkbox" class="gw2check2" /><div class="gw2submit"></div>';
        document.getElementById('rngtext').innerHTML = rngarray[Math.floor((Math.random()*rngarray.length))];
        
        var ua = navigator.userAgent,
        event = (ua.match(/iPad/i)) ? "touchstart" : "click";
        
$(".gw2submit").live(event, function() {  
    var database = document.getElementById("database").innerHTML;
    var databasearray = database.split("@@");
    var level = [Number($(".gw2level1").val()),Number($(".gw2level2").val())];
    var drop1 = document.getElementById("drop1").options[document.getElementById("drop1").selectedIndex].value;
    var drop2 = document.getElementById("drop2").options[document.getElementById("drop2").selectedIndex].value;
    var drop3 = document.getElementById("drop3").options[document.getElementById("drop3").selectedIndex].value;
    var food;
    var notfood;
    var result = " ";
    var newfield = " ";
    var textinput = $(".gw2textinput").val().substring(0, 50).trim();
    var patt1 = /ascending/g;
    var patt2 = /wiki/g;
    var wlinks = [];
    var newliner;
    var resultnumber = 0;
    var databasearraysorting = [];
    
    for (var i=0;i<=1;i++)
        {
           if (level[i]<1)
           {
           level[i] = 1;
           }
           if (level[i]>80)
           {
           level[i] = 80;
           }
           if (level[0]>level[1])
           {
           level = level.reverse();
           }
        }
    
    if (document.getElementById("food").checked === true)
           {
           food = 1;
           }
           else
           {
           food = 0;
           }
           if (document.getElementById("notfood").checked === true)
           {
           notfood = 2;
           }
           else
           {
           notfood = 0;
           }
    var foodtype = food + notfood;

    for (var h=0;h<databasearray.length;h++)
    {
        databasearraysorting[h] = [databasearray[h],databasearray[h].split("@-@")[5]];
    }

    databasearraysorting.sort(function(a, b)
        {
        return b[1] - a[1];
        });

    for (var g=0;g<databasearray.length;g++)
    {
        databasearray[g] = databasearraysorting[g][0];
    }
    
    if (patt1.test(textinput) === true)
        {
        databasearray = databasearray.reverse();
        textinput = textinput.replace(patt1,'').trim();
        }
    
    if (patt2.test(textinput) === true)
        {
        textinput = textinput.replace(patt2,'').trim();
        newfield = document.getElementById("wtable").innerHTML;
        }
    
    for (var k=0;k<databasearray.length;k++)
    {
        var temparray = databasearray[k].split("@-@");
        if (Number(temparray[5]) >= level[0] && Number(temparray[5]) <= level[1])
        {
            if (foodtype == 3 || Number(temparray[6]) == foodtype)
            {
            if (databasearray[k].search(drop1) >=0 && databasearray[k].search(drop2) >=0 && databasearray[k].search(drop3) >=0 && databasearray[k].toLowerCase().search(textinput.toLowerCase()) >=0)
            {
                if (newfield === ' ')
                {
                result= result + '<table class="gw2tth"><tr><td colspan="2" class="gw2tt"><a style="color: #5498ff; text-decoration: none;" href="http://wiki.guildwars2.com/wiki/' + temparray[0] + '">' + temparray[0] + '</a></td></tr><tr><td valign="top" class="gw2tc1">Nourishment (' + temparray[1] + ')<br /><br />Required Level: ' + temparray[5] + '</td><td valign="top">' + temparray[2] + ((temparray[3] === " ")?temparray[3]:'<br />' + temparray[3]) + '<br />' + temparray[4] + '</td></tr></table>';
                resultnumber = resultnumber + 1;
                }
                else
                {
                result = result + '<br />|-<br />|' + temparray[5] + '||' + temparray[1] + '||{{item icon|' + temparray[0] + '|large=y}} || ' + temparray[2] + ((temparray[3] === " ")?temparray[3]:'&lt;br /&gt;' + temparray[3]) + ((temparray[4].search('10') >=0)?'':'&lt;br /&gt;' + temparray[4]);
                resultnumber = resultnumber + 1;
                }
            }
            }
        }
    }
    
    if (newfield != ' ')
    {
    result = result.replace(/\./g,'');
    wlinks = document.getElementById("wlinks").innerHTML.split("@-@");
        for (var m=0;m<wlinks.length;m++)
        {
        result = result.split(wlinks[m].split("@@")[0]).join(wlinks[m].split("@@")[1]);
        }
    }
    document.getElementById('result').innerHTML = ((newfield === ' ')?result:newfield + result + '<br />|}');
    document.getElementById('resultnumber').innerHTML = resultnumber + ' out of ' + databasearray.length;
});
        
    }
}
window.document.body.onload = dbfood;