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

From TermiKnowledge
Jump to navigation Jump to search
 
(47 intermediate revisions by the same user not shown)
Line 1: Line 1:
//single install
var sections = [
const singleInstall = false;
'Normative',
'Research',
'Press',
'Comments'
]


if ((!singleInstall || window.SectionCreator) && mw.config.get('wgNamespaceNumber') >= 0) {
var namespace = mw.config.get('wgCanonicalNamespace');
$(function() {
var url = mw.util.getUrl();
var SectionCreator = {
var page = mw.config.get('wgPageName');
install: function() {
 
//add bootstrap
var baseLink = url.substr(0, url.length - page.length);
$.getScript("https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js");
var pageName = url.substr(url.length - page.length + (namespace.length > 0 ? (namespace.length + 1) : 0));
$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css') );
 
function makeVisualTools() {
$('body').append(
if (namespace != 'Term') return;
'<div class="modal" tabindex="-1" role="dialog" id="sectionCreateModal" aria-hidden="true">'+
  '<div class="modal-dialog" role="document">'+
//Create and register command
    '<div class="modal-content">'+
      '<div class="modal-header">'+
var commandName = 'insertSections';
        '<h5 class="modal-title">Modal title</h5>'+
var title = 'Insert Section';
        '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'+
          '<span aria-hidden="true">&times;</span>'+
var template = [];
        '</button>'+
      '</div>'+
var pageTemplates = {};
      '<div class="modal-body">'+
        '<p>Modal body text goes here.</p>'+
var api = new mw.Api();
      '</div>'+
var rest = new mw.Rest();
      '<div class="modal-footer">'+
        '<button type="button" class="btn btn-primary">Save changes</button>'+
var promises = [];
        '<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>'+
      '</div>'+
sections.forEach(function(e){
    '</div>'+
//Create template
  '</div>'+
template = template.concat([
'</div>';
{
);
type: 'mwTransclusionBlock',
attributes: {
var tool = document.getElementById('t-section_creator');
mw: {
if (tool) tool.parentNode.removeChild(tool);
parts: [ {
template: {
//set up toolbox link
i:0,
mw.util.addPortletLink('p-tb', 'javascript:SectionCreator.shout();',
target: {
"Create section", 't-section_creator', null);
href: "./"+e+":"+pageName,
wt: e+":"+pageName
console.log('Section creator installed');
}
},
params: {}
} ]
}
}
},
},
shout: function() {
{ type: '/mwTransclusionBlock' },
console.log('aaaaa');
{ 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;
}
}
};
}));
window.SectionCreator = SectionCreator;
});
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);
});
SectionCreator.install();
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() {
console.log(namespace);
console.log(sections);
if ( namespace == 'Term' ) {
sections.forEach(function(e){
var url = mw.config.get('wgScript') + '/' + encodeURIComponent( e ) + ":" + encodeURIComponent( pageName );
mw.util.addPortletLink( 'p-tb', url+"?veaction=edit", e, 't-tk-sub-'+e.toLowerCase(), e + " subsection" );
mw.util.addPortletLink('p-cactions', url, e, 'ca-tk-'+e.toLowerCase(), e + " subsection" );
});
}
else if (sections.includes(namespace)) {
var url = mw.config.get('wgScript') + '/' + "Term:" + encodeURIComponent( pageName );
mw.util.addPortletLink( 'p-tb', url+"?veaction=edit", "Term", 't-tk-sub-term', "Term page" );
mw.util.addPortletLink('p-cactions', url, "Term", 'ca-tk-term', "Term page" );
}
}
}
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();
} );
} );
} );

Latest revision as of 13:55, 31 December 2021

var sections = [
	'Normative',
	'Research',
	'Press',
	'Comments'
]

var namespace = mw.config.get('wgCanonicalNamespace');
var url = mw.util.getUrl();
var page = mw.config.get('wgPageName');

var baseLink = url.substr(0, url.length - page.length);
var pageName = url.substr(url.length - page.length + (namespace.length > 0 ? (namespace.length + 1) : 0));

function makeVisualTools() {
	if (namespace != 'Term') return;
	
	//Create and register command
	
	var commandName = 'insertSections';
	var title = 'Insert Section';
	
	var template = [];
	
	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() {
	console.log(namespace);
	console.log(sections);
	if ( namespace == 'Term' ) {
		sections.forEach(function(e){
			var url = mw.config.get('wgScript') + '/' + encodeURIComponent( e ) + ":" + encodeURIComponent( pageName );
			mw.util.addPortletLink( 'p-tb', url+"?veaction=edit", e, 't-tk-sub-'+e.toLowerCase(), e + " subsection" );
			mw.util.addPortletLink('p-cactions', url, e, 'ca-tk-'+e.toLowerCase(), e + " subsection" );
		});
	}
	else if (sections.includes(namespace)) {
		var url = mw.config.get('wgScript') + '/' + "Term:" + encodeURIComponent( pageName );
		mw.util.addPortletLink( 'p-tb', url+"?veaction=edit", "Term", 't-tk-sub-term', "Term page" );
		mw.util.addPortletLink('p-cactions', url, "Term", 'ca-tk-term', "Term page" );
	}
}

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();
			} );
	} );
} );