MediaWiki:Gadget-lib-toolbar.js

Z Wikiźródeł, wolnej biblioteki

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)
		 * add_style - an additional style 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 ($.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) == -1) {
				return;
			}

			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 image1 = document.createElement( 'img' );
				image1.alt = button.alt ? button.alt : title;
				image1.title = title;
				image1.className = "tool tool-button";
				image1.setAttribute('role', 'button');

				if ( button.add_style ) {
					image1.style.cssText = button.add_style;
				}

				if ( button.id ) {
					image1.setAttribute('rel', button.id);
				}

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

				// A section of the button
				var section;
				if ( button.section ) {
					var sections1 = $( toolbar ).find( "div.section-" + button.section );
					if ( sections1.length ) {
						section = sections1[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 sections2 = $( toolbar ).find( "div.section-main" );
						if ( sections2.length ) {
							section = sections2[0];
						}
					}
					group = document.createElement( 'div' );
					group.className = 'group group-' + groupName;
					group.setAttribute('rel', groupName);

					section.appendChild( group );
				}
				
				if ( button.href ) {
					var link1 = document.createElement( 'a' );
					link1.href = button.href;
					link1.appendChild( image1 );
					group.appendChild( link1 );
				} else {
					group.appendChild( image1 );
				}

				if ( button.oncreate ) {
					button.oncreate( image1 );
				}
				return;
			}
			// Old toolbar
			toolbar = document.getElementById( 'toolbar' );
			if ( toolbar ) {
				var image2 = document.createElement( 'img' );
				image2.alt = button.alt ? button.alt : title;
				image2.title = title;
				image2.className = 'mw-toolbar-editbutton';
				if ( button.id ) {
					image2.id = button.id;
				}

				if ( button.oldIcon ) {
					image2.src = button.oldIcon;
				} else if ( button.icon ) {
					image2.src = button.icon;
				}
				image2.style.cursor = 'pointer';

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

				if ( button.href ) {
					var link2 = document.createElement( 'a' );
					link2.href = button.href;
					link2.appendChild( image2 );
					toolbar.appendChild( link2 );
				} else {
					toolbar.appendChild( image2 );
				}
				if ( button.oncreate ) {
					button.oncreate( image2 );
				}
				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 ($.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) == -1) {
				return;
			}

			if ( this.wikieditor ) {
				var modules = [ 'ext.wikiEditor' ];
				if (mw.config.get('wgNamespaceNumber') == 100) {
					modules.push('ext.proofreadpage.page.edit');
				}
				mw.loader.using( modules, function() {
					addButtons();
				} );
			} else {
				mw.loader.using( "ext.gadget.LegacyToolbar2006", function() {
					addButtons();
				} );
			}
		}
	};

	toolbarGadget.init();
}