Difference between revisions of "MediaWiki:Gadget-Section editor test.js"

From TermiKnowledge
Jump to navigation Jump to search
Line 102: Line 102:
function makeToolbarTools() {
function makeToolbarTools() {
var nsN = mw.config.get('wgNamespaceNumber');
var nsN = mw.config.get('wgNamespaceNumber');
console.log('aaaaa');
if ( nsN != 2 && nsN != 3 ) { // User or User talk
if ( nsN != 2 && nsN != 3 ) { // User or User talk
return;
return;

Revision as of 00:07, 31 December 2021

function makeVisualTools() {
	//Create and register command
	
	var commandName = 'insertSections';
	var title = 'Insert Section';
	
	var url = mw.util.getUrl();
	var page = mw.config.get('wgPageName');
	var namespace = mw.config.get('wgCanonicalNamespace');
	
	var baseLink = url.substr(0, url.length - page.length);
	var pageName = url.substr(url.length - page.length + (namespace.length > 0 ? (namespace.length + 1) : 0));
	
	var template = [];
	
	var sections = [
		'Normative',
		'Research',
		'Press',
		'Comments'
	]
	
	var pageTemplates = {};
	
	var api = new mw.Api();
	var rest = new mw.Rest();
	
	var promises = [];
	
	sections.forEach(function(e){
		//Create template
		template = template.concat([
			{
				type: 'mwTransclusionBlock',
				attributes: {
					mw: {
						parts: [ {
							template: {
								i:0,
								target: {
									href: "./"+e+":"+pageName,
									wt: e+":"+pageName
								}
							},
							params: {}
						} ]
					}
				}
			},
			{ type: '/mwTransclusionBlock' },
			{ type: 'paragraph' },
			{ type: '/paragraph' }
		]);
		//Gather template
		promises.push(rest.get("/v1/page/Template:"+e).then(function(res){
			if (res && res.source)
			{
				pageTemplates[e] = res.source;
			}
		}));
	});
	
	Promise.all(promises).then(function(){
		console.log(pageTemplates);
	});
	
	var InsertSectionsCommand = function(name) {
		InsertSectionsCommand.super.call( this, name );
		this.subcommand = new ve.ui.Command( commandName, 'content', 'insert', {
			args: [ template, false, true ],
			supportedSelections: [ 'linear' ]
		});
	}
	OO.inheritClass( InsertSectionsCommand, ve.ui.Command );
	
	InsertSectionsCommand.prototype.execute = function(surface, args, source) {
		sections.forEach(function(section){
			var tm = pageTemplates[section];
			api.create(section+":"+pageName, {}, tm !== undefined ? tm : 'Section: '+section);
		});
		
		var ret = this.subcommand.execute(surface, args, source);
		
		return ret;
	}
	
	ve.ui.commandRegistry.register(new InsertSectionsCommand(commandName));

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

	SectionTemplater.static.name = 'sectiontemplater';
	SectionTemplater.static.group = 'insert';
	SectionTemplater.static.title = title;
	SectionTemplater.static.commandName = commandName;
	ve.ui.toolFactory.register( SectionTemplater );
}

function makeToolbarTools() {
	var nsN = mw.config.get('wgNamespaceNumber');
	console.log('aaaaa');
	if ( nsN != 2 && nsN != 3 ) { // User or User talk
		return;
	}
	var user = wgTitle.split( '/' )[0];
	var url = wgScript + '?title=Special:UserRights&user=' + encodeURIComponent( user );
	mw.util.addPortletLink( 'p-tb', url, 'User rights', 't-userrights', 'List of user groups for ' + user );
}

makeToolbarTools();

//Initialize
mw.hook( 've.loadModules' ).add( function( addPlugin ) {
	addPlugin( function() {
		return mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwwikitext', 'ext.visualEditor.mwtransclusion' ] )
			.then( function() {
				makeVisualTools();
			} );
	} );
} );