labStartProjectFilter

Отмечает в выпадающем списке запуска исследований технологии, которые уже исследуются вами

当前为 2016-10-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name labStartProjectFilter
  3. // @description Отмечает в выпадающем списке запуска исследований технологии, которые уже исследуются вами
  4. // @description fix for new design 2014
  5. // @author cobra3125
  6. // @license MIT
  7. // @version 1.53
  8. // @include https://virtonomica.*/*/main/company/view/*/unit_list
  9. // @include https://virtonomica.*/*/window/unit/view/*/project_create
  10. // @include https://virtonomica.*/*/main/management_action/*/investigations/technologies
  11. // @include https://virtonomica.*/*/main/unit/view/*/investigation
  12. // @namespace https://greasyfork.org/users/2055
  13. // ==/UserScript==
  14.  
  15. // [1] Оборачиваем скрипт в замыкание, для кроссбраузерности (opera, ie)
  16. (function (window, undefined) {
  17.  
  18. // [2] нормализуем window
  19. var w;
  20.  
  21. if (typeof unsafeWindow != undefined) {
  22. w = unsafeWindow
  23. } else {
  24. w = window;
  25. }
  26.  
  27. // [3] не запускаем скрипт во фреймах
  28. // без этого условия скрипт будет запускаться несколько раз на странице с фреймами
  29. if (w.self != w.top) {
  30. return;
  31. }
  32.  
  33. // a function that loads jQuery and calls a callback function when jQuery has finished loading
  34. function addJQuery(callback) {
  35. var script = document.createElement("script");
  36. script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js");
  37. script.addEventListener('load', function () {
  38. var script = document.createElement("script");
  39. script.textContent = "(" + callback.toString() + ")();";
  40. document.body.appendChild(script);
  41. }, false);
  42. document.body.appendChild(script);
  43. }
  44.  
  45.  
  46. // the guts of this userscript
  47. function main() {
  48. // [4] дополнительная проверка наряду с @include
  49. //begin[0] нужно для локального хранилища
  50. (function($) {
  51. // Private data
  52. var isLS=typeof window.localStorage!=='undefined';
  53. // Private functions
  54. function wls(n,v){var c;if(typeof n==="string"&&typeof v==="string"){localStorage[n]=v;return true;}else if(typeof n==="object"&&typeof v==="undefined"){for(c in n){if(n.hasOwnProperty(c)){localStorage[c]=n[c];}}return true;}return false;}
  55. function wc(n,v){var dt,e,c;dt=new Date();dt.setTime(dt.getTime()+31536000000);e="; expires="+dt.toGMTString();if(typeof n==="string"&&typeof v==="string"){document.cookie=n+"="+v+e+"; path=/";return true;}else if(typeof n==="object"&&typeof v==="undefined"){for(c in n) {if(n.hasOwnProperty(c)){document.cookie=c+"="+n[c]+e+"; path=/";}}return true;}return false;}
  56. function rls(n){return localStorage[n];}
  57. function rc(n){var nn, ca, i, c;nn=n+"=";ca=document.cookie.split(';');for(i=0;i<ca.length;i++){c=ca[i];while(c.charAt(0)===' '){c=c.substring(1,c.length);}if(c.indexOf(nn)===0){return c.substring(nn.length,c.length);}}return null;}
  58. function dls(n){return delete localStorage[n];}
  59. function dc(n){return wc(n,"",-1);}
  60.  
  61. /**
  62. * Public API
  63. * $.Storage - Represents the user's data store, whether it's cookies or local storage.
  64. * $.Storage.set("name", "value") - Stores a named value in the data store.
  65. * $.Storage.set({"name1":"value1", "name2":"value2", etc}) - Stores multiple name/value pairs in the data store.
  66. * $.Storage.get("name") - Retrieves the value of the given name from the data store.
  67. * $.Storage.remove("name") - Permanently deletes the name/value pair from the data store.
  68. */
  69. $.extend({
  70. Storage: {
  71. set: isLS ? wls : wc,
  72. get: isLS ? rls : rc,
  73. remove: isLS ? dls :dc
  74. }
  75. });
  76. })(jQuery);
  77. //end[0]
  78. function MultDimArrToStr(anArray){
  79. var str = '';
  80. var len = anArray.length;
  81. for (var i = 0; i < len; ++i) {
  82. if(str != ''){
  83. str += '|';
  84. }
  85. str += anArray[i]['id'] + ';' + anArray[i]['name'] + ';' + anArray[i]['tech'];
  86. }
  87. //str == "4224597;Тракторный завод|4224597;Тракторный завод|4224597;Тракторный завод";
  88. return str;
  89. }
  90.  
  91. function StrToMultDimArr(str){
  92. //alert(str);
  93. //var str = "4224597;Тракторный завод|4224597;Тракторный завод|4224597;Тракторный завод";
  94. var tempArray = str.split('|');
  95. var finalArray = new Array();
  96. var len = tempArray.length;
  97. for (var i = 0; i < len; ++i) {
  98. var tmp = tempArray[i].split(';');
  99. finalArray.push({
  100. id: tmp[0],
  101. name: tmp[1],
  102. tech: tmp[2]
  103. });
  104. }
  105. return finalArray;
  106. }
  107. function inMultDimArray(value, anArray, attr){
  108. var theIndex = -1;
  109. var len = anArray.length;
  110. for (var i = 0; i < len; ++i) {
  111. if (anArray[i][attr].toUpperCase() == value.toUpperCase()) {
  112. theIndex = i;
  113. break;
  114. }
  115. }
  116. return theIndex;
  117. }
  118. function delByVal(anArray, attr, value){
  119. var result = new Array();
  120. var len = anArray.length;
  121. for (var i = 0; i < len; ++i) {
  122. if (anArray[i][attr] != value) result.push(anArray[i]);
  123. }
  124. return result;
  125. }
  126. function countInDimArray(value, anArray){
  127. var cnt = 0;
  128. var len = anArray.length;
  129. for (var i = 0; i < len; ++i) {
  130. if (anArray[i]['name'] == value) {
  131. ++cnt;
  132. }
  133. }
  134. return cnt;
  135. }
  136. function distinct(anArray) {
  137. var result = new Array();
  138. var len = anArray.length;
  139. for (var i = 0; i < len; ++i) {
  140. if (inMultDimArray(anArray[i]['id'], result, 'id') == -1) result.push(anArray[i]);
  141. }
  142. return result;
  143. }
  144. function sort(anArray){
  145. return anArray.sort(function(a, b) {
  146. var compA = a['name'].toUpperCase();
  147. var compB = b['name'].toUpperCase();
  148. var compTA = parseFloat(a['tech'].trim());
  149. var compTB = parseFloat(b['tech'].trim());
  150. return (compA < compB) ? -1 : (compA > compB) ? 1 : (compTA > compTB) ? -1 : (compTA < compTB) ? 1 : 0;
  151. });
  152. }
  153. // @include http://virtonomica.*/*/main/unit/view/*/investigation
  154. //для определения изучаемой в открытой лабе технологии
  155. if (/https:\/\/virtonomica\.\w+\/\w+\/main\/unit\/view\/\w+\/investigation/.test(window.location)) {
  156. var arr = StrToMultDimArr( $.Storage.get('labStartFilterArray') );
  157. if (!$.isArray(arr)){
  158. arr = new Array();
  159. }
  160. var id = (window.location + '').trim().split('/');
  161. id = id[id.length-2];
  162. $('td[class="title"]').each(function() {
  163. if (jQuery(this).text() == 'Текущее исследование'){
  164. var texhName = jQuery(this).parent().children('td').eq(1).children('a').eq(1).text();
  165. var texhLvl = jQuery(this).parent().parent().children('tr').eq(1).children('td').eq(1).children('span').text();
  166. arr = delByVal(arr, 'id', id);
  167. arr.push({
  168. id: id,
  169. name: texhName,
  170. tech: texhLvl
  171. });
  172. arr = sort(arr);
  173. arr = distinct(arr);
  174. $.Storage.set('labStartFilterArray', MultDimArrToStr(arr));
  175. }
  176. });
  177. }
  178. //для сбора списка уже изученных технологий
  179. if (/https:\/\/virtonomica\.\w+\/\w+\/main\/management_action\/\d+\/investigations\/technologies[#]{0,1}/.test(window.location)) {
  180. var arr = new Array();
  181. $('div[class="tech_d"], div[class="tech_s"]').children('a').each(function() {
  182. var id = jQuery(this).attr('href').trim().split('/');
  183. id = id[id.length-1];
  184. arr.push({
  185. id: id,
  186. name: jQuery(this).parent().parent().parent().children('div[class="tech_title_cell"]').children('b').text().trim(),
  187. tech: jQuery(this).text().trim()
  188. });
  189. });
  190. arr = sort(arr);
  191. //alert(MultDimArrToStr(arr));
  192. arr = distinct(arr);
  193. //alert(MultDimArrToStr(arr));
  194. $.Storage.set('labSFTechDoneArray', MultDimArrToStr(arr));
  195. //console.log(arr);
  196. }
  197. //для добавления в выпадающий список отметок о текущих исследованиях
  198. if (/https:\/\/virtonomica\.\w+\/\w+\/window\/unit\/view\/\d+\/project_create$/.test(window.location)) {
  199. var arr = StrToMultDimArr( $.Storage.get('labStartFilterArray') );
  200. var techArr = StrToMultDimArr( $.Storage.get('labSFTechDoneArray') );
  201. var str;
  202. var cnt;
  203. if (arr){
  204. //select name = unit_type
  205. $('select[name="unit_type"]').children().each(function() {
  206. str = jQuery(this).text().trim();
  207. cnt = countInDimArray(str, arr);
  208. idx = inMultDimArray(str, techArr, 'name');
  209. if(idx != -1){
  210. str = str + ' (' + techArr[idx]['tech'] + ')';
  211. }
  212. if(cnt > 0){
  213. str = str + ' +' + cnt;
  214. jQuery(this).css("padding-left","20px");
  215. }
  216. if(cnt > 1){
  217. jQuery(this).css("color","orangered");
  218. }
  219. if(cnt > 0 || idx != -1){
  220. jQuery(this).text(str);
  221. }
  222. });
  223. }
  224. }
  225. //для сбора списка текущих исследований из списка подразделений
  226. if (/https:\/\/virtonomica\.\w+\/\w+\/main\/company\/view\/\d+\/unit_list$/.test(window.location)) {
  227. //Ниже идёт непосредственно код скрипта
  228. //$.Storage.set('test','val');
  229. //alert($.Storage.get('test'));
  230. //alert($.Storage.get('labStartFilterArray'));
  231. var arr;
  232. //alert(arr);
  233. var currDate = (new Date()).getDate() + '';
  234. var lastSaveDate = $.Storage.get('labStartFilterDate');
  235. if (currDate == lastSaveDate){
  236. // arr = StrToMultDimArr( $.Storage.get('labStartFilterArray') );
  237. }else{
  238. $.Storage.set('labStartFilterDate', currDate);
  239. //alert('set date = ' + currDate);
  240. }
  241. if (!$.isArray(arr)){
  242. arr = new Array();
  243. //alert('reset');
  244. }
  245. //alert(arr.toString());
  246. var executed = 0;
  247. // $('td[class="u-c i-lab"]').parent().find('td[class="u-e"]').each(function () {
  248. $('td[class="info i-lab"]').parent().find('td[class="spec"]').each(function () {
  249. var str = jQuery(this).children().text();
  250. var tech = jQuery(this).children().children('b').eq(1).text();
  251. //var url = jQuery(this).parent().children('td[class="u-c i-lab"]').children('a').attr('href');
  252. var url = jQuery(this).parent().children('td[class="info i-lab"]').children('a').attr('href');
  253. var id = url.split('/');
  254. id = id[id.length-1];
  255. //alert(id);
  256. var substr = str.split(' ');
  257. substr[0] = '';
  258. substr[1] = '';
  259. str = substr.join(' ');
  260. //alert(tech);
  261. arr.push({
  262. id: id,
  263. name: str.trim(),
  264. tech: tech.trim()
  265. });
  266. executed = 1;
  267. /*var object2 = {
  268. id: id,
  269. name: str.trim()
  270. };*/
  271. /* merge object2 into arr, recursively */
  272. //$.extend(true, arr, object2);
  273. });
  274. if(1 == executed){
  275. arr = sort(arr);
  276. arr = distinct(arr);
  277. $.Storage.set('labStartFilterArray', MultDimArrToStr(arr));
  278. //alert(MultDimArrToStr(arr));
  279. //alert('saved');
  280. //console.log(arr);
  281. }
  282. }
  283. }
  284.  
  285. // load jQuery and execute the main function
  286. addJQuery(main);
  287. })(window);