Difference between revisions of "MediaWiki:Common.js"

From CelesTek Wiki
Jump to: navigation, search
 
(No difference)

Latest revision as of 10:47, 14 May 2015

/* Any JavaScript here will be loaded for all users on every page load. */
(function () {
  'use strict';
  var mcw = window.mcw = {
  };
  mcw.i18n = {
    hideText: 'hide',
    showText: 'show',
    loadErrorTitle: 'An error occurred loading the content',
    defaultLicense: 'License'
  };
  mcw.events = {
  };
  if (mw.user.options.get('showtoolbar') && !mw.user.options.get('usebetatoolbar')) {
    importScript('MediaWiki:Toolbar.js');
  }
  mw.hook('wikipage.content').add(function ($content) {
    (function () {
      var $tables = $content.find('table.collapsible');
      if (!$tables || !$tables.length) {
        return false;
      }
      var buttonText = ' <span class="collapsible-button">[<span class="jslink">' + mcw.i18n.hideText + '</span>]</span> ';
      $tables.each(function () {
        var $table = $(this),
        $header,
        $collapseButton,
        firstWidth,
        secondWidth;
        if ($table.data('collapsible')) {
          return true;
        }
        $header = $table.find('tr:first .collapse-button');
        if (!$header.length) {
          $header = $table.find('tr:first > th:first');
        }
        if (!$header.length || !$table.find('tr:not(tr:first)').html().trim().length) {
          return true;
        }
        if ($table.hasClass('collapse-button-none')) {
          $header.append(buttonText);
        } else {
          $header.prepend(buttonText);
        }
        $collapseButton = $table.find('.collapsible-button');
        firstWidth = $collapseButton.width();
        $collapseButton.find('> .jslink').text(mcw.i18n.showText);
        secondWidth = $collapseButton.width();
        if (firstWidth != secondWidth) {
          if (firstWidth < secondWidth) {
            $collapseButton.css('min-width', secondWidth);
          } else {
            $collapseButton.css('min-width', firstWidth);
          }
        }
        if (!$table.hasClass('collapsed')) {
          $collapseButton.find('> .jslink').text(mcw.i18n.hideText);
        }
        $table.data('collapsible', true);
      });
      $tables.find('.collapsible-button .jslink').click(function (e) {
        var $table = $(this).closest('table.collapsible');
        e.stopPropagation();
        if ($table.hasClass('collapsed')) {
          $table.removeClass('collapsed').addClass('expanded');
          $(this).text(mcw.i18n.hideText);
        } else {
          $table.removeClass('expanded').addClass('collapsed');
          $(this).text(mcw.i18n.showText);
        }
      });
    }());
    (function () {
      if (!mcw.animate) {
        mcw.animate = setInterval(function () {
          $('.animated').each(function () {
            var $elem = $(this);
            var $current = $elem.children('.active');
            var $next = $current.nextAll(':not(.skip):first');
            if (!$next.length) {
              $next = $elem.children(':not(.skip):first');
            }
            $current.removeClass('active');
            $next.addClass('active');
          });
        }, 2000);
      }
    }());
    (function () {
      var $loadPage = $content.find('.load-page');
      if (!$loadPage.length) {
        return;
      }
      mw.loader.load('jquery.spinner');
      var $buttonText = $('<span>').addClass('mw-editsection-like load-page-button').append('[', $('<span>').addClass('jslink').text(mcw.i18n.hideText), ']');
      $loadPage.find('.mw-headline:first').each(function () {
        var $body = $(this).closest('.load-page'),
        $button = $buttonText.clone(),
        firstWidth,
        secondWidth;
        $button.insertAfter(this);
        $body.find('.mw-editsection').insertAfter($button);
        firstWidth = $button.width();
        $button.children('.jslink').text(mcw.i18n.showText);
        secondWidth = $button.width();
        if (firstWidth !== secondWidth) {
          if (firstWidth > secondWidth) {
            $button.css('min-width', firstWidth);
          } else {
            $button.css('min-width', secondWidth);
          }
        }
      });
      if (mcw.events.loadPage) {
        return;
      }
      $('#mw-content-text').on('click', '.load-page-button > .jslink', function () {
        var
        $button = $(this).parent(),
        $body = $button.closest('.load-page'),
        $contentContainer = $body.find('.load-page-content');
        if (!$body.data('loaded')) {
          var oldButton = $button.html();
          mw.loader.using('jquery.spinner', function () {
            $button.html($.createSpinner());
          });
          new mw.Api().get({
            action: 'parse',
            prop: 'text',
            title: mw.config.get('wgPageName'),
            text: '{' + '{:' + $body.data('page') + '}}'
          }).done(function (data) {
            $contentContainer.html(data.parse.text['*']).removeClass('noscript');
            mw.hook('wikipage.content').fire($contentContainer);
            $button.html(oldButton).children('.jslink').text(mcw.i18n.hideText);
            $body.data('loaded', true);
          }).fail(function (_, error) {
            $button.html(oldButton);
            var errorText = '';
            if (error.textStatus) {
              errorText = error.textStatus;
            } else if (error.error) {
              errorText = error.error.info;
            }
            mw.notify(errorText, {
              title: mcw.i18n.loadErrorTitle,
              autoHide: false
            });
          });
        } else if ($(this).text() === mcw.i18n.showText) {
          $contentContainer.show();
          $(this).text(mcw.i18n.hideText);
        } else {
          $contentContainer.hide();
          $(this).text(mcw.i18n.showText)
          ;
        }
      });
      mcw.events.loadPage = true;
    }());
  });
  $(function () {
    if (document.location.search.indexOf('undo=') !== - 1 && document.getElementsByName('wpAutoSummary') [0]) {
      document.getElementsByName('wpAutoSummary') [0].value = '1';
    }
    $('#mw-content-text').on({
      'mouseenter': function () {
        $(this).find('.animated').removeClass('animated').addClass('paused');
      },
      'mouseleave': function () {
        $(this).find('.paused').removeClass('paused').addClass('animated');
      }
    }, '.grid-generic, .grid-Crafting_Table, .grid-Furnace, .grid-Brewing_Stand');
    mw.loader.using('mediawiki.searchSuggest', function () {
      $('.suggestions:first').addClass('searchbar');
    });
    if (mw.config.get('wgCanonicalSpecialPageName') === 'Upload') {
      if ($('#wpLicense').val() === '') {
        $('#wpLicense').val(mcw.i18n.defaultLicense);
      }
      mw.loader.using('mediawiki.legacy.upload', function () {
        var change = setInterval(function () {
          if (licenseSelectorCheck) {
            $('#wpLicense').change();
            clearInterval(change);
          }
        }, 500);
      });
    }
    mcw.minetip = {
      create: function () {
        var tooltip;
        $('#mw-content-text').on({
          'mouseenter.minetip':
          function (e) {
            var $elem = $(this),
            title = $elem.data('minetip-title'),
            description = $elem.data('minetip-text');
            if (title === undefined || title && title.replace(/&([0-9a-fl-o])|\s+/g, '') === '') {
              var attrTitle = $elem.attr('title') || $elem.find('> a:first').attr('title');
              if (title === undefined) {
                title = attrTitle;
              } else {
                title += attrTitle;
              }
              if (title) {
                $elem.data('minetip-title', title);
              } else {
                return;
              }
            }
            $elem.add('*', $elem).filter('[title]').removeAttr('title');
            if (title === 0) {
              return;
            }
            var text = '<span class="title">' + title + '&f</span>';
            if (description) {
              text += '\n<span class="description">' + description.replace(/\\\//g, '&#47;').replace(/\//g, '<br>') + '&f</span>';
            }
            if (!$('#minetip-tooltip').length) {
              $('body').append('<div id="minetip-tooltip"/>');
            }
            tooltip = $('#minetip-tooltip');
            while (text.match(/&[0-9a-el-o]/)) {
              text = text.replace(/&([0-9a-el-o])(.*?)(&f|$)/g, '<span class="format-$1">$2</span>&f');
            }
            text = text.replace(/&f/g, '');
            tooltip.html(text);
            $elem.trigger('mousemove', e);
          },
          'mousemove.minetip': function (e, trigger) {
            if (!$('#minetip-tooltip').length) {
              $(this).trigger('mouseenter');
              return;
            }
            e = trigger || e;
            var top = e.clientY - 34,
            left = e.clientX + 14,
            width = tooltip.outerWidth(true),
            height = tooltip.outerHeight(true),
            $win = $(window),
            winWidth = $win.width(),
            winHeight = $win.height();
            if (left + width > winWidth) {
              left -= width + 36;
            }
            if (left < 0) {
              left = 0;
              top += 82;
              if (top + height > winHeight) {
                top -= 77 + height;
              }
            } else if (top < 0) {
              top = 0;
            } else if (top + height > winHeight) {
              top = winHeight - height;
            }
            tooltip.css({
              top: top,
              left: left
            });
          },
          'mouseleave.minetip': function () {
            if (!tooltip) {
              return;
            }
            tooltip.remove();
          }
        }, '.minetip, .grid .image, .grid .item, .grid2 .item').off('.minetipNative');
      },
      destroy: function () {
        $('#mw-content-text').off('.minetip .minetipNative');
        $('#minetip-tooltip').remove();
      },
      native: function () {
        $('#mw-content-text').on('mouseenter.minetipNative', '.minetip, .grid .image, .grid .item, .grid2 .item', function () {
          var title = $(this).data('minetip-title'),
          description = $(this).data('minetip-text'),
          existingTitle = $(this).attr('title') || $(this).find('> a:first').attr('title');
          if (title || title === 0 || $(this).attr('title')) {
            $(this).find('[title]').removeAttr('title');
          }
          if (title === 0) {
            $(this).removeAttr('title');
            return;
          } else if (!title && (!existingTitle || !description)) {
            return;
          } else if (!title && existingTitle) {
            $(this).data('minetip-title', existingTitle);
          }
          var text = title || existingTitle;
          if (description) {
            text += '\n' + description;
          }
          text = text.replace(/&([0-9a-fl-o])/g, '').replace(/\\\//g, '&#47;').replace(/\//g, '\n').replace(/&#47;/g, '/');
          $(this).attr('title', text);
        }).off('.minetip');
      }
    };
    if (mcw.useNativeMinetip) {
      mcw.minetip.native();
    } else {
      mcw.minetip.create();
    }
  });
}());
mw.loader.state({
  'site': 'ready'
});