User:Splarka/AddPortletLink fallback

From MediaWiki.org
Jump to: navigation, search

[edit] Proposal

Proposed "fallback" solution to addPortletLink. Alternately, a superfunction like addMenuLink() could call addPortletLink('p-tb',....) and perform generic menu insertion on failure. Note: we should avoid referring to skins by name!

//edit me.
function addPortletLink(portlet, href, text, id, tooltip, accesskey, nextnode) {
        var node = document.getElementById(portlet) || document.getElementById('p-tb');
        if ( !node ) return addMenuLink(href, text, id, tooltip, accesskey);
        node = node.getElementsByTagName( "ul" )[0];
        if ( !node ) return null;
 
        var link = document.createElement( "a" );
        link.appendChild( document.createTextNode( text ) );
        link.href = href;
 
        var item = document.createElement( "li" );
        item.appendChild( link );
        if ( id ) item.id = id;
 
        if ( accesskey ) {
                link.setAttribute( "accesskey", accesskey );
                tooltip += " ["+accesskey+"]";
        }
        if ( tooltip ) {
                link.setAttribute( "title", tooltip );
        }
        if ( accesskey && tooltip ) {
                updateTooltipAccessKeys( new Array( link ) );
        }
 
        if ( nextnode && nextnode.parentNode == node )
                node.insertBefore( item, nextnode );
        else
                node.appendChild( item );  // IE compatibility (?)
 
        return item;
}
 
function addMenuLink(href, text, id, tooltip, accesskey) {
        var node = document.getElementById('p-tb') || document.getElementById('quickbar') || document.getElementById('topbar');
        if ( !node ) return null;
 
        var link = document.createElement( "a" );
        link.appendChild( document.createTextNode( text ) );
        link.href = href;
        if ( id ) link.id = id;
        if ( accesskey ) {
                link.setAttribute( "accesskey", accesskey );
                tooltip += " ["+accesskey+"]";
        }
        if ( tooltip ) {
                link.setAttribute( "title", tooltip );
        }
        if ( accesskey && tooltip ) {
                updateTooltipAccessKeys( new Array( link ) );
        }
        node.appendChild( link );
 
        return link;
}

[edit] Rantings here

Looks ugly in Nostalgia skin, looks quite bad in cologneblue and standard if no quickbar is present. Splarka 00:36, 8 July 2009 (UTC)

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox