Greasy Fork is available in English.

Virtonomica: фильтр в окне снабжения

Добавляет фильтр в окно снабжения

Versão de: 10/05/2015. Veja: a última versão.

// ==UserScript==
// @name           Virtonomica: фильтр в окне снабжения
// @namespace      virtonomica
// @version 	   1.2
// @description    Добавляет фильтр в окно снабжения
// @include        http://*virtonomic*.*/*/window/unit/supply/multiple/vendor:*/product:*/brandname:*
// ==/UserScript==

var run = function() {

    var win = (typeof(unsafeWindow) != 'undefined' ? unsafeWindow : top.window);
    $ = win.$;

	var filterByCountry = '<option value="0">&nbsp;</option>';
	var filterByRegion = '<option value="0">&nbsp;</option>';
	var filterByTown = '<option value="0">&nbsp;</option>';
	var filterByUnitType = '<option value="0">&nbsp;</option>';
	
	/////////////////
	var countries = new Array();
    $('table[class="list"] > tbody > tr > td:nth-child(2) > img').each(function(){
		var img = $(this);
		var country = img.attr('title');
		countries[country] = 1;
    });
	for (key in countries) {
		filterByCountry = filterByCountry + '<option>'+key+'</option>';
	}
	/////////////////
	var regions = new Array();
    $('table[class="list"] > tbody > tr > td:nth-child(2)').each(function(){
		var cell = $(this);
        var first = cell.html().indexOf('&nbsp;') + 6;
        var second = cell.html().indexOf('<br>');
        var region = cell.html().substr(first, second - first);
		regions[region] = 1;
    });
	for (key in regions) {
		if(key != ''){
			filterByRegion = filterByRegion + '<option>'+key+'</option>';
		}
	}
	/////////////////
	var towns = new Array();
    $('table[class="list"] > tbody > tr > td:nth-child(2) > b').each(function(){
		var cell = $(this);
        var town = cell.text();
		towns[town] = 1;
    });
	for (key in towns) {
		if(key != ''){
			filterByTown = filterByTown + '<option>'+key+'</option>';
		}
	}
	/////////////////
	var types = new Array();
    $('table[class="list"] > tbody > tr > td:nth-child(3) > a:nth-child(2)').each(function(){
		var row = $(this);
		var type = row.text();
        var matches = row.text().match(/\(([^)]+)\)$/);
		if(matches != null && matches.length > 1){
			type = matches[1];
		} 
		types[type] = 1;
    });
	for (key in types) {
		filterByUnitType = filterByUnitType + '<option>'+key+'</option>';
	}
	/////////////////
    $('table[class="list"]').first().before('<select id="filterByCountry">'+filterByCountry+'</select>');  
    $('table[class="list"]').first().before('<select id="filterByRegion">'+filterByRegion+'</select>');  
    $('table[class="list"]').first().before('<select id="filterByTown">'+filterByTown+'</select>');  
    $('table[class="list"]').first().before('<select id="filterByUnitType">'+filterByUnitType+'</select>'); 
	///////////////// 
	
	$('#filterByCountry').change( function(){
		var search = $(this).val();

		$('table[class="list"] > tbody > tr > td:nth-child(2) > img').each(function() {
			var img = $(this);
			var country = img.attr('title');
			if (search == '0' || country == search ){
				$(this.parentNode.parentNode).show();
			} else {
				$(this.parentNode.parentNode).hide();
			}
		});
		$('#filterByRegion').val('0');
		$('#filterByTown').val('0');
		$('#filterByUnitType').val('0');
	});
	$('#filterByRegion').change( function(){
		var search = $(this).val();

		$('table[class="list"] > tbody > tr > td:nth-child(2)').each(function() {
			var cell = $(this);
			var first = cell.html().indexOf('&nbsp;') + 6;
			var second = cell.html().indexOf('<br>');
			var region = cell.html().substr(first, second - first);
			if (search == '0' || search == region ){
				$(this.parentNode).show();
			} else {
				$(this.parentNode).hide();
			}
		});
		$('#filterByCountry').val('0');
		$('#filterByTown').val('0');
		$('#filterByUnitType').val('0');
	});
	$('#filterByTown').change( function(){
		var search = $(this).val();

		$('table[class="list"] > tbody > tr > td:nth-child(2) > b').each(function() {
			var cell = $(this);
			var town = cell.text();
			if (search == '0' || search == town) {
				$(this.parentNode.parentNode).show();
			} else {
				$(this.parentNode.parentNode).hide();
			}
		});
		$('#filterByCountry').val('0');
		$('#filterByRegion').val('0');
		$('#filterByUnitType').val('0');
	});
	$('#filterByUnitType').change( function(){
		var search = $(this).val();

		$('table[class="list"] > tbody > tr > td:nth-child(3) > a:nth-child(2)').each(function() {
			var row = $(this);
        	var matches = row.text().match(/\(([^)]+)\)$/);
			if (search == '0' || row.text() == search || (matches != null && matches[1] == search) ){
				$(this.parentNode.parentNode).show();
			} else {
				$(this.parentNode.parentNode).hide();
			}
		});
		$('#filterByCountry').val('0');
		$('#filterByRegion').val('0');
		$('#filterByTown').val('0');
	});
}

if(window.top == window) {
    var script = document.createElement("script");
    script.textContent = '(' + run.toString() + ')();';
    document.documentElement.appendChild(script);
}