tbl_relation.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * for tbl_relation.php
  4. *
  5. */
  6. function show_hide_clauses($thisDropdown)
  7. {
  8. if ($thisDropdown.val() === '') {
  9. $thisDropdown.parent().nextAll('span').hide();
  10. } else {
  11. if ($thisDropdown.is('select[name^="destination_foreign_column"]')) {
  12. $thisDropdown.parent().nextAll('span').show();
  13. }
  14. }
  15. }
  16. /**
  17. * Sets dropdown options to values
  18. */
  19. function setDropdownValues($dropdown, values) {
  20. $dropdown.empty();
  21. var optionsAsString = '';
  22. // add an empty string to the beginning for empty selection
  23. values.unshift('');
  24. $.each(values, function () {
  25. optionsAsString += "<option value='" + this + "'>" + this + "</option>";
  26. });
  27. $dropdown.append($(optionsAsString));
  28. }
  29. /**
  30. * Retrieves and populates dropdowns to the left based on the selected value
  31. *
  32. * @param $dropdown the dropdown whose value got changed
  33. */
  34. function getDropdownValues($dropdown) {
  35. var foreignDb = null, foreignTable = null;
  36. var $tableDd, $columnDd;
  37. var foreign = '';
  38. // if the changed dropdown is for foreign key constraints
  39. if ($dropdown.is('select[name^="destination_foreign"]')) {
  40. $tableDd = $dropdown.parent().find('select[name^="destination_foreign_table"]');
  41. $columnDd = $dropdown.parent().find('select[name^="destination_foreign_column"]');
  42. foreign = '_foreign';
  43. } else { // internal relations
  44. $tableDd = $dropdown.parent().find('select[name^="destination_table"]');
  45. $columnDd = $dropdown.parent().find('select[name^="destination_column"]');
  46. }
  47. // if the changed dropdown is a database selector
  48. if ($dropdown.is('select[name^="destination' + foreign + '_db"]')) {
  49. foreignDb = $dropdown.val();
  50. // if no database is selected empty table and column dropdowns
  51. if (foreignDb === '') {
  52. setDropdownValues($tableDd, []);
  53. setDropdownValues($columnDd, []);
  54. return;
  55. }
  56. } else { // if a table selector
  57. foreignDb = $dropdown.parent()
  58. .find('select[name^="destination' + foreign + '_db"]').val();
  59. foreignTable = $dropdown.val();
  60. // if no table is selected empty the column dropdown
  61. if (foreignTable === '') {
  62. setDropdownValues($columnDd, []);
  63. return;
  64. }
  65. }
  66. var $msgbox = PMA_ajaxShowMessage();
  67. var $form = $dropdown.parents('form');
  68. var url = 'tbl_relation.php?getDropdownValues=true&ajax_request=true' +
  69. '&token=' + $form.find('input[name="token"]').val() +
  70. '&db=' + $form.find('input[name="db"]').val() +
  71. '&table=' + $form.find('input[name="table"]').val() +
  72. '&foreign=' + (foreign !== '') +
  73. '&foreignDb=' + encodeURIComponent(foreignDb) +
  74. (foreignTable !== null ?
  75. '&foreignTable=' + encodeURIComponent(foreignTable) : ''
  76. );
  77. var $server = $form.find('input[name="server"]');
  78. if ($server.length > 0) {
  79. url += '&server=' + $form.find('input[name="server"]').val();
  80. }
  81. $.ajax({
  82. url: url,
  83. datatype: 'json',
  84. success: function (data) {
  85. PMA_ajaxRemoveMessage($msgbox);
  86. if (data.success) {
  87. // if the changed dropdown is a database selector
  88. if (foreignTable === null) {
  89. // set values for table and column dropdowns
  90. setDropdownValues($tableDd, data.tables);
  91. setDropdownValues($columnDd, []);
  92. } else { // if a table selector
  93. // set values for the column dropdown
  94. setDropdownValues($columnDd, data.columns);
  95. }
  96. } else {
  97. PMA_ajaxShowMessage(data.error, false);
  98. }
  99. }
  100. });
  101. }
  102. /**
  103. * Unbind all event handlers before tearing down a page
  104. */
  105. AJAX.registerTeardown('tbl_relation.js', function () {
  106. $('select[name^="destination_foreign"]').unbind('change');
  107. $('select[name^="destination_db"],' +
  108. ' select[name^="destination_table"],' +
  109. ' select[name^="destination_foreign_db"],' +
  110. ' select[name^="destination_foreign_table"]'
  111. ).unbind('change');
  112. });
  113. AJAX.registerOnload('tbl_relation.js', function () {
  114. // initial display
  115. $('select[name^="destination_foreign_column"]').each(function (index, one_dropdown) {
  116. show_hide_clauses($(one_dropdown));
  117. });
  118. // change
  119. $('select[name^="destination_foreign"]').change(function () {
  120. show_hide_clauses($(this));
  121. });
  122. $('select[name^="destination_db"],' +
  123. ' select[name^="destination_table"],' +
  124. ' select[name^="destination_foreign_db"],' +
  125. ' select[name^="destination_foreign_table"]'
  126. ).change(function () {
  127. getDropdownValues($(this));
  128. });
  129. });