Difference between revisions of "MediaWiki:Common.js"

From Wikimedia District of Columbia
Jump to navigation Jump to search
(Testing)
(Add collapsing tables)
Line 20: Line 20:
 
addOnloadHook(customizeSidebarForAnon);
 
addOnloadHook(customizeSidebarForAnon);
 
}
 
}
  +
  +
/* collapsing tables */
  +
  +
var autoCollapse = 2;
  +
var collapseCaption = "hide";
  +
var expandCaption = "show";
  +
  +
window.collapseTable = function( tableIndex ){
  +
var Button = document.getElementById( "collapseButton" + tableIndex );
  +
var Table = document.getElementById( "collapsibleTable" + tableIndex );
  +
  +
if ( !Table || !Button ) {
  +
return false;
  +
}
  +
  +
var Rows = Table.rows;
  +
  +
if ( Button.firstChild.data == collapseCaption ) {
  +
for ( var i = 1; i < Rows.length; i++ ) {
  +
Rows[i].style.display = "none";
  +
}
  +
Button.firstChild.data = expandCaption;
  +
} else {
  +
for ( var i = 1; i < Rows.length; i++ ) {
  +
Rows[i].style.display = Rows[0].style.display;
  +
}
  +
Button.firstChild.data = collapseCaption;
  +
}
 
}
  +
  +
function createCollapseButtons(){
  +
var tableIndex = 0;
  +
var NavigationBoxes = new Object();
  +
var Tables = document.getElementsByTagName( "table" );
  +
  +
for ( var i = 0; i < Tables.length; i++ ) {
  +
if ( hasClass( Tables[i], "collapsible" ) ) {
  +
  +
/* only add button and increment count if there is a header row to work with */
  +
var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
  +
if (!HeaderRow) continue;
  +
var Header = HeaderRow.getElementsByTagName( "th" )[0];
  +
if (!Header) continue;
  +
  +
NavigationBoxes[ tableIndex ] = Tables[i];
  +
Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
  +
  +
var Button = document.createElement( "span" );
  +
var ButtonLink = document.createElement( "a" );
  +
var ButtonText = document.createTextNode( collapseCaption );
  +
  +
Button.className = "collapseButton"; //Styles are declared in Common.css
  +
  +
ButtonLink.style.color = Header.style.color;
  +
ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
  +
ButtonLink.setAttribute( "href", "#" );
  +
addHandler( ButtonLink, "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
  +
ButtonLink.appendChild( ButtonText );
  +
  +
Button.appendChild( document.createTextNode( "[" ) );
  +
Button.appendChild( ButtonLink );
  +
Button.appendChild( document.createTextNode( "]" ) );
  +
  +
Header.insertBefore( Button, Header.firstChild );
  +
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;
  +
}
  +
}
  +
}
  +
}
  +
}
  +
  +
$( createCollapseButtons );
   
 
/* add membership payment button */
 
/* add membership payment button */
 
if ( ( mw.config.get('wgPageName') == 'Membership' ) && ( wgNamespaceNumber == 0 ) ) {
 
if ( ( mw.config.get('wgPageName') == 'Membership' ) && ( wgNamespaceNumber == 0 ) ) {
 
$('#membershipbutton').html('<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="QTRS8N8RD8WNL"><input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>');
 
$('#membershipbutton').html('<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="QTRS8N8RD8WNL"><input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>');
}
 
 
/* add calendar */
 
if ( ( mw.config.get('wgPageName') == 'Calendar' ) && ( wgNamespaceNumber == 0 ) ) {
 
$('#calendarwidget').html('<iframe src="http://www.google.com/calendar/embed?src=wikidc.org_svr4b3s6n6rs8mt189kbc4uegg%40group.calendar.google.com&ctz=America/New_York" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>');
 
 
}
 
}
   

Revision as of 15:48, 28 January 2012

/* sidebar modifications for anonymous users */

function removeSidebarSection(section) {
  try {
    var target = "p-" + section;
    var sidebar = document.getElementById("mw-panel");
    var element = document.getElementById(target);
    sidebar.removeChild(element);
  } catch (e) {
    return;
   }
}

function customizeSidebarForAnon() {
  removeSidebarSection("Internal");
  removeSidebarSection("tb");
}

if (wgUserName == null) {
  addOnloadHook(customizeSidebarForAnon);
}

/* collapsing tables */

var autoCollapse = 2;
var collapseCaption = "hide";
var expandCaption = "show";
 
window.collapseTable = function( tableIndex ){
    var Button = document.getElementById( "collapseButton" + tableIndex );
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
    if ( !Table || !Button ) {
        return false;
    }
 
    var Rows = Table.rows;
 
    if ( Button.firstChild.data == collapseCaption ) {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = "none";
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        Button.firstChild.data = collapseCaption;
    }
}
 
function createCollapseButtons(){
    var tableIndex = 0;
    var NavigationBoxes = new Object();
    var Tables = document.getElementsByTagName( "table" );
 
    for ( var i = 0; i < Tables.length; i++ ) {
        if ( hasClass( Tables[i], "collapsible" ) ) {
 
            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
            if (!HeaderRow) continue;
            var Header = HeaderRow.getElementsByTagName( "th" )[0];
            if (!Header) continue;
 
            NavigationBoxes[ tableIndex ] = Tables[i];
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
            var Button     = document.createElement( "span" );
            var ButtonLink = document.createElement( "a" );
            var ButtonText = document.createTextNode( collapseCaption );
 
            Button.className = "collapseButton";  //Styles are declared in Common.css
 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
            ButtonLink.setAttribute( "href", "#" );
            addHandler( ButtonLink,  "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( "[" ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( "]" ) );
 
            Header.insertBefore( Button, Header.firstChild );
            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;
                }
            }
        }
    }
}
 
$( createCollapseButtons );

/* add membership payment button */
if ( ( mw.config.get('wgPageName') == 'Membership' ) && ( wgNamespaceNumber == 0 ) ) {
  $('#membershipbutton').html('<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="QTRS8N8RD8WNL"><input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>');
}

/* add donate button */
if ( ( mw.config.get('wgPageName') == 'Donate' ) && ( wgNamespaceNumber == 0 ) ) {
  $('#donatebutton').html('<form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="hosted_button_id" value="PSCZL9L3NKNRQ"><input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"></form>');
}