Benutzer:Schnark/js/popups-light.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
//Dokumentation unter [[Benutzer:Schnark/js/popups-light]] <nowiki>
mw.libs.deprecated = $.extend(mw.libs.deprecated, {popups: 'Das Skript [[Benutzer:Schnark/js/popups-light]] wird nicht mehr gepflegt. Verwende stattdessen am besten Hovercards aus den Beta-Funktionen.'});
importScript('Benutzer:Schnark/js/deprecated.js');

/*global mw: true */
(function($){

var popups = {
version: 1.2,

popupWidth: '30em',
imageAttr: '80px|right', //'' zum Deaktivieren
css: '.popup-inner {border: 1px solid black; background: white; padding: 3px; font: 12px sans-serif; color: black; text-align: left;}' +
     '.popup-title {font-weight: bold; font-size: 130%; border-bottom: 1px solid #aaa;}' +
     '.popup-subtitle {font-size: 84%; color: #7d7d7d;}', //TODO mehr/bessere Standardwerte

maxSourceLength: 2000,

redirectedFrom: '', //(Weitergeleitet von $1), wird mit der ersten API-Anfrage miterledigt

fileNamespaces: [],
removeTables: true,
removeTemplates: 'QS-|Löschantragstext|Redundanztext|Widerspruch|Allgemeinverständlichkeit|Belege(?: fehlen)?', //TODO lokalisierbar machen
removeOther: '\\{\\{\\s*Infobox\\b([^{}]*\\{\\{[^{}]*\\}\\})*[^{}]*\\}\\}', //TODO lokalisierbar machen

//fügt Popups zu allen Links in $el hinzu
bind: function ($el) {
  $el.find('a').not('.external, .extiw, .new').filter(function(){
     var $this = $(this),
         path = mw.config.get('wgArticlePath').replace('$1', ''),
         href = $this.attr('href'), title = $this.attr('title');
     if (!href || !title) return false;
     var index = href.indexOf(path);
     if (index == -1) return false;
     var server = href.substr(0, index);
     if (server !== '' && server.replace(/^[^\/]*/, '') != mw.config.get('wgServer').replace(/^[^\/]*/, '')) return false; //anderer Server
     var article = decodeURIComponent(href.substr(index + path.length)).replace(/_/g, ' ');
     if (article.indexOf(mw.config.get('wgFormattedNamespaces')[-1] + ':') === 0) return false;
     return article == title;
  }).hoverIntent(popups.initPopup, function(){});
},

initPopup: function (e) {
  var $link = $(this).unbind('mouseover mouseout');
  var title = $link.attr('title');
  $link.attr('title', '');
  var titleHTML = mw.html.element('div', {'class': 'popup-title'}, title),
      subtitleHTML = mw.html.element('div', {'class': 'popup-subtitle'}, ''),
      textHTML = mw.html.element('div', {'class': 'popup-text'}, new mw.html.Raw(mw.html.element('span', {'class': 'mw-small-spinner'}, ''))),
      $popup = $(mw.html.element('div', {'class': 'popup-inner'}, new mw.html.Raw(titleHTML + subtitleHTML + textHTML)));
  $link.wrap('<span class="popup-outer"/>').parent().hoverIntent(popups.show, popups.hide).append($popup);
  popups.getHTML(title, $popup);
  var ob = $popup.parent()[0];
  popups.show.apply(ob, arguments);
  ob.hoverIntent_s = 1; //HACK
},

show: function (e) {
   var $popup = $(this).children('.popup-inner');
   var x = e.clientX, y = e.clientY,
       w = parseInt($popup.css('width'), 10), h = parseInt($popup.css('height'), 10);
   if (window.innerWidth - x - w < 10) x = Math.floor(window.innerWidth - 5 * w / 3);
   if (x < 0) x = 0;
   if (window.innerHeight - y - h < 10) y = window.innerHeight - h - 10;
   if (y < 0) y = 0;
   $popup.css({top: y, left: x}).show();
},

hide: function (e) {
   $(this).find('.popup-inner').hide();
},

getHTML: function(title, $popup) {
  var data = {action: 'query', prop: 'revisions', redirects: '', titles: title, rvsection: 0, rvprop: 'content', indexpageids: '', format: 'json'};
  if (popups.redirectedFrom === '') {
     data.meta = 'allmessages'; data.ammessages = 'redirectedfrom';
  }
  $.get(mw.util.wikiScript('api'), data, function (json) {
    if (json && json.query && json.query.allmessages && json.query.allmessages[0] && json.query.allmessages[0]['*']) {
       popups.redirectedFrom = json.query.allmessages[0]['*'];
    }

    if (json && json.query && json.query.redirects && json.query.redirects[0] && json.query.redirects[0].from) {
       var from = json.query.redirects[0].from,
           link = mw.html.element('a', {title: from, href: mw.util.getUrl(from) + '?redirect=no'}, from),
           subtitle = popups.redirectedFrom.replace(/\$1/g, link);
       $popup.find('.popup-title').text(from);
       $popup.find('.popup-subtitle').html(subtitle);
       title = json.query.redirects[0].to;
    }

    var text = 'Beim Laden des Wiki-Textes trat ein Fehler auf!';
    if (json && json.query && json.query.pages && json.query.pageids && json.query.pageids[0] && json.query.pages[json.query.pageids[0]] &&
        json.query.pages[json.query.pageids[0]].revisions && json.query.pages[json.query.pageids[0]].revisions[0])
       text = json.query.pages[json.query.pageids[0]].revisions[0]['*'] || '';

    text = text.replace(/<ref[^>]*\/\s*>/g, ''); //wiederholte Fußnoten entfernen
    text = text.replace(/<ref[^>]*>(?:.|\n)*?<\s*\/\s*ref\s*>/g, ''); //Fußnoten entfernen
    if (popups.removeOther) text = text.replace(popups.removeOther, '');
    if (popups.removeTables) text = text.replace(/\{\|(?:.|\n)*?\|\}/g, ''); //Tabellen entfernen
    if (popups.removeTemplates) text = text.replace(popups.removeTemplates, '');
    var firstImage = popups.imageAttr !== '';
    text = text.replace(popups.fileNamespaces, function (alles, filename) { //Bilder entfernen
               if (firstImage) {
                  firstImage = false;
                  return '[[File:' + filename.replace(/\|.*/, '') + '|' + popups.imageAttr + ']]';
               } else {
                  return '';
               }
    });
    text = text.substr(0, popups.maxSourceLength);

    $.getJSON(mw.util.wikiScript('api'), {action: 'parse', title: title, text: text, prop: 'text|displaytitle', format: 'json'}, function (json) {
      var html = 'Beim Erzeugen der Vorschau trat ein Fehler auf!';
      if (json && json.parse && json.parse.text && json.parse.text['*']) html = json.parse.text['*'];
      if (json && json.parse && json.parse.displaytitle) $popup.find('.popup-title').html(json.parse.displaytitle);
      popups.bind($popup.find('.popup-text').html(html));
    });
  });
},

init: function () {
  var ns = mw.config.get('wgNamespaceIds');
  for (var name in ns) if (ns[name] == 6) popups.fileNamespaces.push(name);
  popups.fileNamespaces = new RegExp('\\[\\[(?:' + popups.fileNamespaces.join('|') + '):([^\\[\\]]*(?:\\[\\[[^\\[\\]]*\\]\\][^\\[\\]]*)*)\\]\\]', 'gi');
  if (popups.removeTemplates) popups.removeTemplates = new RegExp('\\{\\{(?:' + popups.removeTemplates + ')[^}]+\\}\\}', 'g');
  if (popups.removeOther) popups.removeOther = new RegExp(popups.removeOther, 'g');
  mw.util.addCSS(
  '.popup-inner {z-index: 1; position: fixed; width: ' + popups.popupWidth + ';}' +
  popups.css
  );
  popups.bind(mw.util.$content);
}
};

if (mw.config.get('debug')) window.popupsLight = popups;

$(document).trigger('loadWikiScript', ['Benutzer:Schnark/js/popups-light.js', popups]);

mw.loader.using('jquery.hoverIntent', function() {
$(popups.init);
});

})(jQuery);
//</nowiki>