role.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // 'use strict';
  2. if (!getCookie('role') && !getCookie('id') && window.location.pathname != '/login.html'){
  3. window.location.href = '/login.html';
  4. }
  5. if(!Array.prototype.indexOf) {
  6. Array.prototype.indexOf = function(what, i) {
  7. i = i || 0;
  8. var L = this.length;
  9. while (i < L) {
  10. if(this[i] === what) return i;
  11. ++i;
  12. }
  13. return -1;
  14. };
  15. }
  16. function setMainActive(loc){
  17. var set = '';
  18. if (typeof(loc) == 'object') {
  19. for (var i = 0; i < loc.length; i++){
  20. if (window.location.pathname == loc[i]){
  21. set = 'active';
  22. }
  23. }
  24. return set;
  25. } else {
  26. if (window.location.pathname == loc){
  27. return 'active';
  28. } else {
  29. return '';
  30. }
  31. }
  32. }
  33. function createMainNavigation() {
  34. var nav = [
  35. {name: 'Параметры', link: 'index.html', child:['/index.html', '/'], access: 2},
  36. {name: 'Настройки', link: 'settings.html', child: '/settings.html', access: 1},
  37. {name: 'Уведомления', link: 'snmp.html', child: '/snmp.html', access: 1},
  38. {name: 'Информация', link: 'info.html', child: '/info.html', access: 2},
  39. {name: 'Журнал', link: 'history.html', child: ['/history.html', '/ups_history.html'], access: 2}
  40. ];
  41. for (var i = 0; i < nav.length; i++) {
  42. var el = document.createElement('a');
  43. el.innerHTML = nav[i].name;
  44. el.href = nav[i].link;
  45. el.className = setMainActive(nav[i].child);
  46. var ls = document.createElement('li');
  47. ls.appendChild(el);
  48. if (+getCookie('role') > nav[i].access){
  49. ls.style.display = 'none';
  50. }
  51. $('nav').appendChild(ls);
  52. }
  53. }
  54. function createSubNavigation(){
  55. var subnav = document.createElement('div');
  56. subnav.className = 'subnav';
  57. var ul = document.createElement('ul');
  58. ul.className = 'nav navbar-nav';
  59. subnav.appendChild(ul);
  60. var li = [document.createElement('li'), document.createElement('li')];
  61. li[0].innerHTML = '<a href="history.html">События Контроллера</a>';
  62. li[1].innerHTML = '<a href="ups_history.html">Состояние ИБП</a>';
  63. if(window.location.pathname == '/history.html') {
  64. li[0].className = 'active';
  65. } else if (window.location.pathname == '/ups_history.html') {
  66. li[1].className = 'active';
  67. }
  68. ul.appendChild(li[0]);
  69. ul.appendChild(li[1]);
  70. $('subnavf').appendChild(subnav);
  71. }
  72. function $(id) {return document.getElementById(id);}
  73. var timeout;
  74. function clockinit(timeS) {
  75. clearInterval(timeout);
  76. customClock(timeS);
  77. timeout = setInterval(customClock, 1000);
  78. }
  79. var customClock = (function() {
  80. var timeDiff;
  81. function addZ(n) {
  82. return (n < 10? '0' : '') + n;
  83. }
  84. function formatTime(d) {
  85. //Format 'dd.mm.yyyy hh:mm:ss'
  86. return addZ(d.getDate()) + '.' +
  87. addZ(d.getMonth() + 1) + '.' +
  88. d.getFullYear() + ' ' +
  89. addZ(d.getHours()) + ':' +
  90. addZ(d.getMinutes()) + ':' +
  91. addZ(d.getSeconds());
  92. }
  93. return function (s) {
  94. var now = new Date();
  95. var then;
  96. // Get the time difference if first run
  97. if (s) {
  98. document.getElementById('clockbox').style.opacity = '1';
  99. then = new Date(now);
  100. //Set time in mileseconds and reset OS timezone
  101. then.setTime( parseInt(s) * 1000 + (then.getTimezoneOffset()*60000));
  102. timeDiff = now - then;
  103. }
  104. now = new Date(now - timeDiff);
  105. if(document.getElementById('clockbox') ){
  106. document.getElementById('clockbox').innerHTML = formatTime(now);
  107. }
  108. };
  109. }());
  110. window.onload = function(){
  111. if (window.location.pathname !== '/login.html' && getCookie('auth') !== '0'){
  112. var navId = document.getElementById('nav');
  113. var li = document.createElement('li');
  114. var logout = document.createElement('a');
  115. logout.href = 'javascript:void(0);';
  116. logout.innerHTML = 'Выход (' + getCookie('uname') + ')';
  117. logout.onclick = logOut;
  118. li.id = 'logout';
  119. li.className = 'logout';
  120. li.appendChild(logout);
  121. navId.appendChild(li);
  122. }
  123. };
  124. function logOut() {
  125. loadXMLDoc('logout.cgi'+'?'+Math.random(), 'GET', function(){
  126. deleteCookie('role');
  127. deleteCookie('id');
  128. window.location.href = '/login.html';
  129. });
  130. return false;
  131. }
  132. function loadXMLDoc(url, method, callback, body) {
  133. var xmlhttp;
  134. if (window.XMLHttpRequest) {
  135. xmlhttp = new XMLHttpRequest();
  136. } else {
  137. xmlhttp = new window.ActiveXObject('Microsoft.XMLHTTP');
  138. }
  139. xmlhttp.open(method, url, true);
  140. xmlhttp.setRequestHeader('Content-Type', 'application/json');
  141. var status;
  142. xmlhttp.onreadystatechange = function () {
  143. if (xmlhttp.readyState == 4) {
  144. status = xmlhttp.status;
  145. if (status == 200) {
  146. pwdIsCorrect = xmlhttp.responseText;
  147. if (typeof callback == 'function') {
  148. callback.apply(xmlhttp);
  149. }
  150. } else if (status == 401) {
  151. alert('Request failed with status code 401');
  152. window.location.reload();
  153. } else {
  154. console.log('Error');
  155. }
  156. }
  157. };
  158. xmlhttp.send(body);
  159. }
  160. function addSort(page){
  161. var sortButton = document.getElementsByClassName('sortable');
  162. for (var i = 0; i < sortButton.length; i++) {
  163. sortButton[i].addEventListener('click', function(){
  164. var name = this.getAttribute('sort');
  165. var direction = this.getAttribute('direction');
  166. switch(page){
  167. case 'alarms':
  168. var target = location.hash.split('#')[1];
  169. getAlarms(target, name, direction);
  170. break;
  171. case 'dryinputs':
  172. getDryInputs(name, direction);
  173. break;
  174. case 'relais':
  175. getRelais(name, direction);
  176. break;
  177. }
  178. if(direction == 1){
  179. this.setAttribute('direction', '-1');
  180. } else {
  181. this.setAttribute('direction', '1');
  182. }
  183. });
  184. }
  185. }
  186. function getCookie(name){
  187. var strCookie, arrCookie;
  188. strCookie = document.cookie,
  189. arrCookie = strCookie.split('; ');
  190. for (var i = 0; i < arrCookie.length; i++){
  191. var arr = arrCookie[i].split('=');
  192. if (arr[0] == name) return unescape(arr[1]);
  193. }
  194. return '';
  195. }
  196. function setCookie(name,value,expirehours){
  197. var cookieString = name + '=' + escape(value);
  198. if (expirehours > 0) {
  199. var date = new Date();
  200. date.setTime(date.getTime() + expirehours * 3600 * 1000);
  201. cookieString = cookieString + '; expires=' + date.toGMTString();
  202. }
  203. document.cookie = cookieString;
  204. }
  205. function deleteCookie(name) {
  206. document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  207. }
  208. function getJSON(url, successHandler, errorHandler) {
  209. var xhr;
  210. xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP');
  211. var fURL = url.split('.').pop() == 'cgi' ? url+'?' : url+'&';
  212. xhr.open('GET', fURL+'_=' + new Date().getTime(), true);
  213. xhr.onreadystatechange = function () {
  214. var status, data;
  215. if (xhr.readyState == 4) {
  216. status = xhr.status;
  217. if (status == 200) {
  218. data = JSON.parse(xhr.responseText);
  219. successHandler(data);
  220. } else {
  221. errorHandler(status);
  222. }
  223. }
  224. };
  225. xhr.send();
  226. }
  227. function checkNotify() {
  228. ( getCookie('netsettings_changed') == 'true' ) ? apply_settings.init() : apply_settings.close();
  229. ( getCookie('device-error') == '1' ) ? device_error.init() : device_error.close();
  230. }
  231. function getParam(e,x) {
  232. switch (x) {
  233. case 0:
  234. return (e.snmp === 'true');
  235. case 1:
  236. return (e.sms === 'true');
  237. case 2:
  238. return (e.email === 'true');
  239. }
  240. }
  241. var snmpArray = [];
  242. function snmpPush(e){
  243. var index = snmpArray.indexOf(e);
  244. if (index !== -1) {
  245. snmpArray.splice(index, 1);
  246. } else {
  247. snmpArray.push(e);
  248. }
  249. if (snmpArray.length > 0){
  250. $('sa').style.height = 'auto';
  251. $('sa').style.opacity = 1;
  252. $('sa_length').textContent = snmpArray.length;
  253. } else {
  254. $('sa').style.height = 0;
  255. $('sa').style.opacity = 0;
  256. }
  257. checkAllStates();
  258. }
  259. function rowsToSubmit(array){
  260. var rows = [];
  261. for (var i = 0; i < array.length; i++) {
  262. var parent = $(array[i]).parentNode.parentNode;
  263. array[i];
  264. var index = rows.indexOf(parent.id);
  265. if (index !== -1) {
  266. rows[index] = parent.id;
  267. } else {
  268. rows.push(parent.id);
  269. }
  270. }
  271. return rows;
  272. }
  273. function createSNMP(id){
  274. getJSON('snmp.cgi', function(data) {
  275. $(id).innerHTML = '';
  276. for (var i = 0; i < data.params.length; i++) {
  277. var param = data.params[i];
  278. var param_row = document.createElement('tr');
  279. param_row.id = param.name;
  280. var param_name = document.createElement('td');
  281. param_name.textContent = param.label;
  282. param_row.appendChild(param_name);
  283. for (var x = 0; x < 1; x++) {
  284. var cb_td = document.createElement('td');
  285. var cb_input = document.createElement('input');
  286. cb_input.type = 'checkbox';
  287. cb_input.checked = getParam(param,x);
  288. cb_input.id = 'cb'+x+param.name;
  289. cb_input.className = 'checkbox';
  290. cb_input.setAttribute('onchange', 'snmpPush(this.id)');
  291. cb_td.appendChild(cb_input);
  292. param_row.appendChild(cb_td);
  293. }
  294. var param_snmp = document.createElement('td');
  295. $(id).appendChild(param_row);
  296. }
  297. utcParam = data.utc;
  298. // clockinit(data.utm);
  299. checkAllStates();
  300. setCookie('netsettings_changed', data.netsettings_changed);
  301. setCookie('profilaction_changed', data.profilaction_changed);
  302. setCookie('need_confirm', data.need_confirm);
  303. checkNotify();
  304. }, function(error){});
  305. }
  306. function snmpSaSubmit(){
  307. var url = 'snmp.cgi?';
  308. var string = '';
  309. for (var i = 0; i < rowsToSubmit(snmpArray).length; i++) {
  310. if(i == rowsToSubmit(snmpArray).length-1) {
  311. string+= $(rowsToSubmit(snmpArray)[i]).id + '=' + arrayToBitMask(rowsToSubmit(snmpArray)[i]);
  312. } else {
  313. string+= $(rowsToSubmit(snmpArray)[i]).id + '=' + arrayToBitMask(rowsToSubmit(snmpArray)[i]) + '&';
  314. }
  315. }
  316. if (string){
  317. loadXMLDoc( url + string, 'GET', updatepage);
  318. } else {
  319. updatepage();
  320. }
  321. $('sa').style.height = 0;
  322. $('sa').style.opacity = 0;
  323. snmpArray = [];
  324. return false;
  325. }
  326. function snmpChangeCancel(){
  327. updatepage();
  328. $('sa').style.height = 0;
  329. $('sa').style.opacity = 0;
  330. snmpArray = [];
  331. }
  332. function checkAll(e,id){
  333. var rows = $('snmp_list').getElementsByTagName('tr');
  334. if ($(id).checked && !$(id).indeterminate){
  335. for (var i = 0; i < rows.length; i++) {
  336. var input = rows[i].children[e].getElementsByTagName('input')[0];
  337. if (!input.checked) {
  338. input.checked = true;
  339. snmpPush(input.id);
  340. }
  341. }
  342. } else if (!$(id).checked && !$(id).indeterminate) {
  343. for (var i = 0; i < rows.length; i++) {
  344. var input = rows[i].children[e].getElementsByTagName('input')[0];
  345. input.checked = false;
  346. snmpPush(input.id);
  347. }
  348. } else if ($(id).checked) {
  349. for (var i = 0; i < rows.length; i++) {
  350. var input = rows[i].children[e].getElementsByTagName('input')[0];
  351. input.checked = true;
  352. snmpPush(input.id);
  353. }
  354. }
  355. }
  356. function checkAllStates(){
  357. allCheckboxStates(1,'cb_snmp');
  358. // allCheckboxStates(2,'cb_sms');
  359. // allCheckboxStates(3,'cb_email');
  360. }
  361. function allCheckboxStates(e,id){
  362. var rows = $('snmp_list').getElementsByTagName('tr');
  363. var c = [];
  364. var nc = [];
  365. for (var i = 0; i < rows.length; i++) {
  366. var input = rows[i].children[e].getElementsByTagName('input')[0];
  367. if (input.checked){
  368. c.push(input.id);
  369. } else {
  370. nc.push(input.id);
  371. }
  372. }
  373. if (c.length && nc.length) {
  374. $(id).checked = false;
  375. $(id).indeterminate = true;
  376. } else if (c.length == rows.length) {
  377. $(id).checked = true;
  378. $(id).indeterminate = false;
  379. } else {
  380. $(id).checked = false;
  381. $(id).indeterminate = false;
  382. }
  383. }
  384. function decimalToBinary(decimal) {
  385. return (decimal >>> 0).toString(2);
  386. }
  387. function binaryToDecimal(binaryString) {
  388. return parseInt(binaryString, 2);
  389. }
  390. function cbToBit(id, n){
  391. var bit = $(id).checked ? 1 : 0;
  392. return bit << n;
  393. }
  394. function arrayToBitMask(id){
  395. // var parent = $(id).id;
  396. var snmp = cbToBit('cb0'+id, 0);
  397. // var sms = cbToBit('cb1'+id, 1);
  398. // var email = cbToBit('cb2'+id, 2);
  399. return binaryToDecimal( decimalToBinary( (snmp) ) );
  400. }
  401. function NotificationBox (id, options) {
  402. this.id = id;
  403. this.options = (options ? options : {});
  404. if(! ('message' in this.options)) this.options.message = 'NotificationBox: Hello World!';
  405. if(! ('bgcolor' in this.options)) this.options.bgcolor = '#009966';
  406. if(! ('textcolor' in this.options)) this.options.textcolor = '#fff';
  407. if(! ('show' in this.options)) this.options.show = true;
  408. if(! ('url' in this.options)) this.options.url = false;
  409. }
  410. NotificationBox.prototype.init = function(){
  411. if (!$(this.id)) {
  412. if (this.options.show) {
  413. var maindiv = document.createElement('div');
  414. maindiv.id = this.id;
  415. maindiv.className = 'notify-box';
  416. maindiv.style.backgroundColor = this.options.bgcolor;
  417. maindiv.style.color = this.options.textcolor;
  418. var string = document.createElement('p');
  419. string.textContent = this.options.message;
  420. maindiv.appendChild(string);
  421. if (this.options.url) {
  422. var confirm = document.createElement('button');
  423. confirm.className = 'btn btn-danger-inverted';
  424. confirm.textContent = 'Подтвердить';
  425. confirm.setAttribute('onclick', 'loadXMLDoc(\''+ this.options.url +'\',\'GET\', updatepage)');
  426. maindiv.appendChild(confirm);
  427. }
  428. if (this.options.cancelButton) {
  429. var cancel = document.createElement('button');
  430. cancel.className = 'btn btn-warning-inverted';
  431. cancel.style.marginLeft = '5px';
  432. cancel.textContent = 'Отмена';
  433. cancel.setAttribute('onclick', 'loadXMLDoc(\''+ this.options.cancelUrl +'\',\'GET\', updatepage)');
  434. maindiv.appendChild(cancel);
  435. }
  436. document.body.insertBefore(maindiv, document.body.childNodes[0]);
  437. fadeIn($(this.id));
  438. }
  439. }
  440. };
  441. NotificationBox.prototype.close = function(){
  442. if ($(this.id) ){
  443. var set = $(this.id);
  444. document.body.removeChild(set);
  445. }
  446. };