tbl_relation.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 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. function setDropdownValues ($dropdown, values, selectedValue) {
  19. $dropdown.empty();
  20. var optionsAsString = '';
  21. // add an empty string to the beginning for empty selection
  22. values.unshift('');
  23. $.each(values, function () {
  24. optionsAsString += '<option value=\'' + escapeHtml(this) + '\'' + (selectedValue === escapeHtml(this) ? ' selected=\'selected\'' : '') + '>' + escapeHtml(this) + '</option>';
  25. });
  26. $dropdown.append($(optionsAsString));
  27. }
  28. /**
  29. * Retrieves and populates dropdowns to the left based on the selected value
  30. *
  31. * @param $dropdown the dropdown whose value got changed
  32. */
  33. function getDropdownValues ($dropdown) {
  34. var foreignDb = null;
  35. var foreignTable = null;
  36. var $databaseDd;
  37. var $tableDd;
  38. var $columnDd;
  39. var foreign = '';
  40. // if the changed dropdown is for foreign key constraints
  41. if ($dropdown.is('select[name^="destination_foreign"]')) {
  42. $databaseDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_db"]');
  43. $tableDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_table"]');
  44. $columnDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_column"]');
  45. foreign = '_foreign';
  46. } else { // internal relations
  47. $databaseDd = $dropdown.parent().find('select[name^="destination_db"]');
  48. $tableDd = $dropdown.parent().find('select[name^="destination_table"]');
  49. $columnDd = $dropdown.parent().find('select[name^="destination_column"]');
  50. }
  51. // if the changed dropdown is a database selector
  52. if ($dropdown.is('select[name^="destination' + foreign + '_db"]')) {
  53. foreignDb = $dropdown.val();
  54. // if no database is selected empty table and column dropdowns
  55. if (foreignDb === '') {
  56. setDropdownValues($tableDd, []);
  57. setDropdownValues($columnDd, []);
  58. return;
  59. }
  60. } else { // if a table selector
  61. foreignDb = $databaseDd.val();
  62. foreignTable = $dropdown.val();
  63. // if no table is selected empty the column dropdown
  64. if (foreignTable === '') {
  65. setDropdownValues($columnDd, []);
  66. return;
  67. }
  68. }
  69. var $msgbox = PMA_ajaxShowMessage();
  70. var $form = $dropdown.parents('form');
  71. var $db = $form.find('input[name="db"]').val();
  72. var $table = $form.find('input[name="table"]').val();
  73. var argsep = PMA_commonParams.get('arg_separator');
  74. var params = 'getDropdownValues=true' + argsep + 'ajax_request=true' +
  75. argsep + 'db=' + encodeURIComponent($db) +
  76. argsep + 'table=' + encodeURIComponent($table) +
  77. argsep + 'foreign=' + (foreign !== '') +
  78. argsep + 'foreignDb=' + encodeURIComponent(foreignDb) +
  79. (foreignTable !== null ?
  80. argsep + 'foreignTable=' + encodeURIComponent(foreignTable) : ''
  81. );
  82. var $server = $form.find('input[name="server"]');
  83. if ($server.length > 0) {
  84. params += argsep + 'server=' + $form.find('input[name="server"]').val();
  85. }
  86. $.ajax({
  87. type: 'POST',
  88. url: 'tbl_relation.php',
  89. data: params,
  90. dataType: 'json',
  91. success: function (data) {
  92. PMA_ajaxRemoveMessage($msgbox);
  93. if (typeof data !== 'undefined' && data.success) {
  94. // if the changed dropdown is a database selector
  95. if (foreignTable === null) {
  96. // set values for table and column dropdowns
  97. setDropdownValues($tableDd, data.tables);
  98. setDropdownValues($columnDd, []);
  99. } else { // if a table selector
  100. // set values for the column dropdown
  101. var primary = null;
  102. if (typeof data.primary !== 'undefined'
  103. && 1 === data.primary.length
  104. ) {
  105. primary = data.primary[0];
  106. }
  107. setDropdownValues($columnDd.first(), data.columns, primary);
  108. setDropdownValues($columnDd.slice(1), data.columns);
  109. }
  110. } else {
  111. PMA_ajaxShowMessage(data.error, false);
  112. }
  113. }
  114. });
  115. }
  116. /**
  117. * Unbind all event handlers before tearing down a page
  118. */
  119. AJAX.registerTeardown('tbl_relation.js', function () {
  120. $('body').off('change',
  121. 'select[name^="destination_db"], ' +
  122. 'select[name^="destination_table"], ' +
  123. 'select[name^="destination_foreign_db"], ' +
  124. 'select[name^="destination_foreign_table"]'
  125. );
  126. $('body').off('click', 'a.add_foreign_key_field');
  127. $('body').off('click', 'a.add_foreign_key');
  128. $('a.drop_foreign_key_anchor.ajax').off('click');
  129. });
  130. AJAX.registerOnload('tbl_relation.js', function () {
  131. /**
  132. * Ajax event handler to fetch table/column dropdown values.
  133. */
  134. $('body').on('change',
  135. 'select[name^="destination_db"], ' +
  136. 'select[name^="destination_table"], ' +
  137. 'select[name^="destination_foreign_db"], ' +
  138. 'select[name^="destination_foreign_table"]',
  139. function () {
  140. getDropdownValues($(this));
  141. }
  142. );
  143. /**
  144. * Ajax event handler to add a column to a foreign key constraint.
  145. */
  146. $('body').on('click', 'a.add_foreign_key_field', function (event) {
  147. event.preventDefault();
  148. event.stopPropagation();
  149. // Add field.
  150. $(this)
  151. .prev('span')
  152. .clone(true, true)
  153. .insertBefore($(this))
  154. .find('select')
  155. .val('');
  156. // Add foreign field.
  157. var $source_elem = $('select[name^="destination_foreign_column[' +
  158. $(this).attr('data-index') + ']"]:last').parent();
  159. $source_elem
  160. .clone(true, true)
  161. .insertAfter($source_elem)
  162. .find('select')
  163. .val('');
  164. });
  165. /**
  166. * Ajax event handler to add a foreign key constraint.
  167. */
  168. $('body').on('click', 'a.add_foreign_key', function (event) {
  169. event.preventDefault();
  170. event.stopPropagation();
  171. var $prev_row = $(this).closest('tr').prev('tr');
  172. var $newRow = $prev_row.clone(true, true);
  173. // Update serial number.
  174. var curr_index = $newRow
  175. .find('a.add_foreign_key_field')
  176. .attr('data-index');
  177. var new_index = parseInt(curr_index) + 1;
  178. $newRow.find('a.add_foreign_key_field').attr('data-index', new_index);
  179. // Update form parameter names.
  180. $newRow.find('select[name^="foreign_key_fields_name"]:not(:first), ' +
  181. 'select[name^="destination_foreign_column"]:not(:first)'
  182. ).each(function () {
  183. $(this).parent().remove();
  184. });
  185. $newRow.find('input, select').each(function () {
  186. $(this).attr('name',
  187. $(this).attr('name').replace(/\d/, new_index)
  188. );
  189. });
  190. $newRow.find('input[type="text"]').each(function () {
  191. $(this).val('');
  192. });
  193. // Finally add the row.
  194. $newRow.insertAfter($prev_row);
  195. });
  196. /**
  197. * Ajax Event handler for 'Drop Foreign key'
  198. */
  199. $('a.drop_foreign_key_anchor.ajax').on('click', function (event) {
  200. event.preventDefault();
  201. var $anchor = $(this);
  202. // Object containing reference to the current field's row
  203. var $curr_row = $anchor.parents('tr');
  204. var drop_query = escapeHtml(
  205. $curr_row.children('td')
  206. .children('.drop_foreign_key_msg')
  207. .val()
  208. );
  209. var question = PMA_sprintf(PMA_messages.strDoYouReally, drop_query);
  210. $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
  211. var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingForeignKey, false);
  212. var params = getJSConfirmCommonParam(this, $anchor.getPostData());
  213. $.post(url, params, function (data) {
  214. if (data.success === true) {
  215. PMA_ajaxRemoveMessage($msg);
  216. PMA_commonActions.refreshMain(false, function () {
  217. // Do nothing
  218. });
  219. } else {
  220. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false);
  221. }
  222. }); // end $.post()
  223. }); // end $.PMA_confirm()
  224. }); // end Drop Foreign key
  225. var windowwidth = $(window).width();
  226. $('.jsresponsive').css('max-width', (windowwidth - 35) + 'px');
  227. });