tazpanel view lib/tazpanel.js @ rev 617

revision of german messages
author Hans-G?nter Theisgen
date Mon Oct 09 17:15:22 2017 +0100 (2017-10-09)
parents c79a4557c4b5
children
line source
2 // Hide Loading panel
3 function hideLoading() {
4 var loading = document.getElementById("loading");
5 // If element presents on page
6 if (loading) loading.style.display='none';
7 }
8 // Attach event handler
9 window.onload = hideLoading;
13 // Confirm page loading break
14 function confirmExit() {
15 return document.getElementById('confirmBreak').textContent;
16 }
17 // Attach event handler
18 window.onbeforeunload = confirmExit;
22 // Set light or dark color theme
23 function setColorTheme() {
24 // Goal: to produce pages that fit the user's defined look and feel, and
25 // may be more accessible as the current user settings (contrast color
26 // schemes, big font sizes, etc.)
27 // Realization in the CSS2:
28 // http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
29 // We can use colors from user's color theme to mimic plain desktop application.
30 //
31 // Alas, nowadays it not works. Webkit-based browser returns hard-coded values,
32 // with exception of these two:
33 // * ButtonText - text on push buttons.
34 // * CaptionText - text in caption, size box, and scrollbar arrow box.
35 //
36 // When user changes color theme, browser re-draws page, so all input elements
37 // (buttons, checkboxes, radiobuttons, drop-down lists, scrollbars, text inputs)
38 // automagically fits user-defined theme. We need to change web document's
39 // colors manually. We can't ask for exact user defined colors for document's
40 // background in any way, we can only imagine if it dark or light.
41 // So, plan is follows:
42 // We use 'ButtonText' color for base document's color, and calculate if it
43 // dark or light. If color of button's text is dark, then color of button's body
44 // is light, and we define entire web document as light; and vice versa.
46 // Get 'ButtonText' color value (as current text's color of body)
47 var body = document.getElementsByTagName('body')[0];
48 var bodyColor = window.getComputedStyle(body, null).color;
49 console.info("Set bodyColor: %s", bodyColor);
51 // Returned value must be in format like "rgb(212, 212, 212)" or "rgba(192, 68, 1, 0)"
52 if (bodyColor.indexOf("rgb") != -1) {
53 // Make array with color components
54 var results = bodyColor.match(/^rgba?\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/);
56 // Calculate luminance (brightness) in range 0..1
57 if (results && results.length >= 3) {
58 r = parseInt(results[1], 10);
59 g = parseInt(results[2], 10);
60 b = parseInt(results[3], 10);
61 min = Math.min(r, g, b); max = Math.max(r, g, b);
62 }
63 var luminance = (min + max) / 2 / 255;
65 // Set class for body
66 body.classList.add( (luminance > 0.5) ? 'dark' : 'light');
67 }
68 // Now we can use cascade styles for any elements lying on light or dark body:
69 // .light h2 { color: #222; }
70 // .dark h2 { color: #CCC; }
71 // Also we can use semi-transparent colors in some cases:
72 // body footer { color: rgba(128, 128, 128, 0.5); }
73 }
77 // Set base font size
78 function setBaseFont() {
79 // Goal: to have on page the same font sizes as in user's system.
80 // User input elements changed its font size automatically, so we can read
81 // font size from (hidden) button and apply this size to document's body.
82 // All other sizes will be relative, and will grow or shrink automatically.
84 // Get document's body
85 var body = document.getElementsByTagName('body')[0];
87 // Make button in the hidden DOM
88 var hiddenButton = document.createElement('BUTTON');
90 // Move button away from look
91 hiddenButton.style.setProperty('position', 'absolute', 1);
92 hiddenButton.style.setProperty('left', '0', 1);
94 // Add some text to button
95 hiddenButton.appendChild(document.createTextNode("SliTaz"));
97 // Add button to DOM
98 body.appendChild(hiddenButton);
100 // Focus to button (it will get colored outline!)
101 hiddenButton.focus();
103 // Get button's style
104 var buttonStyle = getComputedStyle(hiddenButton, null);
106 // Copy styles from button to body
107 with (body.style) {
108 fontFamily = buttonStyle.fontFamily;
109 fontSize = buttonStyle.fontSize;
110 fontWeight = 'normal';
111 }
113 //body.style.color = buttonStyle.outlineColor;
115 console.info('Set fontFamily: %s, fontSize: %s', body.style.fontFamily, body.style.fontSize);
116 console.info('Theme color: %s', buttonStyle.outlineColor);
117 // Remove button from hidden DOM
118 body.removeChild(hiddenButton);
119 }
123 //
124 function dupTableHead() {
125 if (! document.getElementById('head2')) return;
126 var tableHead = document.createElement("TABLE");
127 with (tableHead) {
128 innerHTML = '<thead>' + document.getElementById('head2').innerHTML + '</thead>';
129 setAttribute("id", "head1h");
130 setAttribute("class", "zebra pkglist");
131 }
133 document.body.appendChild(tableHead);
134 }
138 //
139 function scrollHandler(){
140 toolbar = document.getElementById('toolbar');
141 paddingTop = toolbar.offsetTop + toolbar.offsetHeight;
142 paddingTopPx = paddingTop + 'px';
144 headerOffset = document.getElementById('head1').offsetTop;
145 windowScrolled = document.body.scrollTop;
146 if ((headerOffset - windowScrolled) < paddingTop)
147 {
148 // document.getElementById('miscinfo1').textContent = '<';
149 var head1h = document.getElementById('head1h');
150 var head1 = document.getElementById('head1');
152 with (head1h.style) {
153 setProperty('top', paddingTopPx, 1);
154 setProperty('display', 'table', 1);
155 setProperty('left', head1.offsetLeft + 'px', 1);
156 setProperty('width', head1.offsetWidth + 'px', 1);
157 }
159 // document.getElementById('miscinfo1').textContent = '(' + toopop +')P^' + paddingTop + 'L' + head1h.offsetLeft + ':W' + head1h.offsetWidth + ':H' + head1h.offsetHeight + ':T' + head1h.clientTop +',' + head1h.offsetTop;
161 }
162 else
163 {
164 //document.getElementById('miscinfo1').textContent = '>';
165 document.getElementById('head1h').style.display = 'none';
166 }
168 tHeadTr = document.getElementById('head1h').children[0].children[0];
169 tHeadTrO = document.getElementById('head1').children[0].children[0];
170 tHeadTr.children[0].style.setProperty('width', tHeadTrO.children[0].offsetWidth -1 + 'px', 1);
171 tHeadTr.children[1].style.setProperty('width', tHeadTrO.children[1].offsetWidth -1 + 'px', 1);
172 tHeadTr.children[2].style.setProperty('width', tHeadTrO.children[2].offsetWidth -1 + 'px', 1);
173 //tHeadTr.children[3].style.setProperty('width', tHeadTrO.children[3].offsetWidth -1 + 'px', 1);
175 }
179 // Handler for History scroller for Terminal
180 function keydownHandler(e) {
181 // Get code for pressed key
182 var evt = e ? e:event;
183 var keyCode = evt.keyCode;
185 // If pressed "up" or "down"
186 if (keyCode==38 || keyCode==40) {
187 // Local storage used as global variables storage
188 // Get current position in the History, and History size
189 //var cur_hist = window.localStorage.getItem("cur_hist");
190 //var max_hist = window.localStorage.getItem("max_hist");
191 switch(keyCode) {
192 case 38:
193 // "up" key pressed; decrement and normalize position
194 cur_hist--; if (cur_hist < 0) cur_hist = 0;
195 break;
196 case 40:
197 // "down" key pressed; increment and normalize position
198 cur_hist++; if (cur_hist > max_hist) cur_hist = max_hist;
199 break;
200 }
201 // Element "cmd" is a text input, put History element there
202 var cmd = document.getElementById('typeField');
203 cmd.focus();
204 cmd.textContent = ash_history[cur_hist];
206 var hint = ''
207 if (cur_hist < max_hist) hint = '[' + cur_hist + '/' + (max_hist - 1) + '] ';
208 document.getElementById('num_hist').textContent = hint;
210 //window.localStorage.setItem('cur_hist', cur_hist);
211 return false
212 }
213 if (keyCode==13) {
214 document.getElementById('cmd').value=document.getElementById('typeField').textContent;
215 document.getElementById('term').submit();
216 return false
217 }
218 return true
219 }
223 // Add hover effect for menu
224 function menuAddHover() {
225 var toolbarMenu = document.getElementById('toolbarMenu');
226 toolbarMenu.className = 'hover';
228 menus = toolbarMenu.getElementsByTagName('li');
229 for (i = 0; i < menus.length; i++)
230 menus[i].blur();
232 toolbarMenu.focus();
233 toolbarMenu.onblur = menuRmHover;
234 console.log('Add finished');
235 }
237 // Remove hover effect for menu
238 function menuRmHover() {
239 var toolbarMenu = document.getElementById('toolbarMenu');
240 toolbarMenu.className = 'nohover';
242 menus = toolbarMenu.getElementsByTagName('li');
243 for (i = 0; i < menus.length; i++)
244 menus[i].onclick = menuAddHover;
246 console.log('Rm finished');
247 }
250 // Close main menu
251 function closeMenu() {
252 document.getElementById('noMenu').style.display = 'none';
253 closeItem(itemOpened);
254 menuIsClosed = true;
255 //console.log('Menu closed');
256 }
257 // Open main menu
258 function openMenu() {
259 //console.log('openMenu')
260 document.getElementById('noMenu').style.display = 'block';
261 menuIsClosed = false;
262 }
263 // Open main menu item
264 function openItem(el) {
265 if (itemOpened != el) {
266 if (itemOpened)
267 closeItem(itemOpened);
268 el.children[1].className = 'opened';
269 el.focus();
270 itemOpened = el;
271 menuIsClosed = false; openMenu();
272 //console.log('Opened %s', el.tabIndex);
273 //console.log('Menu opened (open)');
274 }
275 }
276 // Close main menu item
277 function closeItem(el) {
278 //console.log('<closeItem: "%s">', el)
279 thisMenu = el.children[1];
280 if (thisMenu.className == 'opened') {
281 thisMenu.className = 'closed';
282 el.blur();
283 itemOpened = '';
284 //console.log('Closed %s', el.tabIndex);
285 }
286 }
288 itemOpened = '';
289 menuIsClosed = true;
291 // Add event handler
292 function addMenuHandlers() {
293 menus = document.getElementById('toolbarMenu').children;
294 for (i = 0; i < menus.length; i++) {
295 menus[i].firstElementChild.onclick = function() {menuItemClick(this)};
296 menus[i].firstElementChild.onmouseover = function() {menuItemHover(this)};
297 menus[i].onblur = function() {menuItemBlur(this)};
298 }
300 // Close menu when click outside menu
301 document.getElementById('noMenu').onclick = closeMenu;
302 }
304 function menuItemClick(el) {
305 topItem = el.parentElement;
306 thisMenu = topItem.children[1];
307 //console.log('Clicked %s class %s', topItem.tabIndex, thisMenu.className);
308 if (thisMenu.className == 'opened') {
309 closeItem(topItem);
310 menuIsClosed = true; closeMenu();
311 //console.log('Menu closed (click)');
312 }
313 else {
314 openItem(topItem);
315 menuIsClosed = false; openMenu();
316 //console.log('Menu opened (click)');
317 }
318 }
320 function menuItemHover(el) {
321 hoverElem = el.parentElement;
322 //console.log('Hovered %s', hoverElem.tabIndex);
323 if (! menuIsClosed) {
324 closeItem(itemOpened);
325 openItem(hoverElem);
326 }
327 }
328 function menuItemBlur(el) {
329 elem = el;
330 //.parentElement;
331 //console.log('Blurred %s', elem.tabIndex);
332 //closeItem(elem);
333 //menuIsClosed = true;
334 //console.log('Menu closed (blur)');
335 }
339 //
340 // AJAX code for dynamic requests
341 //
343 // Global status of the AJAX loader
344 // Increment ajaxLoader on the start, decrement on the finish,
345 // Show it if value > 0
346 ajaxLoader = 0
348 function ajax(cgiUrl, command, ajaxOut) {
349 // (0) show AJAX loader
350 ajaxLoader++;
352 // (1) create object for server request
353 var req = new XMLHttpRequest();
355 // (2) show request status
356 var statusElem = document.getElementById('ajaxStatus');
358 req.onreadystatechange = function() {
359 // onreadystatechange activates on server answer receiving
361 if (req.readyState == XMLHttpRequest.DONE) {
362 // if request done
363 ajaxLoader--;
364 if (ajaxLoader == 0) {
366 statusbar('');
368 if (req.statusText == 'OK') {
369 statusElem.innerHTML = '<span data-img="@ok@"></span>';
370 } else {
371 // show status (Not Found, ...)
372 statusElem.innerHTML = '<span data-img="@delete@">' +
373 req.statusText + '</span>';
374 }
375 } else {
376 statusElem.innerHTML = '<span data-img="@clock@"></span>';
377 }
379 // if status 200 (ОК) - show answer to user
380 if (req.status == 200)
381 document.getElementById(ajaxOut).innerHTML = req.responseText;
382 // here we can add "else" with request errors processing
383 }
384 }
386 // (3) set request address
387 req.open('POST', cgiUrl, true);
389 // (4) request object is ready
390 // send request
391 req.send(command);
393 // (5)
394 statusElem.innerHTML = '<span data-img="@clock@"></span>';
395 }
399 //
400 // Load configuration for new and stored networks
401 //
403 function loadcfg(essid, bssid, keyType) {
404 // looking for stored network
405 for (i = 0; i < networks.length; i++) {
406 if (networks[i].ssid == essid) break;
407 if (typeof networks[i].bssid != 'undefined') {
408 if (networks[i].bssid == bssid) {
409 essid = networks[i].ssid;
410 break;
411 }
412 }
413 }
414 document.getElementById('essid').value = essid;
415 document.getElementById('keyType').value = keyType;
417 password = document.getElementById('password')
418 password.value = '';
419 if (typeof networks[i] != 'undefined') {
420 if (typeof networks[i].psk != 'undefined')
421 password.value = networks[i].psk;
422 else if (typeof networks[i].wep_key0 != 'undefined')
423 password.value = networks[i].wep_key0;
424 else if (typeof networks[i].password != 'undefined')
425 password.value = networks[i].password;
426 }
428 // Not used yet
429 document.getElementById('bssid').value = '';
431 wifiSettingsChange();
432 }
435 //
436 // Toggle all checkboxes on a page
437 //
439 function checkBoxes() {
440 var inputs = document.getElementsByTagName('input');
441 for (var i = 0; i < inputs.length; i++) {
442 if (inputs[i].type && inputs[i].type == 'checkbox') {
443 inputs[i].checked = !inputs[i].checked;
444 countSelPkgs(inputs[i]);
445 }
446 }
447 }
450 //
451 // Count selected packages on the packages list
452 //
454 function countSelPkgs(el) {
455 countSelectedSpan = document.getElementById('countSelected');
456 countSelected = countSelectedSpan.textContent;
457 if (countSelected == '') countSelected = 0;
459 element = (el.type == 'change' ? this : el);
461 if (element.checked)
462 countSelected++;
463 else
464 countSelected--;
466 countSelectedSpan.textContent = countSelected;
468 // Disable buttons when no one package selected
469 panelButtons = document.getElementsByName('do');
470 disabledState = (countSelected == 0) ? true : false;
471 for (var i = panelButtons.length - 1; i >= 0; i--) {
472 panelButtons[i].disabled = disabledState;
473 };
474 }
476 // Attach event handler
477 function setCountSelPkgs() {
478 // The change event does not bubble to the form container
479 pkglist = document.getElementById('pkglist');
480 if (pkglist) {
481 var checkboxes = pkglist.getElementsByTagName('input');
482 for (i = 0; i < checkboxes.length; i++) {
483 checkboxes[i].onchange = countSelPkgs;
484 }
485 }
486 // Disable buttons when no one package selected by default
487 panelButtons = document.getElementsByName('do');
488 for (var i = panelButtons.length - 1; i >= 0; i--) {
489 panelButtons[i].disabled = true;
490 };
491 }
494 //
495 // Improving packages by the community effort.
496 //
498 function improveAction() {
499 improveType = document.getElementById('improveType');
500 improveText = document.getElementById('improveText');
502 if (improveType.value == '')
503 improveText.value = '';
504 else
505 improveText.value = document.getElementById(improveType.value).textContent;
506 }
509 //
510 // Edit and save files "in place"
511 //
513 function editFile() {
514 document.getElementById('edit_button').style.display = 'none';
515 document.getElementById('save_button').style.display = '';
517 with(document.getElementById('fileContent')) {
518 contentEditable = true;
519 onkeydown = insertTab;
520 focus();
521 }
522 }
524 function saveFile(file, fileTitle) {
525 var newArea = document.createElement('TEXTAREA');
526 with(newArea) {
527 name = 'content';
528 textContent = document.getElementById('fileContent').textContent;
529 }
531 var newTitle = document.createElement('INPUT');
532 with(newTitle) {
533 name = 'title';
534 value = fileTitle;
535 }
537 var newForm = document.createElement('FORM');
538 with(newForm) {
539 appendChild(newArea);
540 appendChild(newTitle);
541 method = 'post';
542 action = "?file=" + file;
543 submit();
544 }
545 }
547 function insertTab(e) {
548 var evt = e ? e:event;
549 if (evt.keyCode == 9) {
550 evt.preventDefault();
551 }
552 }
555 //
556 // Show info in the status bar
557 //
559 function statusbar(status, ticker) {
560 var ds = document.getElementById('defaultStatus');
561 var sb = document.getElementById('statusBar');
562 var as = document.getElementById('ajaxStatus');
564 if (status == '') {
565 // show default status (SliTaz copyright and license)
566 sb.style.display = 'none'; ds.style.display = ''; as.innerHTML = '';
567 } else {
568 // show requested status (rich HTML supported)
569 ds.style.display = 'none'; sb.innerHTML = status; sb.style.display = '';
570 // show optional ticker
571 if (ticker != '') {
572 as.innerHTML = '<span data-img="@clock@"></span>';
573 }
574 }
575 }
579 //
580 // Class manipulation from http://www.openjs.com/scripts/dom/class_manipulation.php
581 //
583 function hasClass(ele,cls) {
584 return document.getElementById(ele).className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
585 }
587 function addClass(ele,cls) {
588 if (!this.hasClass(ele,cls))
589 document.getElementById(ele).className += " " + cls;
590 }
592 function removeClass(ele,cls) {
593 if (hasClass(ele,cls)) {
594 var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
595 document.getElementById(ele).className = document.getElementById(ele).className.replace(reg,' ');
596 }
597 }
602 //http://stackoverflow.com/questions/4770025/how-to-disable-scrolling-temporarily
604 // left: 37, up: 38, right: 39, down: 40,
605 // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
606 var keys = [32, 33, 34, 35, 36, 37, 38, 39, 40];
608 function preventDefault(e) {
609 e = e || window.event;
610 if (e.preventDefault)
611 e.preventDefault();
612 e.returnValue = false;
613 }
615 function keydown(e) {
616 for (var i = keys.length; i--;) {
617 if (e.keyCode === keys[i]) {
618 preventDefault(e);
619 return;
620 }
621 }
622 }
624 function wheel(e) {
625 preventDefault(e);
626 }
628 function scrolling(command) {
629 if (command == 'disable') {
630 if (window.addEventListener) {
631 window.addEventListener('DOMMouseScroll', wheel, false); }
632 window.onmousewheel = document.onmousewheel = document.ontouchmove = wheel;
633 document.onkeydown = keydown;
634 } else {
635 if (window.removeEventListener) {
636 window.removeEventListener('DOMMouseScroll', wheel, false); }
637 window.onmousewheel = document.onmousewheel = document.onkeydown = document.ontouchmove = null;
638 }
639 }
644 //
645 // Show/hide popups
646 //
648 function popup(name, action) {
649 if (action == 'show') {
650 scrolling('disable');
651 document.getElementById('toolbar').style.zIndex = '0';
652 removeClass('shader', 'hidden');
653 removeClass(name, 'hidden');
654 } else {
655 addClass(name, 'hidden');
656 addClass('shader', 'hidden');
657 scrolling('enable');
658 document.getElementById('toolbar').style.zIndex = '10';
659 }
660 }