MediaWiki:Common.js/issues.js

/* Issue submission form

mw.loader.using(['mediawiki.api', 'mediawiki.util'], function {       var issuepage = "RuneScape:Issues";        var maxsize = 500;        var $IssueForm, $fieldset, $txtarea, $txtareacount, $msgs, $openForm, pagename;        var status = 0; //0 ready, 1 submitting, 2 submitted        var userSettings;        var settingsName = 'rsw-issue-form';        var defaultSettings = { form: {}, tracked: {} };
 * (function($, mw) {

function msg(message) { $msgs.append('' + message+''); }

function internallink(link,txt) { var t = link; if (txt) { t = txt; }           return '' + t + ''; }

// fetch settings from localStorage function getSettings { var settings; try { // just in case something goes wrong settings = JSON.parse(window.localStorage.getItem(settingsName)); }           catch (err) { settings = {}; }           if (settings === null) { settings = {}; }           userSettings = $.extend({}, defaultSettings, settings); }

// update cached settings and put them back into localStorage // use this over directly setting to userSettings function updateStorage { window.localStorage.setItem(settingsName, JSON.stringify(userSettings)); }       function updateFormData(k,v) { userSettings.form[k] = v;           updateStorage; }       function removeFormData(k) { delete userSettings.form[k]; updateStorage; }       function addTracked(k) { userSettings.tracked[k] = false; }

//TODO: interface to view previously submitted issues

function init { pagename = mw.config.get('wgPageName'); $txtarea = $(' ') .attr({               'id': 'issue-form-txt',                'autofocus': true,                'maxlength': maxsize,                'minlength': 1,                'name': 'issue-form-txt',                'placeholder': 'Enter your issue or question here',

})               .css({ 'height': '20em', 'width': '100%', 'border': '1px solid #aaa', 'padding': '0', 'font-family': 'monospace', });

$txtareacount = $(' ') .css({               'float':'right',                'text-align':'right',                'color':'grey'            }) .append('0/'+maxsize);

$txtarea.keyup(function {               $txtareacount.text($txtarea.val.length + '/'+maxsize);            });

$msgs = $('') .css({               'width':'100%',                'height':'10em',                'overflow-y':'scroll'            }) .append('Waiting to submit...');

$fieldset = $(' ') .append(' Enter your problem below. Be as specific as you can. Make sure to check back to the issue page periodically in case users have followup questions. ') .append('Do not enter personal information such as email addresses. Your response is publicly visible. Submitting this form counts as an edit and is subject to all normal restrictions and policies to do with editing. ') .append('Remember that ' + internallink('RuneScape:RuneScape_Wiki_is_not...#..._Jagex', 'we are not Jagex') + ', so we cannot do anything with suggestions for the game itself. ') .append($txtarea) .append($txtareacount) .append('Status messages: ') .append($msgs);

$IssueForm = $(' ') .attr({               'id': 'issue-form-wrapper',                'method': '',                'name': 'issue-form-wrapper',            }) .addClass('WikiaForm') .append($fieldset);

$openForm = $(' Quick issue form'); $('.toolbar ul.tools').append($openForm);

$openForm.click(function {                $.showCustomModal('Quick issue form', $IssueForm, { id: 'issues-form-modal', width: 500, buttons: [{ message: 'Cancel', handler: function { Api.abort; $('#issues-form-modal').closeModal; }                   }, {                        id: 'startButton', message: 'Submit', defaultButton: true, handler: function { submitForm; }                   }]                });            });        }

function setIssueFormStatus(s) { status = s;           switch (status) { case 0: $('#startButton').removeAttr('disabled').text('Submit'); $txtarea.prop('disabled', false); break; case 1: $('#startButton').attr('disabled', 'disabled').text('Submitting...'); $txtarea.prop('disabled', true); break; case 2: $('#startButton').attr('disabled', 'disabled').text('Submitted!'); $txtarea.prop('disabled', true); break; }       }

function submitForm { if (status > 0) { return; }           if ($txtarea.val.length === 0) { msg('You didn\'t enter anything!'); return; }           setIssueFormStatus(1); msg('Submitting...'); var now = Date.now; var curissuepagename = issuepage + '/' + pagename + '/' + now;

var username = mw.config.get('wgUserName');

function getTxt(time) { return [ ''               ].join('\n'); }

function callAPI(data, method, callback) { data['format'] = 'json'; $.ajax({                   data: data,                    dataType: 'json',                    url: mw.config.get('wgScriptPath') + '/api.php',                    type: method,                    success: function(response) {                        if (response.error)                            msg('API error: ' + response.error.info);                        else                             callback(response);                    },                    error: function(xhr, error) { msg('AJAX error: ' + error); }                }) .fail; }

function edit( title, txt, callback ) { curissuepagename = title; var basetimestamp, curtimestamp; return callAPI( {                   action: 'query',                    prop: 'info|revisions',                    rvprop:  'content|timestamp',                    titles: String( title ),                    rvlimit: 1,                    indexpageids: true,                    format: 'jsonfm',                    intoken: 'edit',                }, 'GET', function ( data ) {                    console.log(data);                    if ( !data.query || !data.query.pages ) {                        return $.Deferred.reject( 'unknown' );                    }                    curtimestamp = data.starttimestamp;

callAPI({                       action: 'edit',                        title: title,                        formatversion: '2',                        token: mw.user.tokens.get('editToken'),                        summary: 'Submit new issue',                        text: txt,

// Protect against errors and conflicts starttimestamp: curtimestamp, createonly: true

}, 'POST', callback);               } ); }

function editcallback(data) { if (data.error) { msg("Oops, an error occured when trying to submit to the page. Details to follow..."); switch (data.error.code) { case "blocked": case "autoblocked": msg("Sorry, you can't submit an issue if you are blocked. If you believe this is in error, use the template on "+internallink('Special:MyTalk','your talk page')+" to request an unblock."); break; case "ratelimited": msg("Whoah! Slow down with the editing/issue submission. You've triggered the rate limit - wait a few minutes before editing/submitting again."); break; case "readonly": msg("Looks like the wiki is in read-only mode. This shouldn't last too long - an hour at most, probably. Try submitting again later."); break; case "articleexists": edit(issuepage + '/' + pagename + '/' + (now+1), getTxt(now), editcallback); return; default: msg("Unknown error occured. Here's the error information, pass this on to an administrator:"); msg("Error code:" + data.error.code); msg("Error info:" + data.error.info); break; }                   setIssueFormStatus(0); } else { msg('Submitted!'); msg('Your issue is recorded ' + internallink(curissuepagename, 'here') + ', so check back later to see if there are questions or if it has been resolved.'); setIssueFormStatus(2); }           }

edit(curissuepagename, getTxt(now), editcallback); }       $(init); }); })(window.jQuery, window.mw);