Jump to content

User:Ocaasi/TWAcontents.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ocaasi (talk | contribs) at 20:17, 24 May 2013 (add). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
(function($,mw) {
	mw.teahouse = {
		'questionform' : ' \
		<div class="wp-teahouse-question-form"> \
			<p>You can put your question in the textbox below.</p>\
			<p><label for="wp-th-question-title">Short summary of your question: </label> \
			<input id="wp-th-question-title" type="text" size="90" /></p>\
			<textarea rows="10" cols="20" id="wp-th-question-text"></textarea> \
			<p>\
			<span class="wp-th-sign-hint">On Wikipedia, you should sign all of your posts by ending them with four tildes ([[User:Ocaasi|Ocaasi]]<sup> [[User talk:Ocaasi|t ]]&#124;[[Special:Contributions/Ocaasi| c]]</sup> 20:17, 24 May 2013 (UTC))</span> \
			<a href="#" id="wp-th-question-ask">Ask my question</a> \
			</p> \
		</div> \
		',
 
		addQuestion : function( title, text ) {
			var wikitext = '==' + title + "==\n" + text + "\n";
 
			$('.wp-teahouse-question-form').hide();
 
			$('.wp-teahouse-ask')
				.find('.selflink')
				.empty()
				.addClass('mw-ajax-loader');
 
			var api = new mw.Api();
 
			api.get( {
				'action' : 'query',
				'titles' : 'Wikipedia:Teahouse/Questions',
				'prop'   : 'revisions|info',
				'intoken' : 'edit',
				'rvprop' : 'content',
				'indexpageids' : 1
			}, {
				'ok' : function(result) {
					result = result.query;
					var page = result.pages[result.pageids[0]];
					var oldText = page.revisions[0]['*'];
					var newText = oldText.replace( /^==/m, wikitext+"==" );
 
					api.post(
						{
							'action' : 'edit',
							'title' : 'Wikipedia:Teahouse/Questions',
							'text' : newText,
							'summary' : 'New question: '+title,
							'token' : page.edittoken
						},
						{
							'ok' : function() {window.location.reload();}
						}
					);
				}
			});
		}
	};
 
	$(function() {
		mw.loader.using( ['jquery.ui.button', 'mediawiki.api'], function() {
			if ( !$('.wp-teahouse-ask').length ) {
				return;
			}
 
			var $form = $(mw.teahouse.questionform);
			$('.wp-teahouse-ask').after($form);
 
			// Prevent flash
			$form.css( 'left', '-10000px' );
 
			// Set up position
			setTimeout( function() {
				var $trigger = $('.wp-teahouse-ask');
				var pos = $trigger.position();
				var hCenter = ( $trigger.parent().width() / 2 );
				$form.css( 'top', pos.top + $trigger.height() + 'px' );
				$form.css( 'left', (hCenter - ($form.width()) / 2) + 'px' );
				$form.hide();
			}, 0);
 
			$form.find('#wp-th-question-ask')
				.button({
					disabled : true
				})
				.click( function(e) {
					e.preventDefault();
 
					var title = $form.find('#wp-th-question-title').val();
					var text = $form.find('#wp-th-question-text').val();
 
					if ( title && /[[User:Ocaasi|Ocaasi]]<sup> [[User talk:Ocaasi|t ]]&#124;[[Special:Contributions/Ocaasi| c]]</sup> 20:17, 24 May 2013 (UTC)\s*$/.test(text) ) {
						mw.teahouse.addQuestion( title, text );
					}
				})
				.end()
				.find('#wp-th-question-text')
					.keypress( function(e) {
						var $textbox = $(this);
						setTimeout( function() {
							if ( (/[[User:Ocaasi|Ocaasi]]<sup> [[User talk:Ocaasi|t ]]&#124;[[Special:Contributions/Ocaasi| c]]</sup> 20:17, 24 May 2013 (UTC)\s*$/).test($textbox.val()) ) {
								$form.find('#wp-th-question-ask')
									.button( 'option','disabled', false );
							} else {
								$form.find('#wp-th-question-ask')
									.button( 'option','disabled', true );
							}
						}, 0 );
					} );
 
			$('.wp-teahouse-ask').click(function(e) {
				$form.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-question-form *') &&
					! $target.is('.wp-teahouse-ask *')
				) {
					$('.wp-teahouse-question-form').fadeOut();
				}
			} );
 
			$(document).keydown( function(e) {
				if ( e.keyCode == 27 ) {// ESC
					$('.wp-teahouse-question-form').fadeOut();
				}
			});
		} );
	} );
} )(jQuery,mediaWiki);

if ( mw.config.get("wgPageName.indexOf") == 'Wikipedia:TWA/testing' ) {
(function($,mw) {
 
	$(function() {
		mw.loader.using( ['jquery.ui.button', '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.get( {
				'action' : 'query',
				'titles' : 'Wikipedia:TWA/testing',
				'prop'   : 'revisions|info',
				'intoken' : 'edit',
				'rvprop' : 'content',
				'indexpageids' : 1
			}, {
				'ok' : function(result) {
					result = result.query;
					var page = result.pages[result.pageids[0]];
 
					api.post(
						{
							'action' : 'edit',
							'section' : section,
							'title' : 'Wikipedia:TWA/testing',
							'appendtext' : wikitext,
							'summary' : '/* ' + headline + ' */' + ' response',
							'token' : page.edittoken
						},
						{
							'ok' : function() {window.location.reload();}
						}
					);
				}
			});
		}
 
				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();
						addResponse( k+1, headline, text );
				}).end()
 
				rText.keypress( function(e) {
						var $textbox = $(this);
						setTimeout( function() {
							if ( (/[[User:Ocaasi|Ocaasi]]<sup> [[User talk:Ocaasi|t ]]&#124;[[Special:Contributions/Ocaasi| c]]</sup> 13:32, 24 May 2013 (UTC)\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);
}