MediaWiki:Gadget-EditButtonGuide.js

Z Nonsensopedii, polskiej encyklopedii humoru

Uwaga: aby zobaczyć zmiany po zapisaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5 lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer: Przytrzymaj Ctrl jednocześnie klikając Odśwież lub naciśnij klawisze Ctrl+F5
  • Konqueror: Kliknij polecenie Odśwież lub naciśnij klawisz F5
  • Opera: Wyczyść pamięć podręczną w Narzędzia → Preferencje
/*
Autorzy: [[User:Polskacafe]], [[User:Ostrzyciel]]
*/

// constants
const message = 'Czy wiesz, że nie musisz nikogo prosić o wprowadzanie poprawek, bo sam możesz to zrobić?' +
	' Wystarczy, że klikniesz jeden z przycisków edycji! Pokażemy ci je, gdy zaakceptujesz tą wiadomość.';
const cookieCounter = 'uceCounter';
const cookieClosed = 'uceDialogClosed';
const forever = new Date('2038-01-01 00:00');	// close enough

// helper functions
function applyChanges() {
	$(".mw-editsection > a").addClass("wyr");
	$("#ca-editsection").addClass("wyrTab");
	setTimeout(function () {
		$(".mw-editsection > a").removeClass("wyr");
		$("#ca-editsection").removeClass("wyrTab");
	}, 3000);
}

function showEditTab() {
	var eButton = $( '#ca-edit' );
	var loc = eButton.offset();
	var y = loc.top + eButton.outerHeight() - 5;
	var x = loc.left + eButton.outerWidth() / 2;
	
	createCSSSelector('.editpopup', 'position: absolute; left: ' + x + '; top: ' + y + ';');
	var popup = new OO.ui.PopupWidget( {
		$content: $( '<p>Test.</p>' ),
		padded: true,
		width: 300
	} );
	
	$( document.body ).append( popup.$element );
	popup.toggle( true );
}

function getCounter() {
	return mw.cookie.get(cookieCounter) || "0";
}

function incrementCounter() {
	var cVal = parseInt(getCounter()) + 1;
	mw.cookie.set(cookieCounter, cVal, 0);		// this is a per-session cookie
}

// main jazz
$(document).ready( function() {
	//if (mw.config.get("wgUserGroups").indexOf("user") !== -1) return;
	if (mw.config.get("wgAction") !== "view") return;
	if (mw.config.get("wgNamespaceNumber") == -1) return;
	if (!mw.config.get("wgIsProbablyEditable")) return;
	if (mw.cookie.get(cookieClosed)) return;
	
	console.debug('elo');
	incrementCounter();
	
	if (parseInt(getCounter()) >= 4) {
		var windowManager = new OO.ui.WindowManager();
		$( 'body' ).append( windowManager.$element );
		var messageInfo = new OO.ui.MessageDialog();
		windowManager.addWindows( [ messageInfo ] );
		windowManager.openWindow( messageInfo, {
		  title: 'Cześć!',
		  message: message,
		  actions: [
		    {
		      action: 'accept',
		      label: 'Zrozumiano!',
		      flags: 'primary'
		    }
		  ]
		} ).closing.done(applyChanges);
		mw.cookie.set(cookieClosed, true, forever);
	}
} );

// funky code from stackoverflow for setting new CSS classes
// it's admittedly horrible, but there doesn't seem to be a better solution for OOUI's popups
function createCSSSelector (selector, style) {
  if (!document.styleSheets) return;
  if (document.getElementsByTagName('head').length === 0) return;

  var styleSheet,mediaType;

  if (document.styleSheets.length > 0) {
    for (var i = 0, l = document.styleSheets.length; i < l; i++) {
      if (document.styleSheets[i].disabled) 
        continue;
      var media = document.styleSheets[i].media;
      mediaType = typeof media;

      if (mediaType === 'string') {
        if (media === '' || (media.indexOf('screen') !== -1)) {
          styleSheet = document.styleSheets[i];
        }
      }
      else if (mediaType=='object') {
        if (media.mediaText === '' || (media.mediaText.indexOf('screen') !== -1)) {
          styleSheet = document.styleSheets[i];
        }
      }

      if (typeof styleSheet !== 'undefined') 
        break;
    }
  }

  if (typeof styleSheet === 'undefined') {
    var styleSheetElement = document.createElement('style');
    styleSheetElement.type = 'text/css';
    document.getElementsByTagName('head')[0].appendChild(styleSheetElement);

    for (i = 0; i < document.styleSheets.length; i++) {
      if (document.styleSheets[i].disabled) {
        continue;
      }
      styleSheet = document.styleSheets[i];
    }

    mediaType = typeof styleSheet.media;
  }

  if (mediaType === 'string') {
    for (var i = 0, l = styleSheet.rules.length; i < l; i++) {
      if(styleSheet.rules[i].selectorText && styleSheet.rules[i].selectorText.toLowerCase()==selector.toLowerCase()) {
        styleSheet.rules[i].style.cssText = style;
        return;
      }
    }
    styleSheet.addRule(selector,style);
  }
  else if (mediaType === 'object') {
    var styleSheetLength = (styleSheet.cssRules) ? styleSheet.cssRules.length : 0;
    for (var i = 0; i < styleSheetLength; i++) {
      if (styleSheet.cssRules[i].selectorText && styleSheet.cssRules[i].selectorText.toLowerCase() == selector.toLowerCase()) {
        styleSheet.cssRules[i].style.cssText = style;
        return;
      }
    }
    styleSheet.insertRule(selector + '{' + style + '}', styleSheetLength);
  }
}