Jump to content

User:Holek/cite-gen.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * Citation templates generator in editor
 *
 * @author Mike "Hołek" Połtyn
 */
if (false && // T35837
( wgAction == 'edit' || wgAction == 'submit' ) )

{
	if (wgNamespaceNumber > -1)
	{
		importScript('User:Holek/cite-gen/i18n.js');
		$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
		while(x<c.length){var m=r.exec(c.substr(x));
		  if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
		  }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
		  o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
		URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
		while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
		t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
		});
		
		var citeGen = new Object();
		citeGen.showHide = function (context) {
			$('#citeGenToolbar').slideToggle('fast', function() {
				context.fn.trigger( 'resize' );
			});
		}
		citeGen.passInfo = function (e) {
			var button = $(e.target).attr("disabled","disabled");
			$.ajax({
				url: 'http://toolserver.org/~citegen/index.php?citelang='+wgContentLanguage+'&s[add-references]=on&template=json&input='+$.URLEncode($('#citeGenInput').val()),
				dataType: 'jsonp',
				jsonp: 'uid',
				success: function (json) {
					$('#citeGenResult').slideDown('fast', function() {
						($.wikiEditor.modules.toolbar.evt.resize)($( 'textarea#wpTextbox1' ).data( 'wikiEditor-context' ));
					});
					if (json.response.length > 0) {
						var resp = json.response[0].book;
					} else {
						var resp = wgCiteGenLang['no-results'];
					}
					$('#citeGenResultOutput').val(resp);
					button.prop('disabled', false);
				}
			});
			e.preventDefault();
		}
		
		if ( typeof $ != 'undefined' && typeof $.fn.wikiEditor != 'undefined' )
		{
			hookEvent( 'load', function(){
				var citeGenDescDiv = $('<div/>').append(wgCiteGenLang['cite-gen-description']),
					citeGenInputBox = $('<input/>').attr({'type':'text','size':'30','id':'citeGenInput'}),
					citeGenSubmit = $('<input/>').attr({'type':'button','value':wgCiteGenLang['generate-template']}).click(citeGen.passInfo),
					citeGenForm = $('<form/>')
									.append(citeGenDescDiv)
									.append(citeGenInputBox)
									.append(citeGenSubmit)
									.submit(function() {citeGenSubmit.click();return false;}),
					
					citeGenOutputBox = $('<input/>').attr({'type':'text','size':'60','id':'citeGenResultOutput'}),
					citeGenResultBox = $('<div/>').attr({'id':'citeGenResult','style':'display:none;'}).append(wgCiteGenLang['generated-template']+' ').append(citeGenOutputBox),
		
					citeGenFormSection = $('<div class="toolbar section" id="citeGenToolbar" />').append(citeGenForm).append(citeGenResultBox);
				$('.section-advanced').append(citeGenFormSection);
				$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
					'section': 'advanced',
					'group': 'insert',
					'tools': {
						'cite-gen': {
							label: wgCiteGenLang['cite-gen-label'],
							type: 'button',
							icon: 'http://upload.wikimedia.org/wikipedia/commons/8/81/Wikieditor-reference.png',
							action: {
								type: 'callback',
								execute: citeGen.showHide
							}
						}
					}
				} );
			} );
		}
	}
}