relation.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. /**
  3. * for table relation
  4. */
  5. var TableRelation = {};
  6. TableRelation.showHideClauses = function ($thisDropdown) {
  7. if ($thisDropdown.val() === '') {
  8. $thisDropdown.parent().nextAll('span').hide();
  9. } else {
  10. if ($thisDropdown.is('select[name^="destination_foreign_column"]')) {
  11. $thisDropdown.parent().nextAll('span').show();
  12. }
  13. }
  14. };
  15. /**
  16. * Sets dropdown options to values
  17. */
  18. TableRelation.setDropdownValues = function ($dropdown, values, selectedValue) {
  19. $dropdown.empty();
  20. var optionsAsString = ''; // add an empty string to the beginning for empty selection
  21. values.unshift('');
  22. $.each(values, function () {
  23. optionsAsString += '<option value=\'' + Functions.escapeHtml(this) + '\'' + (selectedValue === Functions.escapeHtml(this) ? ' selected=\'selected\'' : '') + '>' + Functions.escapeHtml(this) + '</option>';
  24. });
  25. $dropdown.append($(optionsAsString));
  26. };
  27. /**
  28. * Retrieves and populates dropdowns to the left based on the selected value
  29. *
  30. * @param $dropdown the dropdown whose value got changed
  31. */
  32. TableRelation.getDropdownValues = function ($dropdown) {
  33. var foreignDb = null;
  34. var foreignTable = null;
  35. var $databaseDd;
  36. var $tableDd;
  37. var $columnDd;
  38. var foreign = ''; // if the changed dropdown is for foreign key constraints
  39. if ($dropdown.is('select[name^="destination_foreign"]')) {
  40. $databaseDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_db"]');
  41. $tableDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_table"]');
  42. $columnDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_column"]');
  43. foreign = '_foreign';
  44. } else {
  45. // internal relations
  46. $databaseDd = $dropdown.parent().find('select[name^="destination_db"]');
  47. $tableDd = $dropdown.parent().find('select[name^="destination_table"]');
  48. $columnDd = $dropdown.parent().find('select[name^="destination_column"]');
  49. } // if the changed dropdown is a database selector
  50. if ($dropdown.is('select[name^="destination' + foreign + '_db"]')) {
  51. foreignDb = $dropdown.val(); // if no database is selected empty table and column dropdowns
  52. if (foreignDb === '') {
  53. TableRelation.setDropdownValues($tableDd, []);
  54. TableRelation.setDropdownValues($columnDd, []);
  55. return;
  56. }
  57. } else {
  58. // if a table selector
  59. foreignDb = $databaseDd.val();
  60. foreignTable = $dropdown.val(); // if no table is selected empty the column dropdown
  61. if (foreignTable === '') {
  62. TableRelation.setDropdownValues($columnDd, []);
  63. return;
  64. }
  65. }
  66. var $msgbox = Functions.ajaxShowMessage();
  67. var $form = $dropdown.parents('form');
  68. var $db = $form.find('input[name="db"]').val();
  69. var $table = $form.find('input[name="table"]').val();
  70. var argsep = CommonParams.get('arg_separator');
  71. var params = 'getDropdownValues=true' + argsep + 'ajax_request=true' + argsep + 'db=' + encodeURIComponent($db) + argsep + 'table=' + encodeURIComponent($table) + argsep + 'foreign=' + (foreign !== '') + argsep + 'foreignDb=' + encodeURIComponent(foreignDb) + (foreignTable !== null ? argsep + 'foreignTable=' + encodeURIComponent(foreignTable) : '');
  72. var $server = $form.find('input[name="server"]');
  73. if ($server.length > 0) {
  74. params += argsep + 'server=' + $form.find('input[name="server"]').val();
  75. }
  76. $.ajax({
  77. type: 'POST',
  78. url: 'index.php?route=/table/relation',
  79. data: params,
  80. dataType: 'json',
  81. success: function success(data) {
  82. Functions.ajaxRemoveMessage($msgbox);
  83. if (typeof data !== 'undefined' && data.success) {
  84. // if the changed dropdown is a database selector
  85. if (foreignTable === null) {
  86. // set values for table and column dropdowns
  87. TableRelation.setDropdownValues($tableDd, data.tables);
  88. TableRelation.setDropdownValues($columnDd, []);
  89. } else {
  90. // if a table selector
  91. // set values for the column dropdown
  92. var primary = null;
  93. if (typeof data.primary !== 'undefined' && 1 === data.primary.length) {
  94. primary = data.primary[0];
  95. }
  96. TableRelation.setDropdownValues($columnDd.first(), data.columns, primary);
  97. TableRelation.setDropdownValues($columnDd.slice(1), data.columns);
  98. }
  99. } else {
  100. Functions.ajaxShowMessage(data.error, false);
  101. }
  102. }
  103. });
  104. };
  105. /**
  106. * Unbind all event handlers before tearing down a page
  107. */
  108. AJAX.registerTeardown('table/relation.js', function () {
  109. $('body').off('change', 'select[name^="destination_db"], ' + 'select[name^="destination_table"], ' + 'select[name^="destination_foreign_db"], ' + 'select[name^="destination_foreign_table"]');
  110. $('body').off('click', 'a.add_foreign_key_field');
  111. $('body').off('click', 'a.add_foreign_key');
  112. $('a.drop_foreign_key_anchor.ajax').off('click');
  113. });
  114. AJAX.registerOnload('table/relation.js', function () {
  115. /**
  116. * Ajax event handler to fetch table/column dropdown values.
  117. */
  118. $('body').on('change', 'select[name^="destination_db"], ' + 'select[name^="destination_table"], ' + 'select[name^="destination_foreign_db"], ' + 'select[name^="destination_foreign_table"]', function () {
  119. TableRelation.getDropdownValues($(this));
  120. });
  121. /**
  122. * Ajax event handler to add a column to a foreign key constraint.
  123. */
  124. $('body').on('click', 'a.add_foreign_key_field', function (event) {
  125. event.preventDefault();
  126. event.stopPropagation(); // Add field.
  127. $(this).prev('span').clone(true, true).insertBefore($(this)).find('select').val(''); // Add foreign field.
  128. var $sourceElem = $('select[name^="destination_foreign_column[' + $(this).attr('data-index') + ']"]').last().parent();
  129. $sourceElem.clone(true, true).insertAfter($sourceElem).find('select').val('');
  130. });
  131. /**
  132. * Ajax event handler to add a foreign key constraint.
  133. */
  134. $('body').on('click', 'a.add_foreign_key', function (event) {
  135. event.preventDefault();
  136. event.stopPropagation();
  137. var $prevRow = $(this).closest('tr').prev('tr');
  138. var $newRow = $prevRow.clone(true, true); // Update serial number.
  139. var currIndex = $newRow.find('a.add_foreign_key_field').attr('data-index');
  140. var newIndex = parseInt(currIndex) + 1;
  141. $newRow.find('a.add_foreign_key_field').attr('data-index', newIndex); // Update form parameter names.
  142. $newRow.find('select[name^="foreign_key_fields_name"]').not($newRow.find('select[name^="foreign_key_fields_name"]').first()).find('select[name^="destination_foreign_column"]').not($newRow.find('select[name^="foreign_key_fields_name"]').not($newRow.find('select[name^="foreign_key_fields_name"]').first()).find('select[name^="destination_foreign_column"]').first()).each(function () {
  143. $(this).parent().remove();
  144. });
  145. $newRow.find('input, select').each(function () {
  146. $(this).attr('name', $(this).attr('name').replace(/\d/, newIndex));
  147. });
  148. $newRow.find('input[type="text"]').each(function () {
  149. $(this).val('');
  150. }); // Finally add the row.
  151. $newRow.insertAfter($prevRow);
  152. });
  153. /**
  154. * Ajax Event handler for 'Drop Foreign key'
  155. */
  156. $('a.drop_foreign_key_anchor.ajax').on('click', function (event) {
  157. event.preventDefault();
  158. var $anchor = $(this); // Object containing reference to the current field's row
  159. var $currRow = $anchor.parents('tr');
  160. var dropQuery = Functions.escapeHtml($currRow.children('td').children('.drop_foreign_key_msg').val());
  161. var question = Functions.sprintf(Messages.strDoYouReally, dropQuery);
  162. $anchor.confirm(question, $anchor.attr('href'), function (url) {
  163. var $msg = Functions.ajaxShowMessage(Messages.strDroppingForeignKey, false);
  164. var params = Functions.getJsConfirmCommonParam(this, $anchor.getPostData());
  165. $.post(url, params, function (data) {
  166. if (data.success === true) {
  167. Functions.ajaxRemoveMessage($msg);
  168. CommonActions.refreshMain(false, function () {// Do nothing
  169. });
  170. } else {
  171. Functions.ajaxShowMessage(Messages.strErrorProcessingRequest + ' : ' + data.error, false);
  172. }
  173. }); // end $.post()
  174. });
  175. }); // end Drop Foreign key
  176. var windowWidth = $(window).width();
  177. $('.jsresponsive').css('max-width', windowWidth - 35 + 'px');
  178. });