跳转到内容

User:喵/SandBox/iwlnew.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// TODO: Use feature detection instead.

(function($, mw) {
  'use strict';
  mw.loader.using(['mediawiki.util', 'ext.gadget.site-lib', 'jquery.tipsy'], function () {
    // Allow hover on tips and force single instance.
    // Note: The Tipsy constructor is private, so let's use wrapping instead of extending.
    var ExtendedTipsy = function ExtendedTipsy(tipsy) {
      this.tipsy = tipsy;
    };
    ExtendedTipsy.activeTip = null;
    ExtendedTipsy.prototype.showSingleton = function() {
      if (ExtendedTipsy.activeTip && ExtendedTipsy.activeTip != this) {
        ExtendedTipsy.activeTip.hideSingleton();
      }
      
      if (this.delayHideTimer) {
        clearTimeout(this.delayHideTimer);
        this.delayHideTimer = null;
      } else {
        this.tipsy.show();
        ExtendedTipsy.activeTip = this;
      }
    };
    ExtendedTipsy.prototype.hideSingleton = function() {
      if (this.delayHideTimer) {
        clearTimeout(this.delayHideTimer);
        this.delayHideTimer = null;
      }
      this.tipsy.hide();
      ExtendedTipsy.activeTip = null;
    };
    ExtendedTipsy.prototype.delayHideSingleton = function() {
      var delayOut = this.tipsy.options.delayOut;
      if (delayOut == 0) {
        this.hideSingleton.bind(this);
      } else {
        this.delayHideTimer = setTimeout(
          this.hideSingleton.bind(this),
          this.tipsy.options.delayOut
        );
      }
    };
    
    $('.ilh-all', mw.util.$content).each(function(_, item) {
      var exta = $('.ilh-link', item);
      if (exta.length == 0) return;
      var link = $('.ilh-page a', item);
      link.tipsy({
        className: 'ilh-tipsy',
        gravity: 'nw',
        html: true,
        delayOut: 500,
        trigger: 'manual',
        title: function() {
          return '<div>' + wgULS(
            '条目$0不存在,可参考$1维基百科的对应页面$2。',
            '條目$0不存在,可參考$1維基百科的對應頁面$2。')
            .replace('$0', $('.ilh-dest', item).html())
            .replace('$1', $('.ilh-lang', item).text())
            .replace('$2', $('.ilh-link', item).html())
          + '</div>';
        }
      });
      
      var exTipsy = new ExtendedTipsy(link.data('tipsy'));
      
      link.mouseenter(exTipsy.showSingleton.bind(exTipsy));
      link.mouseleave(exTipsy.delayHideSingleton.bind(exTipsy));
    });
    
    $(document.body).on('mouseenter', '.ilh-tipsy', function() {
      if (ExtendedTipsy.activeTip) ExtendedTipsy.activeTip.showSingleton();
    }).on('mouseleave', '.ilh-tipsy', function() {
      if (ExtendedTipsy.activeTip) ExtendedTipsy.activeTip.delayHideSingleton();
    });
  });
})(jQuery, mediaWiki);