Skip to content
Browse files

Clicking on a nick or channel name will insert it into the commandline.

Also added util/misc.insertTextIntoField() and test.
  • Loading branch information...
1 parent 2eb036e commit 8fefff820bce086202e2d9597545c0484a609870 @toolness committed Jul 3, 2012
Showing with 44 additions and 4 deletions.
  1. +5 −0 static/index.html
  2. +7 −2 static/main.js
  3. +13 −1 static/test/test-util-misc.js
  4. +19 −1 static/util/misc.js
View
5 static/index.html
@@ -12,6 +12,11 @@
font-size: 0;
}
+span.nick:hover, span.target:hover {
+ text-decoration: underline;
+ cursor: pointer;
+}
+
#users {
font-size: 14px;
background: #f0f0f0;
View
9 static/main.js
@@ -8,9 +8,10 @@ define([
"user-list-view",
"cmdline",
"login",
- "util/pretty-date"
+ "util/pretty-date",
+ "util/misc"
], function($, _, LogArea, IRC, UserListView, CommandLine, Login,
- prettyDate) {
+ prettyDate, misc) {
function showLoggedMessages(irc, logArea) {
var CHUNK_SIZE = 10;
var lastChunk = -CHUNK_SIZE;
@@ -133,6 +134,10 @@ define([
log("error", "The connection to the server has been lost.");
});
$(window).on('beforeunload', function() { isUnloading = true; });
+ $(window).on('click', 'span.nick, span.target', function(evt) {
+ misc.insertTextIntoField($(this).text(), cmdLine.el);
+ return false;
+ });
return {
login: login,
View
14 static/test/test-util-misc.js
@@ -1,5 +1,6 @@
-defineTests(["util/misc"], function(misc) {
+defineTests(["jquery", "util/misc"], function($, misc) {
var doesNickMatch = misc.doesNickMatch;
+ var insertTextIntoField = misc.insertTextIntoField;
describe("doesNickMatch()", function() {
it("should match joe to joe1", function() {
@@ -34,4 +35,15 @@ defineTests(["util/misc"], function(misc) {
expect(doesNickMatch("joe", "Joe")).to.be(false);
});
});
+
+ describe("insertTextIntoField()", function() {
+ it("should insert text at current cursor position", function() {
+ var input = $('<input type="text">').appendTo('body');
+ input.val('hello');
+ input[0].setSelectionRange(1, 1);
+ insertTextIntoField("SUP", input);
+ expect(input.val()).to.be("hSUPello");
+ input.remove();
+ });
+ });
});
View
20 static/util/misc.js
@@ -1,4 +1,4 @@
-define(function() {
+define(["jquery"], function($) {
return {
doesNickMatch: function(base, actual) {
var match = actual.match(/^[_]*([A-Za-z0-9]+)/);
@@ -7,6 +7,24 @@ define(function() {
if (match[1] == base || match[1] == base + '1')
return true;
return false;
+ },
+ insertTextIntoField: function(text, input) {
+ input = $(input)[0];
+
+ // Use selectionEnd instead of selectionStart because Mobile Chrome
+ // sometimes selects-all while in weird Android keyboard autocomplete
+ // mode.
+ var caret = input.selectionEnd;
+ var oldVal = $(input).val();
+ var newVal = oldVal.slice(0, caret) + text + oldVal.slice(caret);
+ var moveCursor = function() {
+ input.setSelectionRange(caret+text.length, caret+text.length);
+ };
+ $(input).val(newVal).focus().trigger("change");
+ moveCursor();
+ // Move the cursor after a bit because Mobile Chrome positions it
+ // right before the character we want to be at, for some reason.
+ setTimeout(moveCursor, 100);
}
};
});

0 comments on commit 8fefff8

Please sign in to comment.
Something went wrong with that request. Please try again.