User:Jberkel/Gadgets/Test.js

'use strict'; // mw.log.warn("gadget loading");

function el(tag, child, attr, events) { var node = document.createElement(tag); if (child) { if (typeof child !== 'object') child = [child]; for (var i = 0; i < child.length; ++i) { var ch = child[i]; if ((ch === void(null)) || (ch === null)) continue; else if (typeof ch !== 'object') ch = document.createTextNode(String(ch)); node.appendChild(ch); }	}	if (attr) for (var key in attr) { node.setAttribute(key, String(attr[key])); }	if (events) for (var key in events) { node.addEventListener(key, events[key], false); }	return node; }

var uiQuery, uiTitle, uiTabs, uiTabLabels, uiForm; var uiMain = el('div', [	uiTitle = el('div', [ el('strong', "TT"), el('a', "hide", {			'href': 'javascript:void(0);',			'class': 'hide-link'		}, {			'click': function {				uiMain.style.display = 'none';			}		}) ], { 'class': 'title' }),	uiForm = el('form', [ el('fieldset', [			el('legend', "Search for example sentences"),			uiQuery = el('input', null, { 'type': 'text', 'placeholder': "term", 'class': 'search-terms', 'value': (function {					var m;					if ([0, 1, 114].indexOf(mw.config.get('wgNamespaceNumber')) !== -1) // main, Talk, Citations						return '"' + mw.config.get('wgTitle') + '"';					if (mw.config.get('wgNamespaceNumber') === 100)						if (m = /[^/]+\/(.*)/.exec(mw.config.get('wgTitle')))							return m[1];					if (mw.config.get('wgCanonicalSpecialPageName') === 'Search')						return '"' + document.getElementById('searchText').value + '"';					return '';				}) }),			el('input', null, { 'type': 'submit', 'value': "search" })		])	], {		'action': 'javascript:void(0);' }, {		'submit': function { var query = uiQuery.value; console.log("search for "+query); jQuery.ajax({						url: 'https://tatoeba.org/eng/sentences/search?query='+query+'&from=por',						dataType: 'html',					}).done(function (data) {						console.log("data");					}); }	}),	uiTabLabels = el('ul', null, { 'class': 'tab-labels', 'style': 'display: none;' }),	uiTabs = el('div', null, { 'class': 'tabs', 'style': 'display: none;' }) ], {	'class': 'jberkel-tt',	'style': 'display: none; position: fixed;' }); jQuery(uiMain).draggable({	handle: uiTitle }).resizable({	alsoResize: uiTabs });

document.body.appendChild(uiMain);

var link = mw.util.addPortletLink(mw.config.get('skin') === 'vector' ? 'p-views' : 'p-cactions',	'javascript:void(0);', 'TT', 'p-Jberkel-tt', "TT", '9' );

link.addEventListener('click', function {	uiMain.style.display = '';	uiQuery.focus; }, false);