User:Minh Nguyen/vote.js

From OpenStreetMap Wiki
Jump to navigation Jump to search

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.
$( function () {
	// If any reply button replies to the entire thread, label it as a voting action.
	$( '[data-mw-thread-id$="-Voting"] .ext-discussiontools-init-replylink-reply' ).text( 'vote' );
} );

function makeVoteTool( vote, title, icon ) {
	//Create and register command
	var template = [ {
		type: 'mwTransclusionBlock',
		attributes: {
			mw: {
				parts: [ {
					template: {
						target: {
							href: 'Template:Vote',
							wt: 'vote'
						},
						params: {
							1: {
								wt: vote
							}
						}
					}
				} ]
			}
		}
	}, {
		type: '/mwTransclusionBlock'
	} ];

	ve.ui.commandRegistry.register(
		new ve.ui.Command( vote, 'content', 'insert', {
			args: [ template, false, true ],
			supportedSelections: [ 'linear' ]
		} )
	);

	//Create and register wikitext command
	if ( ve.ui.wikitextCommandRegistry ) {
		ve.ui.wikitextCommandRegistry.register(
			new ve.ui.Command( vote, 'mwWikitext', 'wrapSelection', {
				args: [ '{{vote|', '}}', vote ],
				supportedSelections: [ 'linear' ]
			} )
		);
	}

	//Create and register tool
	function VoteTool() {
		VoteTool.parent.apply( this, arguments );
	}
	OO.inheritClass( VoteTool, ve.ui.MWTransclusionDialogTool );

	VoteTool.static.name = vote;
	VoteTool.static.group = 'vote';
	VoteTool.static.title = title;
	VoteTool.static.icon = icon;
	VoteTool.static.commandName = vote;
	
	ve.ui.toolFactory.register( VoteTool );
}

// Initialize
mw.hook( 've.loadModules' ).add( function( addPlugin ) {
	addPlugin( () => {
		makeVoteTool( 'yes', 'Support', 'add' );
		makeVoteTool( 'no', 'Oppose', 'subtract' );
		makeVoteTool( 'abstain', 'Abstain', 'feedback' );
	} );
} );

mw.loader.using( [ 'ext.visualEditor.mediawiki' ] ).then( function() {
	ve.init.mw.targetFactory.on( 'register', function ( name, target ) {
		if ( name !== 'discussionTools' ) return;
		
		target.static.toolbarGroups.unshift( {
			name: 'vote',
			label: 'Vote',
			type: 'list',
			indicator: 'down',
			include: [ 'yes', 'no', 'abstain' ],
		} );
	} );
} );