Jump to content

User:John Vandenberg/welcome.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.
// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
    TwinkleConfig = {};
}

/**
TwinkleConfig.showSharedIPNotice may take arguments:
  true: to show shared ip notice if an IP address
  false: to not print the notice
*/
if( typeof( TwinkleConfig.showSharedIPNotice ) == 'undefined' ) {
    TwinkleConfig.showSharedIPNotice = true;
}

/**
TwinkleConfig.markWarningsAsMinor (boolean)
Defines if a warning should be marked as minor, if false, default is applied as per preference.
*/
if( typeof( TwinkleConfig.markWarningsAsMinor ) == 'undefined' ) {
    TwinkleConfig.markWarningsAsMinor = true;
}

/**
TwinkleConfig.summaryAd (string)
If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
*/
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
    TwinkleConfig.summaryAd = " using [[WP:TWINKLE|TW]]";
}


/* sign = add signature after the tag
      default: true
               false when tag matches /(^w-|welcome)/
*/
function welcometagadd( tag , summary, sign ) {

	var article;

	if( QueryString.exists( 'vanarticle' ) ) {
		article = decodeURI(QueryString.get( 'vanarticle' )).replace( /^(Image|Category):/i, ':$1:' );
	} else {
		article = prompt('Which article?').replace( /^(Image|Category):/i, ':$1:' );
	}

        var welcomeTagRe = new RegExp( "(^w-|welcome)" );
        var isWelcomeTag = welcomeTagRe.exec(tag);

        if (typeof(sign) == 'undefined')
            sign = isWelcomeTag ? false : true;

	var date = new Date();
	var notice = "";
	var header = "";

	var headerRe = new RegExp( "^===\\s*" + date.getUTCMonthName() + "\\s+" + date.getUTCFullYear() + "\\s*===", 'm' );
	var textbox = document.getElementById( 'wpTextbox1' );

	if ( textbox.value.length > 0 ) {
		textbox.value += '\n\n';
	}
	if( !isWelcomeTag && !headerRe.exec( textbox.value ) ) {
		header += "=== " + date.getUTCMonthName() + " " + date.getUTCFullYear() + " ===\n";
	}

	notice += header;
// do not emit article name into first parameter of welcome templates
// as it is usually expecting a different value.
	notice += "{{subst:" + tag + ( article && !isWelcomeTag ? '|' + article + '|subst=subst:' : '' ) + "}}"; // subst=subst: hack only on article given as per MW bug :(

        // dont add signature for welcome messages.
        if ( sign ) notice += " ~~" + "~~";

	if ( TwinkleConfig.showSharedIPNotice && isIPAddress( wgTitle ) ) {
		switch( QueryString.get( 'type' ) ) {
			case 'vand':
			notice +=  "\n:''If this is a shared [[IP address]], and you didn't make any [[Wikipedia:vandalism|unconstructive]] edits, consider [[Wikipedia:Why create an account?|creating an account]] for yourself so you can avoid further irrelevant warnings.'' ";
			break;
			default:
			notice +=  "\n:''If this is a shared [[IP address]], and you didn't make the edit, consider [[Wikipedia:Why create an account?|creating an account]] for yourself so you can avoid further irrelevant notices.'' ";
			break;
		}
	}

	// BEGIN WikiEd -> wpTextbox1
	if (typeof(wikEdUseWikEd) != 'undefined') {
		if (wikEdUseWikEd == true) {
			WikEdUpdateTextarea();
		}
	}
	// END


	textbox.value += notice;
	// BEGIN wpTextbox1 -> WikiEd
	if (typeof(wikEdUseWikEd) != 'undefined') {
		if (wikEdUseWikEd == true) {
			WikEdUpdateFrame();
		}
	}
	// END	

	document.getElementById( 'wpSummary' ).value = summary + ( article ? ' on [[' + article + ']]'  : '' ) + '.' + TwinkleConfig.summaryAd;
	if( TwinkleConfig.markWarningsAsMinor ) {
		document.getElementById( 'wpMinoredit' ).checked = true;
	}
}



//Quick welcome tabs
function add_welcome_tabs() 
{ 
    if ( wgNamespaceNumber == 3 && ( QueryString.equals( 'action', 'edit' ) || QueryString.equals( 'action', 'submit' ) ) ) {
        var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
        var editsummary = 'Welcome.  Thanks for the contribution';

        if ( isIPAddress( wgTitle ) ) {
            addPortletLink('p-cactions', 'javascript:welcometagadd("welcomeip", "'+editsummary+'", true)','Welcome IP', '');
        }
        else {
            addlimenu(tabs, '[0]', 'welcomenotefunc');
            addPortletLink('welcomenotefunc', 'javascript:welcometagadd("w-basic", "'+editsummary+'")', 'Basic', '');
            addPortletLink('welcomenotefunc', 'javascript:welcometagadd("w-link", "'+editsummary+'")', 'Link', '');
            addPortletLink('welcomenotefunc', 'javascript:welcometagadd("w-short", "'+editsummary+'")', 'Short', '');
            addPortletLink('welcomenotefunc', 'javascript:welcometagadd("welcome", "'+editsummary+'")', 'Welcome', '');
            addPortletLink('welcomenotefunc', 'javascript:welcometagadd("welcomeg", "'+editsummary+'")', 'Welcome G', '');
        }
    }
}

$(addWelcomeButtons);
$(add_welcome_tabs);

function twCreateSpanTag ( color, content ) {
    var span = document.createElement( 'span' );
    span.style.color = color;
    span.appendChild( document.createTextNode( content ) );
    return span;
}


function addWelcomeButtons() {

    var otitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-otitle' )[0];
    var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];

    if( !ntitle ) {
        // Nothing to see here, move along...
        return;
    }

    if( !otitle.getElementsByTagName('a')[0] ) {
        // no previous revision available; new page
        return;
    }

    var userAnchorOffset = 3;
    var twNewDiffAnchors = ntitle.getElementsByTagName('a');

    if ( /revertPage/.test(twNewDiffAnchors[0].href) ) {
        userAnchorOffset += 3;
    }

    var twDiffUserAnchor = twNewDiffAnchors[userAnchorOffset];
    var twDiffUserTalkAnchor = twNewDiffAnchors[userAnchorOffset + 1];
    var twDiffUserContribsAnchor = twDiffUserTalkAnchor.nextSibling.nextSibling;

    twDiffUser = twDiffUserAnchor.firstChild.nodeValue.replace("'", "\\'");

    if (twDiffUserTalkAnchor.className != 'new') {
        // not a new user
        return;
    }

    var welcomeNode = document.createElement('strong');
    var welcomeLink = document.createElement('a');

    welcomeLink.href = "javascript:welcomeUser('" + twDiffUser + "')";
    welcomeLink.appendChild( twCreateSpanTag( 'Black', '[' ) );
    welcomeLink.appendChild( twCreateSpanTag( 'Green', 'Welcome' ) );
    welcomeLink.appendChild( twCreateSpanTag( 'Black', ']' ) );

    welcomeNode.appendChild(welcomeLink);

    if ( isIPAddress( twDiffUser ) ) {
        ntitle.insertBefore(welcomeNode, twDiffUserTalkAnchor.nextSibling.nextSibling);
    }
    else {
        ntitle.insertBefore(welcomeNode, twDiffUserContribsAnchor.nextSibling.nextSibling);
    }
}

function welcomeUser( pUser ) {
    // TW scripts expect a query argument of "vanarticle"
    // so we set that with the article name of the good rev.
    var query = {
        'title': 'User talk:' + pUser,
        'action': 'edit',
        'vanarticle': wgPageName.replace(/_/g, ' ')
    };

    Status.debug( 'query:' + query.toSource() );

    switch( TwinkleConfig.userTalkPageMode ) {
        case 'tab':
            window.open( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), '_tab' );
            break;
        case 'blank':
            window.open( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), '_blank', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
            break;
        case 'window':
            default :
            window.open( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), 'twinklewarnwindow', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
            break;
    }
}