MediaWiki:Gadget-editDropdown.js

From wikishia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
** @description
**  adds a method to mw.libs that allows adding collapsed menu items to the edit-tab
**  a drop-down will be added to the edit-tab while first usage
**  it works like the p-cactions menu in vector
**  If the skin is not vector, the items will be added to the p-tb (toolbox)
** @example
 mw.loader.using('ext.gadget.editDropdown', function() {
	mw.libs.commons.ui.addEditLink(
		'https://toolforge.org', 
		"Wikimedia Toolforge", 
		'p-Toollink', 
		"Wikimedia Toolforge. Driven by Wikimedia Foundation"
	);
 });
**
** @compatibility Works in recent browsers
** @author Rainer Rillke, 2012
** @license GPL v.3
**/

/*global jQuery:false, mediaWiki:false*/
/*jshint curly:false */
(function ($, mw) {
'use strict';
var isInit = false;

mw.libs.commons = mw.libs.commons || {};
mw.libs.commons.ui = mw.libs.commons.ui || {};

mw.libs.commons.ui.isEditDropdownCompat = function () {
	return ('vector' === mw.config.get('skin') && 
		(mw.user.options.get('userjs-sm-ed-dropdown') !== '0'));
};

mw.libs.commons.ui.addEditLink = function (href, text, id, tooltip, accesskey, nextnode, altPortletId) {
	if (!altPortletId)
		altPortletId = 'p-tb';

	var addToToolbox = function () {
		return mw.util.addPortletLink(altPortletId, href, text, id, tooltip, accesskey, nextnode);
	};
	if (mw.libs.commons.ui.isEditDropdownCompat()) {
		if (!isInit)
			prepareVector();
		if (!isInit) {
			return addToToolbox();
		}
		return mw.util.addPortletLink('p-edit', href, text, id, tooltip, accesskey);
	} else {
		return addToToolbox();
	}
};

var prepareVector = function () {
	var $caEdit = $('#ca-edit'),
		$editMenu = $('<div>', {
				'class': 'vector-menu vector-menu-dropdown vector-menu-dropdown-noicon editMenu',
				id: 'p-edit'
			}),
		$ul = $('#p-cactions').find('ul').clone().text(''),
		isRTL = $('body').hasClass('rtl');

	// Check whether there is an edit-tab
	if (!$caEdit.length) return;

	// Add link to edit menu
	$caEdit.children('a').first()
		.appendTo($editMenu);

	// Create the drop-down and append it to our menu
	$('<div>', {
		'class': 'vector-menu-content'
	}).append($ul).appendTo($editMenu);

	// Finally add our menu to the edit-button
	var addMethod = isRTL ? 'prepend' : 'append';
	$caEdit[addMethod]($editMenu);
	$caEdit.children('a').css({ 'background-image': 'none' });

	// If we are on file pages, prefill the menu with the "upload a new version"-link
	var $reUpload = $('#mw-imagepage-reupload-link');
	if ($reUpload.length) {
		mw.util.addPortletLink('p-edit',
			$reUpload.find('a').attr('href'),
			$reUpload.find('a').first().text(),
			'p-reupload');
	}
	isInit = true;
};

})(jQuery, mediaWiki);