From f141095312a0b3686b489ffdd2f3f5ef5e3e9bd1 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 23 Feb 2014 10:48:21 -0500 Subject: [PATCH 1/3] Add method to retrieve inputNode element from directive --- js/glowingbear.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/js/glowingbear.js b/js/glowingbear.js index 477d0ce..94bb211 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -995,6 +995,7 @@ weechat.directive('inputBar', function() { templateUrl: 'directives/input.html', controller: function($rootScope, $scope, + $element, connection, models) { @@ -1005,6 +1006,13 @@ weechat.directive('inputBar', function() { }); */ + + /* + * Returns the input element + */ + $scope.getInputNode = function() { + return $element.find('input')[0]; + }; $scope.completeNick = function() { // input DOM node var inputNode = document.getElementById('sendMessage'); From 5f25a96b514ca61a335911d956224e3c38b30bba Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 23 Feb 2014 10:50:55 -0500 Subject: [PATCH 2/3] Work with inputNode element directly Instead of using $scope.command, we work with the inputNode element directly to have control over the caret position. This let us have nick completition in the same way as WeeChat. Fix #74 --- directives/input.html | 2 +- js/glowingbear.js | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/directives/input.html b/directives/input.html index 65bbc45..e9d5b86 100644 --- a/directives/input.html +++ b/directives/input.html @@ -1,6 +1,6 @@
- + diff --git a/js/glowingbear.js b/js/glowingbear.js index 94bb211..7d62e96 100644 --- a/js/glowingbear.js +++ b/js/glowingbear.js @@ -1013,9 +1013,10 @@ weechat.directive('inputBar', function() { $scope.getInputNode = function() { return $element.find('input')[0]; }; + $scope.completeNick = function() { // input DOM node - var inputNode = document.getElementById('sendMessage'); + var inputNode = $scope.getInputNode(); // get current input var inputText = inputNode.value; @@ -1034,7 +1035,7 @@ weechat.directive('inputBar', function() { $scope.iterCandidate = nickComp.iterCandidate; // update current input - $scope.command = nickComp.text; + inputNode.value = nickComp.text; // update current caret position inputNode.focus(); @@ -1044,8 +1045,11 @@ weechat.directive('inputBar', function() { // Send the message to the websocket $scope.sendMessage = function() { - connection.sendMessage($scope.command); - $scope.command = models.getActiveBuffer().addToHistory($scope.command); // log to buffer history + + var input = $scope.getInputNode(); + connection.sendMessage(input.value); + models.getActiveBuffer().addToHistory(input.value); // log to buffer history + input.value = ''; }; // Handle key presses in the input bar @@ -1055,6 +1059,8 @@ weechat.directive('inputBar', function() { return true; } + var inputNode = $scope.getInputNode(); + // Support different browser quirks var code = $event.keyCode ? $event.keyCode : $event.charCode; @@ -1101,7 +1107,6 @@ weechat.directive('inputBar', function() { // Alt+L -> focus on input bar if ($event.altKey && (code === 76 || code === 108)) { $event.preventDefault(); - var inputNode = document.getElementById('sendMessage'); inputNode.focus(); inputNode.setSelectionRange(inputNode.value.length, inputNode.value.length); return true; @@ -1133,13 +1138,13 @@ weechat.directive('inputBar', function() { // Arrow up -> go up in history if (code === 38) { - $scope.command = models.getActiveBuffer().getHistoryUp($scope.command); + inputNode.value = models.getActiveBuffer().getHistoryUp(inputNode.value); return true; } // Arrow down -> go down in history if (code === 40) { - $scope.command = models.getActiveBuffer().getHistoryDown($scope.command); + inputNode.value = models.getActiveBuffer().getHistoryDown(inputNode.value); return true; } }; From 5cd36f077985750a7a9d263127669a1e57f7acd0 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 23 Feb 2014 10:53:00 -0500 Subject: [PATCH 3/3] Remove unnecessary id Encapsulating the feature in a angular directive let us avoid setting global ids for elements. --- directives/input.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/directives/input.html b/directives/input.html index e9d5b86..b432fca 100644 --- a/directives/input.html +++ b/directives/input.html @@ -1,6 +1,6 @@
- +