User:Gryllida/js/addWikiLinks-0.1.js

/* Author : Svetlana Tkachenko svetlana@members.fsf.org Licence: GPLv3+ Description: [beta] wikilink buttons to local or wikipedia, or to an exact title match (auto-detect) Version: 0.1 TODO: [ ] suggest titles (autocomplete?) [ ] check for disambiguations (wikipedia) See also 

technical details of customizing the toolbar snippets of code for common additions to the toolbar
 * http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization
 * http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization/Library



mw.loader.using(['mediawiki.api'], function {	"use strict";	var addWikipediaWikilink = function(context){		var selection = context.$textarea.textSelection( 'getSelection' );		var api = new mw.Api;		var prefix = ;		var postfix = ;        var wikiUrl = "//en.wikipedia.org/w/api.php?action=query&titles="+selection+"&format=json";        $.ajax(wikiUrl, { dataType: "jsonp", success: function( data ) { if(data.query.pages['-1']){// page does not exist locally prefix = '{' + '{W||'; postfix = '}}'; encapsulateMyLink(prefix, postfix, 4); } else {// page exists locally prefix = '{' + '{W|'; postfix = '}}'; encapsulateMyLink(prefix, postfix, 0); }           }        });	};	var addLocalWikilink = function(context){		var selection = context.$textarea.textSelection( 'getSelection' );		var api = new mw.Api;		var prefix = ;		var postfix = ;		api.get({ action: 'query', titles: selection }).done(function(data){ if(data.query.pages['-1']){// page does not exist locally prefix = '|';				postfix = ''; encapsulateMyLink(prefix, postfix, 2); } else {// page exists locally prefix = ;				postfix = ; encapsulateMyLink(prefix, postfix, 0); }		});	};	var encapsulateMyLink= function(prefix, postfix, offset){		var cursorPos = $( '#wpTextbox1' ).textSelection( 'getCaretPosition', { startAndEnd: true } );		$( '#wpTextbox1' ).focus;       $.wikiEditor.modules.toolbar.fn.doAction( $( '#wpTextbox1' ).data( 'wikiEditor-context' ), {				'type': 'encapsulate', 'options': { 'pre': prefix, 'peri': '', 'post': postfix }           },            $( '#wpTextbox1' ) );		$( '#wpTextbox1' ).textSelection( 'setSelection', { start: cursorPos[ 0 ]+offset, end: cursorPos[ 0 ]+offset} );	}	var addSmartWikilink = function(context){		var selection = context.$textarea.textSelection( 'getSelection' );		var api = new mw.Api;		var prefix = ;		var postfix = ;		// https://en.wikipedia.org/w/api.php?action=query&titles=User:Grjhyllida&format=json		// query.pages['-1']		api.get({ action: 'query', titles: selection }).done(function(data){ if(data.query.pages['-1']){ // page does not exist locally var wikiUrl = "//en.wikipedia.org/w/api.php?action=query&titles="+selection+"&format=json"; $.ajax(wikiUrl, {		           dataType: "jsonp",		            success: function( data ) {						if(!data.query.pages['-1']){ // exists at wikipedia							prefix = '{' + '{W|';							postfix = '}}';							encapsulateMyLink(prefix, postfix, 0);						} else {							encapsulateMyLink('?', '', 0);						}		            }		        }); } else { prefix = ; // page exists locally				postfix = ; encapsulateMyLink(prefix, postfix, 0); }		});	};	// == WikiEditor codes ==	var customizeToolbar = function {		// add buttons for W and 		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'main', 'group': 'insert', 'tools': { 'addWPLink': { label: 'Add a wikipedia wikilink', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/8/8a/Wikilinkicon-p.png', 'action': { 'type': 'callback', execute: function(context){ addWikipediaWikilink(context); }					},				},				'addWNLink': { label: 'Add a local wikilink', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/d/d3/Wikilinkicon-n.png', 'action': { 'type': 'callback', execute: function(context){ addLocalWikilink(context); }					},				}			}		} );		// remove the "add a or external link" button		$( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar',{ section: 'main', group: 'insert', tool: 'link' } );		// remove reference button		$( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar',{ section: 'main', group: 'insert', tool: 'reference' } );		// add a smart wikilink button		$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', { 'section': 'main', 'group': 'insert', 'tools': { 'addSmartWikiLink': { label: 'Add a smart wikilink', type: 'button', icon: '//upload.wikimedia.org/wikipedia/commons/thumb/7/7b/Link-icon.svg/22px-Link-icon.svg.png', action: { type: 'callback', execute: function(context){ addSmartWikilink(context); }					}				}			}		} );	};	/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */	if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {		mw.loader.using( 'user.options' ).then( function { // This can be the string "0" if the user disabled the preference (T54542) if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) { $.when(					mw.loader.using( 'ext.wikiEditor' ), $.ready				).then( customizeToolbar ); }		} );	}	// == WikiEditor codes end ==

});