tazpanel view lib/tazpanel.js @ rev 463

Add prism.js syntax highlighter; now files can be edited "in place" without page reloading; allow ANY characters in the Wi-Fi password (bug 126); change web-app layout: main window isn't scrollable, with scrollable contents.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Apr 24 16:00:14 2015 +0300 (2015-04-24)
parents d0dbe11a2060
children 470ab4241de5
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').innerText;
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').innerText = '<';
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').innerText = '(' + 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').innerText = '>';
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.innerText = ash_history[cur_hist];
206 var hint = ''
207 if (cur_hist < max_hist) hint = '[' + cur_hist + '/' + (max_hist - 1) + '] ';
208 document.getElementById('num_hist').innerText = 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').innerText;
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; //.parentElement;
330 //console.log('Blurred %s', elem.tabIndex);
331 //closeItem(elem);
332 //menuIsClosed = true;
333 //console.log('Menu closed (blur)');
334 }
338 //
339 // AJAX code for dynamic requests
340 //
342 function ajax(cgiUrl, command, ajaxOut) {
343 // (1) create object for server request
344 var req = new XMLHttpRequest();
346 // (2) show request status
347 var statusElem = document.getElementById('ajaxStatus');
349 req.onreadystatechange = function() {
350 // onreadystatechange activates on server answer receiving
352 if (req.readyState == XMLHttpRequest.DONE) {
353 // if request done
354 statusElem.innerHTML = req.statusText // show status (Not Found, ОК..)
356 // if status 200 (ОК) - show answer to user
357 if (req.status == 200)
358 document.getElementById(ajaxOut).innerHTML = req.responseText;
359 // here we can add "else" with request errors processing
360 }
361 }
363 // (3) set request address
364 req.open('POST', cgiUrl, true);
366 // (4) request object is ready
367 req.send(command); // send request
369 // (5)
370 statusElem.innerHTML = '<img src="/styles/default/images/loader.gif" />'
371 }
375 //
376 // Load configuration for new and stored networks
377 //
379 function loadcfg(essid, bssid, keyType) {
380 // looking for stored network
381 for (i = 0; i < networks.length; i++) {
382 if (networks[i].ssid == essid) break;
383 if (typeof networks[i].bssid != 'undefined') {
384 if (networks[i].bssid == bssid) {
385 essid = networks[i].ssid;
386 break;
387 }
388 }
389 }
390 document.getElementById('essid').value = essid;
391 document.getElementById('keyType').value = keyType;
393 password = document.getElementById('password')
394 password.value = '';
395 if (typeof networks[i] != 'undefined') {
396 if (typeof networks[i].psk != 'undefined')
397 password.value = networks[i].psk;
398 else if (typeof networks[i].wep_key0 != 'undefined')
399 password.value = networks[i].wep_key0;
400 else if (typeof networks[i].password != 'undefined')
401 password.value = networks[i].password;
402 }
404 // Not used yet
405 document.getElementById('bssid').value = '';
407 wifiSettingsChange();
408 }
411 //
412 // Toggle all checkboxes on a page
413 //
415 function checkBoxes() {
416 var inputs = document.getElementsByTagName('input');
417 for (var i = 0; i < inputs.length; i++) {
418 if (inputs[i].type && inputs[i].type == 'checkbox') {
419 inputs[i].checked = !inputs[i].checked;
420 countSelPkgs(inputs[i]);
421 }
422 }
423 }
426 //
427 // Count selected packages on the packages list
428 //
430 function countSelPkgs(el) {
431 countSelectedSpan = document.getElementById('countSelected');
432 countSelected = countSelectedSpan.innerText;
433 if (countSelected == '') countSelected = 0;
435 element = (el.type == 'change' ? this : el);
437 if (element.checked)
438 countSelected++;
439 else
440 countSelected--;
442 countSelectedSpan.innerText = countSelected;
443 }
445 // Attach event handler
446 function setCountSelPkgs() {
447 // The change event does not bubble to the form container
448 pkglist = document.getElementById('pkglist');
449 if (pkglist) {
450 var checkboxes = pkglist.getElementsByTagName('input');
451 for (i = 0; i < checkboxes.length; i++) {
452 checkboxes[i].onchange = countSelPkgs;
453 }
454 }
455 }
458 //
459 // Improving packages by the community effort.
460 //
462 function improveAction() {
463 improveType = document.getElementById('improveType');
464 improveText = document.getElementById('improveText');
466 if (improveType.value == '')
467 improveText.value = '';
468 else
469 improveText.value = document.getElementById(improveType.value).innerText;
470 }
473 //
474 // Edit and save files "in place"
475 //
477 function editFile() {
478 document.getElementById('edit_button').style.display = 'none';
479 document.getElementById('save_button').style.display = '';
481 with(document.getElementById('fileContent')) {
482 contentEditable = true;
483 onkeydown = insertTab;
484 focus();
485 }
486 }
488 function saveFile(file, fileTitle) {
489 var newArea = document.createElement('TEXTAREA');
490 with(newArea) {
491 name = 'content';
492 textContent = document.getElementById('fileContent').textContent;
493 }
495 var newTitle = document.createElement('INPUT');
496 with(newTitle) {
497 name = 'title';
498 value = fileTitle;
499 }
501 var newForm = document.createElement('FORM');
502 with(newForm) {
503 appendChild(newArea);
504 appendChild(newTitle);
505 method = 'post';
506 action = "?file=" + file;
507 submit();
508 }
509 }
511 function insertTab(e) {
512 var evt = e ? e:event;
513 if (evt.keyCode == 9) {
514 evt.preventDefault();
515 }
516 }