labStartProjectFilter

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

当前为 2014-11-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name labStartProjectFilter
  3. // @description Отмечает в выпадающем списке запуска исследований технологии, которые уже исследуются вами
  4. // @description fix for new design 2014
  5. // @author cobra3125
  6. // @license MIT
  7. // @version 1.51
  8. // @include http://virtonomica.*/*/main/company/view/*/unit_list
  9. // @include http://virtonomica.*/*/window/unit/view/*/project_create
  10. // @include http://virtonomica.*/*/main/management_action/*/investigations/technologies
  11. // @include http://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", "http://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 (/http:\/\/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 (/http:\/\/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 (/http:\/\/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 > 0 || idx != -1){
  217. jQuery(this).text(str);
  218. }
  219. });
  220. }
  221. }
  222. //для сбора списка текущих исследований из списка подразделений
  223. if (/http:\/\/virtonomica\.\w+\/\w+\/main\/company\/view\/\d+\/unit_list$/.test(window.location)) {
  224. //Ниже идёт непосредственно код скрипта
  225. //$.Storage.set('test','val');
  226. //alert($.Storage.get('test'));
  227. //alert($.Storage.get('labStartFilterArray'));
  228. var arr;
  229. //alert(arr);
  230. var currDate = (new Date()).getDate() + '';
  231. var lastSaveDate = $.Storage.get('labStartFilterDate');
  232. if (currDate == lastSaveDate){
  233. // arr = StrToMultDimArr( $.Storage.get('labStartFilterArray') );
  234. }else{
  235. $.Storage.set('labStartFilterDate', currDate);
  236. //alert('set date = ' + currDate);
  237. }
  238. if (!$.isArray(arr)){
  239. arr = new Array();
  240. //alert('reset');
  241. }
  242. //alert(arr.toString());
  243. var executed = 0;
  244. // $('td[class="u-c i-lab"]').parent().find('td[class="u-e"]').each(function () {
  245. $('td[class="info i-lab"]').parent().find('td[class="spec"]').each(function () {
  246. var str = jQuery(this).children().text();
  247. var tech = jQuery(this).children().children('b').eq(1).text();
  248. //var url = jQuery(this).parent().children('td[class="u-c i-lab"]').children('a').attr('href');
  249. var url = jQuery(this).parent().children('td[class="info i-lab"]').children('a').attr('href');
  250. var id = url.split('/');
  251. id = id[id.length-1];
  252. //alert(id);
  253. var substr = str.split(' ');
  254. substr[0] = '';
  255. substr[1] = '';
  256. str = substr.join(' ');
  257. //alert(tech);
  258. arr.push({
  259. id: id,
  260. name: str.trim(),
  261. tech: tech.trim()
  262. });
  263. executed = 1;
  264. /*var object2 = {
  265. id: id,
  266. name: str.trim()
  267. };*/
  268. /* merge object2 into arr, recursively */
  269. //$.extend(true, arr, object2);
  270. });
  271. if(1 == executed){
  272. arr = sort(arr);
  273. arr = distinct(arr);
  274. $.Storage.set('labStartFilterArray', MultDimArrToStr(arr));
  275. //alert(MultDimArrToStr(arr));
  276. //alert('saved');
  277. //console.log(arr);
  278. }
  279. }
  280. }
  281.  
  282. // load jQuery and execute the main function
  283. addJQuery(main);
  284. })(window);