MediaWiki:Gadget-lib-toolbar.js

Z Wikicytatów, wolnej kolekcji cytatów

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
/*
 * @author: [[:pl:User:Beau]]
 */

if ( !window.toolbarGadget ) {
	window.toolbarGadget = {
		/** Version of the gadget */
		version: 7,
		/** A status of the gadget */
		ready: false,
		/** Indicates wheter the user has enabled the new toolbar */
		wikieditor: null,
		/** An array of buttons, which were added before a toolbar was constructed */
		buttons: [],
		/**
		 * Adds a button to the toolbar
		 * @param button An object describing the button, its values:
		 * title - a title of the button
		 * alt - an alternative text displayed when image is not loaded
		 * id - an identifier of the button (img)
		 * href - a link
		 * onclick - a callback
		 * icon - an icon of the button
		 * oldIcon - an icon for the old toolbar (by default icon parameter is used)
		 * newIcon - an icon for the new toolbar (by default icon parameter is used)
		 * section - a name of a section to which the button should be added
		 * group - a name of a group to which the button should be added (it will be created if not exists)
		 * oncreate - a callback invoked after the button has been created
		 */
		addButton: function( button ) {
			if ( !this.ready ) {
				this.buttons.push( button );
				return;
			}
			var title = button.title ? button.title : "";

			// New toolbar
			var toolbar = document.getElementById( 'wikiEditor-ui-toolbar' );
			if ( toolbar ) {
				// FIXME: rewrite it using a toolbar api
				var image = document.createElement( 'img' );
				image.alt = button.alt ? button.alt : title;
				image.title = title;
				image.className = "tool tool-button";
				image.style.cssText = "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 );

				// A section of the button
				var section;
				if ( button.section ) {
					var sections = $( toolbar ).find( "div.section-" + button.section );
					if ( sections.length ) {
						section = sections[0];
					}
				}

				// A group of the button
				var group;
				var groupName = button.group ? button.group : 'custom';
				if ( !group ) {
					var groups = $( section ? section : toolbar ).find( "div.group-" + groupName );
					if ( groups.length ) {
						group = groups[0];
					}
				}
				if ( !group ) {
					// If the section does not exist, place the button in main section
					if ( !section ) {
						var sections = $( toolbar ).find( "div.section-main" );
						if ( sections.length ) {
							section = sections[0];
						}
					}
					group = document.createElement( 'div' );
					group.className = 'group group-' + groupName;
					group.rel = groupName;

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

				if ( button.oncreate ) {
					button.oncreate( image );
				}
				return;
			}
			// Old toolbar
			toolbar = document.getElementById( 'toolbar' );
			if ( toolbar ) {
				var image = document.createElement( 'img' );
				image.alt = button.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 );
				}
				if ( button.oncreate ) {
					button.oncreate( image );
				}
				return;
			}
		},

		/** Sets up the gadget */
		init: function() {
			this.wikieditor = mw.user.options.get( 'usebetatoolbar' ) === 1;
			var that = this;

			var addButtons = function() {
					jQuery( document ).ready( function() {
						that.ready = true;
						for ( var i = 0; i < that.buttons.length; i++ ) {
							that.addButton( that.buttons[i] );
						}
						that.buttons = null;
					} );
				};

			if ( this.wikieditor ) {
				mw.loader.using( "ext.wikiEditor", function() {
					addButtons();
				} );
			} else {
				mw.loader.using( "mediawiki.toolbar", function() {
					addButtons();
				} );
			}
		}
	};

	toolbarGadget.init();
}