diff --git a/Bounce/app/OutputConsole.js b/Bounce/app/OutputConsole.js new file mode 100644 index 0000000..a88c61b --- /dev/null +++ b/Bounce/app/OutputConsole.js @@ -0,0 +1,48 @@ +/** + * Create a console to output data in visible in the UI. + * @param output_element A DOM element to use for output. + * @constructor + */ +var OutputConsole = function (output_element) { + this.output_element = output_element; + this.lineTypedHandler = null; + this.writeLine('Console initialised.'); +}; + +/** + * Write some data to the output. HTML is escaped. + * @param data Data to write. + */ +OutputConsole.prototype.write = function(data) { + var safe_data = goog.string.htmlEscape(data); + safe_data = goog.string.newLineToBr(safe_data); + this.output_element.append(safe_data); +}; + +/** + * Write a line of data. + * @param line + */ +OutputConsole.prototype.writeLine = function(line) { + this.write(line + '\n') +}; + +/** + * Set a handler for a line being typed. Only one handler allowed here. + * @param handler The handler to call. Expects only a "line" parameter - a string of input. + */ +OutputConsole.prototype.lineTyped = function(handler) { + this.lineTypedHandler = handler; +}; + +OutputConsole.prototype.setupInput = function(inputElement) { + var _oc = this; + inputElement.keypress(function(e) { + /* If it's an enter key */ + if(e.which == 13) { + var line = inputElement.val(); + _oc.lineTypedHandler(line + "\n"); + inputElement.val(''); + } + }); +}; \ No newline at end of file diff --git a/Bounce/app/start.js b/Bounce/app/start.js index 4ecc120..ec306cd 100644 --- a/Bounce/app/start.js +++ b/Bounce/app/start.js @@ -3,33 +3,6 @@ var mcu_console; var is_preparing = false; var ui; -/** - * Create a console to output data in visible in the UI. - * @param output_element A DOM element to use for output. - * @constructor - */ -var OutputConsole = function (output_element) { - /** - * Write some data to the output. HTML is escaped. - * @param data Data to write. - */ - this.write = function(data) { - var safe_data = goog.string.htmlEscape(data); - safe_data = goog.string.newLineToBr(safe_data); - output_element.append(safe_data); - }; - - /** - * Write a line of data. - * @param line - */ - this.writeLine = function(line) { - this.write(line + '\n') - }; - - this.writeLine('Console initialised.'); -}; - /** * @@ -344,6 +317,7 @@ BounceUI.prototype.connect_menu_item_clicked_ = function(connectItem, mcu) { // We've now connected the mcu. Update the UI mcu_console.writeLine("Connected"); _ui.currentMcu = mcu; + mcu_console.lineTyped(mcu.send_data); // Add a tick (Check) to the connection menu item //connectItem.setChecked(true); // disconnect any others @@ -386,7 +360,7 @@ BounceUI.prototype.connect_code = function() { BounceUI.prototype.setup_tabs = function() { var right_tabs = new goog.ui.TabPane(document.getElementById('rightTabs')); right_tabs.addPage(new goog.ui.TabPane.TabPage( - document.getElementById('output'), "Output")); + document.getElementById('console'), "Output")); right_tabs.addPage(new goog.ui.TabPane.TabPage( document.getElementById('code_page'), "Code")); this.connect_code(); @@ -400,6 +374,7 @@ $(function () { prepare_blockly_workspace(); mcu_console = new OutputConsole($('#output')); + mcu_console.setupInput($('#consoleInput')); ui = new BounceUI(); ui.setup_menu(); ui.setup_tabs(); diff --git a/Bounce/bounce.css b/Bounce/bounce.css index 10e13d4..f9bb320 100644 --- a/Bounce/bounce.css +++ b/Bounce/bounce.css @@ -234,6 +234,19 @@ code { height: -webkit-calc(100% - 37px); } +#console { + height: -webkit-calc(100% - 1em); +} + +#consoleInput { + position: absolute; + width: 100%; + bottom: 0; + left: 0; + height: 1em; + float: bottom; +} + #output { margin: 1em 0 1em; width: auto; @@ -243,7 +256,6 @@ code { user-select: text; -webkit-user-select: text; cursor: auto; - } #code_page, #code_page pre { diff --git a/Bounce/bounce_window.html b/Bounce/bounce_window.html index 42846a5..96c55b2 100644 --- a/Bounce/bounce_window.html +++ b/Bounce/bounce_window.html @@ -15,6 +15,7 @@ + @@ -67,7 +68,7 @@
-
+
-- no code yet
diff --git a/tests/SpecRunner.html b/tests/SpecRunner.html index d744928..0f51c8e 100644 --- a/tests/SpecRunner.html +++ b/tests/SpecRunner.html @@ -23,6 +23,7 @@ + @@ -32,6 +33,6 @@ -
-- no code yet
+
-- no code yet
diff --git a/tests/spec/BounceSpec.js b/tests/spec/BounceSpec.js index 6896ac9..5bc743a 100644 --- a/tests/spec/BounceSpec.js +++ b/tests/spec/BounceSpec.js @@ -121,9 +121,12 @@ describe("OutputConsole", function() { var element; beforeEach(function() { + setFixtures('
'); + setFixtures('
'); element = $("#console"); spyOn(element, "append").and.stub(); console = new OutputConsole(element); + console.setupInput($('#console_input')); }); it("should write data to a div", function() { @@ -151,4 +154,15 @@ describe("OutputConsole", function() { console.write("if a < b then"); expect(element.append).toHaveBeenCalledWith("if a < b then"); }); + + it("Should send input box to connected code on enter", function() { + var callback = jasmine.createSpy("dummy"); + console.lineTyped(callback); + $('#console_input').val("A test line"); + var e = jQuery.Event("keypress"); + e.which = 13; + $('#console_input').trigger(e); + expect(callback).toHaveBeenCalledWith('A test line\n'); + expect($('#console_input').val()).toEqual(''); + }); }); \ No newline at end of file