মিডিয়াউইকি:Gadget-Prosesize.js

উইকিবই থেকে

লক্ষ্য করুন: প্রকাশ করার পর, পরিবর্তনগুলো দেখতে আপনাকে আপনার ব্রাউজারের ক্যাশে পরিষ্কার করার প্রয়োজন হতে পারে।

  • ফায়ারফক্স / সাফারি: পুনরায় লোড-এ ক্লিক করার সময় শিফট টিপে ধরে রাখুন, অথবা হয় Ctrl-F5 বা Ctrl-R টিপুন (ম্যাকে ⌘-R টিপুন)
  • গুগল ক্রোম: Ctrl-Shift-R (ম্যাকে ⌘-Shift-R) টিপুন
  • ইন্টারনেট এক্সপ্লোরার / এজ: Ctrl ধরে রাখা অবস্থায় Refresh-এ ক্লিক করুন, অথবা Ctrl-F5 টিপুন
  • অপেরা: Ctrl-F5 টিপুন।
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === WARNING: GLOBAL GADGET FILE ===                      |
 * |                  Changes to this page affect many users.                    |
 * | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
 * |_____________________________________________________________________________|
 *
 */
/**
 * Prosesize
 * Documentation at en.wikipedia.org/wiki/Wikipedia:Prosesize
 * Rewrite of [[User:Dr_pda/prosesize.js]].
*/
'use strict';
function convert(str) {
    var mystr = str.toString();
    var outj; // javascript escaped hex
    var outj1;
    var be = new Array();
    be['1'] = "\u09E7";
    be['2'] = "\u09E8";
    be['3'] = "\u09E9";
    be['4'] = "\u09EA";
    be['5'] = "\u09EB";
    be['6'] = "\u09EC";
    be['7'] = "\u09ED";
    be['8'] = "\u09EE";
    be['9'] = "\u09EF";
    be['0'] = "\u09E6";
    be[' '] = '';
    be['-'] = '-';
    outj1 = "";
    for (var i = 0; i < mystr.length; i++) {
        var ch = mystr.substr(i, 1);
        outj = be[ch];
        outj1 += outj;
    }
    return outj1;

}
( function () {
	function sizeFormatter( size ) {
		if ( size > 10240 ) {
			return ( Math.round( size / 1024 ) + '&nbsp;কেবি' );
		} else {
			return ( size + '&nbsp;বাইট' );
		}
	}

	function sizeElement( id, text, size, extraText ) {
		return $( '<li>' )
			.prop( 'id', id )
			.append( $( '<b>' ).text( text ) )
			.append( ' ' + sizeFormatter( size ) + ( extraText || '' ) );
	}

	function getRevisionSize( proseValue ) {
		var Api = new mw.Api();
		function appendResult( size ) {
			var wikiValue = sizeElement( 'wiki-size', 'উইকি পাঠ্য:', convert(size) );
			proseValue.before( wikiValue );
		}
		if ( mw.config.get( 'wgAction' ) === 'submit' ) {
			// Get size of text in edit box
			// eslint-disable-next-line no-jquery/no-global-selector
			appendResult( $( '#wpTextbox1' ).textSelection( 'getContents' ).length );
		} else if ( mw.config.get( 'wgIsArticle' ) ) {
			// Get revision size from API
			Api.get( {
				action: 'query',
				prop: 'revisions',
				rvprop: 'size',
				revids: mw.config.get( 'wgRevisionId' ),
				formatversion: 2
			} ).then( function ( result ) {
				appendResult( result.query.pages[ 0 ].revisions[ 0 ].size );
			} );
		}
	}

	function getFileSize( proseHtmlValue ) {
		// HTML document size not well defined for preview mode or section edit
		if ( mw.config.get( 'wgAction' ) !== 'submit' ) {
			$.get( location ).then( function ( result ) {
				var fsize = sizeElement( 'total-size', 'HTML নথির আকার:', convert(result.length) );
				proseHtmlValue.before( fsize );
			} );
		}
	}

	function getLength( id ) {
		var i;
		var textLength = 0;
		for ( i = 0; i < id.childNodes.length; i++ ) {
			if ( id.childNodes[ i ].nodeType === Node.TEXT_NODE ) {
				textLength += id.childNodes[ i ].nodeValue.length;
			} else if (
				id.childNodes[ i ].nodeType === Node.ELEMENT_NODE &&
				( id.childNodes[ i ].id === 'coordinates' || id.childNodes[ i ].className.indexOf( 'emplate' ) !== -1 )
			) {
				// special case for {{coord}} and {{fact}}-like templates
				// Exclude from length, and don't set background yellow
				id.childNodes[ i ].className += ' prosesize-special-template';
			} else {
				textLength += getLength( id.childNodes[ i ] );
			}
		}
		return textLength;
	}

	function getRefMarkLength( id, html ) {
		var i;
		var textLength = 0;
		for ( i = 0; i < id.childNodes.length; i++ ) {
			if (
				id.childNodes[ i ].nodeType === Node.ELEMENT_NODE &&
				id.childNodes[ i ].className === 'reference'
			) {
				textLength += ( html ) ?
					id.childNodes[ i ].innerHTML.length :
					getLength( id.childNodes[ i ] );
			}
		}
		return textLength;
	}

	function main() {
		var proseValue, refValue, refHtmlValue, proseHtmlValue;
		// eslint-disable-next-line no-jquery/no-global-selector
		var parserOutput = $( '.mw-parser-output' );
		// eslint-disable-next-line no-jquery/no-global-selector
		var prevStats = $( '#document-size-stats' );
		// eslint-disable-next-line no-jquery/no-global-selector
		var prevHeader = $( '#document-size-header' );
		var proseSize = 0;
		var proseSizeHtml = 0;
		var refmarksize = 0;
		var refmarkSizeHtml = 0;
		var wordCount = 0;
		var refSize = 0;
		var refSizeHtml = 0;
		var header = $( '<span>' )
			.prop( 'id', 'document-size-header' )
			.html( 'নথির পরিসংখ্যান:' );
		var output = $( '<ul>' )
			.prop( 'id', 'document-size-stats' );
		var combined = $( '<div>' )
			.prop( 'id', 'document-size' )
			.append( header, output );
		if ( parserOutput.length === 0 ) {
			return;
		}
		if ( prevStats.length ) {
			// If statistics already exist, turn them off and remove highlighting
			prevStats.remove();
			prevHeader.remove();
			parserOutput.children( 'p' ).removeClass( 'prosesize-highlight' );
		} else {
			// Calculate prose size and size of reference markers ([1] etc)
			parserOutput.children( 'p' ).each( function () {
				$( this ).addClass( 'prosesize-highlight' );
				proseSize += getLength( this );
				proseSizeHtml += this.innerHTML.length;
				refmarksize += getRefMarkLength( this, false );
				refmarkSizeHtml += getRefMarkLength( this, true );
				wordCount += this.innerHTML.replace( /(<([^>]+)>)/ig, '' ).split( ' ' ).length;
			} );

			// Calculate size of references (i.e. output of <references/>)
			parserOutput.find( 'ol.references' ).each( function () {
				refSize = getLength( this );
				refSizeHtml = this.innerHTML.length;
			} );

			proseValue = sizeElement( 'prose-size', 'গদ্যের আকার (কেবল পাঠ্য):', convert(proseSize - refmarksize), ' (' + convert(wordCount) + 'টি শব্দ) "পঠনযোগ্য গদ্যের আকার"' );
			refValue = sizeElement( 'ref-size', 'তথ্যসূত্র (কেবল পাঠ্য):', convert(refSize + refmarksize) );
			refHtmlValue = sizeElement( 'ref-size-html', 'তথ্যসূত্র (সকল HTML কোডসহ):', convert(refSizeHtml + refmarkSizeHtml) );
			proseHtmlValue = sizeElement( 'prose-size-html', 'গদ্যের আকার (সকল HTML কোডসহ):', convert(proseSizeHtml - refmarkSizeHtml) );
			output.append( proseHtmlValue, refHtmlValue, proseValue, refValue );
			parserOutput.prepend( combined );
			getFileSize( proseHtmlValue );
			getRevisionSize( proseValue );
		}
	}

	if (
		!mw.config.get( 'wgCanonicalSpecialPageName' )
	) {
		$.ready.then( function () {
			/**
			 * Depending on whether in edit mode or preview/view mode,
			 * show the approppiate response upon clicking the portlet link
			*/
			var func, $portlet, notEnabled = false;
			if (
				mw.config.get( 'wgAction' ) === 'edit' ||
				( mw.config.get( 'wgAction' ) === 'submit' && document.getElementById( 'wikiDiff' ) )
			) {
				notEnabled = true;
				func = function () {
					mw.notify( 'সম্পাদনা মোডে গদ্যের আকার স্ক্রিপ্টটি চালানোর জন্য আপনাকে পাঠ্য প্রাকদর্শন করতে হবে।' );
				};
			} else if ( [ 'view', 'submit', 'historysubmit', 'purge' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
				func = main;
			}
			if ( func ) {
				$portlet = $( mw.util.addPortletLink( 'p-tb', '#', 'পাতার আকার', 't-page-size', 'পাতা ও গদ্যের আকার গণনা করুন' ) );
				if ( notEnabled ) {
					$portlet.addClass( 'prosesize-portlet-link-edit-mode' );
				}
				$portlet.on( 'click', function ( e ) {
					e.preventDefault();
					if ( window.ve && ve.init && ve.init.target && ve.init.target.active ) {
						mw.notify( 'গদ্যের আকার দৃশ্যমান সম্পাদকের সাথে কাজ করে না।' );
					} else {
						func();
					}
				} );
			}
		} );
	}
}() );