User:Writ Keeper/Scripts/teahouseResponder.js

//Most of this code comes from User:Equazcion (from User:Equazcion/TeahouseRespond.js); nearly all credit should go to him.

importStylesheet('User:Equazcion/TeahouseRespond.css'); if ( mw.config.get("wgPageName") == 'Wikipedia:Teahouse/Questions' ) { // (function($,mw) {	$(function { mw.loader.using( ['jquery.ui', 'mediawiki.api'], function {		function addResponse( section, headline, text ) {			var wikitext = '\n\n' + text;			$('.wp-teahouse-respond-form').hide;			$('.wp-teahouse-respond')				.find('.selflink')				.empty				.addClass('mw-ajax-loader');			var api = new mw.Api;			api.getEditToken.done(function(token) {				api.post(				{					'action' : 'edit',					'section' : section,					'title' : 'Wikipedia:Teahouse/Questions',					'appendtext' : wikitext,					'summary' : '/* ' + headline + ' */' + ' response',					'token' : token				}.done(function {window.location.reload;});				); });		}		function makeIndentList(parent, k)		{			var childArray = parent.nextUntil("h2", "dl").find("dd");			var maxIndents = 0;			childArray.each(function(index) {				var curIndents = this.parents("dl").length; if(curIndents > maxIndents) {					maxIndents = curIndents; }			});			var string = " Start a new thread (no indent) Reply to asker (1 indent) ";			var index = 2;			for(index; index < maxIndents; index++)			{				string += "Reply to responder #"+(index-1)+"("+index+" indents) ";			}			string += " ";			return string;		}

var headers = $('h2:gt(0)').find('.editsection'); var k;			var header; headers.each(function(k) {				$(this).prepend(" «  Respond to this discussion     ");

var formCode = 	''+makeIndentList(this, k)+' You can type your response below.  On Wikipedia, you should sign all of your posts by ending them with four tildes (~) Add my response ';

var rLink = $('#wp-teahouse-respond-' + k); rLink.after(formCode); var rForm = $('#wp-teahouse-respond-form-' + k); var rText = $('#wp-th-respond-text-' + k); var rIndent = $('#indentLevel'+ k); var rButton = $('#wp-th-respond-' + k); var headline = $(this).parents('h2').find('span.mw-headline').html; // Prevent flash rForm.css( 'left', '-10000px' ); // Set up position setTimeout( function {					var pos = rLink.position;					var hCenter = ( $(window).width / 2 );					rForm.css( 'top', pos.top + 20 + 'px' );					rForm.css( 'left', (hCenter - (rForm.width) /2) + 'px' );					rForm.hide;				}, 0); rButton.button({disabled : true}).click( function(e) {					e.preventDefault;					var text = rText.val;					var index;					for(index = 0; index < maxIndent; index++)					{						text = ":"+text;					}						addResponse( k+1, headline, text );				}).end rText.keypress( function(e) {						var $textbox = $(this);						setTimeout( function { if ( (/~\s*$/).test($textbox.val) ) { rButton.button( 'option','disabled', false ); } else { rButton.button( 'option','disabled', true ); }						}, 0 );					} );				rLink.click(function(e) {					rForm.toggle('fast');					e.cancelBubble = true; // for IE					if (e.stopPropagation) {						e.stopPropagation;						e.preventDefault;					}				}); $(document).click( function(e) {				var $target = $(e.target);				if ( ! $target.is('.wp-teahouse-respond-form *') && ! $target.is('.wp-teahouse-respond *') ) {					$('.wp-teahouse-respond-form').fadeOut;				}			} ); $(document).keydown( function(e) {					if ( e.keyCode == 27 ) {// ESC						rForm.fadeOut;					}				});  //after loop }); 		});	} ); } )(jQuery,mediaWiki); // }