MediaWiki:Gadget-CollapsibleSidebar.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
'use strict';
(function () {
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
$(function collapsibleSidebar() {
  var gadgetName = 'ext.gadget.CollapsibleSidebar';
  var commonImgUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb';
  var localImgUrl = 'https://zh.wikipedia.org/static/images/mobile/copyright/wikipedia-wordmark-';
  var imgLogoLang = ['ca', 'cs', 'cy', 'el', 'en', 'et', 'hi', 'hy', 'ja', 'ka', 'la', 'ru', 'szl', 'th', 'uk', 'uz', 'zh'].indexOf(mw.config.get('wgContentLanguage')) !== -1 ? mw.config.get('wgContentLanguage') : 'en';
  var images = {
    logo: localImgUrl + imgLogoLang + '.svg',
    logo_zh_hans: localImgUrl + 'zh-hans.svg',
    next: commonImgUrl + '/9/95/Icons8_flat_next.svg/15px-Icons8_flat_next.svg.png',
    prev: commonImgUrl + '/b/bd/Icons8_flat_previous.svg/15px-Icons8_flat_previous.svg.png'
  };
  var isMobile = /android|ipad|iphone|mobile/i.test(navigator.userAgent);
  var isWikipedia = mw.config.get('wgNoticeProject') === 'wikipedia';
  var $body = $('body');
  var $content = $('#content');
  var $footer = $('#footer');
  var $leftNavigation = $('#left-navigation');
  var $mwNavigation = $('#mw-navigation');
  var $mwWikiLogo = $('.mw-wiki-logo');
  var $mwPanel = $('#mw-panel');
  var isSidebarHide = false;
  var $items;
  var updatePositionHelper = function updatePositionHelper(positionLeftObject) {
    for (var _i = 0, _Object$entries = Object.entries(positionLeftObject); _i < _Object$entries.length; _i++) {
      var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
        item = _Object$entries$_i[0],
        positionLeft = _Object$entries$_i[1];
      if (positionLeft === '') {
        continue;
      }
      var $element = $items[item];
      var currentPositionLeft = $element.css('left');
      if (currentPositionLeft !== positionLeft) {
        $element.css('left', positionLeft);
      }
    }
  };
  var updatePosition = function updatePosition() {
    var _ref;
    var innerWidth = window.innerWidth;
    var outerWidth = window.outerWidth;
    var $bodyWidth = $body.width();
    var bodyWidth = (_ref = isMobile ? outerWidth > 0 ? outerWidth : $bodyWidth : innerWidth > 0 ? innerWidth : $bodyWidth) !== null && _ref !== void 0 ? _ref : 0;
    if (bodyWidth >= 982) {
      updatePositionHelper(isSidebarHide ? {
        $sidebarCollapseButton: '0.3em',
        $sliderCollapseLogo: '3em'
      } : {
        $sidebarCollapseButton: '10.3em',
        $sliderCollapseLogo: ''
      });
    } else {
      updatePositionHelper(isSidebarHide ? {
        $sidebarCollapseButton: '0.3em',
        $sliderCollapseLogo: '2.5em'
      } : {
        $sidebarCollapseButton: '9.3em',
        $sliderCollapseLogo: ''
      });
    }
  };
  var hideSidebar = function hideSidebar() {
    isSidebarHide = true;
    $items.$sidebarCollapseButton.attr('src', images.next);
    updatePosition();
    $content.css('margin-left', '1em');
    $footer.css('margin-left', '1em');
    if (!isWikipedia) {
      $leftNavigation.css('margin-left', '1em');
    }
    $mwPanel.hide();
    $items.$sliderCollapseLogo.show();
    mw.storage.set(gadgetName, 'hide');
  };
  var showSidebar = function showSidebar() {
    isSidebarHide = false;
    $items.$sidebarCollapseButton.attr('src', images.prev);
    updatePosition();
    $content.css('margin-left', '');
    $footer.css('margin-left', '');
    if (!isWikipedia) {
      $leftNavigation.css('margin-left', '');
    }
    $mwPanel.show();
    $items.$sliderCollapseLogo.hide();
    mw.storage.set(gadgetName, 'show');
  };
  var collapsibleSidebarMain = function collapsibleSidebarMain(observer) {
    observer === null || observer === void 0 ? void 0 : observer.disconnect();
    var $sidebarCollapseButton = $('<img>').attr({
      id: 'sidebarCollapse',
      src: images.prev,
      draggable: 'false'
    }).css({
      cursor: 'pointer',
      position: 'fixed',
      top: '5.625em',
      width: '0.8em',
      '-moz-user-select': 'none',
      '-webkit-user-select': 'none',
      'user-select': 'none',
      padding: '0.3em',
      'border-radius': '50px',
      border: '1px solid #a7d7f9',
      background: '#fff'
    });
    $sidebarCollapseButton.appendTo($mwNavigation);
    var $originMwLogo = $mwWikiLogo;
    var $newMwLogo = $originMwLogo.clone(false).empty().removeAttr('class').attr({
      id: 'sidebarCollapseLink',
      title: $originMwLogo.attr('title')
    });
    var $sliderCollapseLogo = $('<img>').addClass('mw-no-invert').attr({
      id: 'sliderCollapseLogo',
      src: mw.config.get('wgContentLanguage') === 'zh' ? ['zh-cn', 'zh-hans', 'zh-my', 'zh-sg'].indexOf(mw.config.get('wgUserLanguage')) !== -1 ? images.logo_zh_hans : images.logo : images.logo
    }).css({
      cursor: 'pointer',
      position: 'absolute',
      top: '3em',
      width: '7em',
      display: 'none'
    });
    if (isWikipedia) {
      $sliderCollapseLogo.appendTo($newMwLogo);
    }
    $newMwLogo.appendTo($mwNavigation);
    $items = {
      $sidebarCollapseButton: $sidebarCollapseButton,
      $sliderCollapseLogo: $sliderCollapseLogo
    };
    if (mw.storage.get(gadgetName) === 'hide') {
      hideSidebar();
    }
    updatePosition();
    $sidebarCollapseButton.on('click', function () {
      isSidebarHide ? showSidebar() : hideSidebar();
    }).on('mouseout mouseover', function (_ref2) {
      var type = _ref2.type;
      this.style.background = type === 'mouseout' ? '#fff' : 'rgb(223, 245, 255)';
    });
    $(window).on('resize', function () {
      updatePosition();
    });
  };
  var collapsibleSidebarInit = function collapsibleSidebarInit() {
    if (!$body.hasClass('skin-vector-legacy')) {
      return;
    }
    if (['bo', 'dz'].indexOf(mw.config.get('wgContentLanguage')) !== -1 || $body.hasClass('rtl')) {
      return;
    }
    if (!$mwNavigation.length) {
      var observer = new MutationObserver(function (mutations) {
        var _iterator = _createForOfIteratorHelper(mutations),
          _step;
        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var mutation = _step.value;
            var _iterator2 = _createForOfIteratorHelper(mutation.addedNodes),
              _step2;
            try {
              for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
                var node = _step2.value;
                if (!(node instanceof HTMLElement)) {
                  continue;
                }
                if (node.id === 'mw-navigation') {
                  collapsibleSidebarMain(observer);
                  break;
                }
              }
            } catch (err) {
              _iterator2.e(err);
            } finally {
              _iterator2.f();
            }
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      });
      observer.observe(document.documentElement, {
        childList: true
      });
      return;
    }
    collapsibleSidebarMain();
  };
  collapsibleSidebarInit();
});
})();