Console.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. if(MIDAS == undefined) {
  2. var MIDAS = {};
  3. }
  4. MIDAS.Console = function(inputField, outputField, sendButton) {
  5. var self = this;
  6. var maxLines = 20;
  7. var lines = [];
  8. var sentCallback = function(message) {};
  9. this.register = function(sendResponse) {
  10. sentCallback = sendResponse;
  11. };
  12. this.init = function() {
  13. inputField.keypress(inputTyping);
  14. sendButton.click(userSend);
  15. inputField.focus();
  16. };
  17. this.output = function(message) {
  18. addLine(message, function(){});
  19. }
  20. function addLine(message, callback) {
  21. var messageOutput = $('<p class="line">' + message + "</p>");
  22. lines.push(messageOutput);
  23. messageOutput.hide();
  24. outputField.append(messageOutput);
  25. messageOutput.fadeIn('fast', callback);
  26. if(lines.length >= maxLines) {
  27. lines[0].fadeOut();
  28. lines = lines.slice(1);
  29. }
  30. };
  31. function inputTyping(event) {
  32. if(event.which == 13) {
  33. userSend();
  34. }
  35. };
  36. function userSend() {
  37. var message = inputField.val();
  38. addLine(message, function() { sentCallback(message);});
  39. inputField.val('');
  40. };
  41. };