Add Browser.js.
authorChris Jaekl <cejaekl@yahoo.com>
Tue, 16 Jul 2019 04:02:34 +0000 (00:02 -0400)
committerChris Jaekl <cejaekl@yahoo.com>
Tue, 16 Jul 2019 04:02:34 +0000 (00:02 -0400)
js/src/Browser.js [new file with mode: 0644]

diff --git a/js/src/Browser.js b/js/src/Browser.js
new file mode 100644 (file)
index 0000000..243195d
--- /dev/null
@@ -0,0 +1,40 @@
+// =========
+// Browser
+
+// Encapsulates access to the document object, so that we can 
+// fall back to alternate behaviour during command-line unit testing
+
+var Browser = (function() {
+    var my = {};
+
+    function isDefined(obj) {
+        return !( typeof obj === 'undefined' || obj === null );
+    }
+
+    my.getElementById = function(id) {
+        if ( isDefined(document) ) {
+            var result = document.getElementById(id);
+            if ( isDefined(result) ) {
+                return result;
+            }
+            else {
+                console.log('ERROR!  Document element not found for ID:', id);
+                return undefined;
+            }
+        }
+        else {
+            return undefined; 
+        }
+    };
+
+    my.setOnMouseMove = function(handler) {
+        if ( typeof(document) === 'undefined' || document === null ) {
+            // no-op
+        }
+        else {
+            document.onmousemove = handler;
+        }
+    };
+
+    return my;
+})();