Skip to content

Commit

Permalink
Clicking on a nick or channel name will insert it into the commandline.
Browse files Browse the repository at this point in the history
Also added util/misc.insertTextIntoField() and test.
  • Loading branch information
toolness committed Jul 3, 2012
1 parent 2eb036e commit 8fefff8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 5 additions & 0 deletions static/index.html
Expand Up @@ -12,6 +12,11 @@
font-size: 0; font-size: 0;
} }


span.nick:hover, span.target:hover {
text-decoration: underline;
cursor: pointer;
}

#users { #users {
font-size: 14px; font-size: 14px;
background: #f0f0f0; background: #f0f0f0;
Expand Down
9 changes: 7 additions & 2 deletions static/main.js
Expand Up @@ -8,9 +8,10 @@ define([
"user-list-view", "user-list-view",
"cmdline", "cmdline",
"login", "login",
"util/pretty-date" "util/pretty-date",
"util/misc"
], function($, _, LogArea, IRC, UserListView, CommandLine, Login, ], function($, _, LogArea, IRC, UserListView, CommandLine, Login,
prettyDate) { prettyDate, misc) {
function showLoggedMessages(irc, logArea) { function showLoggedMessages(irc, logArea) {
var CHUNK_SIZE = 10; var CHUNK_SIZE = 10;
var lastChunk = -CHUNK_SIZE; var lastChunk = -CHUNK_SIZE;
Expand Down Expand Up @@ -133,6 +134,10 @@ define([
log("error", "The connection to the server has been lost."); log("error", "The connection to the server has been lost.");
}); });
$(window).on('beforeunload', function() { isUnloading = true; }); $(window).on('beforeunload', function() { isUnloading = true; });
$(window).on('click', 'span.nick, span.target', function(evt) {
misc.insertTextIntoField($(this).text(), cmdLine.el);
return false;
});


return { return {
login: login, login: login,
Expand Down
14 changes: 13 additions & 1 deletion 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 doesNickMatch = misc.doesNickMatch;
var insertTextIntoField = misc.insertTextIntoField;


describe("doesNickMatch()", function() { describe("doesNickMatch()", function() {
it("should match joe to joe1", function() { it("should match joe to joe1", function() {
Expand Down Expand Up @@ -34,4 +35,15 @@ defineTests(["util/misc"], function(misc) {
expect(doesNickMatch("joe", "Joe")).to.be(false); 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();
});
});
}); });
20 changes: 19 additions & 1 deletion static/util/misc.js
@@ -1,4 +1,4 @@
define(function() { define(["jquery"], function($) {
return { return {
doesNickMatch: function(base, actual) { doesNickMatch: function(base, actual) {
var match = actual.match(/^[_]*([A-Za-z0-9]+)/); var match = actual.match(/^[_]*([A-Za-z0-9]+)/);
Expand All @@ -7,6 +7,24 @@ define(function() {
if (match[1] == base || match[1] == base + '1') if (match[1] == base || match[1] == base + '1')
return true; return true;
return false; 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.