Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
m (→‎Autocomplete/Dropdown list: Try using native style adding for margin-top)
Line 18: Line 18:
 
 
 
//A negative margin on the dropdown list to offset the shift down caused by the position:relative and margin-top of the `body`
 
//A negative margin on the dropdown list to offset the shift down caused by the position:relative and margin-top of the `body`
var bodyMarginTop = $('body').css("margin-top");
+
//var bodyMarginTop = $('body').css("margin-top");
var antiBodyMarginTop = '-'+bodyMarginTop;
+
//var antiBodyMarginTop = '-'+bodyMarginTop;
 
//var TextcompleteCss = mw.util.addCSS(
 
//var TextcompleteCss = mw.util.addCSS(
 
// '.dropdown-menu { margin-top: -'+bodyMarginTop+'; }'
 
// '.dropdown-menu { margin-top: -'+bodyMarginTop+'; }'
Line 58: Line 58:
 
dropdown: {
 
dropdown: {
 
maxCount: 500,
 
maxCount: 500,
style: { 'margin-top':antiBodyMarginTop }
+
style: { 'margin-top':('-'+$('body').css("margin-top"))}
 
}
 
}
 
});
 
});

Revision as of 11:28, 20 January 2018

/* Autocomplete/Dropdown list */
// Won't work with CodeEditor, but that's fine since CodeEditor won't be used for wikitext

//mw.loader.load('https://unpkg.com/textcomplete@0.13.1/dist/textcomplete.min.js');
//^Doesn't wait for it to load nor excecute

//https://api.jquery.com/jQuery.getScript/#success-callback
$.getScript( "https://unpkg.{{Color|com/textcomplete@0.13.1/dist/textcomplete.min.js", function( data, textStatus, jqxhr ) {
	console.log( data ); // Data returned
	console.log( textStatus ); // Success
	console.log( jqxhr.status ); // 200
	console.log( "Load was performed. (Script has been loaded but not necessarily executed)" );
	wikiAutocomplete();
});

function wikiAutocomplete() {
	Textarea = Textcomplete.editors.Textarea;
	
	//A negative margin on the dropdown list to offset the shift down caused by the position:relative and margin-top of the `body`
	//var bodyMarginTop = $('body').css("margin-top");
	//var antiBodyMarginTop = '-'+bodyMarginTop;
	//var TextcompleteCss = mw.util.addCSS(
	//	'.dropdown-menu { margin-top: -'+bodyMarginTop+'; }'
	//); 
	
	// A 'strategy' for Template:Color
	templateColorWikiText = '';
	colors = {};
	//the following is awful to look at, I know.
	$.get( "https://zelda.gamepedia.com/Template:Color?action=raw", function( data ) {
		templateColorWikiText = data; //yes I know using a global variable is lazy and ng....
		templateColorWikiText = templateColorWikiText.split("</includeonly>")[0];
		templateColorWikiText = templateColorWikiText.split("#switch:{{{1\|}}}")[1];
		templateColorWikiText = templateColorWikiText.split("\|#default")[0];
		templateColorWikiText.match(/\|[a-zA-Z0-9 ]*/g).forEach(function(value, index){
			colors[value.slice(1)]='';
		});
	});
	
	window.colorStrategy = {
		id: 'color',
		match: /(){{Color\|([a-zA-Z 0-9+\-\_\|}]*)$/m, //Need the first capture group...
		search: function (term, callback) {
			callback(Object.keys(colors).filter(function (name) {
				return name.startsWith(term);
			}));
		},
		template: function (name) {
			return name;
		},
		replace: function (name) {
			return '{{Color\|' + name + '}}';
		}
	};
	
	var editor = new Textarea(document.getElementById('wpTextbox1'));
	var textcomplete = new Textcomplete(editor, {
		dropdown: {
			maxCount: 500,
			style: { 'margin-top':('-'+$('body').css("margin-top"))}
		}
	});
	textcomplete.register([colorStrategy]);
	
}