User:Gryllida/js/feedbackstuff.js

// - adds 'feedback' button next to 'log out' // - converts to buttons which add topics as configured by relevant target // - edit 'targets' variable as needed for your project // - edit 'msgs' variable as needed for your language var grysFeedbackStuff = { ready: 0, name: 'gryfeedbackstuff', nameId: '#gryfeedbackstuff', targets: { 'current': { 'ns': [0], 'pageName': 'Talk:' + mw.config.get('wgPageName'), 'mode': 'bottom-post', 'title': 'This page quality (here)' },		'project':{ 'ns': ['*'], 'pageName': 'Wikinews:Water_cooler/miscellaneous', 'mode': 'bottom-post', 'title': 'General question about Wikinews (here)' },		'software':{ 'ns': ['*'], 'pageName': 'Wikinews:Water_cooler/technical', 'mode': 'top-post', 'title': 'Wikinews website software (here)' }	},	msgs: { 'send': 'Send', // Send 'onCurrentPage': 'onto current page', // onto current page 'newSection': 'new section', // new section 'addTopic': 'Add topic', // add topic 'title': 'Leave feedback', // leave feedback 'sending': 'Sending...', // sending ... 'fail': 'Failed :(', // failed :( 'success': 'Sent :)', // sent successfully :) 'preview': 'Preview', // preview 'feedback': 'Feedback' // feedback },	/*	 * DOM bits and bytes */	makeRadio: function{ $(grysFeedbackStuff.nameId).append(" "); // For each choice var t = new mw.Title( mw.config.get('wgPageName') ); ns = t.getNamespaceId; // 6 $.each(grysFeedbackStuff.targets, function(k,v){			if (v.ns.indexOf('*') > -1 || v.ns.indexOf(ns) > -1 ) {				// Generate the input radio thing				id = grysFeedbackStuff.name + '-' + k;				$input = $("")					.prop("id",id)					.prop("name",grysFeedbackStuff.name)					.prop('value',k);				// Generate the label with a "for" prop				$label=$(" ").prop("for",id).html(v.title);				// Append a line break and an input, then the label				$('#gryfeedback-form').append($input);				$input.after($label);				$('#gryfeedback-form').append(" ");				// Debug output				console.log('#'+k + " ... " + v.title + ',,,'+$('#grysFeedbackStuff-comments').prop('title')+'---');				console.log(k == $('#grysFeedbackStuff-comments').prop('title'));				// Select this option if it's primary (as indicated by span title)				if(k == $('#grysFeedbackStuff-comments').prop('title')){					$input.attr('checked','checked'); }			}		});		grysFeedbackStuff.ready=1;	},	prepareDialog:function{		// Make the in-dialog html		grysFeedbackStuff.$dialog = $( ' ' ).prop('id',grysFeedbackStuff.name)		.append("")		.append(" ")		.append("  ");		//.append("");		// Make the dialog buttons (and what they do)		var formButtons={};		formButtons[ grysFeedbackStuff.msgs.send ] = {			'click':function  {               grysFeedbackStuff.submit;        	},        	id: grysFeedbackStuff.name + '-button',        	text: grysFeedbackStuff.msgs.send		};		formButtons[ grysFeedbackStuff.msgs.preview ] = {			'click':function  {               grysFeedbackStuff.preview; },       	id: grysFeedbackStuff.name + '-preview-button', text: grysFeedbackStuff.msgs.preview };       // Construct the dialog grysFeedbackStuff.$dialog.dialog( {                  width: 500,                   autoOpen: false,                   title: grysFeedbackStuff.msgs.title,                   modal: true,                   buttons: formButtons        } ); },	preview: function{ subject = $('#gryfeedbackstuff-subject').val; message = $('#gryfeedbackstuff-message').val; summary = $('#gryfeedbackstuff-summary').val; // auto-sign if ( message.indexOf( '~' ) === -1 ) { message += ' --~'; }		// 		sectionText = '=='+subject+'==\n'+message+'\n\n'; // wiki things api = new mw.Api; api.get( {			'action' : 'parse',			'text' : sectionText,			'pst': ''		}).done(function(result) {			markup = result.parse.text['*'];			//var page = result.pages[result.pageids[0]];			//var text = page.revisions[0]['*'];			console.log(markup);			$('#gryfeedbackstuff-previewthings').html(markup);		}); },	doStuff : function { var $li = $("") .attr('id','gryfeedbackstuff-menuitem') .text(grysFeedbackStuff.msgs.feedback) .click( grysFeedbackStuff.openDialog ) .insertBefore('#pt-login,#pt-logout'); // dialog contents (html) grysFeedbackStuff.prepareDialog; // show the dialog (currently whenever a page loads; once it works, this will be only by request) $('#grysFeedbackStuff-comments') .attr({class: 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-focus'}) .prepend(		$( ' ' )			.text( grysFeedbackStuff.msgs.addTopic )			.addClass( 'gry-feedbackLink' )			.addClass( 'ui-button-text' )		).click( grysFeedbackStuff.openDialog ); },	/* shows the dialog on the screen */ openDialog: function{ // Choices if(grysFeedbackStuff.ready===0){grysFeedbackStuff.makeRadio;} $('#gryfeedbackstuff-subject').focus; grysFeedbackStuff.$dialog.dialog( 'open' ); },	/*	 * Wiki things (submitting the message) */	submit: function{ // Disable the button, update its text id = grysFeedbackStuff.nameId + '-button'; $(id + " > .ui-button-text").text(grysFeedbackStuff.msgs.sending); $(id).attr('disabled','disabled'); // (optional) adjust pagetitle to the relevant comments page name subject = $('#gryfeedbackstuff-subject').val; message = $('#gryfeedbackstuff-message').val; summary = $('#gryfeedbackstuff-summary').val; // auto-sign if ( message.indexOf( '~' ) === -1 ) { message += ' --~'; }		// 		// get page title and post mode key = $("#gryfeedback-form input[type='radio']:checked").val; pageTitle = grysFeedbackStuff.targets[key].pageName; mode = grysFeedbackStuff.targets[key].mode; // things for the wiki api = new mw.Api; if(mode == 'top-post'){ editSummary = '/* ' + subject + ' */ ' + grysFeedbackStuff.msgs.newSection; sectionText = '=='+subject+'==\n'+message+'\n\n'; // Get page content api.get( {				'action' : 'query',				'titles' : pageTitle,				'prop'  : 'revisions|info',				'intoken' : 'edit',				'rvprop' : 'content',				'indexpageids' : 1			}).done(function(result) {				result = result.query;				var page = result.pages[result.pageids[0]];				var text = page.revisions[0]['*'];				// Add my section at the top [with necessary changes]				arry = text.split("==");				text = arry[0];				arry.shift;				text = text +sectionText+"=="+arry.join("==");				// Submit the edit				api.postWithEditToken( {						'action' : 'edit', 'title' : pageTitle, 'text' : text, 'summary' : editSummary }				).done(grysFeedbackStuff.ok).fail(grysFeedbackStuff.fail);			}).fail(grysFeedbackStuff.fail); } else if (mode =='bottom-post'){ api.newSection( pageTitle, subject, message ).done( grysFeedbackStuff.ok ).fail( grysFeedbackStuff.fail ); }	},	/*	 * Again DOM bits and bytes (relating to the end) */	ok: function{ id = grysFeedbackStuff.nameId + '-button > .ui-button-text'; $(id).text(grysFeedbackStuff.msgs.success); /*// Success; remove the OK button grysFeedbackStuff.$dialog.dialog( {			buttons: []		}); // Show a success message $a=$('').append(grysFeedbackStuff.msgs.onCurrentPage); $a.attr('href',window.location.pathname); $msg = $(' ') .append(grysFeedbackStuff.msgs.success) .append('') .append($a); $('#gryfeedbackstuff').prepend($msg);*/ },	fail: function{ id = grysFeedbackStuff.nameId + '-button'; $(id+' > .ui-button-text').text(grysFeedbackStuff.msgs.fail); $(id).prop('disabled', false); } }; $(document).ready( function { 	mw.loader.using( [ 'mediawiki.feedback', 'mediawiki.api', 'jquery.chosen', 'jquery.spinner'], grysFeedbackStuff.doStuff ); } );