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

Материал из War Thunder Wiki
Перейти к: навигация, поиск
Строка 241: Строка 241:
 
$(document).ready(function() {
 
$(document).ready(function() {
 
$('.ttx-cockpit').click(function( event ) {
 
$('.ttx-cockpit').click(function( event ) {
var cockpitId = Number($(this).attr('data-id'));
+
var cockpitId = $(this).attr('data-id');
 
if(!($('#ttx-popup')[0])) {
 
if(!($('#ttx-popup')[0])) {
 
$('<div id="ttx-popup"></div><span id="ttx-popup-close">✖</span><div id="ttx-popup-overlay"></div>').prependTo('.content-area');
 
$('<div id="ttx-popup"></div><span id="ttx-popup-close">✖</span><div id="ttx-popup-overlay"></div>').prependTo('.content-area');
 
}
 
}
$("<iframe width='100%' height='100%' src='https://roundme.com/embed/248664/" + cockpitId + "' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>").prependTo('#ttx-popup');
+
$("<iframe width='100%' height='100%' src='https://roundme.com/embed/" + cockpitId + "' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>").prependTo('#ttx-popup');
 
$('#ttx-popup-overlay').fadeIn(400,
 
$('#ttx-popup-overlay').fadeIn(400,
 
function(){
 
function(){

Версия 17:23, 15 ноября 2018

/* Код открытия внешних ссылок в новой вкладке */
function externalLinks() {
	$('a').each(function() {
	  var a = new RegExp('/' + window.location.host + '/');
	  if(!a.test(this.href) && this.href != '') {
		  $(this).click(function(event) {
			  event.preventDefault();
			  event.stopPropagation();
			  window.open(this.href, '_blank');
		  });
	  }
	});
}


/* Код замены символов */
function charReplace() {
	if(!($("#content").html().indexOf('</textarea>') + 1)) {
		$('#content').find("*").each(function(indx, element){
		if (($(this).children().length == 0) && ($(this).html().search(/[▀▂▅▃▄␠]/) != -1)){
			$(this).contents().filter(function() {
				return this.nodeType === 3;
			}).replaceWith(function() {
				return this.nodeValue.replace(/([▀▂▅▃▄␠])/g,"<span style='font-family:wtfSymbols;'>$1</span>");
			});
		}
	});
	}
}

/* Код получения cookie */
function getCookie(name) {
	var matches = document.cookie.match(new RegExp(
		"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
	));
	return matches ? decodeURIComponent(matches[1]) : undefined;
}

function ttxZebra() {
	$('.ttx-table-line:nth-child(odd)').css('background-color', 'inherit');

	count = 0;

	$('.ttx-table-line').each(function(){
		if (!($(this).hasClass("ttx-collapsed"))){
			if (count % 2 == 0) {
				$(this).css('background-color','#DBE0E2');
			} else {
				$(this).css('background-color','inherit');
			}
			count++;
		}
	});
}

/* Код смены режима для карточек ТТХ */
function ttxSwichMode() {
    $(document).ready(function(){
		$('.ttx-unvisible').css('display','none');
		$('#ttx-rb-button').css('background-color','#263238');
		$('#ttx-rb-button').css('color','#EEEEEE');
		$('.ttx-ab').css('display','none');
		$('.ttx-sb').css('display','none');
		ttxZebra();
		var cookie = getCookie("ttxMode");
		if (cookie != undefined && navigator.cookieEnabled){
			switch (cookie) {
				case "ab":
					$('#ttx-ab-button').css('background-color','#263238');
					$('#ttx-ab-button').css('color','#EEEEEE');
					$('#ttx-rb-button').removeAttr("style");
					$('#ttx-sb-button').removeAttr("style");
					$('.ttx-ab').css('display','');
					$('.ttx-not-ab').addClass('ttx-collapsed');
					$('.ttx-rb').css('display','none');
					$('.ttx-sb').css('display','none');
					ttxZebra();
					break;
				case "rb":
					$('#ttx-rb-button').css('background-color','#263238');
					$('#ttx-rb-button').css('color','#EEEEEE');
					$('#ttx-ab-button').removeAttr("style");
					$('#ttx-sb-button').removeAttr("style");
					$('.ttx-ab').css('display','none');
					$('.ttx-not-ab').removeClass('ttx-collapsed');
					$('.ttx-rb').css('display','');
					$('.ttx-sb').css('display','none');
					ttxZebra();
					break;
				case "sb":
					$('#ttx-sb-button').css('background-color','#263238');
					$('#ttx-sb-button').css('color','#EEEEEE');
					$('#ttx-ab-button').removeAttr("style");
					$('#ttx-rb-button').removeAttr("style");
					$('.ttx-ab').css('display','none');
					$('.ttx-not-ab').removeClass('ttx-collapsed');
					$('.ttx-rb').css('display','none');
					$('.ttx-sb').css('display','');
					ttxZebra();
					break;
			}
		}
        $('.ttx-switch-mode-button').click(function() {
            switch ($(this).attr('id')) {
			  case "ttx-ab-button":
				$('#ttx-ab-button').css('background-color','#263238');
				$('#ttx-ab-button').css('color','#EEEEEE');
				$('#ttx-rb-button').removeAttr("style");
				$('#ttx-sb-button').removeAttr("style");
				$('.ttx-ab').css('display','');
				$('.ttx-not-ab').addClass('ttx-collapsed');
				$('.ttx-rb').css('display','none');
				$('.ttx-sb').css('display','none');
				document.cookie = "ttxMode=ab; path=/; expires=0";
				ttxZebra();
				break;
			  case "ttx-rb-button":
				$('#ttx-rb-button').css('background-color','#263238');
				$('#ttx-rb-button').css('color','#EEEEEE');
				$('#ttx-ab-button').removeAttr("style");
				$('#ttx-sb-button').removeAttr("style");
				$('.ttx-ab').css('display','none');
				$('.ttx-not-ab').removeClass('ttx-collapsed');
				$('.ttx-rb').css('display','');
				$('.ttx-sb').css('display','none');
				document.cookie = "ttxMode=rb; path=/; expires=0";
				ttxZebra();
				break;
			  case "ttx-sb-button":
				$('#ttx-sb-button').css('background-color','#263238');
				$('#ttx-sb-button').css('color','#EEEEEE');
				$('#ttx-ab-button').removeAttr("style");
				$('#ttx-rb-button').removeAttr("style");
				$('.ttx-ab').css('display','none');
				$('.ttx-not-ab').removeClass('ttx-collapsed');
				$('.ttx-rb').css('display','none');
				$('.ttx-sb').css('display','');
				document.cookie = "ttxMode=sb; path=/; expires=0";
				ttxZebra();
				break;
			}
        });
    });
}

/* Код разворачивающихся блоков ТТХ */
function ttxCollapseBlock(){
	$(document).ready(function(){
		$('.ttx-table-collapse').addClass('ttx-collapsed');
		ttxZebra();
		$('#ttx-short').css('background-color','#263238');
		$('#ttx-short').css('color','#EEEEEE');
		var cookie = getCookie("ttxCollapsed");
		if (cookie != undefined && navigator.cookieEnabled){
			if (cookie == 0){
				$('.ttx-table-collapse').addClass('ttx-collapsed');
				ttxZebra();
				$('#ttx-short').css('background-color','#263238');
				$('#ttx-short').css('color','#EEEEEE');
				$('#ttx-detail').removeAttr("style");
			} else {
				$('.ttx-table-collapse').removeClass('ttx-collapsed');
				ttxZebra();
				$('#ttx-detail').css('background-color','#263238');
				$('#ttx-detail').css('color','#EEEEEE');
				$('#ttx-short').removeAttr("style");
			}
		}
		$('.ttx-switch-size-button').click(function() {
			switch ($(this).attr('id')) {
			  case "ttx-short":
				$('.ttx-table-collapse').addClass('ttx-collapsed');
				$('#ttx-short').css('background-color','#263238');
				$('#ttx-short').css('color','#EEEEEE');
				$('#ttx-detail').removeAttr("style");
				document.cookie = "ttxCollapsed=0; path=/; expires=0";
				ttxZebra();
				break;
			  case "ttx-detail":
				$('.ttx-table-collapse').removeClass('ttx-collapsed');
				$('#ttx-detail').css('background-color','#263238');
				$('#ttx-detail').css('color','#EEEEEE');
				$('#ttx-short').removeAttr("style");
				document.cookie = "ttxCollapsed=1; path=/; expires=0";
				ttxZebra();
				break;
			}
		});
	});
}

/* Код кнопки "Купить" ТТХ карточек */
function ttxBuyButton() {
	var logout = document.getElementById('pt-logout');
	var el = document.getElementsByClassName("ttx-buy-button");
	var i;
	if (logout == null && getCookie('identity_name') === undefined) {
		var os = 'https://warthunder.ru/ru/free_registration?r=wiki';
		var log = '';
		if (navigator.userAgent.indexOf ('PlayStation')!= -1) os = 'https://store.playstation.com/ru-ru/product/EP4432-CUSA00182_00-WARTHUNDER000000';
		if (navigator.userAgent.indexOf ('Android')!= -1) os = 'https://play.google.com/store/apps/details?id=com.gaijinent.WarThunderCompanion';
		if (navigator.userAgent.indexOf ('iPhone')!= -1) os = 'https://itunes.apple.com/us/app/assistant-for-war-thunder/id899236988?l=ru&ls=1&mt=8';
		if (navigator.userAgent.indexOf ('iPad')!= -1) os = 'https://itunes.apple.com/us/app/assistant-for-war-thunder/id899236988?l=ru&ls=1&mt=8';
		if ((navigator.userAgent.indexOf ('Android')!= -1) || (navigator.userAgent.indexOf ('iPhone')!= -1) || (navigator.userAgent.indexOf ('iPad')!= -1)) {
			log = 'СКАЧАТЬ&nbsp;ASSISTANT';
		} else {
			log = 'СКАЧАТЬ&nbsp;ИГРУ';
		}
		for (i = 0; i < el.length; i++){
			el[i].innerHTML = log;
			el[i].setAttribute('onclick', "window.open ('" + os + "');");
		}
	} else {
		log = 'КУПИТЬ';
		for (i = 0; i < el.length; i++){
			var link = el[i].getAttribute('data-title');
			el[i].innerHTML = log;
			el[i].setAttribute('onclick', "yaCounter48855122.reachGoal('BUY_BUTTON'); window.open ('" + link + "'); return true;");
		}
	}
}

/* Код веток техники */
function treeGroup() {
	$(document).ready(function(){
		$('.tree-group-collapse').css('display', 'none');
		$('.tree-group-collapse').css('position', 'absolute');
		$(".tree-group").click(function (e) {
			$(".tree-group-collapse").slideUp();
			$(this).siblings(".tree-group-collapse").slideToggle("slow");
			e.stopPropagation();
		});
		$(document).on("click", "body", function(event) {
			if (event.target.className != 'tree-item-js')
				$(".tree-group-collapse").slideUp();
		});
	});
}

function ttxCockpit() {
	$(document).ready(function() {
		$('.ttx-cockpit').click(function( event ) {
			var cockpitId = $(this).attr('data-id');
			if(!($('#ttx-popup')[0])) {
				$('<div id="ttx-popup"></div><span id="ttx-popup-close">✖</span><div id="ttx-popup-overlay"></div>').prependTo('.content-area');
			}
			$("<iframe width='100%' height='100%' src='https://roundme.com/embed/" + cockpitId + "' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>").prependTo('#ttx-popup');
			$('#ttx-popup-overlay').fadeIn(400,
				function(){
					$('#ttx-popup').css('display', 'block').animate({opacity: 1}, 200); 
			});
		});
	});
	
	$(document).on("click", '#ttx-popup-close, #ttx-popup-overlay', function(){
		$('#ttx-popup').animate({opacity: 0}, 200, 
				function(){ 
					$('#ttx-popup').remove();
					$('#ttx-popup-close').css('display', 'none');
					$('#ttx-popup-overlay').fadeOut(400);
				}
			);
	});
}

$(document).ready(function(){
    externalLinks();
    charReplace();
    ttxSwichMode();
    ttxCollapseBlock();
    ttxBuyButton();
    treeGroup();
    ttxCockpit();
});