1
// XMLTerm Page Commands
3
// CONVENTION: All pre-defined XMLTerm Javascript functions and global
4
// variables should begin with an upper-case letter.
5
// This would allow them to be easily distinguished from
6
// user defined functions, which should begin with a lower case
10
///var gStyleRuleNames;
12
WRITE_LOG = function (str) {};
13
DEBUG_LOG = function (str) {};
16
var userAgent = navigator.userAgent;
18
if (userAgent && (userAgent.search(/Mozilla\/4\./i) == -1)) {
30
var Tips = new Array(); // Usage tip strings
31
var TipNames = new Array(); // Usage tip names
32
var TipCount = 0; // No. of tips
33
var SelectedTip = 0; // Selected random tip
35
// Set prompt using form entry
36
function DefineTip(tip, name) {
38
TipNames[TipCount] = name;
43
DefineTip('Click the new tip link to the left to get a new tip!',
46
DefineTip('User level setting (at the top) controls amount of help information',
49
DefineTip('Beginner level setting displays keyboard shortcuts at the top of the page',
52
DefineTip('Icons setting controls whether directory listings use icons',
55
DefineTip('Windows setting controls whether double-clicking creates new windows',
58
DefineTip('Single click an explicit (underlined) hyperlink; double click implicit (usually blue) hyperlinks',
61
DefineTip('Clicking on command prompt expands/collapses command output display.',
64
DefineTip('Press F1 (or control-Home) key to collapse output of all commands.',
67
DefineTip('"js:SetPrompt(HTMLstring);" sets prompt to any HTML string.',
70
DefineTip('Beginners may click the SetPrompt button for a cool Mozilla prompt from dmoz.org!',
73
DefineTip('Double-clicking on a previous command line re-executes the command.',
76
DefineTip('Type "js:func(arg);" to execute inline Javascript function func.',
79
DefineTip('Inline Javascript ("js:...") can be used to produce HTML output.',
82
DefineTip('XMLterm supports full screen commands like "less", "vi", and "emacs -nw"".',
85
DefineTip('"xls" produces a clickable listing of directory contents.',
88
DefineTip('"xls -t" prevents display of icons even if Icons setting is "on".',
91
DefineTip('"xcat text-file" displays a text file with clickable URLs.',
94
DefineTip('"xcat image.gif" displays an image file inline!',
97
DefineTip('"xcat http://mozilla.org" displays a web page inline using IFRAMEs.',
100
DefineTip('"xcat -h 1000 http://mozilla.org" displays using an IFRAME 1000 pixels high.',
103
DefineTip('"xcat doc.html" display an HTML document inline using IFRAMEs.',
106
// Display random usage tip
107
// (should cease to operate after a few cycles;
108
// need to use Prefs to keep track of that)
110
var ranval = Math.random();
111
SelectedTip = Math.floor(ranval * TipCount);
112
if (SelectedTip >= TipCount) SelectedTip = 0;
114
DEBUG_LOG("xmlterm: NewTip "+SelectedTip+","+ranval+"\n");
116
var tipdata = document.getElementById('tipdata');
117
tipdata.firstChild.data = Tips[SelectedTip];
125
function ExplainTip(tipName) {
126
DEBUG_LOG("xmlterm: ExplainTip("+tipName+")\n");
129
var tipdata = document.getElementById('tipdata');
130
tipdata.firstChild.data = "";
132
tipName = TipNames[SelectedTip];
135
ShowHelp('xmltermTips.html#'+tipName,0,120);
140
// F1 key - Hide all output
141
function F1Key(isShift, isControl) {
142
return DisplayAllOutput(false);
145
// F2 key - Show all output
146
function F2Key(isShift, isControl) {
147
return DisplayAllOutput(true);
151
function F7Key(isShift, isControl) {
156
function F8Key(isShift, isControl) {
161
function F9Key(isShift, isControl) {
162
return NewXMLTerm('');
166
function ScrollHomeKey(isShift, isControl) {
167
DEBUG_LOG("ScrollHomeKey("+isShift+","+isControl+")\n");
170
return F1Key(isShift, 0);
172
ScrollWin(isShift,isControl).scroll(0,0);
178
function ScrollEndKey(isShift, isControl) {
179
DEBUG_LOG("ScrollEndKey("+isShift+","+isControl+")\n");
182
return F2Key(isShift, 0);
184
ScrollWin(isShift,isControl).scroll(0,99999);
190
function ScrollPageUpKey(isShift, isControl) {
191
DEBUG_LOG("ScrollPageUpKey("+isShift+","+isControl+")\n");
193
ScrollWin(isShift,isControl).scrollBy(0,-120);
197
// Scroll PageDown key
198
function ScrollPageDownKey(isShift, isControl) {
199
DEBUG_LOG("ScrollPageDownKey("+isShift+","+isControl+")\n");
201
ScrollWin(isShift,isControl).scrollBy(0,120);
206
function ScrollWin(isShift, isControl) {
207
if (isShift && (window.frames.length > 0)) {
208
return window.frames[window.frames.length-1];
214
// Set history buffer size
215
function SetHistory(value) {
216
DEBUG_LOG("SetHistory("+value+")\n");
217
window.xmlterm.setHistory(value, document.cookie);
222
function SetPrompt(value) {
223
DEBUG_LOG("SetPrompt("+value+")\n");
224
window.xmlterm.setPrompt(value, document.cookie);
228
// Create new XMLTerm window
229
function NewXMLTerm(firstcommand) {
230
newwin = window.openDialog( "chrome://xmlterm/content/xmlterm.xul",
231
"xmlterm", "chrome,dialog=no,resizable",
233
WRITE_LOG("NewXMLTerm: "+newwin+"\n")
237
// Handle resize events
238
function Resize(event) {
239
DEBUG_LOG("Resize()\n");
240
window.xmlterm.resize();
244
// Form Focus (workaround for form input being transmitted to xmlterm)
245
function FormFocus() {
246
DEBUG_LOG("FormFocus()\n");
247
window.xmlterm.ignoreKeyPress(true, document.cookie);
251
// Form Blur (workaround for form input being transmitted to xmlterm)
252
function FormBlur() {
253
DEBUG_LOG("FormBlur()\n");
254
window.xmlterm.ignoreKeyPress(false, document.cookie);
258
// Set user level, icons mode etc.
259
function UpdateSettings(selectName) {
261
DEBUG_LOG("UpdateSettings: "+selectName+"\n");
263
var selectedIndex = document.xmltform1[selectName].selectedIndex;
264
var selectedOption = document.xmltform1[selectName].options[selectedIndex].value;
266
DEBUG_LOG("UpdateSettings: selectedOption=="+selectedOption+"\n");
272
if (selectedOption == "advanced") {
273
AlterStyle("DIV.beginner", "display", "none");
274
AlterStyle("DIV.intermediate", "display", "none");
276
} else if (selectedOption == "intermediate") {
277
AlterStyle("DIV.intermediate", "display", "block");
278
AlterStyle("DIV.beginner", "display", "none");
281
AlterStyle("DIV.beginner", "display", "block");
282
AlterStyle("DIV.intermediate", "display", "block");
287
// Change icon display style in the style sheet
289
if (selectedOption == "on") {
290
AlterStyle("SPAN.noicons", "display", "none");
291
AlterStyle("SPAN.icons", "display", "inline");
292
AlterStyle("IMG.icons", "display", "inline");
293
AlterStyle("TR.icons", "display", "table-row");
296
AlterStyle("SPAN.noicons", "display", "inline");
297
AlterStyle("SPAN.icons", "display", "none");
298
AlterStyle("IMG.icons", "display", "none");
299
AlterStyle("TR.icons", "display", "none");
307
DEBUG_LOG("UpdateSettings: Unknown selectName "+selectName+"\n");
311
window.xmltform1Index[selectName] = selectedIndex;
312
window.xmltform1Option[selectName] = selectedOption;
319
// Alter style in stylesheet of specified document doc, or current document
320
// if doc is omitted.
321
function AlterStyle(ruleName, propertyName, propertyValue, doc) {
323
DEBUG_LOG("AlterStyle("+ruleName+"{"+propertyName+":"+propertyValue+"})\n");
325
if (!doc) doc = window.document;
327
var sheet = doc.styleSheets[0];
329
for (r = 0; r < sheet.cssRules.length; r++) {
330
DEBUG_LOG("selectorText["+r+"]="+sheet.cssRules[r].selectorText+"\n");
332
if (sheet.cssRules[r].selectorText.toLowerCase() == ruleName.toLowerCase()) {
334
// Ugly workaround for accessing rules until bug 53448 is fixed
335
// (****CSS DOM IS BROKEN****; selectorText seems to be null for all rules)
336
///if (gStyleRuleNames[r] == ruleName) {
338
DEBUG_LOG("cssText["+r+"]="+sheet.cssRules[r].cssText+"\n");
339
var style = sheet.cssRules[r].style;
340
DEBUG_LOG("style="+style.getPropertyValue(propertyName)+"\n");
342
style.setProperty(propertyName,propertyValue,"");
348
// Handle default meta command
349
function MetaDefault(arg1) {
350
return Load("http://"+arg1);
353
// Load URL in XMLTermBrowser window
355
DEBUG_LOG("Load:url="+url+"\n");
361
// Control display of all output elements
362
function DisplayAllOutput(flag) {
363
var outputElements = document.getElementsByName("output");
364
for (i=0; i<outputElements.length-1; i++) {
365
var outputElement = outputElements[i];
366
///outputElement.style.display = (flag) ? "block" : "none";
369
outputElement.removeAttribute('xmlt-block-collapsed');
371
outputElement.setAttribute('xmlt-block-collapsed', 'true');
374
var promptElements = document.getElementsByName("prompt");
375
for (i=0; i<promptElements.length-1; i++) {
376
promptElement = promptElements[i];
377
///promptElement.style.setProperty("text-decoration",
378
/// (flag) ? "none" : "underline", "");
381
promptElement.removeAttribute('xmlt-underline');
383
promptElement.setAttribute('xmlt-underline', 'true');
388
// ScrollEndKey(0,0);
396
// Centralized event handler
401
// textlink - hyperlink
402
// prompt - command prompt
403
// command - command line
405
// (Following are inline or in new window depending upon window.xmltform1Option.windowsMode)
406
// cdxls - change directory and list contents (doubleclick)
407
// xcat - display file (doubleclick)
408
// exec - execute file (doubleclick)
410
// send - transmit arg to LineTerm
411
// sendln - transmit arg+newline to LineTerm
412
// createln - transmit arg+newline as first command to new XMLTerm
414
// entryNumber: >=0 means process only if current entry
415
// <0 means process anytime
417
// arg1: relative pathname of file/directory
418
// arg2: absolute pathname prefix (for use outside current entry;
419
// use relative pathname in current working directory)
421
function HandleEvent(eventObj, eventType, targetType, entryNumber,
423
DEBUG_LOG("HandleEvent("+eventObj+","+eventType+","+targetType+","+entryNumber+","+arg1+","+arg2+")\n");
425
// Entry independent targets
426
if (targetType === "textlink") {
427
// Single click opens hyperlink
429
DEBUG_LOG("textlink = "+arg1+"\n");
432
} else if (targetType === "prompt") {
433
// Single click on prompt expands/collapses command output
434
var outputElement = document.getElementById("output"+entryNumber);
435
var helpElement = document.getElementById("help"+entryNumber);
436
var promptElement = document.getElementById("prompt"+entryNumber);
437
DEBUG_LOG(promptElement.style.getPropertyValue("text-decoration"));
439
var collapsed = outputElement.getAttribute('xmlt-block-collapsed');
442
///if (outputElement.style.display == "none") {
443
///outputElement.style.display = "block";
444
///promptElement.style.setProperty("text-decoration","none","");
446
outputElement.removeAttribute('xmlt-block-collapsed');
447
promptElement.removeAttribute('xmlt-underline');
450
///outputElement.style.display = "none";
451
///promptElement.style.setProperty("text-decoration","underline","");
453
outputElement.setAttribute('xmlt-block-collapsed', 'true');
454
promptElement.setAttribute('xmlt-underline', 'true');
456
ShowHelp("",entryNumber);
460
} else if (eventType === "click") {
462
DEBUG_LOG("clickCount="+eventObj.detail+"\n");
464
var shiftClick = eventObj.shiftKey;
465
var dblClick = (eventObj.detail == 2);
467
// Execute shell commands only on double-click for safety
468
// Use single click for "selection" and prompt expansion only
471
var currentEntryNumber = window.xmlterm.currentEntryNumber;
473
var currentCmdElement = document.getElementById("command"+currentEntryNumber);
474
var currentCommandEmpty = true;
475
if (currentCmdElement && currentCmdElement.hasChildNodes()) {
477
if (currentCmdElement.firstChild.nodeType == Node.TEXT_NODE) {
478
DEBUG_LOG("textLength = "+currentCmdElement.firstChild.data.length+"\n");
479
currentCommandEmpty = (currentCmdElement.firstChild.data.length == 0);
482
currentCommandEmpty = false;
485
DEBUG_LOG("empty = "+currentCommandEmpty+"\n");
487
if (targetType === "command") {
490
var commandElement = document.getElementById(targetType+entryNumber);
491
var command = commandElement.firstChild.data;
492
if (currentCommandEmpty) {
493
window.xmlterm.sendText("\025"+command+"\n", document.cookie);
495
window.xmlterm.sendText(command, document.cookie);
499
// Targets which may be qualified only for current entry
501
if ((entryNumber >= 0) &&
502
(window.xmlterm.currentEntryNumber != entryNumber)) {
503
DEBUG_LOG("NOT CURRENT COMMAND\n");
509
if ( (targetType === "cdxls") ||
510
(targetType === "xcat") ||
511
(targetType === "exec") ) {
518
var isCurrentCommand = (Math.abs(entryNumber)+1 ==
519
window.xmlterm.currentEntryNumber);
521
if ( (arg2 != null) &&
522
(!isCurrentCommand || (window.xmltform1Option.windowsMode === "on")) ) {
524
filename = arg2+arg1;
528
if (targetType === "exec")
529
filename = "./"+filename;
533
if (targetType === "cdxls") {
534
// Change directory and list contents
537
} else if (targetType === "xcat") {
541
} else if (targetType === "exec") {
547
if (shiftClick || (window.xmltform1Option.windowsMode === "on")) {
549
sendStr = prefix + filename + suffix;
551
} else if (currentCommandEmpty) {
553
sendStr = prefix + filename + suffix;
557
sendStr = filename + " ";
567
if (action === "send") {
568
DEBUG_LOG("send = "+sendStr+"\n");
569
window.xmlterm.sendText(sendStr, document.cookie);
571
} else if (action === "sendln") {
572
DEBUG_LOG("sendln = "+sendStr+"\n\n");
573
window.xmlterm.sendText("\025"+sendStr+"\n", document.cookie);
575
} else if (action === "createln") {
576
DEBUG_LOG("createln = "+sendStr+"\n\n");
577
newwin = NewXMLTerm(sendStr+"\n");
587
// Set history buffer count using form entry
588
function SetHistoryValue() {
589
var field = document.getElementById('inputvalue');
590
return SetHistory(field.value);
593
// Set prompt using form entry
594
function SetPromptValue() {
595
var field = document.getElementById('inputvalue');
596
return SetPrompt(field.value);
600
if (!window.webcastIntervalId)
603
DEBUG_LOG("XMLTermCommands.js: Webcast: "+window.webcastFile+"\n");
607
if (window.xmltform1Option.showIcons == "on") {
608
style += "SPAN.noicons {display: none}\n";
609
style += "SPAN.icons {display: inline}\n";
610
style += "IMG.icons {display: inline}\n";
611
style += "TR.icons {display: table-row}\n";
614
var exported = window.xmlterm.exportHTML(window.webcastFile, 0644, style,
615
0, false, document.cookie);
616
DEBUG_LOG("XMLTermCommands.js: exported="+exported+"\n");
619
function InitiateWebcast() {
620
var field = document.getElementById('inputvalue');
622
window.webcastFile = field.value ? field.value : "";
623
window.webcastFile = "/var/www/html/users/xmlt.html";
625
WRITE_LOG("XMLTermCommands.js: InitiateWebcast: "+window.webcastFile+"\n");
628
window.webcastIntervalId = window.setInterval(Webcast, 2000);
631
function TerminateWebcast() {
632
window.webcastFile = null;
634
if (window.webcastIntervalId) {
635
window.clearInterval(window.webcastIntervalId);
636
window.webcastIntervalId = null;
640
function ToggleWebcast() {
641
if (window.webcastIntervalId)
647
// Insert help element displaying URL in an IFRAME before output element
648
// of entryNumber, or before the SESSION element if entryNumber is zero.
649
// Height is the height of the IFRAME in pixels.
650
// If URL is the null string, simply delete the help element
652
function ShowHelp(url, entryNumber, height) {
654
if (!height) height = 120;
656
DEBUG_LOG("xmlterm: ShowHelp("+url+","+entryNumber+","+height+")\n");
659
beforeID = "output"+entryNumber;
660
helpID = "help"+entryNumber;
662
beforeID = "session";
666
var beforeElement = document.getElementById(beforeID);
668
if (!beforeElement) {
669
DEBUG_LOG("InsertIFrame: beforeElement ID="+beforeID+"not found\n");
673
var parentNode = beforeElement.parentNode;
675
var helpElement = document.getElementById(helpID);
677
// Delete help element
678
parentNode.removeChild(helpElement);
680
// *NOTE* Need to flush display here to avoid black flash?
683
if (url.length > 0) {
684
// Create new help element
685
helpElement = document.createElement("div");
686
helpElement.setAttribute('id', helpID);
687
helpElement.setAttribute('class', 'help');
689
var closeElement = document.createElement("span");
690
closeElement.setAttribute('class', 'helplink');
691
closeElement.appendChild(document.createTextNode("Close help frame"));
692
closeElement.appendChild(document.createElement("p"));
694
var iframe = document.createElement("iframe");
695
iframe.setAttribute('id', helpID+'frame');
696
iframe.setAttribute('class', 'helpframe');
697
iframe.setAttribute('width', '100%');
698
iframe.setAttribute('height', height);
699
iframe.setAttribute('frameborder', '0');
700
iframe.setAttribute('src', url);
702
helpElement.appendChild(iframe);
703
helpElement.appendChild(closeElement);
705
DEBUG_LOG(helpElement);
707
// Insert help element
708
parentNode.insertBefore(helpElement, beforeElement);
710
// NOTE: Need to do this *after* node is inserted into document
711
closeElement.setAttribute('onClick', 'return ShowHelp("",'+entryNumber
719
function AboutXMLTerm() {
720
DEBUG_LOG("xmlterm: AboutXMLTerm\n");
722
var tipdata = document.getElementById('tipdata');
723
tipdata.firstChild.data = "";
725
ShowHelp('xmltermAbout.html',0,120);
730
function Redirect() {
731
window.location = window.redirectURL;
735
function GetQueryParam(paramName) {
736
var url = document.location.href;
738
var paramExp = new RegExp("[?&]"+paramName+"=([^&]*)");
740
var matches = url.match(paramExp);
743
if (matches && (matches.length > 1)) {
744
paramVal = matches[1];
750
// onLoad event handler
751
function LoadHandler() {
752
WRITE_LOG("xmlterm: LoadHandler ... "+window.xmlterm+"\n");
754
// Ugly workaround for accessing rules in stylesheet until bug 53448 is fixed
755
///var sheet = document.styleSheets[0];
756
//WRITE_LOG("sheet.cssRules.length="+sheet.cssRules.length+"\n");
758
///var styleElement = (document.getElementsByTagName("style"))[0];
759
///var styleText = styleElement.firstChild.data;
761
///gStyleRuleNames = styleText.match(/\b[\w-.]+(?=\s*\{)/g);
762
//WRITE_LOG("gStyleRuleNames.length="+gStyleRuleNames.length+"\n");
767
window.xmltform1Index = new Object();
768
window.xmltform1Option = new Object();
770
window.xmltform1Index.userLevel = 2;
771
window.xmltform1Option.userLevel = "advanced";
773
window.xmltform1Index.showIcons = 0;
774
window.xmltform1Option.showIcons = "off";
776
window.xmltform1Index.windowsMode = 0;
777
window.xmltform1Option.windowsMode = "off";
779
if (!window.xmlterm && exportCount) {
780
var redirectURL = document.location.href;
781
var urlLen = redirectURL.indexOf("?");
783
if (urlLen > 0) redirectURL = redirectURL.substr(0,urlLen);
785
DEBUG_LOG("exportCount="+exportCount+"\n");
787
var minrefresh = GetQueryParam("minrefresh");
788
var maxrefresh = GetQueryParam("maxrefresh");
789
var refresh = GetQueryParam("refresh");
790
var id = GetQueryParam("id");
793
if (!maxrefresh) maxrefresh = 10*minrefresh;
794
if (!refresh) refresh = minrefresh;
797
if (exportCount > id*1)
798
refresh = minrefresh;
802
if (refresh > maxrefresh) refresh=maxrefresh;
804
var refreshTime = refresh*1000;
806
redirectURL += "?minrefresh="+minrefresh + "&maxrefresh="+maxrefresh +"&refresh="+refresh + "&id="+exportCount;
808
DEBUG_LOG("redirectURL="+redirectURL+"\n");
810
window.redirectURL = redirectURL;
811
window.timeoutId = window.setTimeout(Redirect, refreshTime);
814
window.scrollTo(0,4000); // Scroll to bottom of page
819
// The following code fragment is skipped because the chrome takes care of
820
// XMLterm initialization. This should eventually be deleted
821
if (window.xmlterm) {
822
// XMLTerm already initialized
826
DEBUG_LOG("LoadHandler: WINDOW.ARGUMENTS="+window.arguments+"\n");
828
WRITE_LOG("Trying to make an XMLTerm Shell through the component manager...\n");
830
var xmltshell = Components.classes["@mozilla.org/xmlterm/xmltermshell;1"].createInstance();
832
xmltshell = xmltshell.QueryInterface(Components.interfaces.mozIXMLTermShell);
833
DEBUG_LOG("Interface xmltshell2 = " + xmltshell + "\n");
836
WRITE_LOG("Failed to create XMLTerm shell\n");
841
// Store the XMLTerm shell in the window
842
window.xmlterm = xmltshell;
844
// Content window same as current window
845
var contentWindow = window;
847
// Initialize XMLTerm shell in content window with argvals
848
window.xmlterm.init(contentWindow, "", "");
850
DEBUG_LOG("LoadHandler:"+document.cookie+"\n");
852
DEBUG_LOG("contentWindow="+contentWindow+"\n");
853
DEBUG_LOG("document="+document+"\n");
854
DEBUG_LOG("documentElement="+document.documentElement+"\n");
856
// Handle resize events
857
//contentWindow.addEventListener("onresize", Resize);
858
contentWindow.onresize = Resize;
860
// Set focus to appropriate frame
861
contentWindow.focus();
863
//contentWindow.xmlterm = xmlterm;
865
DEBUG_LOG(contentWindow.xmlterm);
867
// The following code is for testing IFRAMEs only
868
DEBUG_LOG("[Main] "+window+"\n");
869
DEBUG_LOG(window.screenX+", "+window.screenY+"\n");
870
DEBUG_LOG(window.scrollX+", "+window.scrollY+"\n");
871
DEBUG_LOG(window.pageXOffset+", "+window.pageYOffset+"\n");
873
DEBUG_LOG("IFRAME checks\n");
874
var iframe = document.getElementById('iframe1');
876
DEBUG_LOG("iframe="+iframe+"\n");
878
frames=document.frames;
879
DEBUG_LOG("frames="+frames+"\n");
880
DEBUG_LOG("frames.length="+frames.length+"\n");
882
framewin = frames[0];
884
DEBUG_LOG("framewin="+framewin+"\n");
885
DEBUG_LOG("framewin.document="+framewin.document+"\n");
887
DEBUG_LOG(framewin.screenX+", "+framewin.screenY+"\n");
888
DEBUG_LOG(framewin.scrollX+", "+framewin.scrollY+"\n");
889
DEBUG_LOG(framewin.pageXOffset+", "+framewin.pageYOffset+"\n");
891
var body = framewin.document.getElementsByTagName("BODY")[0];
892
DEBUG_LOG("body="+body+"\n");
894
var height= body.scrollHeight;
895
DEBUG_LOG("height="+height+"\n");
897
// iframe.height = 800;
898
// iframe.width = 700;
900
// framewin.sizeToContent();
902
framewin.xmltshell = xmltshell;
903
DEBUG_LOG(framewin.xmltshell+"\n");
905
DEBUG_LOG("xmlterm: LoadHandler completed\n");