MediaWiki:Gadget-lib-beau.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
function beau$callAPI(query) {
	var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
	var params = [];
	for (var field in query) {
		var value = query[field];
		params.push(field + '=' + encodeURIComponent(value));
	}
	params.push('format=json');
	url += '?' + params.join('&');
	mw.loader.load(url);
}

function importScriptFromWikipedia(page) {
	mw.loader.load('//pl.wikipedia.org/w/index.php?action=raw&ctype=text/javascript&title=' + encodeURIComponent(page));
}

function importStylesheetFromWikipedia(page) {
	mw.loader.load('//pl.wikipedia.org/w/index.php?action=raw&ctype=text/css&title=' + encodeURIComponent(page), 'text/css');
}

(function () {
	var deferredModern = $.Deferred();
	var deferredClassic = $.Deferred();

	/**
	 * Dodaje przycisk na pasek narzędziowy.
	 * @param button Obiekt opisujący przycisk, jego parametry to
	 * title - tytuł przycisku
	 * alt - tekst alternatywny
	 * id - identyfikator przycisku (obiektu img)
	 * href - link
	 * onclick - funkcja wywoływana po naciśnięciu
	 * icon - ikona przycisku
	 * oldIcon - ikona przycisku dla starego paska (domyślnie icon)
	 * newIcon - ikona przycisku dla nowego paska (domyślnie icon)
	 * section - nazwa sekcji, do której przycisk ma zostać dodany
	 * group- nazwa grupy, do której przycisk ma zostać dodany (jeśli takiej nie ma, zostanie stworzona)
	 */
	function addButton(button) {
		deferredModern.done(function () {
			addButtonModern(button);
		});
		deferredClassic.done(function () {
			addButtonClassic(button);
		});
	}

	// Nowy pasek narzędzi
	// FIXME: zrobić to lepiej, jak będzie istniała dokumentacja do API...
	function addButtonModern(button) {
		var toolbar = document.getElementById('wikiEditor-ui-toolbar');
		if (!toolbar) {
			return;
		}

		var title = button.title || '';

		var image = document.createElement('img');
		image.alt = button.alt || title;
		image.title = title;
		image.className = 'tool tool-button';
		image.style.cssText = 'width:22px; height:18px; padding-top:4px';

		if (button.id) {
			image.id = button.id;
		}

		if (button.newIcon) {
			image.src = button.newIcon;
		} else if (button.icon) {
			image.src = button.icon;
		}

		var link = document.createElement('a');
		if (button.href) {
			link.href = button.href;
		}
		if (button.onclick) {
			link.onclick = button.onclick;
		}

		link.appendChild(image);

		// Sekcja nowego przycisku
		var section;
		if (button.section) {
			var sections = $(toolbar).find('div.section-'+button.section).toArray();
			if (sections.length) {
				section = sections[0];
			}
		}

		// Grupa nowego przycisku
		var group;
		var groupName = button.group || 'custom';
		if (!group) {
			var groups = $(section || toolbar).find('div.group-'+groupName).toArray();
			if (groups.length) {
				group = groups[0];
			}
		}
		if (!group) {
			// Jeśli sekcja nie istnieje, umieść grupę w głównej
			if (!section) {
				var sections = $(toolbar).find('div.section-main').toArray();
				if (sections.length) {
					section = sections[0];
				}
			}
			group = document.createElement('div');
			group.className = 'group group-' + groupName;
			group.rel = groupName;

			section.appendChild(group);
		}
		group.appendChild(link);
	}

	// Stary pasek narzędzi
	function addButtonClassic(button) {
		var toolbar = MonobookToolbar.getToolbar();
		if (!toolbar) {
			return;
		}

		var title = button.title || '';

		var image = document.createElement('img');
		image.alt = button.alt || title;
		image.title = title;
		image.className = 'mw-toolbar-editbutton';

		if (button.id) {
			image.id = button.id;
		}

		if (button.oldIcon) {
			image.src = button.oldIcon;
		} else if (button.icon) {
			image.src = button.icon;
		}

		image.style.cursor = 'pointer';

		if (button.onclick) {
			image.onclick = button.onclick;
		}

		if (button.href) {
			var link = document.createElement('a');
			link.href = button.href;
			link.appendChild(image);
			toolbar.appendChild(link);
		} else {
			toolbar.appendChild(image);
		}
	}

	mw.loader.using('user.options', function () {
		$(function ($) {
			// Modern toolbar
			if (mw.user.options.get('usebetatoolbar')) {
				mw.loader.using('ext.wikiEditor', function () {
					deferredModern.resolve();
				});
			} else {
				deferredModern.reject();
			}
			// Classic toolbar
			if (!mw.user.options.get('usebetatoolbar') || mw.user.options.get('gadget-ForceMonobookToolbar')) {
				mw.loader.using('ext.gadget.MonobookToolbar', function () {
					deferredClassic.resolve();
				});
			} else {
				deferredClassic.reject();
			}
		});
	});

	window.toolbarGadget = {
		addButton: addButton
	};

})();