1 /* vim: set expandtab sw=4 ts=4 sts=4: */
6 function show_hide_clauses ($thisDropdown) {
7 if ($thisDropdown.val() === '') {
8 $thisDropdown.parent().nextAll('span').hide();
10 if ($thisDropdown.is('select[name^="destination_foreign_column"]')) {
11 $thisDropdown.parent().nextAll('span').show();
17 * Sets dropdown options to values
19 function setDropdownValues ($dropdown, values, selectedValue) {
21 var optionsAsString = '';
22 // add an empty string to the beginning for empty selection
24 $.each(values, function () {
25 optionsAsString += '<option value=\'' + escapeHtml(this) + '\'' + (selectedValue === this ? ' selected=\'selected\'' : '') + '>' + escapeHtml(this) + '</option>';
27 $dropdown.append($(optionsAsString));
31 * Retrieves and populates dropdowns to the left based on the selected value
33 * @param $dropdown the dropdown whose value got changed
35 function getDropdownValues ($dropdown) {
37 var foreignTable = null;
42 // if the changed dropdown is for foreign key constraints
43 if ($dropdown.is('select[name^="destination_foreign"]')) {
44 $databaseDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_db"]');
45 $tableDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_table"]');
46 $columnDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_column"]');
48 } else { // internal relations
49 $databaseDd = $dropdown.parent().find('select[name^="destination_db"]');
50 $tableDd = $dropdown.parent().find('select[name^="destination_table"]');
51 $columnDd = $dropdown.parent().find('select[name^="destination_column"]');
54 // if the changed dropdown is a database selector
55 if ($dropdown.is('select[name^="destination' + foreign + '_db"]')) {
56 foreignDb = $dropdown.val();
57 // if no database is selected empty table and column dropdowns
58 if (foreignDb === '') {
59 setDropdownValues($tableDd, []);
60 setDropdownValues($columnDd, []);
63 } else { // if a table selector
64 foreignDb = $databaseDd.val();
65 foreignTable = $dropdown.val();
66 // if no table is selected empty the column dropdown
67 if (foreignTable === '') {
68 setDropdownValues($columnDd, []);
72 var $msgbox = PMA_ajaxShowMessage();
73 var $form = $dropdown.parents('form');
74 var url = 'tbl_relation.php?getDropdownValues=true&ajax_request=true' +
75 '&db=' + $form.find('input[name="db"]').val() +
76 '&table=' + $form.find('input[name="table"]').val() +
77 '&foreign=' + (foreign !== '') +
78 '&foreignDb=' + encodeURIComponent(foreignDb) +
79 (foreignTable !== null ?
80 '&foreignTable=' + encodeURIComponent(foreignTable) : ''
82 var $server = $form.find('input[name="server"]');
83 if ($server.length > 0) {
84 url += '&server=' + $form.find('input[name="server"]').val();
89 success: function (data) {
90 PMA_ajaxRemoveMessage($msgbox);
91 if (typeof data !== 'undefined' && data.success) {
92 // if the changed dropdown is a database selector
93 if (foreignTable === null) {
94 // set values for table and column dropdowns
95 setDropdownValues($tableDd, data.tables);
96 setDropdownValues($columnDd, []);
97 } else { // if a table selector
98 // set values for the column dropdown
100 if (typeof data.primary !== 'undefined'
101 && 1 === data.primary.length
103 primary = data.primary[0];
105 setDropdownValues($columnDd.first(), data.columns, primary);
106 setDropdownValues($columnDd.slice(1), data.columns);
109 PMA_ajaxShowMessage(data.error, false);
116 * Unbind all event handlers before tearing down a page
118 AJAX.registerTeardown('tbl_relation.js', function () {
119 $('body').off('change',
120 'select[name^="destination_db"], ' +
121 'select[name^="destination_table"], ' +
122 'select[name^="destination_foreign_db"], ' +
123 'select[name^="destination_foreign_table"]'
125 $('body').off('click', 'a.add_foreign_key_field');
126 $('body').off('click', 'a.add_foreign_key');
127 $('a.drop_foreign_key_anchor.ajax').off('click');
130 AJAX.registerOnload('tbl_relation.js', function () {
132 * Ajax event handler to fetch table/column dropdown values.
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"]',
140 getDropdownValues($(this));
145 * Ajax event handler to add a column to a foreign key constraint.
147 $('body').on('click', 'a.add_foreign_key_field', function (event) {
148 event.preventDefault();
149 event.stopPropagation();
155 .insertBefore($(this))
159 // Add foreign field.
160 var $source_elem = $('select[name^="destination_foreign_column[' +
161 $(this).attr('data-index') + ']"]:last').parent();
164 .insertAfter($source_elem)
170 * Ajax event handler to add a foreign key constraint.
172 $('body').on('click', 'a.add_foreign_key', function (event) {
173 event.preventDefault();
174 event.stopPropagation();
176 var $prev_row = $(this).closest('tr').prev('tr');
177 var $newRow = $prev_row.clone(true, true);
179 // Update serial number.
180 var curr_index = $newRow
181 .find('a.add_foreign_key_field')
183 var new_index = parseInt(curr_index) + 1;
184 $newRow.find('a.add_foreign_key_field').attr('data-index', new_index);
186 // Update form parameter names.
187 $newRow.find('select[name^="foreign_key_fields_name"]:not(:first), ' +
188 'select[name^="destination_foreign_column"]:not(:first)'
190 $(this).parent().remove();
192 $newRow.find('input, select').each(function () {
194 $(this).attr('name').replace(/\d/, new_index)
197 $newRow.find('input[type="text"]').each(function () {
200 // Finally add the row.
201 $newRow.insertAfter($prev_row);
205 * Ajax Event handler for 'Drop Foreign key'
207 $('a.drop_foreign_key_anchor.ajax').on('click', function (event) {
208 event.preventDefault();
209 var $anchor = $(this);
211 // Object containing reference to the current field's row
212 var $curr_row = $anchor.parents('tr');
214 var drop_query = escapeHtml(
215 $curr_row.children('td')
216 .children('.drop_foreign_key_msg')
220 var question = PMA_sprintf(PMA_messages.strDoYouReally, drop_query);
222 $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
223 var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingForeignKey, false);
225 'is_js_confirmed': 1,
228 $.post(url, params, function (data) {
229 if (data.success === true) {
230 PMA_ajaxRemoveMessage($msg);
231 PMA_commonActions.refreshMain(false, function () {
235 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false);
238 }); // end $.PMA_confirm()
239 }); // end Drop Foreign key
241 var windowwidth = $(window).width();
242 $('.jsresponsive').css('max-width', (windowwidth - 35) + 'px');