multi_column_sort.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview Implements the shiftkey + click remove column
  4. * from order by clause funcationality
  5. * @name columndelete
  6. *
  7. * @requires jQuery
  8. */
  9. function captureURL (url) {
  10. var URL = {};
  11. url = '' + url;
  12. // Exclude the url part till HTTP
  13. url = url.substr(url.search('sql.php'), url.length);
  14. // The url part between ORDER BY and &session_max_rows needs to be replaced.
  15. URL.head = url.substr(0, url.indexOf('ORDER+BY') + 9);
  16. URL.tail = url.substr(url.indexOf('&session_max_rows'), url.length);
  17. return URL;
  18. }
  19. /**
  20. * This function is for navigating to the generated URL
  21. *
  22. * @param object target HTMLAnchor element
  23. * @param object parent HTMLDom Object
  24. */
  25. function removeColumnFromMultiSort (target, parent) {
  26. var URL = captureURL(target);
  27. var begin = target.indexOf('ORDER+BY') + 8;
  28. var end = target.indexOf(PMA_commonParams.get('arg_separator') + 'session_max_rows');
  29. // get the names of the columns involved
  30. var between_part = target.substr(begin, end - begin);
  31. var columns = between_part.split('%2C+');
  32. // If the given column is not part of the order clause exit from this function
  33. var index = parent.find('small').length ? parent.find('small').text() : '';
  34. if (index === '') {
  35. return '';
  36. }
  37. // Remove the current clicked column
  38. columns.splice(index - 1, 1);
  39. // If all the columns have been removed dont submit a query with nothing
  40. // After order by clause.
  41. if (columns.length === 0) {
  42. var head = URL.head;
  43. head = head.slice(0,head.indexOf('ORDER+BY'));
  44. URL.head = head;
  45. // removing the last sort order should have priority over what
  46. // is remembered via the RememberSorting directive
  47. URL.tail += PMA_commonParams.get('arg_separator') + 'discard_remembered_sort=1';
  48. }
  49. URL.head = URL.head.substring(URL.head.indexOf('?') + 1);
  50. var middle_part = columns.join('%2C+');
  51. params = URL.head + middle_part + URL.tail;
  52. return params;
  53. }
  54. AJAX.registerOnload('keyhandler.js', function () {
  55. $('th.draggable.column_heading.pointer.marker a').on('click', function (event) {
  56. var url = $(this).parent().find('input').val();
  57. var argsep = PMA_commonParams.get('arg_separator');
  58. if (event.ctrlKey || event.altKey) {
  59. event.preventDefault();
  60. var params = removeColumnFromMultiSort(url, $(this).parent());
  61. if (params) {
  62. AJAX.source = $(this);
  63. PMA_ajaxShowMessage();
  64. params += argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
  65. $.post('sql.php', params, AJAX.responseHandler);
  66. }
  67. } else if (event.shiftKey) {
  68. event.preventDefault();
  69. AJAX.source = $(this);
  70. PMA_ajaxShowMessage();
  71. var params = url.substring(url.indexOf('?') + 1);
  72. params += argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true';
  73. $.post('sql.php', params, AJAX.responseHandler);
  74. }
  75. });
  76. });
  77. AJAX.registerTeardown('keyhandler.js', function () {
  78. $(document).off('click', 'th.draggable.column_heading.pointer.marker a');
  79. });