role.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 createSubNavigation(){
  17. var subnav = document.createElement('div');
  18. subnav.className = 'subnav';
  19. var ul = document.createElement('ul');
  20. ul.className = 'nav navbar-nav';
  21. subnav.appendChild(ul);
  22. var li = [document.createElement('li'), document.createElement('li')];
  23. li[0].innerHTML = '<a href="history.html">События Контроллера</a>';
  24. li[1].innerHTML = '<a href="ups_history.html">Состояние ИБП</a>';
  25. if(window.location.pathname == '/history.html') {
  26. li[0].className = 'active';
  27. } else if (window.location.pathname == '/ups_history.html') {
  28. li[1].className = 'active';
  29. }
  30. ul.appendChild(li[0]);
  31. ul.appendChild(li[1]);
  32. $('subnavf').appendChild(subnav);
  33. }
  34. function $(id) {return document.getElementById(id);}
  35. var timeout;
  36. function clockinit(timeS) {
  37. clearInterval(timeout);
  38. customClock(timeS);
  39. timeout = setInterval(customClock, 1000);
  40. }
  41. var customClock = (function() {
  42. var timeDiff;
  43. function addZ(n) {
  44. return (n < 10? '0' : '') + n;
  45. }
  46. function formatTime(d) {
  47. //Format 'dd.mm.yyyy hh:mm:ss'
  48. return addZ(d.getDate()) + '.' +
  49. addZ(d.getMonth() + 1) + '.' +
  50. d.getFullYear() + ' ' +
  51. addZ(d.getHours()) + ':' +
  52. addZ(d.getMinutes()) + ':' +
  53. addZ(d.getSeconds());
  54. }
  55. return function (s) {
  56. var now = new Date();
  57. var then;
  58. // Get the time difference if first run
  59. if (s) {
  60. document.getElementById('clockbox').style.opacity = '1';
  61. then = new Date(now);
  62. //Set time in mileseconds and reset OS timezone
  63. then.setTime( parseInt(s) * 1000 + (then.getTimezoneOffset()*60000));
  64. timeDiff = now - then;
  65. }
  66. now = new Date(now - timeDiff);
  67. if(document.getElementById('clockbox') ){
  68. document.getElementById('clockbox').innerHTML = formatTime(now);
  69. }
  70. };
  71. }());
  72. window.onload = function(){
  73. if (window.location.pathname !== '/login.html' && getCookie('auth') !== '0'){
  74. var navId = document.getElementById('nav');
  75. var li = document.createElement('li');
  76. var logout = document.createElement('a');
  77. logout.href = 'javascript:void(0);';
  78. logout.innerHTML = 'Выход (' + getCookie('uname') + ')';
  79. logout.onclick = logOut;
  80. li.id = 'logout';
  81. li.className = 'logout';
  82. li.appendChild(logout);
  83. navId.appendChild(li);
  84. }
  85. };
  86. function logOut() {
  87. loadXMLDoc('logout.cgi'+'?'+Math.random(), 'GET', function(){
  88. deleteCookie('role');
  89. deleteCookie('id');
  90. window.location.href = '/login.html';
  91. });
  92. return false;
  93. }
  94. function loadXMLDoc(url, method, callback, body) {
  95. var xmlhttp;
  96. if (window.XMLHttpRequest) {
  97. xmlhttp = new XMLHttpRequest();
  98. } else {
  99. xmlhttp = new window.ActiveXObject('Microsoft.XMLHTTP');
  100. }
  101. xmlhttp.open(method, url, true);
  102. xmlhttp.setRequestHeader('Content-Type', 'application/json');
  103. var status;
  104. xmlhttp.onreadystatechange = function () {
  105. if (xmlhttp.readyState == 4) {
  106. status = xmlhttp.status;
  107. if (status == 200) {
  108. pwdIsCorrect = xmlhttp.responseText;
  109. if (typeof callback == 'function') {
  110. callback.apply(xmlhttp);
  111. }
  112. } else {
  113. console.log('Error');
  114. }
  115. }
  116. };
  117. xmlhttp.send(body);
  118. }
  119. function addSort(page){
  120. var sortButton = document.getElementsByClassName('sortable');
  121. for (var i = 0; i < sortButton.length; i++) {
  122. sortButton[i].addEventListener('click', function(){
  123. var name = this.getAttribute('sort');
  124. var direction = this.getAttribute('direction');
  125. switch(page){
  126. case 'alarms':
  127. var target = location.hash.split('#')[1];
  128. getAlarms(target, name, direction);
  129. break;
  130. case 'dryinputs':
  131. getDryInputs(name, direction);
  132. break;
  133. case 'relais':
  134. getRelais(name, direction);
  135. break;
  136. }
  137. if(direction == 1){
  138. this.setAttribute('direction', '-1');
  139. } else {
  140. this.setAttribute('direction', '1');
  141. }
  142. });
  143. }
  144. }
  145. function getCookie(name){
  146. var strCookie, arrCookie;
  147. strCookie = document.cookie,
  148. arrCookie = strCookie.split('; ');
  149. for (var i = 0; i < arrCookie.length; i++){
  150. var arr = arrCookie[i].split('=');
  151. if (arr[0] == name) return unescape(arr[1]);
  152. }
  153. return '';
  154. }
  155. function setCookie(name,value,expirehours){
  156. var cookieString = name + '=' + escape(value);
  157. if (expirehours > 0) {
  158. var date = new Date();
  159. date.setTime(date.getTime() + expirehours * 3600 * 1000);
  160. cookieString = cookieString + '; expires=' + date.toGMTString();
  161. }
  162. document.cookie = cookieString;
  163. }
  164. function deleteCookie(name) {
  165. document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  166. }
  167. function getJSON(url, successHandler, errorHandler) {
  168. var xhr;
  169. xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP');
  170. var fURL = url.split('.').pop() == 'cgi' ? url+'?' : url+'&';
  171. xhr.open('GET', fURL+'_=' + new Date().getTime(), true);
  172. xhr.onreadystatechange = function () {
  173. var status, data;
  174. if (xhr.readyState == 4) {
  175. status = xhr.status;
  176. if (status == 200) {
  177. data = JSON.parse(xhr.responseText);
  178. successHandler(data);
  179. } else {
  180. errorHandler(status);
  181. }
  182. }
  183. };
  184. xhr.send();
  185. }
  186. function checkNotify() {
  187. ( getCookie('netsettings_changed') == 'true' ) ? apply_settings.init() : apply_settings.close();
  188. ( getCookie('device-error') == '1' ) ? device_error.init() : device_error.close();
  189. }
  190. function getParam(e,x) {
  191. switch (x) {
  192. case 0:
  193. return (e.snmp === 'true');
  194. case 1:
  195. return (e.sms === 'true');
  196. case 2:
  197. return (e.email === 'true');
  198. }
  199. }
  200. function decimalToBinary(decimal) {
  201. return (decimal >>> 0).toString(2);
  202. }
  203. function binaryToDecimal(binaryString) {
  204. return parseInt(binaryString, 2);
  205. }
  206. function cbToBit(id, n){
  207. var bit = $(id).checked ? 1 : 0;
  208. return bit << n;
  209. }
  210. function arrayToBitMask(id){
  211. // var parent = $(id).id;
  212. var snmp = cbToBit('cb0'+id, 0);
  213. var sms = cbToBit('cb1'+id, 1);
  214. // var email = cbToBit('cb2'+id, 2);
  215. return binaryToDecimal( decimalToBinary( (snmp | sms) ) );
  216. }
  217. function NotificationBox (id, options) {
  218. this.id = id;
  219. this.options = (options ? options : {});
  220. if(! ('message' in this.options)) this.options.message = 'NotificationBox: Hello World!';
  221. if(! ('bgcolor' in this.options)) this.options.bgcolor = '#009966';
  222. if(! ('textcolor' in this.options)) this.options.textcolor = '#fff';
  223. if(! ('show' in this.options)) this.options.show = true;
  224. if(! ('url' in this.options)) this.options.url = false;
  225. }
  226. NotificationBox.prototype.init = function(){
  227. if (!$(this.id)) {
  228. if (this.options.show) {
  229. var maindiv = document.createElement('div');
  230. maindiv.id = this.id;
  231. maindiv.className = 'notify-box';
  232. maindiv.style.backgroundColor = this.options.bgcolor;
  233. maindiv.style.color = this.options.textcolor;
  234. var string = document.createElement('p');
  235. string.textContent = this.options.message;
  236. maindiv.appendChild(string);
  237. if (this.options.url) {
  238. var confirm = document.createElement('button');
  239. confirm.className = 'btn btn-danger-inverted';
  240. confirm.textContent = 'Подтвердить';
  241. confirm.setAttribute('onclick', 'loadXMLDoc(\''+ this.options.url +'\',\'GET\', updatepage)');
  242. maindiv.appendChild(confirm);
  243. }
  244. if (this.options.cancelButton) {
  245. var cancel = document.createElement('button');
  246. cancel.className = 'btn btn-warning-inverted';
  247. cancel.style.marginLeft = '5px';
  248. cancel.textContent = 'Отмена';
  249. cancel.setAttribute('onclick', 'loadXMLDoc(\''+ this.options.cancelUrl +'\',\'GET\', updatepage)');
  250. maindiv.appendChild(cancel);
  251. }
  252. document.body.insertBefore(maindiv, document.body.childNodes[0]);
  253. fadeIn($(this.id));
  254. }
  255. }
  256. };
  257. NotificationBox.prototype.close = function(){
  258. if ($(this.id) ){
  259. var set = $(this.id);
  260. document.body.removeChild(set);
  261. }
  262. };