User:Ricordisamoa/Duplicate.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/* <nowiki>
 * Duplicate.js by [[User:Ricordisamoa]]
 * uses jQuery & Ajax
 * adds a link to tag the current image as {{Duplicate}} of another
*/
$(document).ready(function(){
	if(mw.config.get('wgNamespaceNumber')!=6||mw.config.get('wgAction')!='view') return;// the page is a file description
	$(mw.util.addPortletLink('p-cactions','#','Tag as Duplicate','ca-duplicate','Add {{Duplicate}} to the file description page'))
	.click(function(event){
		event.preventDefault();
		$(this).remove();
		$.get(
			mw.util.wikiScript('api'),
			{
				action:'query',
				titles:mw.config.get('wgPageName'),
				prop:'duplicatefiles',
				format:'json'
			}
		).done(function(data){
			data=data.query.pages;
			data=data[Object.keys(data)[0]];
			var dups='';
			if('duplicatefiles' in data){
				dups=['<br>',$('<select>')
				.append($.map(data.duplicatefiles,function(e){
					return $('<option>').text(e.name.replace(/_/g,' ')).val(e.name.replace(/_/g,' '));
				})).wrap('<label>...or select an actual duplicate from the list: </label>').parent()];
			}
			$('<div>')
			.append(
				$('<input>')
				.autocomplete({
					source:function(request,response){
						$.get(
							mw.util.wikiScript('api'),
							{
								action:'query',
								format:'json',
								list:'allimages',
								aiprop:'',
								aifrom:request.term,
								ailimit:10
							},
							function(data){
								response($.map(data.query.allimages,function(e){
									return [e.name.replace(/_/g,' ')];
								}));
							}
						);
					}
				})
				.wrap('<label>Type the file name of which this file is a duplicate (without prefix): </label>')
				.parent()
			)
			.append(dups)
			.dialog({
				title:'Tag as Duplicate',
				buttons:[
					{
						text:'Cancel',
						specialButton:'cancel',
						click:function(){
							$(this).dialog('close');
						}
					},
					{
						text:'Insert notice',
						specialButton:'proceed',
						click:function(){
							var input=$(this).dialog('widget').find('input');
							var select=$(this).dialog('widget').find('select');
							$(this).dialog('close');
							var val=input.val();
							if(val==''){
								if(select.length==1&&select.val()!='') val=select.val();
								else return;
							}
							$.post(
								mw.util.wikiScript('api'),
								{
									action:'edit',
									prependtext:'{{Duplicate|'+val+'}}\n',
									title:mw.config.get('wgPageName'),
									summary:'[[User:Ricordisamoa/Duplicate|tagging]] as [[Template:Duplicate|duplicate]] of [[File:'+val+'|'+val+']]',
									token:mw.user.tokens.get('csrfToken')
								}
							)
							.done(function(data){
								jsMsg('Template added!');
								setTimeout(function(){document.location.reload();},2500);
							})
							.fail(function(){
								jsMsg('The ajax request failed.');
							});
						}
					}
				]
			});
		});
	});
});
/* </nowiki> */