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

Материал из ЮУГМУ Вики
Нет описания правки
Нет описания правки
 
(не показано 77 промежуточных версий этого же участника)
Строка 1: Строка 1:
if (document.readyState !== 'loading') {
// Get the select element and the forms
    console.log('document is already ready, just execute code here');
var currentPageHref = decodeURI(window.location.href);
    myInitCode();
var test = 'Вики-энциклопедия_университета:Главная';
} else {
if (currentPageHref .indexOf(test) > -1) {
    document.addEventListener('DOMContentLoaded', function () {
var forms = document.querySelectorAll('.createbox');
        console.log('document was not ready, place code here');
var select = document.createElement('select');
        myInitCode();
select.id = 'select-template';
    });
select.className = 'mw-ui-input mw-ui-input-inline mw-inputbox-centered';
 
 
// Create the select options
var options = [
  { value: 'Шаблон:Личность', text: 'Шаблон: Личность' },
  { value: 'Шаблон:Структура', text: 'Шаблон: Структура' },
  { value: 'Шаблон:Событие', text: 'Шаблон: Событие' }
];
var opt;
for (var i = 0; i < options.length; i++) {
  opt = document.createElement('option');
  opt.value = options[i].value;
  opt.text = options[i].text;
  select.appendChild(opt);
}
}


function myInitCode() {
// Add event listener to the select element
    const node = document.getElementById("content");
select.addEventListener('change', function(e) {
    const content = document.createElement("ul");
  var value = e.target.value;
    content.classList.add("custom-top-menu");
 
    
   var form;
    const items = [
   for (var i = 0; i < forms.length; i++) {
      {
    form = forms[i];
        href: "/wiki/Вики-энциклопедия_университета",
    var preloadInput = form.querySelector('input[name="preload"]');
        title: "Главная",
 
        text: "Главная",
    if (preloadInput) {
        spanText: "Главная"
       preloadInput.value = value;
      },
     }
      {
   }
        href: "/wiki/Правила",
});
        title: "Правила",
var form = document.querySelector('.createbox');
        text: "Правила",
form.insertBefore(select, form.querySelector('input[name="veaction"]'));
        spanText: "Правила"
      },
      {
        href: "/wiki/Служебная:Все_страницы",
        title: "Все страницы",
        text: "Статьи",
        spanText: "Статьи"
      },
      {
        href: "/wiki/Служебная:Свежие_правки",
        title: "Свежие правки",
        text: "История правок",
        spanText: "История правок"
      },
      {
        href: "/wiki/Инструкции",
        title: "Инструкции",
        text: "Инструкции",
        spanText: "Инструкции"
      },
      {
        href: "/wiki/Контакты",
        title: "Заголовок 2",
        text: "Контакты",
        spanText: "Контакты"
      }
    ];
    
    items.forEach((item) => {
      const li = document.createElement("li");
      const a = document.createElement("a");
      a.href = item.href;
      a.title = item.title;
      a.textContent = item.text;
      const span = document.createElement("span");
      span.textContent = item.spanText;
      a.appendChild(span);
       li.appendChild(a);
      content.appendChild(li);
     });
    
    node.parentNode.insertBefore(content, node);
      var currentPageHref = decodeURI(window.location.href);
      var links = document.querySelectorAll('.custom-top-menu a');
      links.forEach(function(link) {
      var linkhref = link.getAttribute('href').trim();
      console.log(currentPageHref + '=' + linkhref);
      if (currentPageHref.indexOf(linkhref) > -1) {
          link.closest('li').classList.add('a-current');
      }
  });
}
}

Текущая версия от 16:31, 19 мая 2024

// Get the select element and the forms
var currentPageHref = decodeURI(window.location.href);
var test = 'Вики-энциклопедия_университета:Главная';
if (currentPageHref .indexOf(test) > -1) {
var forms = document.querySelectorAll('.createbox');
var select = document.createElement('select');
select.id = 'select-template';
select.className = 'mw-ui-input mw-ui-input-inline mw-inputbox-centered';


// Create the select options
var options = [
  { value: 'Шаблон:Личность', text: 'Шаблон: Личность' },
  { value: 'Шаблон:Структура', text: 'Шаблон: Структура' },
  { value: 'Шаблон:Событие', text: 'Шаблон: Событие' }
];
var opt;
for (var i = 0; i < options.length; i++) {
  opt = document.createElement('option');
  opt.value = options[i].value;
  opt.text = options[i].text;
  select.appendChild(opt);
}

// Add event listener to the select element
select.addEventListener('change', function(e) {
  var value = e.target.value;

  var form;
  for (var i = 0; i < forms.length; i++) {
    form = forms[i];
    var preloadInput = form.querySelector('input[name="preload"]');

    if (preloadInput) {
      preloadInput.value = value;
    }
  }
});
var form = document.querySelector('.createbox');
form.insertBefore(select, form.querySelector('input[name="veaction"]'));
}