Greasy Fork is available in English.

Virtonomica: цена/качество

Расчет соотношения цена/качество

  1. // ==UserScript==
  2. // @name Virtonomica: цена/качество
  3. // @namespace SemenovArt
  4. // @description Расчет соотношения цена/качество
  5. // @include http://virtonomic*.*/*/window/unit/supply/create/*/step2
  6. // @version 0.1
  7. // ==/UserScript==
  8.  
  9.  
  10.  
  11.  
  12.  
  13. var run = function() {
  14.  
  15. /////////CONFIG START\\\\\\\\\\\
  16. var sort_on_load = true; //сортировка после загрузки страницы
  17. var sorting = 'asc'; //направление сортировки по умолчанию: asc - по взрастанию, desc по убыванию
  18. //////////CONFIG END\\\\\\\\\\\\
  19.  
  20. var win = (typeof(unsafeWindow) != 'undefined' ? unsafeWindow : top.window);
  21. $ = win.$;
  22.  
  23.  
  24. var sorting_asc;
  25. var table = $("table[class='unit-list-2014']");
  26. var head = table.find("thead:first").find("tr:first"); //находим заголовок таблицы
  27. var arrows = '<th onmouseover="this.className = \'filter_light\'" onmouseout="this.className = \'\'" onclick="sort_by_price(\'asc\');" class="">';
  28. arrows += '<div class="field_title">Цена/кач'
  29. arrows += '<div class="asc" title="сортировка по возрастанию"><a href="#" onclick="sort_by_price(\'asc\');"><img id="asc_sort_price" src="/img/up_gr_sort.png"></a></div>'
  30. arrows += '<div class="desc" title="сортировка по убыванию"><a href="#" onclick="sort_by_price(\'asc\');"><img id="desc_sort_price" src="/img/down_gr_sort.png"></a></div>'
  31. arrows += '</div></th>'
  32.  
  33. head.find("th:nth-child(4)").after(arrows);
  34. head.find('th').css('background', '#5bd65b');
  35.  
  36. function row(id, price) {
  37. this.id = id;
  38. this.price = price;
  39. }
  40. var prices = [];
  41.  
  42. table.find('tbody tr').each(function(i, val) {
  43. if ($(this).attr('class') == 'ordered') { $(this).find("td:first").attr('colspan', '10'); } //зеленую полоску с информацией о заказе растягиваем и пропускаем
  44. if ($(this).attr('id') == undefined || $(this).attr('id')[0] != 'r') { return; }
  45. var cels = $('td', this);
  46. var price = parseFloat($(cels[5]).text().replace(/ /g, ''));
  47. var qual = parseFloat($(cels[6]).text().replace(/ /g, ''));
  48. var qp = 0.00;
  49.  
  50. if (!isNaN(price) && !isNaN(qual))
  51. var qp = (price / qual).toFixed(2);
  52.  
  53.  
  54. $(cels[5]).after('<td class="digits">' + ((qp == 0) ? '---' : (qp + '$')) + '</td>'); //добавляем столбец в каждую строку
  55.  
  56. prices[i] = new row($(this).attr('id'), qp); //будем хранить id строки продукта и цену, для дальнейшей сортировки
  57. });
  58.  
  59.  
  60. if (sort_on_load) sort_by_price(sorting);
  61.  
  62. function sortFunc(a, b){
  63. return (b['price'] - a['price']) * sorting_asc
  64. }
  65.  
  66.  
  67.  
  68. function sort_by_price(direction){
  69. var table = $("table[class='unit-list-2014']");
  70. sorting_asc = (direction == 'asc') ? 1 : -1;
  71. img = $('#' + direction + '_sort_price');
  72.  
  73. $( "th[class='active_sort']" ).attr('class', '').attr('onmouseout', "this.className = ''");
  74. $( "img[src='/img/down_wh_sort.png']" ).attr('src', '/img/down_gr_sort.png');
  75. $( "img[src='/img/up_wh_sort.png']" ).attr('src', '/img/up_gr_sort.png');
  76. $( "div[class='desc hide_active']" ).attr('class', 'desc');
  77. $( "div[class='asc hide_active']" ).attr('class', 'asc');
  78.  
  79.  
  80. prices.sort(sortFunc);
  81. for (var i = prices.length - 1; i >= 0; i--) {
  82. if(prices[i] == null) continue;
  83. var row = $('#'+prices[i]['id']);
  84. //если строка с заказом то под нее добавляем зеленую строчку с информацией о заказе
  85. if(row.attr('class') == 'ordered_offer')
  86. {
  87. row.prev().appendTo(table);
  88. row.appendTo(table);
  89. $('#ordered'+(prices[i]['id']).substring(1)).appendTo(table);
  90. continue;
  91. }
  92. row.prev().appendTo(table);
  93. row.appendTo(table);
  94. //if ((i % 2) == 1) row.attr('class', 'odd'); //else $('#'+row['id']).css('background', '#ffffff');//перекрашиваем нечетные строки в серый
  95. };
  96.  
  97. }
  98.  
  99. }
  100.  
  101. // Хак, что бы получить полноценный доступ к DOM
  102. var script = document.createElement("script");
  103. script.textContent = '(' + run.toString() + ')();';
  104. document.documentElement.appendChild(script);