From 4f4b1a97d3a2c6962fb9c1c230ab054deed82926 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= <aless@guru.si>
Date: Wed, 2 Sep 2015 22:02:29 +0200
Subject: Bugfixed CodeQ Console, loaded it from prolog.html, added CSS and
 cursor blinking.

---
 js/codeq/console.js | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

(limited to 'js/codeq')

diff --git a/js/codeq/console.js b/js/codeq/console.js
index bc7f3c6..023fcc1 100644
--- a/js/codeq/console.js
+++ b/js/codeq/console.js
@@ -161,11 +161,11 @@
 
     codeq.makeConsole = function (jqElt, options) {
         var lines = [], // console content
-            maxLines = options.maxLines || 10000, // how many lines to display at most
+            maxLines = (options && options.maxLines) || 10000, // how many lines to display at most
             currentRow = 0, // cursor position
             currentCol = 0,
-            jqContent = $('<div></div>'), // the rendering of the console
-            jqInput = $('<textarea style="left: 0; bottom: 0; width: 2px; position: absolute;"></textarea>'), // for receiving keypresses, clipboard content
+            jqContent = $('<div class="cq-con"></div>'), // the rendering of the console
+            jqInput = $('<textarea style="left: -9999px; bottom: 0; width: 2px; position: absolute;"></textarea>'), // for receiving keypresses, clipboard content
             inputDisabled = false, // whether to ignore input
             lineBuffered = true, // whether we are line-buffered or not buffered
 
@@ -217,7 +217,7 @@
                         jqLine.append(jq1);
                         jqLine.append(jqCursor);
                         jqLine.append(jq2);
-                        jq1.text(content.substr(0, currentCol - 1));
+                        jq1.text(content.substr(0, currentCol));
                         jqCursor.text(content.charAt(currentCol));
                         jq2.text(content.substr(currentCol + 1));
                     }
@@ -516,6 +516,10 @@
                 'destroy': function () {
                     var n = lines.length,
                         i, lineDescriptor;
+                    if (blinkTimer !== null) {
+                        clearInterval(blinkTimer);
+                        blinkTimer = null;
+                    }
                     // detach all jQuery objects from internal structures
                     for (i = 0; i < n; i++) {
                         lineDescriptor = lines[i];
@@ -601,6 +605,8 @@
 
             clipboardPasteInProgress = false, // whether the previous keydown was a CTRL+V or shift+insert
             aKeyIsDown = false, // whether a key is currently pressed: if it's not and there is input, then we got a paste via mouse
+            cursorBlinkRate = (options && options.cursorBlinkRate) || 750, // default: 750 ms
+            blinkTimer = null,
             i, j, a, b, c;
 
         // copy default key handlers
@@ -668,23 +674,37 @@
             // the element supports input events, use them in preference to keypress events
             jqInput.on('input', function (evt) {
                 // this event cannot be prevented/cancelled in the DOM scheme of things, but we can simulate prevention, because we have a "detached" display
-                handleInput(evt.input);
+                var t = jqInput.val();
+                jqInput.val('');
+                handleInput(t);
             });
         }
         else {
             // the element does not support the "modern" input events, so it ought to support the old keypress events
             jqInput.on('keypress', function (evt) {
                 // this event cannot be prevented/cancelled in the DOM scheme of things, but we can simulate prevention, because we have a "detached" display
-                var keyChar = false;
-                if ('char' in evt) keyChar = evt.char;
-                else if ('charCode' in evt) keyChar = String.fromCharCode(evt.charCode);
-                else return; // ignore input
-                handleInput(keyChar);
+                var keyChar;
+                try {
+                    if ('char' in evt) keyChar = evt.char;
+                    else if ('charCode' in evt) keyChar = String.fromCharCode(evt.charCode);
+                    else return; // ignore input
+                    handleInput(keyChar);
+                }
+                finally {
+                    jqInput.val('');
+                }
             });
         }
 
+        // start the cursor blinking if so instructed
+        if ((typeof cursorBlinkRate === 'number') && (cursorBlinkRate >= 50)) { // have some sense of sanity
+            blinkTimer = setInterval(function () {
+                jqContent.find('.cq-con-cursor').toggleClass('inverted');
+            }, cursorBlinkRate);
+        }
+
         // emit greeting if provided
-        if (options.greeting) {
+        if (options && options.greeting) {
             lines.push({content: options.greeting, className: 'greeting'});
             currentRow++;
         }
-- 
cgit v1.2.1