tazpanel annotate lib/tazpanel.js @ rev 498

Use bottom panel as status bar; settings: data picker, lock icon for locked users; font: add 'calendar', 'modem', and 'vpn' icons; etc...
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon May 18 17:13:57 2015 +0300 (2015-05-18)
parents 7ca14d55e705
children 3117717c007d
rev   line source
al@419 1
al@419 2 // Hide Loading panel
al@419 3 function hideLoading() {
al@419 4 var loading = document.getElementById("loading");
al@419 5 // If element presents on page
al@419 6 if (loading) loading.style.display='none';
al@419 7 }
al@419 8 // Attach event handler
al@419 9 window.onload = hideLoading;
al@419 10
al@419 11
al@419 12
al@419 13 // Confirm page loading break
al@419 14 function confirmExit() {
al@462 15 return document.getElementById('confirmBreak').innerText;
al@419 16 }
al@419 17 // Attach event handler
al@419 18 window.onbeforeunload = confirmExit;
al@419 19
al@419 20
al@419 21
al@419 22 // Set light or dark color theme
al@419 23 function setColorTheme() {
al@419 24 // Goal: to produce pages that fit the user's defined look and feel, and
al@419 25 // may be more accessible as the current user settings (contrast color
al@419 26 // schemes, big font sizes, etc.)
al@419 27 // Realization in the CSS2:
al@419 28 // http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
al@419 29 // We can use colors from user's color theme to mimic plain desktop application.
al@419 30 //
al@419 31 // Alas, nowadays it not works. Webkit-based browser returns hard-coded values,
al@419 32 // with exception of these two:
al@419 33 // * ButtonText - text on push buttons.
al@419 34 // * CaptionText - text in caption, size box, and scrollbar arrow box.
al@419 35 //
al@419 36 // When user changes color theme, browser re-draws page, so all input elements
al@419 37 // (buttons, checkboxes, radiobuttons, drop-down lists, scrollbars, text inputs)
al@419 38 // automagically fits user-defined theme. We need to change web document's
al@419 39 // colors manually. We can't ask for exact user defined colors for document's
al@419 40 // background in any way, we can only imagine if it dark or light.
al@419 41 // So, plan is follows:
al@419 42 // We use 'ButtonText' color for base document's color, and calculate if it
al@419 43 // dark or light. If color of button's text is dark, then color of button's body
al@419 44 // is light, and we define entire web document as light; and vice versa.
al@419 45
al@419 46 // Get 'ButtonText' color value (as current text's color of body)
al@419 47 var body = document.getElementsByTagName('body')[0];
al@419 48 var bodyColor = window.getComputedStyle(body, null).color;
al@419 49 console.info("Set bodyColor: %s", bodyColor);
al@419 50
al@419 51 // Returned value must be in format like "rgb(212, 212, 212)" or "rgba(192, 68, 1, 0)"
al@419 52 if (bodyColor.indexOf("rgb") != -1) {
al@419 53 // Make array with color components
al@419 54 var results = bodyColor.match(/^rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/);
al@419 55
al@419 56 // Calculate luminance (brightness) in range 0..1
al@419 57 if (results && results.length >= 3) {
al@419 58 r = parseInt(results[1], 10);
al@419 59 g = parseInt(results[2], 10);
al@419 60 b = parseInt(results[3], 10);
al@419 61 min = Math.min(r, g, b); max = Math.max(r, g, b);
al@419 62 }
al@419 63 var luminance = (min + max) / 2 / 255;
al@419 64
al@419 65 // Set class for body
al@437 66 body.classList.add( (luminance > 0.5) ? 'dark' : 'light');
al@419 67 }
al@419 68 // Now we can use cascade styles for any elements lying on light or dark body:
al@419 69 // .light h2 { color: #222; }
al@419 70 // .dark h2 { color: #CCC; }
al@419 71 // Also we can use semi-transparent colors in some cases:
al@419 72 // body footer { color: rgba(128, 128, 128, 0.5); }
al@419 73 }
al@419 74
al@419 75
al@419 76
al@419 77 // Set base font size
al@419 78 function setBaseFont() {
al@419 79 // Goal: to have on page the same font sizes as in user's system.
al@419 80 // User input elements changed its font size automatically, so we can read
al@419 81 // font size from (hidden) button and apply this size to document's body.
al@419 82 // All other sizes will be relative, and will grow or shrink automatically.
al@419 83
al@419 84 // Get document's body
al@419 85 var body = document.getElementsByTagName('body')[0];
al@419 86
al@419 87 // Make button in the hidden DOM
al@419 88 var hiddenButton = document.createElement('BUTTON');
al@419 89
al@419 90 // Move button away from look
al@419 91 hiddenButton.style.setProperty('position', 'absolute', 1);
al@419 92 hiddenButton.style.setProperty('left', '0', 1);
al@419 93
al@419 94 // Add some text to button
al@419 95 hiddenButton.appendChild(document.createTextNode("SliTaz"));
al@419 96
al@419 97 // Add button to DOM
al@419 98 body.appendChild(hiddenButton);
al@419 99
al@419 100 // Focus to button (it will get colored outline!)
al@419 101 hiddenButton.focus();
al@419 102
al@419 103 // Get button's style
al@419 104 var buttonStyle = getComputedStyle(hiddenButton, null);
al@419 105
al@419 106 // Copy styles from button to body
al@419 107 with (body.style) {
al@419 108 fontFamily = buttonStyle.fontFamily;
al@419 109 fontSize = buttonStyle.fontSize;
al@419 110 fontWeight = 'normal';
al@419 111 }
al@419 112
al@419 113 //body.style.color = buttonStyle.outlineColor;
al@419 114
al@419 115 console.info('Set fontFamily: %s, fontSize: %s', body.style.fontFamily, body.style.fontSize);
al@419 116 console.info('Theme color: %s', buttonStyle.outlineColor);
al@419 117 // Remove button from hidden DOM
al@419 118 body.removeChild(hiddenButton);
al@419 119 }
al@419 120
al@419 121
al@419 122
al@419 123 //
al@419 124 function dupTableHead() {
al@419 125 if (! document.getElementById('head2')) return
al@419 126 var tableHead = document.createElement("TABLE");
al@419 127 with (tableHead) {
al@419 128 innerHTML = '<thead>' + document.getElementById('head2').innerHTML + '</thead>'
al@419 129 setAttribute("id", "head1h");
al@419 130 setAttribute("class", "zebra pkglist");
al@419 131 }
al@419 132
al@419 133 document.body.appendChild(tableHead);
al@419 134 }
al@419 135
al@419 136
al@419 137
al@419 138 //
al@419 139 function scrollHandler(){
al@419 140 toolbar = document.getElementById('toolbar');
al@419 141 paddingTop = toolbar.offsetTop + toolbar.offsetHeight;
al@419 142 paddingTopPx = paddingTop + 'px';
al@419 143
al@419 144 headerOffset = document.getElementById('head1').offsetTop;
al@419 145 windowScrolled = document.body.scrollTop;
al@419 146 if ((headerOffset - windowScrolled) < paddingTop)
al@419 147 {
al@419 148 // document.getElementById('miscinfo1').innerText = '<';
al@419 149 var head1h = document.getElementById('head1h');
al@419 150 var head1 = document.getElementById('head1');
al@419 151
al@419 152 with (head1h.style) {
al@419 153 setProperty('top', paddingTopPx, 1);
al@419 154 setProperty('display', 'table', 1);
al@419 155 setProperty('left', head1.offsetLeft + 'px', 1);
al@419 156 setProperty('width', head1.offsetWidth + 'px', 1);
al@419 157 }
al@419 158
al@419 159 // document.getElementById('miscinfo1').innerText = '(' + toopop +')P^' + paddingTop + 'L' + head1h.offsetLeft + ':W' + head1h.offsetWidth + ':H' + head1h.offsetHeight + ':T' + head1h.clientTop +',' + head1h.offsetTop;
al@419 160
al@419 161 }
al@419 162 else
al@419 163 {
al@426 164 //document.getElementById('miscinfo1').innerText = '>';
al@419 165 document.getElementById('head1h').style.display = 'none';
al@419 166 }
al@419 167
al@419 168 tHeadTr = document.getElementById('head1h').children[0].children[0];
al@419 169 tHeadTrO = document.getElementById('head1').children[0].children[0];
al@419 170 tHeadTr.children[0].style.setProperty('width', tHeadTrO.children[0].offsetWidth -1 + 'px', 1);
al@419 171 tHeadTr.children[1].style.setProperty('width', tHeadTrO.children[1].offsetWidth -1 + 'px', 1);
al@419 172 tHeadTr.children[2].style.setProperty('width', tHeadTrO.children[2].offsetWidth -1 + 'px', 1);
al@428 173 //tHeadTr.children[3].style.setProperty('width', tHeadTrO.children[3].offsetWidth -1 + 'px', 1);
al@419 174
al@419 175 }
al@419 176
al@419 177
al@419 178
al@419 179 // Handler for History scroller for Terminal
al@419 180 function keydownHandler(e) {
al@419 181 // Get code for pressed key
al@419 182 var evt = e ? e:event;
al@419 183 var keyCode = evt.keyCode;
al@419 184
al@419 185 // If pressed "up" or "down"
al@419 186 if (keyCode==38 || keyCode==40) {
al@419 187 // Local storage used as global variables storage
al@419 188 // Get current position in the History, and History size
al@419 189 //var cur_hist = window.localStorage.getItem("cur_hist");
al@419 190 //var max_hist = window.localStorage.getItem("max_hist");
al@419 191 switch(keyCode) {
al@419 192 case 38:
al@419 193 // "up" key pressed; decrement and normalize position
al@419 194 cur_hist--; if (cur_hist < 0) cur_hist = 0;
al@419 195 break;
al@419 196 case 40:
al@419 197 // "down" key pressed; increment and normalize position
al@419 198 cur_hist++; if (cur_hist > max_hist) cur_hist = max_hist;
al@419 199 break;
al@419 200 }
al@419 201 // Element "cmd" is a text input, put History element there
al@419 202 var cmd = document.getElementById('typeField');
al@419 203 cmd.focus();
al@419 204 cmd.innerText = ash_history[cur_hist];
al@419 205
al@419 206 var hint = ''
al@419 207 if (cur_hist < max_hist) hint = '[' + cur_hist + '/' + (max_hist - 1) + '] ';
al@419 208 document.getElementById('num_hist').innerText = hint;
al@419 209
al@419 210 //window.localStorage.setItem('cur_hist', cur_hist);
al@419 211 return false
al@419 212 }
al@419 213 if (keyCode==13) {
al@419 214 document.getElementById('cmd').value=document.getElementById('typeField').innerText;
al@419 215 document.getElementById('term').submit();
al@419 216 return false
al@419 217 }
al@419 218 return true
al@419 219 }
al@419 220
al@419 221
al@419 222
al@419 223 // Add hover effect for menu
al@419 224 function menuAddHover() {
al@419 225 var toolbarMenu = document.getElementById('toolbarMenu');
al@419 226 toolbarMenu.className = 'hover';
al@419 227
al@419 228 menus = toolbarMenu.getElementsByTagName('li');
al@419 229 for (i = 0; i < menus.length; i++)
al@419 230 menus[i].blur();
al@419 231
al@419 232 toolbarMenu.focus();
al@419 233 toolbarMenu.onblur = menuRmHover;
al@419 234 console.log('Add finished');
al@419 235 }
al@419 236
al@419 237 // Remove hover effect for menu
al@419 238 function menuRmHover() {
al@419 239 var toolbarMenu = document.getElementById('toolbarMenu');
al@419 240 toolbarMenu.className = 'nohover';
al@419 241
al@419 242 menus = toolbarMenu.getElementsByTagName('li');
al@419 243 for (i = 0; i < menus.length; i++)
al@419 244 menus[i].onclick = menuAddHover;
al@419 245
al@419 246 console.log('Rm finished');
al@419 247 }
al@419 248
al@419 249
al@419 250 // Close main menu
al@419 251 function closeMenu() {
al@419 252 document.getElementById('noMenu').style.display = 'none';
al@419 253 closeItem(itemOpened);
al@419 254 menuIsClosed = true;
al@419 255 //console.log('Menu closed');
al@419 256 }
al@419 257 // Open main menu
al@419 258 function openMenu() {
al@419 259 //console.log('openMenu')
al@419 260 document.getElementById('noMenu').style.display = 'block';
al@419 261 menuIsClosed = false;
al@419 262 }
al@419 263 // Open main menu item
al@419 264 function openItem(el) {
al@419 265 if (itemOpened != el) {
al@419 266 if (itemOpened)
al@419 267 closeItem(itemOpened);
al@419 268 el.children[1].className = 'opened';
al@419 269 el.focus();
al@419 270 itemOpened = el;
al@419 271 menuIsClosed = false; openMenu();
al@419 272 //console.log('Opened %s', el.tabIndex);
al@419 273 //console.log('Menu opened (open)');
al@419 274 }
al@419 275 }
al@419 276 // Close main menu item
al@419 277 function closeItem(el) {
al@419 278 //console.log('<closeItem: "%s">', el)
al@419 279 thisMenu = el.children[1];
al@419 280 if (thisMenu.className == 'opened') {
al@419 281 thisMenu.className = 'closed';
al@419 282 el.blur();
al@419 283 itemOpened = ''
al@419 284 //console.log('Closed %s', el.tabIndex);
al@419 285 }
al@419 286 }
al@419 287
al@419 288 itemOpened = '';
al@419 289 menuIsClosed = true;
al@419 290
al@419 291 // Add event handler
al@419 292 function addMenuHandlers() {
al@419 293 menus = document.getElementById('toolbarMenu').children;
al@419 294 for (i = 0; i < menus.length; i++) {
al@419 295 menus[i].firstElementChild.onclick = function() {menuItemClick(this)};
al@419 296 menus[i].firstElementChild.onmouseover = function() {menuItemHover(this)};
al@419 297 menus[i].onblur = function() {menuItemBlur(this)};
al@419 298 }
al@419 299
al@419 300 // Close menu when click outside menu
al@419 301 document.getElementById('noMenu').onclick = closeMenu;
al@419 302 }
al@419 303
al@419 304 function menuItemClick(el) {
al@419 305 topItem = el.parentElement;
al@419 306 thisMenu = topItem.children[1];
al@419 307 //console.log('Clicked %s class %s', topItem.tabIndex, thisMenu.className);
al@419 308 if (thisMenu.className == 'opened') {
al@419 309 closeItem(topItem);
al@419 310 menuIsClosed = true; closeMenu();
al@419 311 //console.log('Menu closed (click)');
al@419 312 }
al@419 313 else {
al@419 314 openItem(topItem);
al@419 315 menuIsClosed = false; openMenu();
al@419 316 //console.log('Menu opened (click)');
al@419 317 }
al@419 318 }
al@419 319
al@419 320 function menuItemHover(el) {
al@419 321 hoverElem = el.parentElement;
al@419 322 //console.log('Hovered %s', hoverElem.tabIndex);
al@419 323 if (! menuIsClosed) {
al@419 324 closeItem(itemOpened);
al@419 325 openItem(hoverElem);
al@419 326 }
al@419 327 }
al@419 328 function menuItemBlur(el) {
al@419 329 elem = el; //.parentElement;
al@419 330 //console.log('Blurred %s', elem.tabIndex);
al@419 331 //closeItem(elem);
al@419 332 //menuIsClosed = true;
al@419 333 //console.log('Menu closed (blur)');
al@419 334 }
al@419 335
al@419 336
al@419 337
al@419 338 //
al@419 339 // AJAX code for dynamic requests
al@419 340 //
al@419 341
al@419 342 function ajax(cgiUrl, command, ajaxOut) {
al@419 343 // (1) create object for server request
al@419 344 var req = new XMLHttpRequest();
al@419 345
al@419 346 // (2) show request status
al@419 347 var statusElem = document.getElementById('ajaxStatus');
al@419 348
al@419 349 req.onreadystatechange = function() {
al@419 350 // onreadystatechange activates on server answer receiving
al@419 351
al@419 352 if (req.readyState == XMLHttpRequest.DONE) {
al@419 353 // if request done
al@498 354 if (req.statusText == 'OK') {
al@498 355 statusElem.innerHTML = "<span data-icon='ok'></span>"
al@498 356 } else {
al@498 357 statusElem.innerHTML = "<span data-icon='delete'>" +
al@498 358 req.statusText + "</span>" // show status (Not Found, ...)
al@498 359 }
al@419 360
al@419 361 // if status 200 (ОК) - show answer to user
al@419 362 if (req.status == 200)
al@419 363 document.getElementById(ajaxOut).innerHTML = req.responseText;
al@419 364 // here we can add "else" with request errors processing
al@419 365 }
al@419 366 }
al@419 367
al@419 368 // (3) set request address
al@419 369 req.open('POST', cgiUrl, true);
al@419 370
al@419 371 // (4) request object is ready
al@419 372 req.send(command); // send request
al@419 373
al@419 374 // (5)
al@498 375 statusElem.innerHTML = '<span data-img="clock"></span>'
al@419 376 }
al@419 377
al@419 378
al@419 379
al@419 380 //
al@419 381 // Load configuration for new and stored networks
al@419 382 //
al@419 383
al@419 384 function loadcfg(essid, bssid, keyType) {
al@419 385 // looking for stored network
al@419 386 for (i = 0; i < networks.length; i++) {
al@419 387 if (networks[i].ssid == essid) break;
al@419 388 if (typeof networks[i].bssid != 'undefined') {
al@419 389 if (networks[i].bssid == bssid) {
al@419 390 essid = networks[i].ssid;
al@419 391 break;
al@419 392 }
al@419 393 }
al@419 394 }
al@419 395 document.getElementById('essid').value = essid;
al@419 396 document.getElementById('keyType').value = keyType;
al@419 397
al@419 398 password = document.getElementById('password')
al@419 399 password.value = '';
al@419 400 if (typeof networks[i] != 'undefined') {
al@419 401 if (typeof networks[i].psk != 'undefined')
al@419 402 password.value = networks[i].psk;
al@419 403 else if (typeof networks[i].wep_key0 != 'undefined')
al@419 404 password.value = networks[i].wep_key0;
al@419 405 else if (typeof networks[i].password != 'undefined')
al@419 406 password.value = networks[i].password;
al@419 407 }
al@419 408
al@420 409 // Not used yet
al@420 410 document.getElementById('bssid').value = '';
al@420 411
al@419 412 wifiSettingsChange();
al@419 413 }
al@426 414
al@426 415
al@426 416 //
al@426 417 // Toggle all checkboxes on a page
al@426 418 //
al@426 419
al@428 420 function checkBoxes() {
al@428 421 var inputs = document.getElementsByTagName('input');
al@428 422 for (var i = 0; i < inputs.length; i++) {
al@428 423 if (inputs[i].type && inputs[i].type == 'checkbox') {
al@428 424 inputs[i].checked = !inputs[i].checked;
al@428 425 countSelPkgs(inputs[i]);
al@426 426 }
al@426 427 }
al@426 428 }
al@426 429
al@428 430
al@428 431 //
al@428 432 // Count selected packages on the packages list
al@428 433 //
al@428 434
al@428 435 function countSelPkgs(el) {
al@437 436 countSelectedSpan = document.getElementById('countSelected');
al@428 437 countSelected = countSelectedSpan.innerText;
al@428 438 if (countSelected == '') countSelected = 0;
al@428 439
al@428 440 element = (el.type == 'change' ? this : el);
al@428 441
al@428 442 if (element.checked)
al@428 443 countSelected++;
al@428 444 else
al@428 445 countSelected--;
al@428 446
al@428 447 countSelectedSpan.innerText = countSelected;
al@428 448 }
al@428 449
al@428 450 // Attach event handler
al@428 451 function setCountSelPkgs() {
al@428 452 // The change event does not bubble to the form container
al@428 453 pkglist = document.getElementById('pkglist');
al@428 454 if (pkglist) {
al@428 455 var checkboxes = pkglist.getElementsByTagName('input');
al@428 456 for (i = 0; i < checkboxes.length; i++) {
al@428 457 checkboxes[i].onchange = countSelPkgs;
al@428 458 }
al@428 459 }
al@428 460 }
al@462 461
al@462 462
al@462 463 //
al@462 464 // Improving packages by the community effort.
al@462 465 //
al@462 466
al@462 467 function improveAction() {
al@462 468 improveType = document.getElementById('improveType');
al@462 469 improveText = document.getElementById('improveText');
al@462 470
al@462 471 if (improveType.value == '')
al@462 472 improveText.value = '';
al@462 473 else
al@462 474 improveText.value = document.getElementById(improveType.value).innerText;
al@462 475 }
al@463 476
al@463 477
al@463 478 //
al@463 479 // Edit and save files "in place"
al@463 480 //
al@463 481
al@463 482 function editFile() {
al@463 483 document.getElementById('edit_button').style.display = 'none';
al@463 484 document.getElementById('save_button').style.display = '';
al@463 485
al@463 486 with(document.getElementById('fileContent')) {
al@463 487 contentEditable = true;
al@463 488 onkeydown = insertTab;
al@463 489 focus();
al@463 490 }
al@463 491 }
al@463 492
al@463 493 function saveFile(file, fileTitle) {
al@463 494 var newArea = document.createElement('TEXTAREA');
al@463 495 with(newArea) {
al@463 496 name = 'content';
al@463 497 textContent = document.getElementById('fileContent').textContent;
al@463 498 }
al@463 499
al@463 500 var newTitle = document.createElement('INPUT');
al@463 501 with(newTitle) {
al@463 502 name = 'title';
al@463 503 value = fileTitle;
al@463 504 }
al@463 505
al@463 506 var newForm = document.createElement('FORM');
al@463 507 with(newForm) {
al@463 508 appendChild(newArea);
al@463 509 appendChild(newTitle);
al@463 510 method = 'post';
al@463 511 action = "?file=" + file;
al@463 512 submit();
al@463 513 }
al@463 514 }
al@463 515
al@463 516 function insertTab(e) {
al@463 517 var evt = e ? e:event;
al@463 518 if (evt.keyCode == 9) {
al@463 519 evt.preventDefault();
al@463 520 }
al@463 521 }
al@498 522
al@498 523
al@498 524 //
al@498 525 // Show info in the status bar
al@498 526 //
al@498 527
al@498 528 function statusbar(status, ticker) {
al@498 529 var ds = document.getElementById('defaultStatus');
al@498 530 var sb = document.getElementById('statusBar');
al@498 531 var as = document.getElementById('ajaxStatus');
al@498 532
al@498 533 if (status == '') {
al@498 534 // show default status (SliTaz copyright and license)
al@498 535 sb.style.display = 'none'; ds.style.display = ''; as.innerHTML = '';
al@498 536 } else {
al@498 537 // show requested status (rich HTML supported)
al@498 538 ds.style.display = 'none'; sb.innerHTML = status; sb.style.display = '';
al@498 539 // show optional ticker
al@498 540 if (ticker != '') {
al@498 541 as.innerHTML = '<img src="/styles/default/images/loader.gif"/>';
al@498 542 }
al@498 543 }
al@498 544 }
al@498 545