User:Theo's Little Bot/afchprefs/afchelper.js/prefs.js

/* Uploaded from https://github.com/WPAFC/afch, commit: fc98b3011610b14b4735c4613c0acea697c9b75e (origin/feature-prefs) */ // // Script should be located at User:Theo%27s Little Bot/afchprefs/afchelper.js/prefs.js

/* 	Preferences can be referenced in the code in following manner: afcHelper_preferences.prefname

TODO add more documentation, especially on how to add prefs and such

var afcHelper_defaultPreferences = { 'summary': '(AFCH)', 'afc_logCSD': true, 'afc_customDeclineRationales': [], 'dosomething': true }

function afcHelper_assemblePrefSetter { var afcHelper_preferencedetails = [ {			'type':'category', 'id':'general', 'title':'General', 'preferences': [ {					'id':'summary', 'type':'text', 'prompt':'What text should be appended to the edit summary when using the helper script?' }			]		},{			'type':'category', 'id':'afc', 'title':'Articles for creation', 'preferences': [ {					'id':'afc_logCSD', 'type':'checkbox', 'prompt':'Should we automatically log CSD nominations to User:' + mw.config.get('wgUserName') + '/CSD log?' },				{					'id':'afc_customDeclineRationales', 'type':'userlist', 'prompt':'Enter custom decline rationales that will appear when reviewing AFC submissions.' }			]		}/*,{			'type':'category', 'id':'ffu', 'title':'Files for upload', 'preferences': [ {					'id':'dosomething', 'type':'checkbox', 'prompt':'Should we do something?' }			]		}*/	];

$('#afcHelper_prefs').html('Articles for creation helper script preferences manager  ') $('#afcHelper_prefstatus').html('Loading preferences manager...')

for (var i = 0; i < afcHelper_preferencedetails.length; i++) { // Set up the header var category = afcHelper_preferencedetails[i]; var category_div = $(' ') .attr('id', category.id) .append(' ' + category.title + ' '); // Now set up the prefs var prefs = category.preferences; for (var g = 0; g < prefs.length; g++) { var pref = prefs[g]; var preftype = pref.type;

if (preftype === 'userlist') { var current_vals = afcHelper_preferences[pref.id]; if (current_vals.length === 0) current_vals.push('Enter text here');

var maintable = $(' ') .attr('id',pref.id) .attr('type','userlist') .attr('style','width:80%; margin-left:2em;') .attr('class','afcHelper_pref wikitable');

$.each(current_vals, function(index,value) { maintable.append(afcHelper_makeRowWithInput(value,pref.id)); });

var buttonrow = $(' ') .attr('class','afcHelper_addbutton'); var button = $(' Add additional row ') .attr('onclick',"afcHelper_makeRowWithInput('Enter text here','" + pref.id + "')"); buttonrow.append($(' ').append(button));

maintable.append(buttonrow);

var pref_div = $(' ') .html(''+pref.prompt+' ') .append(maintable); category_div.append($('').append(pref_div)); } else { var input_code = $(' ') .attr('id',pref.id) .attr('type',preftype) .attr('class','afcHelper_pref'); switch (preftype) { case 'checkbox': if (afcHelper_preferences[pref.id] == true) input_code.attr('checked', 'checked'); break; default: input_code.attr('value',afcHelper_preferences[pref.id]); break; }				var pref_div = $(' ') .html(''+pref.prompt+' ') .append(input_code); category_div.append($('').append(pref_div)); }		};		// And finally add this category to the list $('#afcHelper_prefsetter').append(category_div); };

$('#afcHelper_prefs').append('Save preferences or restore defaults '); $('#afcHelper_prefstatus').html('Preferences manager loaded.') }

function afcHelper_restoreDefaults { $('#afcHelper_prefstatus').html('Restoring to default preferences...') afcHelper_savePrefsApi(afcHelper_defaultPreferences,'restored'); }

function afcHelper_savePrefs { $('.afcHelper_pref').each(function(index,pref) {		var jqpref = $(pref);		switch (jqpref.attr('type')) {			case 'checkbox':				var value = ((jqpref.attr("checked") != undefined) ? true : false);				break;			case 'userlist':				var value = [];				jqpref.find('input[type=text]').each(function(index,val) { jqval = $(val); if (jqval.val !== "Enter text here") value.push($.trim(jqval.val)); });				break;			default:				var value = $.trim(jqpref.val);				break;		}		afcHelper_preferences[jqpref.attr('id')] = value;	}); $('#afcHelper_prefstatus').html('Saving preferences...') afcHelper_savePrefsApi(afcHelper_preferences,'saved'); }

function afcHelper_savePrefsApi(prefs,verb) { var tokenreq = { 'action': 'tokens', 'type': 'options' };

var api = new mw.Api; api.post(tokenreq) .done(function(tokendata) {				if (tokendata) {					var optionsreq = {							'action': 'options',							'token': tokendata.tokens.optionstoken,							'optionname': 'userjs-afch',							'optionvalue': JSON.stringify(prefs)					};					api.post(optionsreq)							.done(function(data) { $('#afcHelper_prefstatus').html('Preferences ' + verb + ' successfully! (Reload page to see changes)');							})							.fail(function(error) {								$('#afcHelper_prefstatus').html('Could not save preferences! Error info: ' + error); 							});				} else {					$('#afcHelper_prefstatus').html('<span class="afcHelper_notice">Could not save preferences! Error info: could not get save token'); 				}			} )			.fail(function(error) {				$('#afcHelper_prefstatus').html('<span class="afcHelper_notice">Could not save preferences! Error info: could not get save token (' + error + ')'); 			}); }

function afcHelper_makeRowWithInput(value,tableid) { var maintable = $('#'+tableid); var row = $(' '); var input = $(' ') .attr('type','text') .attr('style','width:89%; float:left;') .attr('value',value); var deletebutton = $('<a>') .attr('class','afchelper_deleterow') .attr('style','width:10%; float:right;') .attr('onclick','$(this).parent.remove;') .text('[remove]'); row.append($(' ').append(input,deletebutton)); maintable.find('.afcHelper_addbutton').before(row); return row; }

function afcHelper_getUserPrefs(defaultprefs) { /* 	Given a dictionary of default preferences, returns a	dictionary of those prefs += saved prefs, with saved preferences taking precedent over defaults */	var newprefs = $.extend({},defaultprefs); // Get previously saved preferences var oldprefs = mw.user.options.get('userjs-afch'); if (oldprefs) oldprefs = JSON.parse(oldprefs) else oldprefs = {}; // Update afcHelper_preferences with user-set preferences $.each(oldprefs, function(key, value) {		if (key in newprefs) {			// Our lazy way of phasing out old preferences...			newprefs[key] = value;		}	}); return newprefs; }

// Update the preferences dictionary with user preferences [critical] var afcHelper_preferences = afcHelper_getUserPrefs(afcHelper_defaultPreferences);

// If on the prefs page, display the pref setter if (wgPageName.indexOf('Wikipedia:WikiProject_Articles_for_creation/Helper_script/Preferences') !== -1) { afcHelper_assemblePrefSetter; }