tazpanel annotate lib/tazpanel.js @ rev 430

boot.cgi: large cmdline
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Apr 06 13:08:02 2015 +0200 (2015-04-06)
parents b4817a7f661b
children b0146d791379
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@419 15 return "$(_ 'Confirm break')";
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@419 66 body.className = (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@419 354 statusElem.innerHTML = req.statusText // show status (Not Found, ОК..)
al@419 355
al@419 356 // if status 200 (ОК) - show answer to user
al@419 357 if (req.status == 200)
al@419 358 document.getElementById(ajaxOut).innerHTML = req.responseText;
al@419 359 // here we can add "else" with request errors processing
al@419 360 }
al@419 361 }
al@419 362
al@419 363 // (3) set request address
al@419 364 req.open('POST', cgiUrl, true);
al@419 365
al@419 366 // (4) request object is ready
al@419 367 req.send(command); // send request
al@419 368
al@419 369 // (5)
al@419 370 statusElem.innerHTML = '<img src="/styles/default/images/loader.gif" />'
al@419 371 }
al@419 372
al@419 373
al@419 374
al@419 375 //
al@419 376 // Load configuration for new and stored networks
al@419 377 //
al@419 378
al@419 379 function loadcfg(essid, bssid, keyType) {
al@419 380 // looking for stored network
al@419 381 for (i = 0; i < networks.length; i++) {
al@419 382 if (networks[i].ssid == essid) break;
al@419 383 if (typeof networks[i].bssid != 'undefined') {
al@419 384 if (networks[i].bssid == bssid) {
al@419 385 essid = networks[i].ssid;
al@419 386 break;
al@419 387 }
al@419 388 }
al@419 389 }
al@419 390 document.getElementById('essid').value = essid;
al@419 391 document.getElementById('keyType').value = keyType;
al@419 392
al@419 393 password = document.getElementById('password')
al@419 394 password.value = '';
al@419 395 if (typeof networks[i] != 'undefined') {
al@419 396 if (typeof networks[i].psk != 'undefined')
al@419 397 password.value = networks[i].psk;
al@419 398 else if (typeof networks[i].wep_key0 != 'undefined')
al@419 399 password.value = networks[i].wep_key0;
al@419 400 else if (typeof networks[i].password != 'undefined')
al@419 401 password.value = networks[i].password;
al@419 402 }
al@419 403
al@420 404 // Not used yet
al@420 405 document.getElementById('bssid').value = '';
al@420 406
al@419 407 wifiSettingsChange();
al@419 408 }
al@426 409
al@426 410
al@426 411 //
al@426 412 // Toggle all checkboxes on a page
al@426 413 //
al@426 414
al@428 415 function checkBoxes() {
al@428 416 var inputs = document.getElementsByTagName('input');
al@428 417 for (var i = 0; i < inputs.length; i++) {
al@428 418 if (inputs[i].type && inputs[i].type == 'checkbox') {
al@428 419 inputs[i].checked = !inputs[i].checked;
al@428 420 countSelPkgs(inputs[i]);
al@426 421 }
al@426 422 }
al@426 423 }
al@426 424
al@428 425
al@428 426 //
al@428 427 // Count selected packages on the packages list
al@428 428 //
al@428 429
al@428 430 function countSelPkgs(el) {
al@428 431 countSelected = countSelectedSpan.innerText;
al@428 432 if (countSelected == '') countSelected = 0;
al@428 433
al@428 434 element = (el.type == 'change' ? this : el);
al@428 435
al@428 436 if (element.checked)
al@428 437 countSelected++;
al@428 438 else
al@428 439 countSelected--;
al@428 440
al@428 441 countSelectedSpan.innerText = countSelected;
al@428 442 }
al@428 443
al@428 444 // Attach event handler
al@428 445 function setCountSelPkgs() {
al@428 446 // The change event does not bubble to the form container
al@428 447 pkglist = document.getElementById('pkglist');
al@428 448 if (pkglist) {
al@428 449 var checkboxes = pkglist.getElementsByTagName('input');
al@428 450 for (i = 0; i < checkboxes.length; i++) {
al@428 451 checkboxes[i].onchange = countSelPkgs;
al@428 452 }
al@428 453 }
al@428 454 countSelectedSpan = document.getElementById('countSelected');
al@428 455 }