diff --git a/evennia/server/portal/telnet.py b/evennia/server/portal/telnet.py index 96da59b72f..999999afa7 100644 --- a/evennia/server/portal/telnet.py +++ b/evennia/server/portal/telnet.py @@ -353,4 +353,5 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session): """ Send other oob data """ - print "telnet.send_default not implemented yet! ", args + if not cmdname == "options": + print "telnet.send_default not implemented yet! ", args diff --git a/evennia/server/portal/webclient.py b/evennia/server/portal/webclient.py index 47a191f59c..c0fa3e3dcc 100644 --- a/evennia/server/portal/webclient.py +++ b/evennia/server/portal/webclient.py @@ -185,5 +185,6 @@ class WebSocketClient(Protocol, Session): client instead. """ - print "send_default", cmdname, args, kwargs - session.sendLine(json.dumps([cmdname, args, kwargs])) + if not cmdname == "options": + print "send_default", cmdname, args, kwargs + session.sendLine(json.dumps([cmdname, args, kwargs])) diff --git a/evennia/web/webclient/static/webclient/js/evennia.js b/evennia/web/webclient/static/webclient/js/evennia.js index 76b9669411..bb180e7fc6 100644 --- a/evennia/web/webclient/static/webclient/js/evennia.js +++ b/evennia/web/webclient/static/webclient/js/evennia.js @@ -143,7 +143,10 @@ An "emitter" object must have a function // var DefaultEmitter = function () { var listeners = {}; - // Emit data to all listeners tied to a given cmdname + // Emit data to all listeners tied to a given cmdname. + // If the cmdname is not recognized, call a listener + // named 'default' with arguments [cmdname, args, kwargs]. + // If no 'default' is found, ignore silently. // // Args: // cmdname (str): Name of command, used to find @@ -155,7 +158,10 @@ An "emitter" object must have a function log("DefaultEmitter.emit:", cmdname, args, kwargs); if (listeners[cmdname]) { listeners[cmdname].apply(this, [args, kwargs]); - }; + } + else if (listeners["default"]) { + listeners["default"].apply(this, [cmdname, args, kwargs]); + } }; // Bind listener to event @@ -291,7 +297,7 @@ An "emitter" object must have a function // function log() { if (Evennia.debug) { - console.log(arguments); + console.log(JSON.stringify(arguments)); } } diff --git a/evennia/web/webclient/static/webclient/js/webclient_gui.js b/evennia/web/webclient/static/webclient/js/webclient_gui.js index 04eb12b292..da208990ce 100644 --- a/evennia/web/webclient/static/webclient/js/webclient_gui.js +++ b/evennia/web/webclient/static/webclient/js/webclient_gui.js @@ -17,10 +17,8 @@ // -// // Manage history for input line -// -var inputlog = function() { +var input_history = function() { var history_max = 21; var history = new Array(); var history_pos = 0; @@ -39,7 +37,7 @@ var inputlog = function() { }; var add = function (input) { // add a new entry to history, don't repeat latest - if (input != history[history.length-1]) { + if (input && input != history[history.length-1]) { if (history.length >= history_max) { history.shift(); // kill oldest entry } @@ -52,112 +50,100 @@ var inputlog = function() { add: add} }(); -$.fn.appendCaret = function() { - /* jQuery extension that will forward the caret to the end of the input, and - won't harm other elements (although calling this on multiple inputs might - not have the expected consequences). - - Thanks to - http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area - for the good starting point. */ - return this.each(function() { - var range, - // Index at where to place the caret. - end, - self = this; - - if (self.setSelectionRange) { - // other browsers - end = self.value.length; - self.focus(); - // NOTE: Need to delay the caret movement until after the callstack. - setTimeout(function() { - self.setSelectionRange(end, end); - }, 0); - } - else if (self.createTextRange) { - // IE - end = self.value.length - 1; - range = self.createTextRange(); - range.collapse(true); - range.moveEnd('character', end); - range.moveStart('character', end); - // NOTE: I haven't tested to see if IE has the same problem as - // W3C browsers seem to have in this context (needing to fire - // select after callstack). - range.select(); - } - }); -}; - - +// // GUI Event Handlers +// -$(document).keydown( function(event) { - // catch all keyboard input, handle special chars +// Grab text from inputline and send to Evennia +function doSendText() { + inputfield = $("#inputfield"); + outtext = inputfield.val(); + input_history.add(outtext); + inputfield.val(""); + log("sending outtext", outtext); + Evennia.msg("text", [outtext], {}); +} + +// catch all keyboard input, handle special chars +function onKeydown (event) { var code = event.which; inputfield = $("#inputfield"); inputfield.focus(); if (code === 13) { // Enter key sends text - outtext = inputfield.val(); - inputlog.add(outtext); - inputfield.val(""); - log("sending outtext", outtext); - Evennia.msg("text", [outtext], {}); - event.preventDefault() + doSendText(); + event.preventDefault(); } else if (code === 38) { // Arrow up - inputfield.val(inputlog.back()).appendCaret(); + inputfield.val(input_history.back()); + event.preventDefault(); } else if (code === 40) { // Arrow down - inputfield.val(inputlog.fwd()).appendCaret(); + inputfield.val(input_history.fwd()); + event.preventDefault(); } +}; -}); - -// client size setter - -function set_window_size() { +// Handle resizing of client +function doWindowResize() { var winh = $(document).height(); var formh = $('#inputform').outerHeight(true); $("#messagewindow").css({'height': winh - formh - 1}); } -// Event - called when window resizes -$(window).resize(set_window_size); - - -// -// Listeners -// - -function doText(args, kwargs) { - // append message to previous ones - log("doText:", args, kwargs); - $("#messagewindow").append( - "
" + args[0] + "
"); - // scroll message window to bottom - $("#messagewindow").animate({scrollTop: $('#messagewindow')[0].scrollHeight}); +// Handle text coming from the server +function onText(args, kwargs) { + // append message to previous ones, then scroll so latest is at + // the bottom. + mwin = $("#messagewindow"); + mwin.append("
" + args[0] + "
"); + mwin.scrollTop(mwin[0].scrollHeight); } -function doPrompt(args, kwargs) { +// Handle prompt output from the server +function onPrompt(args, kwargs) { // show prompt $('prompt').replaceWith( "
" + args[0] + "
"); } +// Handler unrecognized commands from server +function onDefault(cmdname, args, kwargs) { + mwin = $("#messagewindow"); + mwin.append( + "
" + + "Received unknown server command:
" + + cmdname + ", " + + JSON.stringify(args) + ", " + + JSON.stringify(kwargs) + "

"); + mwin.scrollTop(mwin[0].scrollHeight); +} + +// +// Register Events +// + +// Event when client window changes +$(window).resize(doWindowResize); + +// Evenit when any key is pressed +$(document).keydown(onKeydown); + +// Event when client finishes loading $(document).ready(function() { - // a small timeout to stop 'loading' indicator in Chrome - Evennia.init() + // This is safe to call, it will always only + // initialize once. + Evennia.init(); // register listeners log("register listeners ..."); - Evennia.emitter.on("text", doText); - Evennia.emitter.on("prompt", doPrompt); - set_window_size(); + Evennia.emitter.on("text", onText); + Evennia.emitter.on("prompt", onPrompt); + Evennia.emitter.on("default", onDefault); + doWindowResize(); - // set an idle timer to avoid proxy servers to time out on us (every 3 minutes) + // set an idle timer to send idle every 3 minutes, + // to avoid proxy servers timing out on us setInterval(function() { log('Idle tick.'); Evennia.msg("text", ["idle"], {}); diff --git a/evennia/web/webclient/templates/webclient/webclient.html b/evennia/web/webclient/templates/webclient/webclient.html index 9e19c9af6f..c0315f2694 100644 --- a/evennia/web/webclient/templates/webclient/webclient.html +++ b/evennia/web/webclient/templates/webclient/webclient.html @@ -13,6 +13,7 @@
+