console.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. $(document).ready(function(){
  2. var history = 1;
  3. var current_command = 1;
  4. var dragging = 0;
  5. var url = location.href + (location.href.match(/\/$/) ? '' : '/');
  6. var ansi_up = new AnsiUp;
  7. $('.console-line:last-child .edit').focus();
  8. $(document).on('click', '.console-line:last-child', function(e) {
  9. $('.console-line:last-child .edit').focus();
  10. });
  11. $(document).on('keydown', 'body', function(e) {
  12. if ((e.ctrlKey && e.keyCode == 86) || !e.ctrlKey) {
  13. $('.console-line:last-child .edit').focus();
  14. } else {
  15. return;
  16. }
  17. if (e.keyCode == 38) {
  18. var anakin = $('.console-line:last-child .edit');
  19. if(current_command<history) {
  20. current_command++;
  21. }
  22. anakin.text($(".console-line:nth-last-child("+current_command+") > .edit").text());
  23. anakin.focus();
  24. jQuery.event.trigger({type:'keyup',which:35,charCode:35,ctrlKey: false});
  25. return false;
  26. } else if (e.keyCode == 40) {
  27. var anakin = $('.console-line:last-child .edit');
  28. if(current_command>1) {
  29. current_command--;
  30. }
  31. anakin.html($(".console-line:nth-last-child("+current_command+") > .edit").text());
  32. } else if (e.keyCode == 13) {
  33. var anakin = $('.console-line:last-child .edit');
  34. var anakin_str = anakin.text().split(" ");
  35. var command= "";
  36. var cc = 0;
  37. history++;
  38. current_command=1;
  39. anakin_str.forEach(function(x){
  40. if (cc==0){
  41. command="cmd="+x;
  42. cc++;
  43. } else{
  44. command+=" "+x;
  45. }
  46. });
  47. if(anakin_str[0]=="clear"){
  48. window.location.reload();
  49. return false;
  50. }
  51. $.ajax({
  52. url: url,
  53. type: 'get',
  54. data: command,
  55. success: function(data) {
  56. if(jQuery.type(data)=="string"){
  57. var appendix = '';
  58. appendix += $.trim(data) + "\n";
  59. if(anakin_str[0]=="help")
  60. appendix+="\033[1m\033[31mclear\033[0m\033[0m\n Clear screen.";
  61. var node = $("<div style='white-space: pre-wrap;'/>");
  62. node.html(ansi_up.ansi_to_html(appendix));
  63. anakin.parent().append(node);
  64. }
  65. },
  66. error: function(data) {
  67. anakin.parent().append("<div>server made a boo boo</div>");
  68. },
  69. complete: function(data) {
  70. var node = $("#console-template").clone().appendTo("body");
  71. node.show();
  72. node.removeAttr('id');
  73. $('.edit:last-child',node).focus();
  74. }
  75. });
  76. anakin.attr('contenteditable',false);
  77. return false;
  78. }
  79. });
  80. });