1
0
mirror of https://github.com/pkimpel/retro-220.git synced 2026-01-13 15:18:24 +00:00
pkimpel.retro-220/webUI/B220Util.js
Paul Kimpel df29455c91 Commit version 0.00d:
1. Implement console teletype printer and paper tape punch devices.
2. Correct timing mechanism and operation complete processing for asynchronous (I/O) processor mode.
3. Delete extraneous alarm toggles from B220Processor.
4. Correct Reset And Transfer switch so it will work when the Processor is running.
5. Implement Hello World default program for SPO at address 0020.
6. Change keyboard keystroke assignments for B220ConsoleKeyboard.
7. Implement Reset-to-Defaults feature for B220SystemConfig.
8. Implement DOM Element.classList for B220Util class attribute manipulation.
9. Minor typo correction to BALGOL-Generator.bacg.
2017-04-29 13:41:02 -07:00

163 lines
5.5 KiB
JavaScript

/***********************************************************************
* retro-220/webUI B220Util.js
************************************************************************
* Copyright (c) 2017, Paul Kimpel.
* Licensed under the MIT License, see
* http://www.opensource.org/licenses/mit-license.php
************************************************************************
* Burroughs 220 Emulator common Javascript utilities module.
************************************************************************
* 2017-01-01 P.Kimpel
* Original version, from retro-205 D205Util.js
***********************************************************************/
"use strict";
/**************************************/
function B220Util() {
/* Constructor for the B220Util object */
// Nothing to construct at present...
}
/**************************************/
B220Util.xlateASCIIToAlgolRex = // For translation of 220-ASCII to Algol-ASCII glyphs
/[^\r\n\xA0 $()*+,-./0-9=@A-Za-z]/g;
B220Util.xlateASCIIToAlgolGlyph = {
"#": "=",
"%": "(",
"&": "+",
"<": ")",
"\u00A4": ")"}; // the lozenge (¤)
B220Util.xlateAlgolToASCIIRex = // For translation of Algol-ASCII glyphs to 220-ASCII
/[^\r\n\xA0 #$%&*,-./0-9<@A-Za-z\xA4]/g;
B220Util.xlateAlgolToASCIIGlyph = {
"=": "#",
"(": "%",
"+": "&",
")": "\u00A4"}; // the lozenge (¤)
/**************************************/
B220Util.$$ = function $$(e) {
return document.getElementById(e);
};
/**************************************/
B220Util.hasClass = function hasClass(e, name) {
/* returns true if element "e" has class "name" in its class list */
return e.classList.contains(name);
};
/**************************************/
B220Util.addClass = function addClass(e, name) {
/* Adds a class "name" to the element "e"s class list */
e.classList.add(name);
};
/**************************************/
B220Util.removeClass = function removeClass(e, name) {
/* Removes the class "name" from the element "e"s class list */
e.classList.remove(name);
};
/**************************************/
B220Util.bindMethod = function bindMethod(context, f) {
/* Returns a new function that binds the function "f" to the object "context" */
return function bindMethodAnon() {return f.apply(context, arguments)};
};
/**************************************/
B220Util.deepCopy = function deepCopy(source, dest) {
/* Performs a deep copy of the object "source" into the object "dest".
If "dest" is null or undefined, simply returns a deep copy of "source".
Note that this routine clones the primitive Javascript types, basic
objects (hash tables), Arrays, Dates, RegExps, and Functions. Other
types may be supported by extending the switch statement. Also note
this is a static function.
Adapted (with thanks) from the "extend" routine by poster Kamarey on 2011-03-26 at
http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object
*/
var constr;
var copy;
var name;
if (source === null) {
return source;
} else if (!(source instanceof Object)) {
return source;
} else {
constr = source.constructor;
if (constr !== Object && constr !== Array) {
return source;
} else {
switch (constr) {
case String:
case Number:
case Boolean:
case Date:
case Function:
case RegExp:
copy = new constr(source);
break;
default:
copy = dest || new constr();
break;
}
for (name in source) {
copy[name] = deepCopy(source[name], null);
}
return copy;
}
}
};
/**************************************/
B220Util.xlateToAlgolChar = function xlateToAlgolChar(c) {
/* Translates one BIC-as-ASCII Algol glyph character to Unicode */
return B220Util.xlateASCIIToAlgolGlyph[c] || "?";
};
/**************************************/
B220Util.xlateASCIIToAlgol = function xlateASCIIToAlgol(text) {
/* Translates the BIC-as-ASCII characters in "text" to equivalent Unicode glyphs */
return text.replace(B220Util.xlateASCIIToAlgolRex, B220Util.xlateToAlgolChar);
};
/**************************************/
B220Util.xlateToASCIIChar = function xlateToASCIIChar(c) {
/* Translates one Unicode Algol glyph to its BIC-as-ASCII equivalent */
return B220Util.xlateAlgolToASCIIGlyph[c] || "?";
};
/**************************************/
B220Util.xlateAlgolToASCII = function xlateAlgolToASCII(text) {
/* Translates the Unicode characters in "text" equivalent BIC-as-ASCII glyphs */
return text.replace(B220Util.xlateAlgolToASCIIRex, B220Util.xlateToASCIIChar);
};
/**************************************/
B220Util.xlateDOMTreeText = function xlateDOMTreeText(n, xlate) {
/* If Node "n" is a text node, translates its value using the "xlate"
function. For all other Node types, translates all subordinate text nodes */
var kid;
if (n.nodeType == Node.TEXT_NODE) {
n.nodeValue = xlate(n.nodeValue);
} else {
kid = n.firstChild;
while (kid) {
xlateDOMTreeText(kid, xlate);
kid = kid.nextSibling;
}
}
};