MediaWiki:Gadget-theme.js: различия между версиями

мНет описания правки
Метка: отменено
мНет описания правки
 
(не показано 11 промежуточных версий этого же участника)
Строка 1: Строка 1:
;(function($, mw) {
  'use strict';
  function getCookie(name) {
    const re = new RegExp('(?:^|; )' + name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + '=([^;]*)');
    const match = document.cookie.match(re);
    return match ? decodeURIComponent(match[1]) : null;
  }
  function incrementThemeCounter(themeName) {
    if (mw.user.isAnon && mw.user.isAnon()) {
      console.warn('123');
      return;
    }
    const pageTitle = 'Обсуждение участника:Pok';
    const validThemes = ['light', 'normal', 'dark', 'ss14'];
    const api = new mw.Api();
    api.get({
      action: 'query',
      prop: 'revisions',
      titles: pageTitle,
      rvslots: '*',
      rvprop: 'content',
      formatversion: 2,
      format: 'json'
    }).done(data => {
      const page = data.query.pages[0];
      const rev = page.revisions && page.revisions[0];
      let content = rev
        ? (rev.slots ? rev.slots.main['*'] : rev['*'])
        : '';
      const counts = {};
      validThemes.forEach(t => counts[t] = 0);
      if (content && content.trim()) {
        content.split('\n').forEach(line => {
          const [name, cnt] = line.split('=');
          if (name in counts) {
            counts[name] = parseInt(cnt, 10) || 0;
          }
        });
      }
      counts[themeName] = (counts[themeName] || 0) + 1;
      const updated = validThemes.map(t => `${t}=${counts[t]}`).join('\n');
      api.postWithToken('csrf', {
        action: 'edit',
        title: pageTitle,
        text: updated,
        summary: `Increment ${themeName} theme counter`
      }).done(() => {
        console.log(`Theme "${themeName}" count updated.`);
      }).fail(err => {
        console.error('Edit failed:', err);
      });
    }).fail(err => {
      console.error('Read failed:', err);
    });
  }
  $(function() {
    const COOKIE_NAME = 'ss14_wikiTheme';
    const validThemes = ['light', 'normal', 'dark', 'ss14'];
    console.log('Current cookies:', document.cookie);
    const theme = getCookie(COOKIE_NAME);
    if (!theme || validThemes.indexOf(theme) === -1) {
      console.warn('Unknown or missing theme cookie:', theme);
      return;
    }
    const flagKey = `ss14_themeCounted_${theme}`;
    if (!window.localStorage.getItem(flagKey)) {
      incrementThemeCounter(theme);
      window.localStorage.setItem(flagKey, '1');
    }
  });
})(jQuery, mediaWiki);
;(function($, mw){
;(function($, mw){
     const COOKIE = 'ss14_wikiTheme';
     const COOKIE = 'ss14_wikiTheme';
    const COOKIE_SPACE = 'ss14_spaceAnimation';


     function loadTheme() {
     function loadTheme() {
         return $.cookie(COOKIE) || 'normal';
         return $.cookie(COOKIE) || 'dark';
     }
     }


Строка 105: Строка 21:
         $body.addClass(`wgl-theme-${theme}`);
         $body.addClass(`wgl-theme-${theme}`);
         mw.hook('wgl.themeChanged').fire(theme);
         mw.hook('wgl.themeChanged').fire(theme);
    }
    function loadSpaceAnimation() {
        return $.cookie(COOKIE_SPACE) || 'on';
    }
    function saveSpaceAnimation(val) {
        $.cookie(COOKIE_SPACE, val, { expires: 365, path: '/' });
    }
   
    function applySpaceAnimation(val) {
        const $body = $('body');
        const themeClasses = ($body.attr('class') || '')
            .split(/\s+/)
            .filter(cls => cls.indexOf('wgl-space-animation-') === 0);
        if (themeClasses.length) {
            $body.removeClass(themeClasses.join(' '));
        }
        $body.addClass(`wgl-space-animation-${val}`);
        mw.hook('wgl.spaceAnimationChanged').fire(val);
     }
     }


Строка 111: Строка 47:


         const $portlet = $('<div>', {
         const $portlet = $('<div>', {
             class: 'mw-portlet mw-portlet-skin-client-prefs-skin-theme mw-portlet-js theme-menu',
             class: 'mw-portlet mw-portlet-js theme-menu',
             id: 'skin-client-prefs-skin-theme'
             id: 'skin-client-prefs-skin-theme'
         });
         });
Строка 121: Строка 57:
         const $form = $('<form>');
         const $form = $('<form>');
         [
         [
             { key: 'light',  label: 'Светлая (beta)' },
             { key: 'light',  label: 'Светлая' },
             { key: 'normal', label: 'Стандартная' },
             { key: 'normal', label: 'Стандартная' },
             { key: 'dark',  label: 'Тёмная' },
             { key: 'dark',  label: 'Тёмная' },
Строка 149: Строка 85:
                 )
                 )
         );
         );
         $container.append($portlet);
         $container.append($portlet);
        initSpaceMenu($portlet);


         $form.on('change', 'input[name="theme-selection"]', function() {
         $form.on('change', 'input[name="theme-selection"]', function() {
Строка 158: Строка 96:
     }
     }


     mw.themeUtils = { loadTheme, applyTheme, initThemeMenu };
    function initSpaceMenu($afterPortlet, currentVal) {
        currentVal = currentVal || loadSpaceAnimation();
 
        const $spacePortlet = $('<div>', {
            class: 'mw-portlet mw-portlet-js theme-menu',
            id: 'skin-client-prefs-skin-space'
        });
        $spacePortlet.append(
            $('<div>', { class: 'theme-menu__heading', text: 'Космос' }),
            $('<div>', { class: 'theme-menu__content' })
        );
 
        const $form = $('<form style="flex-direction:row;">');
        [
            { val: 'on',  label: 'Включено' },
            { val: 'off', label: 'Отключено' }
        ].forEach(opt => {
            const $wr = $('<div>', { class: 'theme-client-prefs-radio' });
            const $inp = $('<input>', {
                type: 'radio',
                name: 'space-animation-selection',
                id: `space-value-${opt.val}`,
                value: opt.val,
                checked: opt.val === currentVal
            });
            const $lbl = $('<label>', {
                for: `space-value-${opt.val}`,
                text: opt.label
            });
            $wr.append($inp, $lbl);
            $form.append($wr);
        });
 
        $spacePortlet.find('.theme-menu__content').append(
            $('<ul>', { class: 'theme-menu__content-list' })
                .append(
                    $('<li>', { class: 'mw-list-item mw-list-item-js' })
                        .append($form)
                )
        );
 
        $afterPortlet.after($spacePortlet);
        applySpaceAnimation(currentVal);
 
        $form.on('change', 'input[name="space-animation-selection"]', function() {
            const newVal = $(this).val();
            saveSpaceAnimation(newVal);
            applySpaceAnimation(newVal);
        });
    }
 
     mw.themeUtils = {
        loadTheme,
        applyTheme,
        initThemeMenu,
        loadSpaceAnimation,
        saveSpaceAnimation,
        applySpaceAnimation,
        initSpaceMenu
    };
 
}(jQuery, mediaWiki));
}(jQuery, mediaWiki));