User:Mxn/PageNumbers.js

function init_setting ( variable_name, cookie_name, init ) { /* Sets JS variable to (in order of preference): 1. previously initialized JS variable value 2. current cookie value 3. provided init parameter 4. false

Then, if cookie was not previously set, set it to current value of JS variable (this bit currently disabled) // get current value of appropriate cookie var cookie_val = mw.cookie.get( cookie_name ); //     some code so that people's cookies will change from digits to boolean values. remove after a week or so. //     in case you are wondering, this is to avoid the mixed use of digits as numbers and as booleans. --Eliyak //       if ( cookie_val === "0" ) { cookie_val = false; mw.cookie.set( cookie_name, false ); } //       else if ( cookie_val === "1" ) { cookie_val = true; mw.cookie.set( cookie_name, true ); }

// If JS variable was not previously initialized in this page load, set it now to the cookie value. self[ variable_name ] = self[ variable_name ] || cookie_val; // If JS variable still has no value, use provided init value. If no init value provided, use false. if ( typeof self[ variable_name ] === "undefined" || self[ variable_name ] === null ) self[ variable_name ] = init || false; // If cookie was not set, set it now to the current value of the JS variable. // disabling for now - cookie should not need to be set if value was initialized but not changed afterwards. // if ( typeof cookie_val === "undefined" ) mw.cookie.set( cookie_name, self[ variable_name ] );

// If JS variable is now the string "false", convert to boolean false (to fix JS confusion where "false" string evaluates to true). if ( self[ variable_name ] === "false" ) self[ variable_name ] = false; }

var layout = function { if ( !self.ws_layouts ) self.ws_layouts = {}; else self.ws_layouts.names = []; var n = 0; for( var key in self.ws_layouts ) { self.ws_layouts[ key ].number = n;	// get number easily in the future self.ws_layouts.names[ n ] = key;	// get name from number easily as well n++; }   n -= 1; function toggle { set_by_number( ( self.ws_layouts[ self.layout_name ].number + 1 ) % n, true ); }   function set_by_number ( number, persist ) { set_by_name( self.ws_layouts.names[ number ], persist ); }   function set_by_name ( name, persist ) { var selected_layout = self.ws_layouts[ name ]; if ( !selected_layout ) return;	// doesn't exist

$.each( selected_layout, function ( selector, style ) { $( selector ).attr( "style", style ); } ); $( "#d-textLayout" ).children( "a" ).html( name );

self.layout_name = name; if ( persist ) mw.cookie.set( "layout", name );

if ( self.proofreadpage_source_href ) pagenumbers.refresh_offsets; }

function init { var name; if ( document.getElementById( "#text-wrap" ) ) return;	// do nothing if we're already set up

// remove all these classes to maintain backwards-compatibility $( ".text, .lefttext, .centertext, .indented-page, .prose" ).removeClass; $( ".mw-content-ltr" ).wrapInner( '    ' );

// get_optlist; var portletLink = mw.util.addPortletLink( "p-do", "#", ws_msg( "layout" ), "d-textLayout" ); $( portletLink ).click( function ( e ) {           e.preventDefault;            toggle;        } );

// If cookie is not set, default layout is first available option. Use index "0" in case layout name is ever changed. init_setting( "layout_name", "layout", "0" );

if ( self.layout_overrides_have_precedence || ! mw.cookie.get( "layout") ) { name = $( "#dynamic_layout_overrider" ).text; }       name = name || self.layout_name; if ( $.isNumeric( name ) ) set_by_number( name, false ); else if ( name ) set_by_name( name, false ); else set_by_number( 0, false ); }   return { init: init }; };

var pagenumbers = function {

// some shared variables to avoid selecting these elements repeatedly var container; var div_pagenumbers; var dp_y; var y_prev; var pagenumbers_collection; var div_ss; var div_highlight;

function toggle_inline { self.proofreadpage_numbers_inline = !layout || !self.proofreadpage_numbers_inline; // toggle inline view, unless layouts are not set up       $( "#d-pageNumbers_inline" ).children( "a" ).html( ws_msg( self.proofreadpage_numbers_inline ? "page_numbers_beside" : "page_numbers_inline" ) ); mw.cookie.set( "pagenums_inline", self.proofreadpage_numbers_inline ); refresh_display; }

var show_params = { display: "", link_text: ws_msg( "hide_page_numbers" ), visible: true }; var hide_params = { display: "none", link_text: ws_msg( "show_page_numbers" ), visible: false };

function toggle_visible { var params = self.proofreadpage_numbers_visible ? hide_params : show_params; pagenumbers_collection.css( "display", params.display ); $( "#d-pageNumbers_visible" ).children( "a" ).html( params.link_text ); self.proofreadpage_numbers_visible = params.visible; mw.cookie.set( "pagenums_visible", params.visible ); }

function pagenum_in { if ( self.proofreadpage_disable_highlighting ) return false; if ( !div_highlight ) return false;	// couldn't find it       var id = this.id.substring( 11 );

var page_span = $( document.getElementById( id ) ); var next = self.pagenum_ml.eq( self.pagenum_ml.index( page_span ) + 1 ); if ( next.length === 0 ) next = div_ss;

var c_os = container.offset;	// we need to use document offsets in case a page break occurs within a positioned element var ps_os = page_span.offset; var n_os = next.offset;

ps_os = { top: ps_os.top - c_os.top, left: ps_os.left - c_os.left }; n_os = { top: n_os.top - c_os.top, left: n_os.left - c_os.left };

div_highlight.css( { "display": "block", "top": ps_os.top + "px" } ); div_highlight.children.eq( 0 ).css( {           "height": page_span.height + "px",            "width": ( ps_os.left < 1 ) ? "100%" : ( ( container.width - ps_os.left ) + "px" )        } ); // div_ss.height ~= height of 1 line of text div_highlight.children.eq( 1 ).css( "height", ( n_os.top - ps_os.top - page_span.height ) + "px" ); div_highlight.children.eq( 2 ).css( { "height": next.height + "px", "width": n_os.left + "px" } ); return true; }

function pagenum_out { if( self.proofreadpage_disable_highlighting ) return false; if ( !div_highlight ) return false;	// couldn't find it       div_highlight.css( "display", "none" ); div_highlight.children.eq( 0 ).css( "width", "0px" ); div_highlight.children.eq( 1 ).css( "height", "0px" ); div_highlight.children.eq( 2 ).css( "width", "0px" ); return true; }

function init { if ( pagenumbers_collection ) return false; // skip if pagenumbers are already set up       // get_optlist;

init_setting( "proofreadpage_numbers_visible", "pagenums_visible", true ); var portletLink = mw.util.addPortletLink( "p-do", "#", self.proofreadpage_numbers_visible ? ws_msg( "hide_page_numbers" ) : ws_msg( "show_page_numbers" ), "d-pageNumbers_visible", "", "n" ); $( portletLink ).on( "click", function ( e ) {           e.preventDefault;            toggle_visible;        } );

init_setting( "proofreadpage_numbers_inline", "pagenums_inline", false ); // if layouts are not initialized, show pagenumbers inline since "beside" view won't work if ( !layout ) self.proofreadpage_numbers_inline = true; portletLink = mw.util.addPortletLink( "p-do", "#", self.proofreadpage_numbers_inline ? ws_msg( "page_numbers_beside" ) : ws_msg( "page_numbers_inline" ), "d-pageNumbers_inline"); $( portletLink ).on( "click", function ( e ) {           e.preventDefault;            toggle_inline;        } );

var opacity = "background-color:#000000; opacity:0.2; -ms-filter:alpha(opacity=20); filter:alpha(opacity=20);"; // store container for the highlight to shared variable "div_highlight" div_highlight = $( ' '         + '  '          + ' '          + '  '          + ' ' );

// assign new div element to shared variable "div_ss" div_ss = $( '  ' );	// empty span following some text

// put divs in the innermost text container if ( layout ) { container = $( "#text" ); container.append( div_ss, div_highlight ); } else { $( ".mw-content-ltr" ).append( div_ss, div_highlight ); }

self.pagenum_ml = $( ".pagenum" ); refresh_display; }

function refresh_display { var init = !pagenumbers_collection;   // determine if we need to set things up

// JQuery collection of all pagenumber elements if ( !init ) pagenumbers_collection.remove; pagenumbers_collection = $;

if ( div_pagenumbers ) div_pagenumbers.remove; if ( !self.proofreadpage_numbers_inline ) { // html div container for page numbers - store in shared variable "div_pagenumbers" div_pagenumbers = $( ' ' ) .appendTo( "#text-wrap" );   // put pagenumbers container div in the outer text container dp_y = div_pagenumbers.offset.top; y_prev = { val: -10 }; }       self.pagenum_ml.each( init ? init_elem : setup_elem );

if ( self.proofreadpage_numbers_inline ) pagenumbers_collection.off( "mouseenter mouseleave" ); else { pagenumbers_collection.hover( pagenum_in, pagenum_out ); }   }

function refresh_offsets { if ( self.proofreadpage_numbers_inline || !div_pagenumbers ) return false; // do nothing if container is not set up

dp_y = div_pagenumbers.offset.top; y_prev = { val: -10 }; pagenumber = pagenumbers_collection.first; self.pagenum_ml.each( function ( i, page_span ) {           refresh_elem_offset ( page_span, pagenumber );            pagenumber = pagenumber.next;        } ); return true; }

function refresh_elem_offset ( page_span, pagenumber ) { var y = $( page_span ).offset.top; pagenumber.css( "top", y - dp_y ); if ( self.proofreadpage_numbers_visible && y - y_prev.val > 5 ) { y_prev.val = y;           pagenumber.css( "display", "" ); }       else { pagenumber.css( "display", "none" ); }   }

function init_elem ( i, page_span ) { var name = page_span.getAttribute("data-page-number") || page_span.id;

// what if two pages have the same number? increment the id       var pagenumber_id = "pagenumber_" + page_span.id; var count; if ( pagenumbers_collection.is( "#" + pagenumber_id ) ) { count = ( pagenumbers_collection.filter( "[id ^= '" + pagenumber_id + "']" ).length + 1 ); page_span.id += ( "_" + count ); pagenumber_id += ( "_" + count ); }       $.data( page_span, "pagenumber_id", pagenumber_id ); var page_title = decodeURI( page_span.title ).replace(/%26/g, "&"); var page_url = mw.config.get( "wgArticlePath" ) .replace( "$1", encodeURIComponent( page_title.replace( / /g, "_" ) ) ) // encodeURIComponent encodes '/', which breaks subpages .replace( /%2F/g, "/" );

// don't know what the next 2 lines accomplish var ll = page_span.parentNode.nextSibling; var class_str = ( ll && ll.tagName === "A" && ll.className === "new" ) ? ' class="new" ' : '';

$.data( page_span, "link_str", '' + mw.html.escape( name ) + '' );

setup_elem( i, page_span ); }

var inline_params = { elem: "span", style: "margin:0px; padding:0px; color:#666666;", link_pre: "&#x0020;[", link_post: "]&#x0020;" };   var beside_params = { elem: "div", style: "position:absolute;", link_pre: "[", link_post: "]" };

function setup_elem ( i, page_span ) { var params = self.proofreadpage_numbers_inline ? inline_params : beside_params; var pagenumber = $( '<' + params.elem + ' id= "' + $.data( page_span, "pagenumber_id" ) + '" class= "pagenumber noprint" '         + 'style= "' + ( self.proofreadpage_numbers_visible ? '' : 'display:none; ' ) + params.style + '">'          + params.link_pre + $.data( page_span, "link_str" ) + params.link_post          + '' );

if ( !self.proofreadpage_numbers_inline ) refresh_elem_offset ( page_span, pagenumber ); page_span.innerHTML = self.proofreadpage_numbers_inline ?  : ;       pagenumber.appendTo( self.proofreadpage_numbers_inline ? page_span : div_pagenumbers );

pagenumbers_collection = pagenumbers_collection.add( pagenumber ); }

return { init: init, refresh_offsets: refresh_offsets }; };

if ( $.inArray( mw.config.get( "wgAction" ), [ "view", "submit", "purge" ] ) !== -1 ) { if ( !self.debug_page_layout     // don't do anything on DoubleWiki or difference comparison views      && document.URL.indexOf( "match=" ) === -1      && document.URL.indexOf( "diff=" ) === -1      && ( mw.config.get( "wgNamespaceNumber" ) === 114 || self.proofreadpage_source_href ) ) { if ( ! $.isEmptyObject( self.ws_layouts ) ) $.when(               mw.loader.using( 'mediawiki.cookie' ),                $.ready            ).then( layout.init ); $( function{           if ( $( ".pagenum" ).length !== 0 ) {                mw.loader.using( 'mediawiki.cookie' ).done( pagenumbers.init );                $(window).load( pagenumbers.refresh_offsets );            }	} ); }

var position = window.location.hash.substring( 1 ); if ( position ) { document.getElementById( position ).scrollIntoView; } }