用户:850710247liu/common.js

来自萌娘文库
< 用户:850710247liu
850710247liu讨论 | 贡献2021年3月11日 (四) 20:38的版本 (创建页面,内容为“这里的任何JavaScript将为所有用户在每次页面载入时加载。: // 判断是否触摸屏幕,强制设置ua if (navigator.maxTouchPoints && nav…”
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */
// 判断是否触摸屏幕,强制设置ua
  if (navigator.maxTouchPoints && navigator.maxTouchPoints > 2) {
    Object.defineProperty(navigator, 'userAgent', {
      value: navigator.userAgent.replace('Macintosh', 'ipadpro'),
      writable: false
    });
  };
  //地图缓存问题修正
  for(var key in window.localStorage){
            if(window.localStorage.hasOwnProperty(key) && key.toString() != 'wikiYsOpacData' ){
               window.localStorage.removeItem(key);
            }
        }
//地图
window.HOST_TYPE = "2"; window.BMap_loadScriptTime = (new Date).getTime();
const ossVersion = '3.0.13';
window.imgossVersion = '3.0.13';window.orderData = []
$.when(
	mw.loader.load("https://wiki.biligame.com/ys/MediaWiki:Map.css?action=raw&ctype=text/css","text/css"))
	.then(function(){
		$.getScript("https://wiki.biligame.com/ys/MediaWiki:Map.js?action=raw&ctype=text/javascript");
        $(".mapBtnGroup").append('<div class="btn-wrap custom-select" style="width:108px"><select class="btn-i18n">\n  <option value="zh">Language</option>\n  <option value="zh">简体中文</option>\n  <option value="en">English</option>\n  <option value="jp">日本語</option>\n</select></div>');
		$("<input>").attr({"id":"searchBox","type":"text"}).css("height","20px")
			.appendTo($("<div>").attr("class","mapSearch").append($("<span>").html("搜索:")).insertBefore($(".markerList")))
			.on('input',function () {
			    $(".select-item").stop().hide()
			        .filter(":contains('"+$(this).val()+"')").show();
			    $(".select-item").parents(".orderItem").stop().hide()
			        .filter(":contains('"+$(this).val()+"')").show();
		});
		var x, i, j, l, ll, selElmnt, a, b, c;
		/*look for any elements with the class "custom-select":*/
		x = document.getElementsByClassName("custom-select");
		l = x.length;
		for (i = 0; i < l; i++) {
			selElmnt = x[i].getElementsByTagName("select")[0];
			ll = selElmnt.length;
			/*for each element, create a new DIV that will act as the selected item:*/
			a = document.createElement("DIV");
			a.setAttribute("class", "select-selected");
			a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
			x[i].appendChild(a);
			/*for each element, create a new DIV that will contain the option list:*/
			b = document.createElement("DIV");
			b.setAttribute("class", "select-items select-hide");
			for (j = 1; j < ll; j++) {
			    /*for each option in the original select element,
			    create a new DIV that will act as an option item:*/
			    c = document.createElement("DIV");
			    c.innerHTML = selElmnt.options[j].innerHTML;
			    c.addEventListener("click", function(e) {
			        /*when an item is clicked, update the original select box,
			        and the selected item:*/
			        var y, i, k, s, h, sl, yl;
			        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
			        sl = s.length;
			        h = this.parentNode.previousSibling;
			        for (i = 0; i < sl; i++) {
						if (s.options[i].innerHTML == this.innerHTML) {
				            s.selectedIndex = i;
				            h.innerHTML = this.innerHTML;
				            y = this.parentNode.getElementsByClassName("same-as-selected");
				            yl = y.length;
				            for (k = 0; k < yl; k++) {
				            	y[k].removeAttribute("class");
				            }
				            this.setAttribute("class", "same-as-selected");
							$(".btn-i18n").trigger('change');
				            break;
		        		}
		        	}
		        	h.click();
		    	});
		    	b.appendChild(c);
			}
			x[i].appendChild(b);
			a.addEventListener("click", function(e) {
				/*when the select box is clicked, close any other select boxes,
				and open/close the current select box:*/
				e.stopPropagation();
				closeAllSelect(this);
				this.nextSibling.classList.toggle("select-hide");
				this.classList.toggle("select-arrow-active");
		    });
		}
		function closeAllSelect(elmnt) {
			/*a function that will close all select boxes in the document,
			except the current select box:*/
			var x, y, i, xl, yl, arrNo = [];
			x = document.getElementsByClassName("select-items");
			y = document.getElementsByClassName("select-selected");
			xl = x.length;
			yl = y.length;
			for (i = 0; i < yl; i++) {
				if (elmnt == y[i]) {
					arrNo.push(i)
				} else {
					y[i].classList.remove("select-arrow-active");
				}
			}
			for (i = 0; i < xl; i++) {
				if (arrNo.indexOf(i)) {
					x[i].classList.add("select-hide");
				}
			}
		}
		/*if the user clicks anywhere outside the select box,
		then close all select boxes:*/
		document.addEventListener("click", closeAllSelect);
		console.log("C");
		$(".allScreen").on("click touchstart", (function() {
		    $(".map-wrap").toggleClass("fs");
		    if($(".map-wrap").parent()[0].tagName=="BODY") {
		        $("#mapDiv").prepend($(".map-wrap"));
		    }else{
		        $("body").prepend($(".map-wrap"));
		    }
	    }));
	});

$(function() {
	//解决手机上按键选项hover效果不消失的问题
	 
	
	//筛选页面列表筛选
	$('.filter li').on('mouseenter touchstart',function(e) {
		$(this).children().addClass('hover');
	});
	
	$('.filter li').on('mouseleave touchend',function(e) {
		$(this).children().removeClass('hover');
	});
	
	$('.filter li').on('click',function(e) {
		$(this).siblings().children().removeClass('active');
		$(this).children().toggleClass('active');
		var item = new Array();
		$('.filter').find('.active').parent().each(function() {
			var data_type = $(this).parent().attr('data-type');
			item.push("[data-param" + data_type + "='" + $(this).attr("data-value") + "']");
		});
		
		if(item.length != 0) {
			$('#wiki_table tbody').children().hide(400);
			$('#wiki_table tbody').children(item.toString().replace(/,/g, '')).show(300);
			//$('#wiki_table tbody').children().not(item.toString().replace(/,/g, '')).hide(1000);
		} else {
			$('#wiki_table tbody').children().show(300);
		}
	});
});
$(function InitCardSelect() {
	var self = {};
	
	var selectOptions;			//所有筛选按钮
	var filters = {};			//当前筛选规则
	var filterKeys = [];		//所有筛选Key
	var showAll = true;			//不筛选,显示全部
	var tableRows;				//所有要被筛选的行
	var computedRows;			//筛选后的行
	var $sorter;				//当前进行排序的表头
	var sortDesent = false;		//反向排序
	
	function isNil(s) {
		return s === '' || s === undefined || s === null || s === false
	}
	
	//点击筛选按钮时
	function OnSelectOptionClick(e) {
		var $x = e.data;
		e.preventDefault();
		
		var label = '['+$.trim($x.text())+'] 筛选';
		console.time(label);
		
		//显示全部
		if ($x.FilterKey == 0) {
			for (i in selectOptions) {
				selectOptions[i].Select = false;
				selectOptions[i].removeClass('selected');
			}
			filters = {};
			showAll = true;
			
			//更新表格
			self.FilterRows();
			console.timeEnd(label);
			return;
		}
		showAll = false;
		
		var select = !$x.Select;
		$x.Select = select;
		
		//变更筛选状态
		var key = $x.FilterKey;
		filters[key] = filters[key] || {};
		
		var opt = $x.FilterOpt == "AND" ? "AND" : "OR";
		filters[key][opt] = filters[key][opt] || [];
		if (select) {
			filters[key][opt].push($x.FilterValue);
		} else {
			filters[key][opt] = filters[key][opt].filter(function(x) {
				return x !== $x.FilterValue;
			});
			if (filters[key][opt].length < 1) delete filters[key][opt];
		}
		
		
		select ? $x.addClass('selected') : $x.removeClass('selected');
		self.FilterRows();
		console.timeEnd(label);
		
	}
	
	
	function OnHeaderSortClick(e) {
		var $x = e.data;
		e.preventDefault();
		
		var label = '['+$.trim($x.text())+'] 排序';
		console.time(label);
		if ($sorter !== $x) {
			//第一次:新的排序
			$sorter = $x;
			sortDesent = false;
		} else if (!sortDesent) {
			//第二次:切换排序
			sortDesent = true;
		} else {
			//第三次:不进行排序
			$sorter = null;
		}
		
		self.SortRows();
		
		console.timeEnd(label);
	}

	//初始化
	self.Init = function () {
		selectOptions = $('.cardSelectOption').toArray().map(function(x) {
			var $x = $(x);
			
			var dataOption = $.trim($x.data('option'));
			var dataGroup = $.trim($x.data('group'));
			
			if (isNil(dataOption) || isNil(dataGroup)) return $x;
			var splt = dataOption.split('|');
			$x.FilterKey = dataGroup;
			$x.FilterValue = $.trim(splt[1]);
			$x.FilterOpt = $.trim($x.data('opt') || 'OR').toUpperCase();
			
			if (filterKeys.indexOf(dataGroup) < 0)
				filterKeys.push(dataGroup);
			
			$x.click($x, OnSelectOptionClick);
			
			return $x;
		});
		
		//$head = $('#CardSelectTabHeader');
		
		tableRows = $('#CardSelectTr>tbody>tr').toArray().map( function(x) {
			var $x = $(x);
			
			//if (x == $head[0]) continue;
			
			for (i in filterKeys) {
				var key = filterKeys[i];
				var val = $x.data('param'+key);
				if (val === undefined) continue;
				
				if (typeof val == 'number'){
					val = val.toString(10);
				}
				
				var splt = val.split(',');
				for (j in splt) {
					var s = $.trim(splt[j]);
					if (!isNil(s)) {
						$x.FilterData = $x.FilterData || {};
						$x.FilterData[key] = $x.FilterData[key] || [];
						$x.FilterData[key].push(s);
					}
				}
				
			}
			
			return $x;
		});
		
		//computedRows = tableRows.concat([]);
		
		$('#CardSelectTr>thead>tr>th').each(function(index, x) {
			var $x = $(x);
			$x.off('click').off('mousedown');
			
			if ($x.hasClass('headerSort')) {
				$x.Index = index;
				//$x.SortKey = $x.data('group');
				$x.click($x, OnHeaderSortClick);
			}
		});
		
	}
	
	//t1中包含t2中所有项
	function includeAll(t1, t2) {
		for (i in t2) {
			if (t1.indexOf(t2[i]) < 0) return false;
		}
		return true;
	}
	//t1中包含t2中的任一项
	function includeAny(t1, t2) {
		if (t2.length == 0) return true;
		for (i in t2) {
			if (t1.indexOf(t2[i]) > -1) return true;
		}
		return false;
	}
	
	
	//使用show/hide来显示表格行
	self.FilterRows = function() {
		if (showAll) {
			filters = {};
		}
		
		$('#CardSelectTr>tbody').hide();
		
		for (var idx in tableRows) {
			var $x = tableRows[idx];
			var data = $x.FilterData;
			var hide = false;
			
			for (key in filters) {
				hide = !data || !data[key];
				
				if (!hide && filters[key]['AND']) {
					hide = !includeAll(data[key],filters[key]['AND']);
				}
				if (!hide && filters[key]['OR']) {
					hide = !includeAny(data[key],filters[key]['OR']);
				}
				if (hide) break;
			}
			
			if (!hide != !$x.Hide) {
				$x.Hide = hide;
				hide ? $x.hide() : $x.show();
			}
		}
		
		
		$('#CardSelectTr .headerSort').removeClass('headerSortDown headerSortUp');
		$('#CardSelectTr>tbody').show();
		$sorter = null;
	}
	
	
	function grabSortData($tr, index) {
		$tr.SortData = $tr.SortData || [];
		if ($tr.SortData[index] !== undefined) return $tr.SortData[index];
		
		var td = $tr.children('td')[index];
		if (!td) {
			$tr.SortData[index] = -1;
			return -1;
		}
		
		var text = $.trim(td.textContent);
		$tr.SortData[index] = text;
		return text;
	}
	
	//对行排序,不显示的行会被移除
	self.SortRows = function() {
		
		//var residues = [];
		
		if (!$sorter) {
			computedRows = tableRows.filter(function($x) { return !$x.Hide; });
			$('#CardSelectTr .headerSort').removeClass('headerSortDown headerSortUp');
		}
		else if (!sortDesent) {
			var index = $sorter.Index;
			computedRows = tableRows.filter(function($x) { return !$x.Hide; });
			computedRows.sort(function compareFunction($x, $y) {
				var x = grabSortData($x, index);
				var y = grabSortData($y, index);
				var nx = + x;
				var ny = + y;
				
				return (isNaN(nx) || isNaN(ny)) 
						? isNaN(nx) && isNaN(ny) 
							? x < y ? -1 : 1
							: isNaN(nx) ? 1 : -1	//字符串排后
						: nx === ny
							? 0
							: nx < ny ? -1 : 1;		//数值小的排前
				//console.log (`${x} ~ ${y} => ${ret}`);
				//return ret;
			});
			$('#CardSelectTr .headerSort').removeClass('headerSortDown headerSortUp');
			$sorter.addClass('headerSortDown');
			
		}
		else {
			computedRows.reverse();
			$('#CardSelectTr .headerSort').removeClass('headerSortDown headerSortUp');
			$sorter.addClass('headerSortUp');
		}
		
		$('#CardSelectTr>tbody').hide()
			.prepend(computedRows)
			.show();
	}
	
	
	$('.cardSelectOption').off('click').off('mousedown');

	if ($('#CardSelectTabHeader').parent().is('tbody')){
		$('#CardSelectTr').prepend( $('<thead/>').append($('#CardSelectTabHeader')));
	}
	
	self.Init();
	
	console.log('CardSelectTr.js Initialized.');
	return self;
});

/* -- 为页面加载JS脚本或CSS样式表 -- */
/* 参见[[模板:ResourceLoader]]*/
/* 来源:碧蓝航线WIKI*/
$('.resourceLoader').each(function () {
  var $x = $(this);
  var text = $.trim($x.text());

  if (!text) return;

  //加载模块
  if ($x.data('isModule') === true)
    return mw.loader.load(text);

  //自动补充MediaWiki命名空间
  var ns = text.match('^.*?:');
  if (!ns) text = 'MediaWiki:' + text;

  //加载CSS样式表
  var mime = ($x.data('mime') || "text/javascript").toLowerCase();
  if (mime == "text/css") {
    if (text.slice(-4).toLowerCase() !== '.css') text = text + '.css';
    //if ($x.data('debug') !== true) text = text + '&debug=false';
    return mw.loader.load("//wiki.biligame.com/ys/index.php?title=" + text + "&action=raw&ctype=text/css", "text/css");
  }

  //加载JS脚本
  if (ns && ns[0].toLowerCase() !== 'mediawiki:') {
    return console.log('ResourceLoader: 不允许加载MediaWiki以外的js脚本');
  }
  if (text.slice(-3).toLowerCase() !== '.js') text = text + '.js';
  //if ($x.data('debug') !== true) text = text + '&debug=false';
  return mw.loader.load("//wiki.biligame.com/ys/index.php?title=" + text + "&action=raw&ctype=text/javascript", "text/javascript");
});


//音频事件
$('.audio-controls').click(function () {
    var audio = $(this).parent().find('audio').get(0);
    var controlBtn = $(this).find('.icon-btn');
    audio.addEventListener('ended', function() {
      audio.currentTime = 0;
      audio.pause();
      controlBtn.removeClass('icon-pause').addClass('icon-play');
    }, false); // 监听播放完成事件
    // 改变暂停/播放icon
    if (audio.paused) {
      audio.play();
      controlBtn.removeClass('icon-play').addClass('icon-pause');
    } else {
      audio.pause();
      controlBtn.removeClass('icon-pause').addClass('icon-play');
    }
  })
  
  
/* ==Popover弹出框== */
//找到Bootstrap初始化的jQuery
function getjQuery(jquery) {
        var x = [jquery, $, window.$, window.jQuery];
        var _j = window.$;
        
        for (var i = 0; i < 4; i++) {
                var j = x[i];
                if (j && j.fn) {
                        if (typeof j.fn.popover == 'function')
                                return j;
                        j.noConflict();
                        j = window.$;
                        window.$ = _j;
                        if (j && j.fn && typeof j.fn.popover == 'function')
                                return j;
                }
        }
        throw('getjQuery:未能找到Bootstrap初始化的jQuery');
}
$(function() {
        var $ = getjQuery();
        
        $('[data-toggle="wafer-popover"]').popover({
                container: false,
                content: function() {
                        var $c = $(this);
                        var content = '<p class="head">' + ($c.data('head') || '详细信息') + '</p>'
                                                + '<p>' + $c.data('text') + '</p>';
                        return content;
                },
                html: true,
                placement: 'auto bottom',
                template: 
'<div class="popover wafer-popover" role="tooltip"><div class="arrow"></div><div class="popover-content"></div></div>',
                trigger: 'hover'
        }).find('a').attr('title','');
});
/* --Popover弹出框-- */