User:Oil4/templates.js

/* */ /** * templates.js * * Insert various templates from the edit toolbar. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */

$(document).ready(function {	// http://www.netlobo.com/url_query_string_javascript.html	function getParam(name) {		name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');		var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); 		var results = regex.exec(window.location.href);		if (results == null) {			return '';		} else {			return decodeURIComponent(results[1]);		}	}

function setCookie(name, value, expires) { var d = new Date; d.setDate(d.getDate + expires); document.cookie = name + '=' + escape(value) + ';path=/'; }

function getCookie(name) { if (document.cookie.length > 0) { var start = document.cookie.indexOf(name + '='); if (start != -1) { start = start + name.length + 1; var end = document.cookie.indexOf(';', start); if (end == -1) { end = document.cookie.length; }				return unescape(document.cookie.substring(start, end)); } 		}		return ''; }

// Needle and haystack should contain strings. Each element in haystack will be trimmed before comparison. function inArray(needle, haystack) { for (var i in haystack) { if (needle == $.trim(haystack[i])) { return true; }		}

return false; }

var toolbarTemplates = (window.toolbarTemplates instanceof Array) ? window.toolbarTemplates : [];

toolbarTemplates = toolbarTemplates.concat([		{ name: 'Delete', ns: '|File', open: '\n\n', sample: 'Reason' },		{ name: 'Delete Personal', ns: 'File', open: '\n\n', sample: 'Personal image' },		{ name: 'Blacklist', ns: '|File', open: '\n\n', sample: 'Reason' },               { name: 'Transparency', ns: 'File', open: , close: , sample: 'Transparency needed' }), { name: 'NPOV', ns: , open: , close: '\n\n', sample: '' }, { name: 'Move', ns: '', open: '\n\n', sample: 'Page' }, { name: 'Merge', ns: '', open: '\n\n', sample: 'Page' }, { name: 'Under construction', ns: '|User|Project', open: '\n\n', close: , sample:  }, { name: 'Disputed', ns: , open: '\n\n', close: , sample: '' }, { name: 'Cleanup', ns: , open: '\n\n', close: , sample: '' }, { name: 'Clarify', ns: , open: , close: , sample:  }, { name: 'Incomplete', ns: , open: '\n\n', close: , sample: '' }, { name: 'In Use', ns: '|Project', open: '\n\n', close: , sample:  }, { name: 'Map Nomination', ns: , open: '\n\n', close: , sample: '' }, { name: 'Obsolete', ns: , open: '\n\n', close: , sample: '' }, { name: 'Protect', ns: , open: '\n\n', close: , sample: '' }, { name: 'Too Big', ns: , open: '\n\n', close: , sample: '' }, { name: 'Vote for Deletion', ns: , open: '\n\n', close: , sample: '' }, { name: 'Wikify', ns: '|Update', open: '\n\n', close: , sample:  }, { name: 'Stub', ns: , open: '\n\n', close: , sample: '' }, { name: 'Section Stub', ns: , open: '\n\n', close: , sample: '' },

{ name: 'GEMW 1', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'GEMW 2', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Block', ns: 'User_talk', open: ' ', close: , sample:  }, { name: 'Welcome Notice', ns: 'User_talk', open:  + wgUserName + , close: , sample:  }, { name: 'No Personal Images', ns: 'User_talk', open:  + wgUserName + ' ', close: , sample: '' }, { name: 'No Vandal', ns: 'User_talk', open: ' ', close: , sample:  }, { name: 'Error 1', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Error 2', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Test 1', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Test 2', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Test 3', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Test 4', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Test 5', ns: 'User_talk', open: ' ', close: , sample:  }, { name: 'Deleted User Talk', ns: 'User_talk', open: ' ', close: , sample:  }, { name: 'POV 1', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'POV 2', ns: 'User_talk', open: '', close: ' ', sample: '__CONTENTPAGE__' }, { name: 'Vandal', ns: 'User_talk', open: '', close: ' ', sample: 'number of offense' }, { name: 'User Edit', ns: 'User_talk', open:  + wgUserName + ' ', close: , sample: '' } ]);

/* Try to keep track of the last visited "content" page. */	if (wgCanonicalNamespace.search(/talk|special/gi) == -1) { setCookie('templatesContentReferrer', wgPageName.replace(/_/g, ' ')); }

/* Append template list when editing user talk pages. */	if (wgAction == 'edit' || wgAction == 'submit') { var haveTemplates = false;

var $select = $(' ').change(function {			var i = $(this).val;

if (i == '-1') { return; }

var tpl = toolbarTemplates[i]; insertTags(tpl.open, tpl.close, tpl.sample.replace('__CONTENTPAGE__', getCookie('templatesContentReferrer')));

var fillSummary = (typeof(window.toolbarFillSummary) != 'undefined') ? window.toolbarFillSummary : true; var fillHeading = (typeof(window.toolbarFillHeading) != 'undefined') ? window.toolbarFillHeading : true; var newSection = (getParam('section') == 'new');

if ((newSection && fillHeading) || (!newSection && fillSummary)) { var summary = $('#wpSummaryEnhanced').val;

if ($.trim(summary.replace(/\/\*.*?\*\//, )) == ) { $('#wpSummaryEnhanced').val(summary + tpl.name); }			}		});

$select.append($(' ').val('-1').text('[Templates]'));

for (var i in toolbarTemplates) { var tpl = toolbarTemplates[i]; var ns = tpl.ns.toLowerCase; // normalize namespace list ns = ns.replace(/ /g, '_').replace(/main/g, '').replace(/image/g, 'file'); if (tpl.ns == '*' || inArray(wgCanonicalNamespace.toLowerCase, ns.split('|'))) { $select.append($(' ').val(i).text(tpl.name)); haveTemplates = true; }		}

if (haveTemplates) { $('#toolbar').prepend($select); }	} }); /* */