MediaWiki:Common.js

From Unofficial Zero Manual
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Any JavaScript here will be loaded for all users on every page load. */

$(function() {
  $(".showExtra").click(function() {
    $(this).closest("table").find(".extraRow").fadeIn("slow");$(this).closest("tr").remove();
    
  });
  filterTable();
});

function customizeToolbar() {
  /* Your code goes here */

  /** https://meta.wikimedia.org/wiki/User:Krinkle/Scripts/InsertWikiEditorButton
   * Extra buttons in toolbar
   * @stats [[File:Krinkle_InsertWikiEditorButton.js]]
   */
  $.ajax(
    { dataType: 'script', cache: true,
      url: 'https://meta.wikimedia.org/w/index.php?title=User:Krinkle/Scripts/InsertWikiEditorButton.js&action=raw&ctype=text/javascript'
    }).then(function () {
      // Horizontal Rule
      krInsertWikiEditorButton({
        id: "mw-customeditbutton-rule",
        label: 'Horizontal Rule',
        section: 'advanced',
        icon: '//upload.wikimedia.org/wikipedia/commons/b/b4/Toolbaricon_rule.png',
        sampleText: '',
        insertBefore: "----",
        insertAfter: "\n",
      });
      // Strikethrough
      krInsertWikiEditorButton({
        id: "mw-customeditbutton-strike",
        label: 'Strike',
        section: 'advanced',
        icon: '//upload.wikimedia.org/wikipedia/commons/1/1a/Toolbaricon_strike.png',
        sampleText: 'Cancelled',
        insertBefore: "<s>",
        insertAfter: "</s>",
      });
      // Math
      krInsertWikiEditorButton({
        id: 'mw-customeditbutton-math',
        label: 'Math',
        section: 'advanced',
        icon: '//upload.wikimedia.org/wikipedia/commons/3/38/Button_Math.png',
        sampleText: 'x = y + z',
        insertBefore: "<math>",
        insertAfter: "</math>",
      });
      // Blockquote
      krInsertWikiEditorButton({
        id: 'mw-customeditbutton-blockquote',
        label: 'Blockquote',
        section: 'advanced',
        icon: '//upload.wikimedia.org/wikipedia/commons/f/fd/Toolbar_blockquote.png',
        insertBefore: "<blockquote>\n",
        insertAfter: "\n</blockquote>"
      });
      // Code
      krInsertWikiEditorButton({
        id: 'mw-customeditbutton-code',
        label: 'Code',
        section: 'advanced',
        icon: '//upload.wikimedia.org/wikipedia/commons/6/62/Toolbar_code.png',
        insertBefore: "<code>\n",
        insertAfter: "\n</code>"
      });
      // OnlyInclude
      krInsertWikiEditorButton({
        id: 'mw-customeditbutton-code',
        label: 'OnlyInclude',
        section: 'advanced',
        icon: '//upload.wikimedia.org/wikipedia/commons/9/99/Toolbaricon_templatelink.png',
        insertBefore: "<onlyinclude>\n",
        insertAfter: "\n</onlyinclude>"
      });
    });
};

// 公告滚动
function AutoScroll(obj) {
  $(obj).find("ul:first").animate({
    marginTop:"-25px"
  },500,function(){
    $(this).css({marginTop: "0px"}).find("li:first").appendTo(this);
  });
}

$(document).ready(function() {
  setInterval('AutoScroll("#scrollDiv")',5000);

  /* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
  if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
    mw.loader.using( 'user.options', function () {
      if ( mw.user.options.get( 'usebetatoolbar' ) ) {
	mw.loader.using( ['ext.wikiEditor.toolbar','jquery.wikiEditor.toolbar.config'] );
        $('#wpTextbox1').on('wikiEditor-toolbar-doneInitialSections', function() {
          customizeToolbar();
        });
      }
    } );
  }
});

// 筛选功能
function filterTable(){
  $("table.filterable").each(function(){
    var i=0;
    var cols;
    $(this).find("tr:first-child th, tr:first-child td").each(function(){
      if (!$(this).hasClass("unfilterable")){
	cols=[];
	$(this).closest("table").find("tr td:nth-child("+(i+1)+")").each(function(){
	  cols.push($(this).text());
	});
	cols = arrayUnique(cols);
	l=0;
	for (j=0; j<cols.length; j++){
	  t=charLength(cols[j]);
	  if (l<t) l=t;
	}
	$(this).css("position","relative");
	$(this).html('<a href="javascript:void(0)" class="showFilterMenu">'+$(this).html()+'▼</a>');
	$(this).append($('<div class="filterMenu hidden" style="position: absolute; top: '+$(this).height()+35+'px; left: 0; width: '+(50+l*7)+'px; text-align: left; padding: 5px; border: 1px #333333 solid; -moz-border-radius: 8px; -webkit-border-radius: 8px; border-radius: 8px; background: inherit; z-index: 1"></div>'));
	for (j=0; j<cols.length; j++){
	  $(this).find(".filterMenu").append('<div><input type="checkbox" value="'+cols[j]+'" col="'+(i+1)+'" class="filterOption" checked>'+cols[j]+'</div>');
	}
      }
      i++;
    });
    $(this).find("tr:nth-child(n+1)").attr("condition", 0);
  });
  $(".showFilterMenu").click(function(){
    if ($(this).parent().find(".filterMenu:visible").length){
      $(".filterMenu").slideUp(150);
    }else{
      $(".filterMenu").slideUp(150);
      $(this).parent().find(".filterMenu").slideDown(150);
    }
  });
  $(document).mouseup(function(e){
    var container = $(".filterMenu");
    if (!container.is(e.target) && container.has(e.target).length === 0){
      container.slideUp(150);
    }
  });
  $(".filterOption").click(function(){
    col=$(this).attr("col");
    val=$(this).val();
    if ($(this).is(":checked")) chg=1; else chg=-1;
    $(this).closest("table").find("tr:nth-child(n+1)").each(function(){
      if ($(this).find("td:nth-child("+col+")").text()==val){
	var cond=$(this).attr("condition");
	cond=Number(cond)+chg;
	$(this).attr("condition", cond);
	if (cond==0) $(this).show();
	else $(this).hide();
      }
    });
  });
}

function arrayUnique(a) {
  return a.reduce(function(p, c) {
    if (p.indexOf(c) < 0) p.push(c);
    return p;
  }, []);
}

function charLength(s){
  return s.length+(encodeURI(s).split(/%..|./).length-1-s.length)/2;
}