MediaWiki:Gadget-communitynoticeboard/content.js

/* _____________________________________________________________________________ * |                                                                             | * |                    === WARNING: GLOBAL GADGET FILE ===                      | * |                 Changes to this page affect many users. | * | Please discuss changes on the talk page or on FT:Gadget before editing. | * |_____________________________________________________________________________| * Adopted from Wikimedia Foundation's Teahose project * Community noticeboard "Ask a question" feature, used by the FAMEPedia Foundation's Noticeboard project. */ // (function($,mw) {	mw.teahouse = {		'questionform' : ' \		 \			 Write out your question in the textbox below. Please be specific in your question rather than general. If it\'s about a particular FAMEPedia page, please name it. \			 Short summary of your question: \			 \			 \			 \			 On FAMEPedia, you should sign all of your non-article posts by ending them with four tildes (~) \			Ask my question \			 \			 \			 (Note: this question submission form will not work without them.) \			 \		 \		',

addQuestion : function( title, text ) { $('.fp-cn-question-form').hide; $('.fp-cn-ask') .empty .addClass('mw-ajax-loader');

var api = new mw.Api; api.postWithEditToken({				'action' : 'edit',				'title' : 'FAMEPedia:Community noticeboard',				'section' : 'new',				'sectiontitle' : title,				'text' : text,			}).done(function {				location.reload;			}); }	};

$(function {		mw.loader.using( ['jquery.ui', 'mediawiki.api'], function { if ( !$('.fp-cn-ask').length ) { return; }

var $form = $(mw.teahouse.questionform); $('.fp-cn-ask').after($form);

// Prevent flash $form.css( 'left', '-10000px' );

// Set up position setTimeout( function {				var $trigger = $('.fp-cn-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('#fp-cnb-question-ask') .button({					disabled : true				}) .click( function(e) {					e.preventDefault;

var title = $form.find('#fp-cnb-question-title').val; var text = $form.find('#fp-cnb-question-text').val;

if ( title && /~\s*$/.test(text) ) { mw.teahouse.addQuestion( title, text ); }				})				.end				.find('#fp-cnb-question-text')					.keypress( function(e) { var $textbox = $(this); setTimeout( function {							if ( (/~\s*$/).test($textbox.val) ) {								$form.find('#fp-cnb-question-ask')									.button( 'option','disabled', false );							} else {								$form.find('#fp-cnb-question-ask')									.button( 'option','disabled', true );							}						}, 0 ); } );

$('.fp-cn-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('.fp-cn-question-form *') && ! $target.is('.fp-cn-ask *') ) {					$('.fp-cn-question-form').fadeOut;				}			} );

$(document).keydown( function(e) {				if ( e.keyCode == 27 ) {// ESC					$('.fp-cn-question-form').fadeOut;				}			}); } );	} ); } )(jQuery,mediaWiki);

if ( mw.config.get("wgPageName") == 'FAMEPedia:Community noticeboard' ) { (function($,mw) {	$(function { mw.loader.using( ['jquery.ui', 'mediawiki.api'], function {		function addResponse( section, headline, text ) {			var wikitext = '\n\n:' + text;			$('.fp-cn-respond-form').hide;			$('.fp-cn-respond')				.find('.selflink')				.empty				.addClass('mw-ajax-loader');			var api = new mw.Api;			api.postWithToken( 'csrf', {					'action' : 'edit', 'section' : section, 'title' : 'FAMEPedia:Community noticeboard', 'appendtext' : wikitext, 'summary' : '/* ' + headline + ' */' + ' response' }).done ( function {location.reload;} );		}			var headers = $('h2:gt(0)').find('.mw-editsection:first'); 			var $a, k, matches;       			headers.each(function(k) { $a = $(this).find('a'); if( !$a ) { return; } $a.each(function{					matches = $(this).attr( 'href' ).match( /&(?:ve|)section=(\d+)/ );					if( matches ) {						k = matches[1];						return false;					}				}); $(this).prepend(" «  Join this discussion     "); var formCode = 	' You can type your response below.  On FAMEPedia, you should sign all of your posts by ending them with four tildes (~) Add my response '; var rLink = $('#fp-cn-respond-' + k); rLink.after(formCode); var rForm = $('#fp-cn-respond-form-' + k); var rText = $('#fp-cnb-respond-text-' + k); var rButton = $('#fp-cnb-respond-' + k); var headline = $(this).parents('h2').find('span.mw-headline').html; headline = headline.replace(/ [\d]*<\/span> /, ''); // 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;						addResponse( k, 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('.fp-cn-respond-form *') && ! $target.is('.fp-cn-respond *') ) {					$('.fp-cn-respond-form').fadeOut;				}			} ); $(document).keydown( function(e) {					if ( e.keyCode == 27 ) {// ESC						rForm.fadeOut;					}				});  //after loop }); 		});	} ); } )(jQuery,mediaWiki); } //