diff --git a/exec/load/lightbar.js b/exec/load/lightbar.js index e8c10f6f81c1ff39a144692505b53270cec47ae3..f5d421c2ea33acd594bce4f020a087682a8820eb 100644 --- a/exec/load/lightbar.js +++ b/exec/load/lightbar.js @@ -51,6 +51,7 @@ function Lightbar(items) this.getval=Lightbar_getval; this.clear=Lightbar_clearitems; this.add=Lightbar_additem; + this.failsafe_getval=Lightbar_failsafe_getval; if(items==undefined) this.items=new Array(); else @@ -81,120 +82,85 @@ function Lightbar_clearitems() this.items=new Array(); } -function Lightbar_getval(current) +function Lightbar_failsafe_getval() { var retval; - if(current!=undefined) - this.current=current; - retval=lightbar_func(this.xpos, this.ypos, this.items, this.direction, this.fg - , this.bg, this.hfg, this.hbg, this.kfg, this.khfg, this.current - , this.align); - if(retval==null) { - for(i=0; i<this.items.length; i++) { - if(this.items[i] != undefined && this.items[i].text != undefined) { - writeln((i+1)+": "+this.items[i].text); - } - } - write("Choose an option: "); - i=console.getnum(i); - if(this.items[i]==undefined) - return(null); - if(this.items[i].retval==undefined) - return(undefined); - retval=this.items[i].retval; - } - for(i=0; i<this.items.length; i++) { - if(retval==this.items[i].retval) { - this.current=i; - break; + if(this.items[i] != undefined && this.items[i].text != undefined) { + writeln((i+1)+": "+this.items[i].text); } } + write("Choose an option: "); + i=console.getnum(i); + if(this.items[i]==undefined) + return(null); + if(this.items[i].retval==undefined) + return(undefined); + this.current=i; + retval=this.items[i].retval; + return(retval); } /* - * Super-Overlord Lightbar function... draws and returns selected value. - * Parameters: - * xpos: Horizontal position of lightbar menu (1-based) - * xpos: Vertical position of lightbar menu (1-based) - * items: an array of objects each having the following properties: - * text - The displayed text. A | prefixes a hotkey - * retval - The value to return if this is selected - * OPTIONAL Properties: - * width - The width of this item. If not specified, is the width of - * the text. Otherwise, the text is truncated or padded with - * spaces to fit the width. - * direction: 0 for vertical, 1 for horizontal. - * Horizontal menus always have one space of padding added between - * items. - * fg: Foreground colour of a non-current item - * bg: Background colour of a non-current item - * hfg: Foreground colour of a current item - * hbg: Background colour of a current item - * kfg: Hotkey forground colour for non-current item - * khfg: Hotkey foreground colour for current item - * current: Index of currently highlighted item (ToDo: This should be passed by reference (how?)!) - * align: If width is greater than the text length, a zero indicates the text - * should be left-aligned, a 1 indicates it should be right-aligned, and - * a 2 indicates it should be centered. - * Return Values: - * null for error, retval from the selected item for non-errors + * Super-Overlord Lightbar method... draws and returns selected value. */ -function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg, current, align) +function Lightbar_getval(current) { - var attr=bg<<4|fg; - var cattr=hbg<<4|hfg; - var kattr=bg<<4|kfg; - var kcattr=hbg<<4|khfg; + var attr=this.bg<<4|this.fg; + var cattr=this.hbg<<4|this.hfg; + var kattr=this.bg<<4|this.kfg; + var kcattr=this.hbg<<4|this.khfg; var ret=undefined; + if(current!=undefined) + this.current=current; if(!(user.settings & USER_ANSI)) { - alert("ANSI not supported for lightbar!"); - return(null); + return(this.failsafe_getval()); } if(!(user.settings & USER_COLOR)) { - alert("Colour not supported for lightbar!"); - return(null); + return(this.failsafe_getval()); } - if(direction < 0 || direction > 1) { + if(this.direction < 0 || this.direction > 1) { alert("Unknown lightbar direction!"); - return(null); + return(this.failsafe_getval()); } /* Check that a vertical lightbar fits on the screen */ - if(direction==0 && (ypos+items.length-1 > console.screen_rows)) { + if(this.direction==0 && (this.ypos+this.items.length-1 > console.screen_rows)) { alert("Screen too short for lightbar!"); - return(null); + return(this.failsafe_getval()); } /* Ensure current is valid */ - if(current<0 || current >= items.length) { + if(this.current<0 || this.current >= this.items.length) { alert("current parameter is out of range!"); - return(null); + this.current=0; + if(this.items.length<=0) + return(null); } /* Check that a horizontal lightbar fits on the screen */ - if(direction==1) { - var end=xpos; + if(this.direction==1) { + var end=this.xpos; var i; - for(i=0; i<items.length; i++) { - if(items[i].width==undefined) { - if(items[i]==undefined) { + for(i=0; i<this.items.length; i++) { + if(this.items[i].width==undefined) { + if(this.items[i]==undefined) { alert("Sparse items array!"); return(null); } - if(items[i].text==undefined) { + if(this.items[i].text==undefined) { alert("No text for item "+i+"!"); - return(null); + return(this.failsafe_getval()); } - var cleaned=items[i].text; + var cleaned=this.items[i].text; cleaned=cleaned.replace(/\|/g,''); end+=cleaned.length+1; } else { - end+=items[i].width + end+=this.items[i].width } } } @@ -205,35 +171,35 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg var j; /* Draw items */ - var curx=xpos; - var cury=ypos; - var cursx=xpos; - var cursy=ypos; - for(i=0; i<items.length; i++) { + var curx=this.xpos; + var cury=this.ypos; + var cursx=this.xpos; + var cursy=this.ypos; + for(i=0; i<this.items.length; i++) { var width; - var cleaned=items[i].text; + var cleaned=this.items[i].text; cleaned=cleaned.replace(/\|/g,''); width=cleaned.length; - if(items[i]==undefined) { + if(this.items[i]==undefined) { alert("Sparse items array!"); - return(null); + return(this.failsafe_getval()); } - if(items[i].text==undefined) { + if(this.items[i].text==undefined) { alert("No text for item "+i+"!"); - return(null); + return(this.failsafe_getval()); } - if(items[i].width!=undefined) - width=items[i].width; + if(this.items[i].width!=undefined) + width=this.items[i].width; console.gotoxy(curx, cury); - if(i==current) { + if(i==this.current) { cursx=curx; cursy=cury; } j=0; if(cleaned.length < width) { - if(align==1) { - if(current==i) + if(this.align==1) { + if(this.current==i) console.attributes=cattr; else console.attributes=attr; @@ -241,7 +207,7 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg console.write(' '); } if(align==2) { - if(current==i) + if(this.current==i) console.attributes=cattr; else console.attributes=attr; @@ -249,25 +215,25 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg console.write(' '); } } - for(; j<items[i].text.length; j++) { + for(; j<this.items[i].text.length; j++) { if(width > -1 && j > width) break; - if(items[i].text.substr(j,1)=='|') { - if(current==i) + if(this.items[i].text.substr(j,1)=='|') { + if(this.current==i) console.attributes=kcattr; else console.attributes=kattr; j++; } else { - if(current==i) + if(this.current==i) console.attributes=cattr; else console.attributes=attr; } - console.write(items[i].text.substr(j,1)); + console.write(this.items[i].text.substr(j,1)); } - if(current==i) + if(this.current==i) console.attributes=cattr; else console.attributes=attr; @@ -275,7 +241,7 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg console.write(" "); j++; } - if(direction==0) + if(this.direction==0) cury++; else { console.attributes=attr; @@ -291,53 +257,53 @@ function lightbar_func(xpos, ypos, items, direction, fg, bg, hfg, hbg, kfg, khfg var key=console.getkey(K_UPPER); switch(key) { case KEY_UP: - if(direction==0) { - if(current==0) - current=items.length; - current--; + if(this.direction==0) { + if(this.current==0) + this.current=this.items.length; + this.current--; } break; case KEY_DOWN: - if(direction==0) { - current++; - if(current==items.length) - current=0; + if(this.direction==0) { + this.current++; + if(this.current==this.items.length) + this.current=0; } break; case KEY_LEFT: - if(direction==1) { - if(current==0) - current=items.length; - current--; + if(this.direction==1) { + if(this.current==0) + this.current=this.items.length; + this.current--; } break; case KEY_RIGHT: - if(direction==1) { - current++; - if(current==items.length) - current=0; + if(this.direction==1) { + this.current++; + if(this.current==this.items.length) + this.current=0; } break; case KEY_HOME: - current=0; + this.current=0; break; case KEY_END: - current=items.length-1; + this.current=this.items.length-1; break; case '\r': case '\n': - if(items[current].retval==undefined) + if(this.items[this.current].retval==undefined) return(undefined); - return(items[current].retval); + return(this.items[this.current].retval); break; default: - for(i=0; i<items.length; i++) { - if(items[i].text.indexOf('|'+key)!=-1) { - current=i; - if(items[current].retval==undefined) + for(i=0; i<this.items.length; i++) { + if(this.items[i].text.indexOf('|'+key)!=-1) { + this.current=i; + if(this.items[this.current].retval==undefined) return(undefined); /* Let it go through once more to highlight */ - ret=items[current].retval; + ret=this.items[this.current].retval; break; } }