Virtonomica:Notes

Система добавления и показа оповещений

当前为 2017-10-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Virtonomica:Notes
  3. // @name:en Virtonomica:Notes
  4. // @namespace Virtonomica
  5. // @description Система добавления и показа оповещений
  6. // @description:en The list of notes
  7. // @version 0.26
  8. // @include https://*virtonomic*.*/*/main/unit/view/*
  9. // @include https://*virtonomic*.*/*/main/company/view/*/unit_list
  10. // @include https://*irtonomic*.*/*/main/politics/president/*
  11. // @include https://*virtonomic*.*/*/main/politics/governor/*
  12. // @include https://*virtonomic*.*/*/main/politics/mayor/*
  13. // ==/UserScript==
  14. var run = function() {
  15. var win = (typeof(unsafeWindow) != 'undefined' ? unsafeWindow : top.window);
  16. $ = win.$;
  17.  
  18. var loc_storage = function(){
  19. return({
  20. 'save': function (name, val){
  21. try {
  22. window.localStorage.setItem( name, JSON.stringify( val ) );
  23. } catch(e) {
  24. out = "Ошибка добавления в локальное хранилище";
  25. //console.log(out);
  26. }
  27. },
  28. 'load': function(name){
  29. obj = JSON.parse( window.localStorage.getItem(name) );
  30. if ( obj == null ) obj = new Object();
  31. return obj;
  32. }
  33. });
  34. }
  35. var LS = new loc_storage();
  36.  
  37. // определяем интерфейс
  38. // autodefined language interface
  39. var lang = 'undef';
  40. //var bt_logout = $("li[class='icon menulogout']");
  41. //var logout_string = bt_logout.attr('title');
  42. var logout_string = $("a[href*='user/logout']").text();
  43. console.log(logout_string);
  44. // язык по умолчанию
  45. lang = 'En';
  46. if (logout_string == 'Выход') {
  47. lang = 'Ru';
  48. } else if(logout_string == 'Logout') {
  49. lang = 'En';
  50. }
  51. console.log(lang);
  52. if ( lang == 'undef') {
  53. alert('Unsupported language for userscript "Notes"');
  54. return;
  55. }
  56.  
  57. // Строки зависимые от языка
  58. // language definitions
  59. var LangMsg = new Object();
  60. LangMsg['Ru'] = new Object();
  61. LangMsg['En'] = new Object();
  62.  
  63. LangMsg['Ru']['governor'] = "Губернатор";
  64. LangMsg['En']['governor'] = "Governor";
  65. LangMsg['Ru']['president'] = "Президент";
  66. LangMsg['En']['president'] = "President";
  67. LangMsg['Ru']['mayor'] = "Мэр";
  68. LangMsg['En']['mayor'] = "Mayor";
  69.  
  70. LangMsg['Ru']['notes'] = "Напоминание";
  71. LangMsg['En']['notes'] = "Notes";
  72.  
  73. LangMsg['Ru']['list_notes'] = "Список напоминаний";
  74. LangMsg['En']['list_notes'] = "List of Notes";
  75.  
  76. LangMsg['Ru']['save'] = "Сохранить";
  77. LangMsg['En']['save'] = "Save";
  78. LangMsg['Ru']['del'] = "Удалить";
  79. LangMsg['En']['del'] = "Delete";
  80.  
  81. LangMsg['Ru']['add_notes'] = "Добавить напоминание";
  82. LangMsg['En']['add_notes'] = "Add Notes";
  83.  
  84. LangMsg['Ru']['export'] = "Окно экспорта/импорта";
  85. LangMsg['En']['export'] = "Windwow export/import";
  86. LangMsg['Ru']['import'] = "Импортировать данные из окна";
  87. LangMsg['En']['import'] = "Importing data from textarea";
  88. LangMsg['Ru']['data_export'] = "Данные экспорта";
  89. LangMsg['En']['data_export'] = "Data exporting";
  90. // --- virtonomica user interface
  91. LangMsg['Ru']['comp'] = "компании";
  92. LangMsg['En']['comp'] = "of a company";
  93. // Стили
  94. var st = $("style");
  95. if ( $(".my_btn", st).length == 0 ) {
  96. st.append(".my_btn{cursor:pointer;opacity:0.5;float:left;color: white;}");
  97. st.append(".my_btn:hover{opacity:1.0}");
  98. st.append(".my_btn img {width:24px;}");
  99. }
  100.  
  101. //console.log( LangMsg );
  102. /**
  103. * записать данные в локальнео хранилище, с проверкой ошибок
  104. */
  105. function ToStorage(name, val)
  106. {
  107. try {
  108. window.localStorage.setItem( name, JSON.stringify( val ) );
  109. } catch(e) {
  110. out = "Ошибка добавления в локальное хранилище";
  111. //console.log(out);
  112. }
  113. }
  114.  
  115. function getFromStorage(obj, id_shop)
  116. {
  117. if (obj[id_shop] == null) return '';
  118. return JSON.stringify(obj[id_shop]);
  119. }
  120.  
  121. /**
  122. * Добавить заметку к текущему предприятию
  123. * следует вызывать на страницах где одно подраздление, ИД которого виден в url
  124. *
  125. * @param msg текст сообщения, можно использовать html теги
  126. */
  127. function addNotes( msg ){
  128. // объект для хранения сообщений
  129. notes = JSON.parse( window.localStorage.getItem('notes') );
  130. if ( notes == null ) notes = new Object();
  131.  
  132. // Идентификатор подразделения
  133. var id = /(\d+)/.exec(location.href)[0];
  134. //var head = $("#headerInfo");
  135. //var title = $("h1", head).text();
  136. var title = $("div.title h1").text();
  137. cobsole.log('addNotes, title=' + title);
  138. alert(title);
  139.  
  140. head = $("div.officePlace");
  141. var type = head.text();
  142. var nn = type.indexOf("компании");
  143. if (nn > 0){
  144. type = type.substring(0, nn);
  145. var ptrn = /\s*((\S+\s*)*)/;
  146. type = type.replace(ptrn, "$1");
  147. ptrn = /((\s*\S+)*)\s*/;
  148. type = type.replace(ptrn, "$1");
  149. } else {
  150. type = '';
  151. }
  152.  
  153. if ( notes[id] == null ) notes[id] = new Object();
  154.  
  155. var d = new Date();
  156.  
  157. if ( notes[id]['text'] != null) {
  158. // сообщение для этого подраздления уже есть
  159. msg = notes[id]['text'] + "<br>" + msg;
  160. }
  161.  
  162. notes[id]['text'] = msg;
  163. // Количество миллисекунд
  164. notes[id]['time'] = d.getTime();
  165. notes[id]['name'] = title;
  166. notes[id]['type'] = type;
  167.  
  168. ToStorage('notes', notes);
  169. }
  170.  
  171. var txt = "<table><tr><td><td><table width=100%><tr><td align=rigth id=notes_title><td align=center><h3>" + LangMsg[ lang ]['notes'] + "</h3><div id=notes_link style='color:grey'></div></table><td>&nbsp;";
  172. txt+= "";
  173. txt+= "<tr><td>&nbsp;<td align=center id=notes_form>&nbsp;<td>&nbsp;" + "<tr><td colspan=3></table>";
  174. var div_form = "<div id=notes style='background: none repeat scroll 0% 0% rgb(223, 223, 223); z-index: 1002; position: absolute; border: 1px solid rgb(0, 0, 0); display: none;'>" + txt + "</div>";
  175.  
  176. var div_export = "<div id=notes_export style='background: none repeat scroll 0% 0% rgb(223, 223, 223); z-index: 1003; position: absolute; border: 1px solid rgb(0, 0, 0); display: none;'>"
  177. + "<h3 style='margin:4px'>" + LangMsg[ lang ]['data_export'] + "</h3>"
  178. + "<table><tr><td>&nbsp;<td>"
  179. + "<textarea name=export_text id=export_text rows=10 cols=64></textarea>"
  180. + "<br><center><span id=export_load></span></center>"
  181. + "</table>";
  182. + "</div>";
  183.  
  184. var tu = $("table.unit-list-2014");
  185. if (tu.length == 1) {
  186. // Это у нас список подразделений - рисуем кнопку отображения отчета со сылками
  187. // Оповещения
  188. notes = JSON.parse( window.localStorage.getItem('notes') );
  189. if ( notes == null ) notes = new Object();
  190. var len = 0;
  191.  
  192. // проверить, какие оповещения отразить как актуальные
  193. for (key in notes){
  194. len++;
  195. }
  196.  
  197. // http://cdn1.iconfinder.com/data/icons/Upojenie_by_SoundForge/Icons/Notes.png
  198. // http://www.iconsearch.ru/uploads/icons/crystalclear/24x24/kedit.png
  199.  
  200. var wc = $("<li><div id=main_notes class=my_btn> <img alt='" + LangMsg[ lang ]['list_notes'] +"' src=http://cdn1.iconfinder.com/data/icons/humano2/32x32/apps/gnome-sticky-notes-applet.png> <span id=all_notes> ("+ len+")</span></div>").click( function() {
  201. $("#notes").toggle();
  202. if( $('#notes').is(':visible') ) {
  203. // код для visible
  204. // Оповещения
  205. notes = JSON.parse( window.localStorage.getItem('notes') );
  206. if ( notes == null ) notes = new Object();
  207.  
  208. // Формируем ссылку на торговый зал
  209. var url = /^https:\/\/virtonomic[as]\.(\w+)\/\w+\//.exec(location.href)[0];
  210. console.log(url);
  211.  
  212. var all_count = 0;
  213. $("#notes_form").html('');
  214. var out = $("<table>");
  215. for (key in notes){
  216. if (notes[key] == null) continue;
  217. if (notes[key]['time'] == null) continue;
  218.  
  219. // подсчитываем сколько у нас оповещениий
  220. // TODO - учитывать только акутальные
  221. all_count++;
  222.  
  223. var d = new Date();
  224. d.setTime(notes[key]['time']);
  225.  
  226. link = "<a href=" + url + "main/unit/view/" + key + ">" + notes[key]['name'] + "</a>";
  227. if ( notes[key]['link'] != null ) {
  228. link = "<a href=" + notes[key]['link'] + ">" + notes[key]['name'] + "</a>";
  229. }
  230. span = $("<span name=" + key + " style='cursor:pointer;' title='del'><font color=red><b>Х</b></font></span>").click(
  231. function(){
  232. idp = $(this).attr('name');
  233. //alert('del = ' + idp);
  234. delete notes[idp];
  235. ToStorage('notes', notes);
  236. $(this).parent().css("text-decoration","line-through").css("color","grey");
  237. //$("#main_notes").click().click();
  238. });
  239.  
  240. out_tr = $("<tr>");
  241. out_tr.append("<td>" + d.toLocaleDateString())
  242. .append("<td>" + link)
  243. .append("<td>(" + notes[key]['type'] + ")")
  244. .append("<td style='border: 1px solid gray; border-radius: 4px 4px 4px 4px; box-shadow: 0 1px 3px 0 #999999; display: block; float: left; margin-top: 4px; overflow: hidden; padding: 2px 4px; text-align:left'>" + notes[key]['text'])
  245. .append("<td>").append(span);
  246. out.append(out_tr);
  247. str = "(" + all_count +")";
  248. if (all_count == 0) str = '';
  249. $("#all_notes").html("(" + all_count +")");
  250. }
  251. $("#notes_form").html( out );
  252.  
  253. }
  254. });
  255.  
  256. // http://cdn1.iconfinder.com/data/icons/basicset/save_32.png
  257. // http://www.iconsearch.ru/uploads/icons/ultimategnome/48x48/stock_export.png
  258. var wc_export = $("<span id=main_notes_export style='cursor:pointer; color: white;'><img src=http://cdn1.iconfinder.com/data/icons/basicset/save_32.png title='" + LangMsg[ lang]['export']+ "' alt='О" + LangMsg[ lang]['export']+ "'></span>").click( function() {
  259. $("#export_text").val( JSON.stringify( notes ) );
  260. $("#notes_export").toggle();
  261. });
  262.  
  263. // http://cdn1.iconfinder.com/data/icons/freeapplication/png/24x24/Load.png
  264. // http://www.iconsearch.ru/uploads/icons/freeapplication/24x24/load.png
  265. var wc_load = $("<img src=http://cdn1.iconfinder.com/data/icons/freeapplication/png/24x24/Load.png title='" + LangMsg[ lang ]['import'] + "' alt='" + LangMsg[ lang ]['import'] + "'>").click( function() {
  266. //alert("Load");
  267. var text = $("#export_text").val() ;
  268. try {
  269. notes = JSON.parse( text );
  270. ToStorage('notes', notes);
  271. $("#notes_export").hide();
  272. $("#main_notes").click().click();
  273. } catch(e) {
  274. alert("Неверные данным для импорта");
  275. }
  276. });
  277.  
  278. var container = $('ul.tabu');
  279. container.append( wc );
  280.  
  281. $("table.unit-top").before ( div_form);
  282. //container.append( div_form );
  283. //var container = $('#topblock');
  284. //container.append( $('<table><tr><td>').append(wc) );
  285. //container.append( div_form );
  286. $("#notes_title").append(wc_export).append( div_export );
  287. $("#export_load").append(wc_load);
  288.  
  289.  
  290. return;
  291. }
  292.  
  293. // Идентификатор подразделения
  294. var id = /(\d+)/.exec(location.href)[0];
  295. console.log("id=" + id);
  296. // тип подразделения
  297. var type = '';
  298. // название подразделения
  299. var title = '';
  300. var pos = location.href.indexOf("politics");
  301. // проверяем на объект политики
  302. if (pos > 0) {
  303. console.log("politics");
  304. // вычитываем название объекта политики
  305. var polit_name = "";
  306. var el = $("#headerInfoCenter h1");
  307. var el2 = $("a", el);
  308. if (el2.length > 0 ) {
  309. polit_name = el2.text();
  310. } else {
  311. polit_name = el.text();
  312. }
  313. if ( location.href.indexOf("president") > 0 ) {
  314. type = LangMsg[ lang ]['president'];
  315. }
  316. if ( location.href.indexOf("governor") > 0 ) {
  317. type = LangMsg[ lang ]['governor'] ;
  318. }
  319. if ( location.href.indexOf("mayor") > 0 ) {
  320. type = LangMsg[ lang ]['mayor'] ;
  321. polit_name = el.html();
  322. pos = polit_name.indexOf("<img");
  323. polit_name = $.trim( polit_name.substr(0, pos) );
  324. }
  325. console.log( polit_name );
  326. title = polit_name;
  327. console.log( type );
  328. } else {
  329. // обработка обыных юнитов (units)
  330. var title = $("div.title h1").text();
  331. var type = $.trim( $('ul.tabu li').eq(1).text() );
  332.  
  333. }
  334. // Оповещения
  335. notes = JSON.parse( window.localStorage.getItem('notes') );
  336. if ( notes == null ) notes = new Object();
  337. if ( notes[id] == null) notes[id] = new Object();
  338.  
  339. notes_html = "";
  340. if ( notes[id]['text'] != null) notes_html = notes[id]['text'];
  341.  
  342. var form = "";
  343. form += "<span id=notes_error style='color:red;'></span><br>";
  344. form += "<div id=notes_preview style='border: 1px solid gray; border-radius: 4px 4px 4px 4px; box-shadow: 0 1px 3px 0 #999999; display: block; float: left; margin-top: 4px; margin-right: 4px; overflow: hidden; padding: 2px 4px; text-align:left'>" + notes_html +"</div>";
  345. form += " <textarea name=notes_txt id=notes_txt rows=5 cols=48>";
  346. form += notes_html;
  347. form+= "</textarea><br>";
  348.  
  349. var form_button = $("<button id=notes_btn style='cursor:pointer'>" +LangMsg[ lang ]['save'] + "</button>").click( function() {
  350. $("#notes_error").text('');
  351. var text = $("#notes_txt").val() ;
  352. $("#notes_preview").html( text );
  353. if (text == '') {
  354. $("#notes_error").text('Ошибка - нет описания');
  355. return;
  356. }
  357.  
  358. var d = new Date();
  359. // Оповещения
  360. notes = JSON.parse( window.localStorage.getItem('notes') );
  361. if ( notes == null ) notes = new Object();
  362.  
  363. if ( notes[id] == null) notes[id] = new Object();
  364.  
  365. notes[id]['text'] = text;
  366. notes[id]['time'] = d.getTime();
  367. notes[id]['name'] = title;
  368. notes[id]['type'] = type;
  369. notes[id]['link'] = location.href;
  370.  
  371. ToStorage('notes', notes);
  372. $("#notes").toggle();
  373. });
  374.  
  375. var form_button_del = $(" <button style='cursor:pointer'>" +LangMsg[ lang ]['del'] + "</button>").click( function() {
  376. // Оповещения
  377. notes = JSON.parse( window.localStorage.getItem('notes') );
  378. if ( notes == null ) notes = new Object();
  379.  
  380. if ( notes[id] == null) notes[id] = new Object();
  381.  
  382. $("#notes_txt").val("");
  383. $("#notes_preview").html( "" );
  384. delete notes[id];
  385. ToStorage('notes', notes);
  386. });
  387.  
  388. // http://cdn1.iconfinder.com/data/icons/oxygen/32x32/actions/document-new.png
  389. // http://www.iconsearch.ru/uploads/icons/ull_icons/24x24/message_add.png
  390. var wc = $("<li class=my_btn><img alt='"+ LangMsg[ lang ]['add_notes'] + "' src=http://cdn1.iconfinder.com/data/icons/oxygen/32x32/actions/document-new.png title='"+ LangMsg[ lang ]['add_notes'] + "'> </li>").click( function() {
  391. $("#notes").toggle();
  392. // Обновить зампетку
  393. notes = JSON.parse( window.localStorage.getItem('notes') );
  394. if ( notes == null ) notes = new Object();
  395. if ( notes[id] == null) notes[id] = new Object();
  396.  
  397. $("#notes_txt").change(function() {
  398. console.log('change ' + $("#notes_txt").val() );
  399. $("#notes_preview").html( $("#notes_txt").val() );
  400. });
  401. if ( notes[id]['text'] == null ) return;
  402. $("#notes_txt").val( notes[id]['text'] );
  403. $("#notes_preview").html( notes[id]['text'] );
  404. if (notes[id]['link'] != null) {
  405. $("#notes_link").html( notes[id]['link'] );
  406. }
  407. });
  408. //var container = $('#topblock');
  409. //var container = $('#topblock').next();
  410. var container = $('ul.tabu');
  411.  
  412. if (tu.length == 0) {
  413. container = $("li:last", container).prev().parent();
  414. container.append(wc) ;
  415. //container.append( $('<table><tr><td>').append(wc) );
  416. $("#childMenu").before ( div_form);
  417.  
  418. $("#notes_form").append(form).append(form_button).append(form_button_del);
  419. }
  420. }
  421.  
  422. if(window.top == window) {
  423. var script = document.createElement("script");
  424. script.textContent = '(' + run.toString() + ')();';
  425. document.documentElement.appendChild(script);
  426. }