Մասնակից:Xelgen/SAEsections.js

Վիքիդարանից՝ ազատ գրադարանից

Ծանուցում. Հիշելուց հետո կատարված փոփոխությունները տեսնելու համար մաքրեք ձեր զննարկիչի հիշապահեստը. Mozilla / Firefox / Safari՝ Ctrl+Shift+R (Cmd+Shift+R Mac OS X-ում) Konqueror՝ F5 Opera՝ Tools→Preferences ընտրացանկից։ Internet Explorer՝ Ctrl+F5

//This Script simplifies creating sections for later transclusion, based on http://www.mediawiki.org/wiki/Extension:Labeled_Section_Transclusion
//After you Ctrl + Double Clik on single word, will make that word bold and make a section with word's  Proper case form as section label
//If there's more then one word, you can select text and use button at main section if WikiEditor
//Currently it uses doublehashes for sections
//Scrip was created by [[User:Xelgen]] for Proofreading of Soviet Armenian Encylopedia at hy.wikisource.org
//Current version will work only on pages of Soviet Armenia Encylopedia (based on title of the page)
//Please submit bugs, and ideas to my userpage

jQuery( function () {
  //I don't check for Edit action, as event is binded to Edit textarea. This will currently work only for properly titled SAE pages, you can take out second condition, or add ORs if needed for other books in future
  $('#wpTextbox1').dblclick(function(e) {
    if(e.ctrlKey && mw.config.get( 'wgPageName' ).substr(0,66) == 'Էջ:Հայկական_Սովետական_Հանրագիտարան_(Soviet_Armenian_Encyclopedia)_')  
	{
		//alert('control pressed');			
		addSAESection();
    }
		
    //if(e.altKey) alert('alt pressed');
  });
}
);

function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}

var addSAESection = function() {

		var textarea = document.getElementById("wpTextbox1");		
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		var sel = textarea.value.substring(start, end);
		
		if (sel.length<=2) return false; //If selection is empty or ony symbol, then it looks like user mistake, and we stop here
			
		sel = sel.trim(); //Trim whitespaces		
		sel = sel.replace("ԵՎ", "և"); //This should solve Ev issue, with few problems
		
		//sectname = sel.substring(0,1) + sel.substr(1).toLocaleLowerCase();
		sectname = toProperCase(sel);
		
		insertTags("\r\n## " + sectname + " ##\r\n'''","'''",'');
};

//We're adding button here
var customizeToolbar = function() {

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'main',
        'group': 'insert',
        'tools': {
                'smile': {
                        label: 'Բաժին', // or use labelMsg for a localized label, see above
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/thumb/0/02/Oxygen480-actions-irc-join-channel.svg/22px-Oxygen480-actions-irc-join-channel.svg.png',
                        action: {
							type: 'callback',
								execute: function(context){
									addSAESection();
							} 
						}
                }
        }
} );
};
 
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar . . . */
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) !== -1 && mw.config.get( 'wgPageName' ).substr(0,66) == 'Էջ:Հայկական_Սովետական_Հանրագիտարան_(Soviet_Armenian_Encyclopedia)_') {
        mw.loader.using( 'user.options', function () {
                if ( mw.user.options.get('usebetatoolbar') ) {
                        mw.loader.using( 'ext.wikiEditor', function () {
                                $(document).ready( customizeToolbar );
                        } );
                }
        } );
}