مستخدم:حبيشان/ve-custom.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

function makeCiteShamelaTool() {

	// Create and register command
	function CiteShamelaCommand() {
		CiteShamelaCommand.parent.call( this, 'CiteShamela' );
	}
	OO.inheritClass( CiteShamelaCommand, ve.ui.Command );

	CiteShamelaCommand.prototype.execute = function ( surface ) {
		var surfaceModel;
		mytext = prompt("نص الشاملة؟");
		var params = {
		    action: "expandtemplates",
		    text: "{{استشهاد بالشاملة|" + mytext + "|expand=json}}",
		    prop: "wikitext",
		    format: "json"
		},
		api = new mw.Api();
		
		api.get( params ).done( function ( data ) {
			var rData = JSON.parse(data.expandtemplates.wikitext)
			if (rData.error) {
				mw.notify( rData.error, { title: 'استشهاد بالشاملة', type:'error'});
			} else {
				surfaceModel = ve.init.target.getSurface().getModel();
				surfaceModel.getFragment().collapseToEnd().insertContent( [ {
					type: 'mwTransclusionInline',
					attributes: {
						mw: {
							parts: [ rData ]
						}
					}
				}]);
			}
		});
		return true;
	};

	ve.ui.commandRegistry.register( new CiteShamelaCommand() );

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

	CiteShamelaTool.static.name = 'CiteShamela';
	CiteShamelaTool.static.group = 'utility';
	CiteShamelaTool.static.title = 'استشهاد بالشاملة';
	CiteShamelaTool.static.icon = 'references';
	CiteShamelaTool.static.commandName = 'CiteShamela';
	CiteShamelaTool.static.autoAddToCatchall = false;
	CiteShamelaTool.static.deactivateOnSelect = false;

	ve.ui.toolFactory.register( CiteShamelaTool );
}

mw.loader.using( [ 'ext.visualEditor.mediawiki' ] ).then( function() {
	function addGroup( target ) {
		target.static.toolbarGroups.push( {
			name: 'mytoolmenu',
			label: 'قائمتي',
			type: 'list',
			indicator: 'down',
			include: [ { group: 'mytoolmenu' } ],
		} );
	}
	for ( var n in ve.init.mw.targetFactory.registry ) {
		addGroup( ve.init.mw.targetFactory.lookup( n ) );
	}
	ve.init.mw.targetFactory.on( 'register', function ( name, target ) {
		addGroup( target );
	} );
} );

// Initialize
mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init' ).then( function () {
	mw.libs.ve.addPlugin( function () {
		return mw.loader.using( [ 'ext.visualEditor.core' ] )
			.then( function () {
				makeCiteShamelaTool();
			} );
	} );
} );