User:EdoDodo/discussioninvite.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.
addOnloadHook(discussioninvite);
 
function discussioninvite() {
	if( wgNamespaceNumber == 3 ) {
		var username = wgTitle.split( '/' )[0].replace( /\"/, "\\\""); // only first part before any slashes
 
		mw.util.addPortletLink( 'p-discussion', "javascript:discussioninvite.callback(\"" + username + "\")", "Inv", "discussion-invite", "Easy invitation", "");
	}
}

/**
discussioninvite.callback = function discussioninviteCallback( uid ) {
	if( uid == wgUserName ){
		alert( 'Is it really so bad that you\'re talking back to yourself?' );
		return;
	}
 
	var Window = new SimpleWindow( 600, 350 );
	Window.setTitle( "Discussion Invite" ); 
	var form = new QuickForm( discussioninvite.callback.evaluate );
 
	invitetarget = other;
 
	form.append( {
			type: 'field',
			label: 'Work area',
			name: 'work_area'
		} );
 
	var result = form.render();
	Window.setContent( result );
	Window.display();
 
	// We must init the
	var evt = document.createEvent( "Event" );
	evt.initEvent( 'change', true, true );
	result.invitetarget[0].dispatchEvent( evt );
}
 
discussioninvite.prev_page = '';
discussioninvite.prev_section = '';
discussioninvite.prev_message = '';
 
discussioninvite.callback.change_target = function friendlytagCallbackChangeTarget(e) {
	var value = e.target.value;
	var root = e.target.form;
	var old_area;
 
	if(root.section) {
		discussioninvite.prev_section = root.section.value;
	}
	if(root.message) {
		discussioninvite.prev_message = root.message.value;
	}
	if(root.page) {
		discussioninvite.prev_page = root.page.value;
	}
 
	for( var i = 0; i < root.childNodes.length; ++i ) {
		var node = root.childNodes[i];
		if( 
			node instanceof Element &&
			node.getAttribute( 'name' ) == 'work_area' 
		) {
			old_area = node;
			break;
		}
	}
	var work_area = new QuickForm.element( { 
			type: 'field',
			label: 'Invite information',
			name: 'work_area'
		} );

		work_area.append( { 
				type:'input',
				name:'page',
				label:'Full page name',
				tooltip:'The full page name where you left the message.  For example: "Wikipedia talk:Friendly".',
				value: discussioninvite.prev_page
			} );

		work_area.append( { 
				type:'input',
				name:'section',
				label:'Linked section (optional)',
				tooltip:'The section heading on the page where you left a message. Leave empty for no section to be linked.',
				value: discussioninvite.prev_section
			} );
 
	work_area.append( { type:'textarea', label:'Additional message (optional):', name:'message', tooltip:'An additional message that you would like to leave below the ' template.  Your signature will be added to the end of the message if you leave one.' } );
 
	work_area.append( { type:'submit' } );
	work_area = work_area.render();
	old_area.parentNode.replaceChild( work_area, old_area );
	root.message.value = discussioninvite.prev_message;
}
 
discussioninvite.callbacks = {
	main: function( self ) {
		self.statelem.status( 'Grabbing edit token...' );
		var xmlDoc = self.responseXML;
		var editToken = xmlDoc.evaluate( '//page/@edittoken', xmlDoc, null, XPathResult.STRING_TYPE, null ).stringValue;
 
		var invitePageName = (self.params.invitetarget == 'mytalk')?wgUserName:self.params.page;
 
		//clean talkback heading: strip section header markers, were erroneously suggested in the documentation
		var text = '\n==' + DiscussionInviteConfig.inviteHeading.replace(/^\s*=+\s*(.*?)\s*=+$\s*/, "$1") + '==\n{\{subst:User:EdoDodo/Invite|';
		text += invitePageName;
 
		if( self.params.section != '' ) {
			text += '|' + self.params.section;
		}
 
		text += '|ts=\~\~\~\~\~\}\}';
 
		if( self.params.message != '' ) {
			text += self.params.message;
		}
		
		text += '\~\~\~\~';
 
		var query = {
			'action': 'edit',
			'title': wgPageName,
			'appendtext': text,
			'token': editToken,
			'summary': 'Discussion invitation ([\['+ (self.params.invitetarget == 'other'?'':'User:') + invitePageName + (self.params.section?('#'+self.params.section):'') +']])',
			'minor': DiscussionInviteConfig.markInviteAsMinor ? 1 : undefined
		};
 
		var wikipedia_api = new Wikipedia.api( 'Edit token grabbed; adding discussion invitation to user talk page...', query, null, self.statelem );
		wikipedia_api.post();
	}
}
 
discussioninvite.callback.evaluate = function discussioninviteCallbackEvaluate(e) {
	var invitetarget = e.target.getChecked( 'invitetarget' )[0];
	var page = null;
	var section = e.target.section.value;
	if( invitetarget == 'usertalk' || invitetarget == 'other' ) {
		page = e.target.page.value;
 
		if( invitetarget == 'usertalk' ) {
			if( page == '' ) {
				alert( 'You must specify the username of the user whose talk page you left a message on' );
				return;
			}
		} else {
			if( page == '' ) {
				alert( 'You must specify the full page name when your message is not on a user talk page' );
				return;
			}			
		}
	}
 
	var params = {
		invitetarget: invitetarget,
		page: page,
		section: section,
		message: e.target.message.value
	};
 
	Status.init( e.target );
 
	Wikipedia.actionCompleted.redirect = wgPageName;
	Wikipedia.actionCompleted.notice = "Discussion invitation complete; reloading talk page in a few seconds";
 
	var query = {
		'action': 'query',
		'prop': 'info',
		'intoken': 'edit',
		'titles': wgPageName
	};
 
	var wikipedia_api = new Wikipedia.api( 'Status', query, discussioninvite.callbacks.main );
	wikipedia_api.params = params;
	wikipedia_api.post();
}
**/