server_variables.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * Unbind all event handlers before tearing down a page
  4. */
  5. AJAX.registerTeardown('server_variables.js', function () {
  6. $(document).off('click', 'a.editLink');
  7. $('#serverVariables').find('.var-name').find('a img').remove();
  8. });
  9. AJAX.registerOnload('server_variables.js', function () {
  10. var $editLink = $('a.editLink');
  11. var $saveLink = $('a.saveLink');
  12. var $cancelLink = $('a.cancelLink');
  13. $('#serverVariables').find('.var-name').find('a').append(
  14. $('#docImage').clone().css('display', 'inline-block')
  15. );
  16. /* Launches the variable editor */
  17. $(document).on('click', 'a.editLink', function (event) {
  18. event.preventDefault();
  19. editVariable(this);
  20. });
  21. /* Allows the user to edit a server variable */
  22. function editVariable (link) {
  23. var $link = $(link);
  24. var $cell = $link.parent();
  25. var $valueCell = $link.parents('.var-row').find('.var-value');
  26. var varName = $link.data('variable');
  27. var $mySaveLink = $saveLink.clone().css('display', 'inline-block');
  28. var $myCancelLink = $cancelLink.clone().css('display', 'inline-block');
  29. var $msgbox = PMA_ajaxShowMessage();
  30. var $myEditLink = $cell.find('a.editLink');
  31. $cell.addClass('edit'); // variable is being edited
  32. $myEditLink.remove(); // remove edit link
  33. $mySaveLink.click(function () {
  34. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
  35. $.post($(this).attr('href'), {
  36. ajax_request: true,
  37. type: 'setval',
  38. varName: varName,
  39. varValue: $valueCell.find('input').val()
  40. }, function (data) {
  41. if (data.success) {
  42. $valueCell
  43. .html(data.variable)
  44. .data('content', data.variable);
  45. PMA_ajaxRemoveMessage($msgbox);
  46. } else {
  47. if (data.error === '') {
  48. PMA_ajaxShowMessage(PMA_messages.strRequestFailed, false);
  49. } else {
  50. PMA_ajaxShowMessage(data.error, false);
  51. }
  52. $valueCell.html($valueCell.data('content'));
  53. }
  54. $cell.removeClass('edit').html($myEditLink);
  55. });
  56. return false;
  57. });
  58. $myCancelLink.click(function () {
  59. $valueCell.html($valueCell.data('content'));
  60. $cell.removeClass('edit').html($myEditLink);
  61. return false;
  62. });
  63. $.get($mySaveLink.attr('href'), {
  64. ajax_request: true,
  65. type: 'getval',
  66. varName: varName
  67. }, function (data) {
  68. if (typeof data !== 'undefined' && data.success === true) {
  69. var $links = $('<div />')
  70. .append($myCancelLink)
  71. .append('&nbsp;&nbsp;&nbsp;')
  72. .append($mySaveLink);
  73. var $editor = $('<div />', { 'class': 'serverVariableEditor' })
  74. .append(
  75. $('<div/>').append(
  76. $('<input />', { type: 'text' }).val(data.message)
  77. )
  78. );
  79. // Save and replace content
  80. $cell
  81. .html($links)
  82. .children()
  83. .css('display', 'flex');
  84. $valueCell
  85. .data('content', $valueCell.html())
  86. .html($editor)
  87. .find('input')
  88. .focus()
  89. .keydown(function (event) { // Keyboard shortcuts
  90. if (event.keyCode === 13) { // Enter key
  91. $mySaveLink.trigger('click');
  92. } else if (event.keyCode === 27) { // Escape key
  93. $myCancelLink.trigger('click');
  94. }
  95. });
  96. PMA_ajaxRemoveMessage($msgbox);
  97. } else {
  98. $cell.removeClass('edit').html($myEditLink);
  99. PMA_ajaxShowMessage(data.error);
  100. }
  101. });
  102. }
  103. });