/public/js/script.js

https://github.com/anod/Web-Admin-Lite-Perl · JavaScript · 136 lines · 101 code · 16 blank · 19 comment · 15 complexity · d827847f1a97ff63f6a2c28c4e2aea8e MD5 · raw file

  1. var history_position = 0;
  2. var terminal_history = [];
  3. var key_code =
  4. {
  5. arrow : {left: 37, up: 38, right: 39, down: 40 },
  6. enter : 13
  7. }
  8. $(function(){
  9. $('#terminal_command').keydown(function (e) {
  10. var code = e.keyCode || e.which;
  11. switch (code) {
  12. case key_code.arrow.up:
  13. history('up');
  14. return false;
  15. break;
  16. case key_code.arrow.down:
  17. history('down');
  18. return false;
  19. break;
  20. case key_code.arrow.left:
  21. case key_code.arrow.right:
  22. default:
  23. return true;
  24. }
  25. });
  26. $("#terminal_command").keypress(function(e) {
  27. var code = (e.keyCode ? e.keyCode : e.which);
  28. if (code != key_code.enter) {
  29. return true;
  30. }
  31. execute(this.value);
  32. e.preventDefault();
  33. return false;
  34. });
  35. $("#custom_commands").change(function() {
  36. var cmd = $(this).val();
  37. if (cmd == "") {
  38. return;
  39. }
  40. set_command(cmd);
  41. $(this).val("");
  42. });
  43. });
  44. $(function(){
  45. $("#update_time_toggle").click(function() {
  46. $("#update_time_dialog").dialog({
  47. modal: true ,
  48. resizable: false
  49. });
  50. });
  51. });
  52. function set_command(cmd) {
  53. $("#terminal_command").val(cmd);
  54. $("#terminal_command").focus();
  55. }
  56. function history(dir) {
  57. if (dir == 'down') {
  58. if (!terminal_history.length) {
  59. return;
  60. }
  61. if (history_position + 1 > terminal_history.length) {
  62. return;
  63. }
  64. history_position++;
  65. set_command(terminal_history[history_position]);
  66. return;
  67. }
  68. if (dir == 'up') {
  69. if (!terminal_history.length) {
  70. set_command("");
  71. return;
  72. }
  73. if (history_position <= 0) {
  74. set_command("");
  75. return;
  76. }
  77. history_position--;
  78. set_command(terminal_history[history_position]);
  79. return;
  80. }
  81. }
  82. function execute(cmd) {
  83. terminal_history.push(cmd);
  84. var cmd_escaped = encodeURIComponent(cmd);
  85. $.ajax({
  86. type: "POST",
  87. url: "/terminal/execute",
  88. data: "cmd="+cmd_escaped,
  89. cache: false,
  90. success: function(msg){
  91. $("#terminal_window_ouput").append("&gt;"+cmd+"<br/>");
  92. $("#terminal_window_ouput").append(nl2br(msg,false)+"<br/>")
  93. $("#terminal_command").val("");
  94. $("#terminal_window_ouput").scrollTop($("#terminal_window_ouput")[0].scrollHeight);
  95. }
  96. });
  97. }
  98. function nl2br (str, is_xhtml) {
  99. // Converts newlines to HTML line breaks
  100. //
  101. // version: 1107.2516
  102. // discuss at: http://phpjs.org/functions/nl2br
  103. // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  104. // + improved by: Philip Peterson
  105. // + improved by: Onno Marsman
  106. // + improved by: Atli Þór
  107. // + bugfixed by: Onno Marsman
  108. // + input by: Brett Zamir (http://brett-zamir.me)
  109. // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  110. // + improved by: Brett Zamir (http://brett-zamir.me)
  111. // + improved by: Maximusya
  112. // * example 1: nl2br('Kevin\nvan\nZonneveld');
  113. // * returns 1: 'Kevin\nvan\nZonneveld'
  114. // * example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
  115. // * returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
  116. // * example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
  117. // * returns 3: '\nOne\nTwo\n\nThree\n'
  118. var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
  119. return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
  120. }