Jump to content

User:Perhelion/WikiEditorEmoticons.js

From Meta, a Wikimedia project coordination wiki

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
* Insert WikiEditor (sMirC) emoticons buttons
* @created 2014-08-16
* @source [[mw:Extension:WikiEditor/Toolbar customization]], [[c:SMirC]]
* @version 0.2.3 (22:24, 5 June 2018 (UTC))
* @author [[User:Perhelion]], 2014
* @license released in the public domain
* @ToDo:
* Classic toolbar support?
* VE support?
* @required modules: mw.user, wikiEditor
**/
/* global jQuery, mediaWiki*/
/* jshint eqeqeq:true, forin:false, laxbreak:true, multistr:true,
trailing:true, undef:true, unused:true, white:false, smarttabs:true */
( function ( $, mw ) {
'use strict';

// $( '#wpTextbox1' ).wikiEditor( 'removeFromToolbar', { 'section': 'emoticons' }); // Remove
// window.customSmilies = [':-)',';-)',':-D',':-S','X-)','mm','lol','doh','=>'];

// Pool of available smilies
var smilyPool = {
	':-)': [ '6/69', 'smile' ],
	';-)': [ 'e/ec', 'wink' ],
	':-D': [ 'e/e3', 'beam' ],
	':-S': [ '8/8c', 'dunno' ],
	'X-)': [ '6/66', 'grin' ],
	'=>': [ '9/90', 'happy' ],
	'lol': [ '5/5a', 'laugh' ],
	'doh': [ 'a/a2', 'doh' ],
	'mm': [ '0/00', 'mmm' ],
	'-.-': [ '6/6d', 'bored' ],
	'"-:': [ '0/0b', 'whistle' ],
	'^_^': [ '9/91', 'chuckle' ],
	'B)': [ '0/08', 'cool' ],
	'ʕ•ᴥ•ʔ': [ 'e/e3', 'bearhug' ],
	'0:)': [ '3/3f', 'angel' ],
	'3:D': [ '4/40', 'devil' ],
	'*,,*': [ 'c/c2', 'vamp' ],
	'(.V.)': [ 'b/bf', 'alien' ],
	'☠': [ 'b/b3', 'dead' ],
	'@_,@': [ 'b/b3', 'stoned' ],
	'(゚*゚)': [ '5/56', 'puke' ],
	'(°₀°)': [ '5/54', 'jealous' ],
	'>:[]': [ '0/0e', 'angry' ],
	'♨': [ '0/0d', 'hot' ],
	':-{}': [ '9/93', 'freezing' ],
	'x_x': [ '3/3d', 'spent' ],
	'hi': [ '5/5a', 'hi' ],
	'stop': [ '4/47', 'beware' ],
	'-<-@': [ '6/62', 'congrats' ],
	'*<:o)': [ 'f/f5', 'party' ],
	'yes': [ '1/1f', 'thumbsup' ],
	'no': [ 'c/c8', 'thumbsdown' ],
	'O_O': [ '0/0c', 'shock' ],
	'°o°': [ '5/5f', 'goggle' ],
	'8-O': [ 'e/e8', 'scream' ],
	// 35
	':-(': [ '3/39', 'sad' ],
	':-|': [ 'd/d5', 'medium' ],
	'8-(': [ 'a/aa', 'puppyeyes' ],
	'8)': [ '6/66', 'rolleyes' ],
	'(~_~)': [ 'e/e3', 'unnerved' ],
	';,(': [ 'a/ae', 'cry' ],
	'<3': [ 'b/b6', 'love' ],
	':-*': [ '8/80', 'kiss' ],
	'???': [ '3/35', 'what' ],
	'zZz': [ '4/4b', 'zzz' ],
	';-/': [ 'c/c2', 'sceptic' ],
	':-x': [ '5/58', 'silent' ],
	':-P': [ 'c/c3', 'tongue' ],
	'%Þ': [ '1/1e', 'crazy' ],
	'<3Þ': [ '6/64', 'horny' ],
	'$Þ': [ 'e/ec', 'greedy' ],
	'●^_^●': [ '0/00', 'shy' ],
	'●°.°●': [ '7/7f', 'embarassed' ],
	'8-)': [ 'd/d2', 'nerd' ],
	'8-°': [ 'd/d8', 'wasntI' ],
	'8-[': [ '8/85', 'worry' ],
	'o_O': [ '8/8b', 'wtf' ],
	'(-°-)': [ '1/17', 'martial' ],
	'xmas': [ 'a/ab', 'xmas' ],
	'C(_)': [ '3/33', 'coffeebreak' ],
	'm(': [ '9/95', 'facepalm' ],
	'daisy': [ '2/23', 'daisy' ],
	'☀': [ '5/5d', 'sunny' ],
	'☁': [ '3/37', 'cloudy' ],
	'☂': [ 'd/d2', 'rainy' ],
	'☈': [ '2/27', 'thundery' ],
	'☃': [ '9/9e', 'snowy' ],
	'≈': [ '5/55', 'hazy' ],
	'\\S/': [ 'd/d3', 'super' ],
	'wine': [ '0/07', 'wine' ],
	'palm': [ '9/90', 'palm' ],
	'(_*_)': [ '1/10', 'ass' ]
	},
	specialSmilyPool = [ // non sMirc
		[ 'e/e0', 'Thumbs_up_icon', '=b' ] // 👍
	],
	$toolbar,
	smilies = [];

// Add smilies to the toolbar in the specified order and count
var order = window.customSmilies;

if ( $.isArray( order ) ) {
	for ( var i = 0, l = order.length; i < l; i++ ) {
		var smilie = smilyPool[ order[ i ] ];
		if ( !smilie ) { throw new Error( 'Unknown smilie: ' + order[ i ] ); } else { // Dict to array
			smilie.push( order[ i ] );
			smilies.push( smilie );
		}
	}
} else { // Dict object to array
	for ( var k in smilyPool ) {
		smilyPool[ k ].push( k );
		smilies.push( smilyPool[ k ] );
	}
}

var cUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/', // Commons
	/** sMirC emoticons only **/
	s = 'SMirC-',
	sMList = {},
	sRlen = Math.max( Math.ceil( smilies.length / 2 ), 35 ), // Max 35 preferred
	sRo = [ '¹', '²', '', '' ], // ,'³','⁴' Max 2 rows supported?
	sRi = 0, // smilie row index
	fns = '[[' + mw.config.get( 'wgFormattedNamespaces' )[ 6 ] + ':';

if ( smilies.length > sRlen ) {
	addSmilie( sRo[ sRi ], smilies.splice( 0, sRlen ), s );
	sRi++;
}
if ( smilies.length ) {
	addSmilie( sRo[ sRi ], smilies, s );
	sRi++;
}
if ( specialSmilyPool.length ) // add special smilie
	addSmilie( sRo[ sRi ], specialSmilyPool, '' );

/**
* Adds smilies to toolbar object (make buttons).
*
* @param      {string}  gp      { group/row name suffix }
* @param      {array}   sL      { list of smilies }
* @param      {string}  s       { group name }
*/
function addSmilie( gp, sL, s ) {
	var sb = {}; // Smilie buttons
	for ( var i = 0, l = sL.length; i < l; i++ ) {
		// for (var i in sL) {
		var id = s + sL[ i ][ 1 ];
		var k = sL[ i ][ 2 ];
		var src = cUrl + sL[ i ][ 0 ] + '/' + id + '.svg/22px-' + id + '.svg.png';
		sb[ k ] = {
			label: k,
			type: 'button',
			icon: src,
			action: {
				type: 'encapsulate',
				options: { pre: fns + id + '.svg|x20px|' + k + ']]' }
			}
		};
	}
	sMList[ gp ] = {
		label: gp,
		tools: sb
	};
}

function wikiEditorEmoticons( $toolbar ) { // make to toolbar
	$toolbar = $toolbar[0]? $toolbar : $( '#wpTextbox1' );
	if ( mw.libs.wikiEditorEmoticons instanceof Object || !$toolbar[0] ) return; // load only once
	$toolbar.wikiEditor( 'addToToolbar', {
		sections: {
			emoticons: {
				type: 'toolbar',
				// 'deferLoad': true,
				groups: sMList
			}
		}
	} );
	// made section button (hack)
	$( '#wikiEditor-ui-toolbar .tabs .tab-emoticons a' ).append( $( '<img>' )
		.attr( {
			'src': cUrl + '5/59/Cute_Ball_-_Games.png/22px-Cute_Ball_-_Games.png',
			'width': 22,
			'height': 22,
			'title': 'Emoticons',
			'rel': s,
			'role': 'button',
			'class': 'tool tool-button'
		} ) );
	// made bar wider (hack)
	if ( $( document ).width() < 1100 )	{
		mw.loader.using( 'mediawiki.util', function () {
			mw.util.addCSS( '.section-emoticons .label {display:none}' +
			'.section-emoticons {width:900px;margin-left:-5px}' +
			'#content {margin-left:140px; margin-right:-54px; padding: 9px}' +
			'#mw-panel {margin-left:-22px}' );
		} );
	}
	mw.libs.wikiEditorEmoticons = wikiEditorEmoticons;
}

function handleQueue( $toolbar ) {
	if ( $.fn.wikiEditor ) { wikiEditorEmoticons( $toolbar ); }
	else {
		$toolbar.on( 'wikiEditor-toolbar-doneInitialSections', function () {
			wikiEditorEmoticons( $toolbar ); }
		);
	}
}

// Check if view is in edit mode and that the required modules are available. Then, customize the toolbar…
$( function () {
	var ns = mw.config.get( 'wgNamespaceNumber' );
	if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) !== -1 ) &&
	( ns % 2 === 1 || [ 2, 4 ].indexOf( ns ) !== -1 ) &&
	mw.config.get( 'wgPageContentModel' ) === 'wikitext' ) {
		mw.loader.using( 'user.options', function () {
			if ( mw.user.options.get( 'usebetatoolbar' ) ) {
				$toolbar = $( '#wpTextbox1' );
				if ( $toolbar[0] ) {
					handleQueue( $toolbar );
				} else mw.loader.using( 'ext.wikiEditor' ).always( function () {
					handleQueue( $( '#wpTextbox1' ) );
				} );
			// mw.hook( 'resourceloader.loadEnd' ).add( wikiEditorEmoticons ); -- removed somewhere in "1.32.0-wmf.6"
			}
		} );
	}
} );
}( jQuery, mediaWiki ) );