1 /* vim: set expandtab sw=4 ts=4 sts=4: */
6 function show_hide_clauses($thisDropdown)
8 if ($thisDropdown.val() === '') {
9 $thisDropdown.parent().nextAll('span').hide();
11 if ($thisDropdown.is('select[name^="destination_foreign_column"]')) {
12 $thisDropdown.parent().nextAll('span').show();
18 * Sets dropdown options to values
20 function setDropdownValues($dropdown, values, selectedValue) {
22 var optionsAsString = '';
23 // add an empty string to the beginning for empty selection
25 $.each(values, function () {
26 optionsAsString += "<option value='" + this + "'" + (selectedValue == this ? " selected='selected'" : "") + ">" + this + "</option>";
28 $dropdown.append($(optionsAsString));
32 * Retrieves and populates dropdowns to the left based on the selected value
34 * @param $dropdown the dropdown whose value got changed
36 function getDropdownValues($dropdown) {
37 var foreignDb = null, foreignTable = null;
38 var $tableDd, $columnDd;
40 // if the changed dropdown is for foreign key constraints
41 if ($dropdown.is('select[name^="destination_foreign"]')) {
42 $tableDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_table"]');
43 $columnDd = $dropdown.parent().parent().parent().find('select[name^="destination_foreign_column"]');
45 } else { // internal relations
46 $tableDd = $dropdown.parent().find('select[name^="destination_table"]');
47 $columnDd = $dropdown.parent().find('select[name^="destination_column"]');
50 // if the changed dropdown is a database selector
51 if ($dropdown.is('select[name^="destination' + foreign + '_db"]')) {
52 foreignDb = $dropdown.val();
53 // if no database is selected empty table and column dropdowns
54 if (foreignDb === '') {
55 setDropdownValues($tableDd, []);
56 setDropdownValues($columnDd, []);
59 } else { // if a table selector
60 foreignDb = $dropdown.parent().parent().parent()
61 .find('select[name^="destination' + foreign + '_db"]').val();
62 foreignTable = $dropdown.val();
63 // if no table is selected empty the column dropdown
64 if (foreignTable === '') {
65 setDropdownValues($columnDd, []);
69 var $msgbox = PMA_ajaxShowMessage();
70 var $form = $dropdown.parents('form');
71 var url = 'tbl_relation.php?getDropdownValues=true&ajax_request=true' +
72 '&token=' + $form.find('input[name="token"]').val() +
73 '&db=' + $form.find('input[name="db"]').val() +
74 '&table=' + $form.find('input[name="table"]').val() +
75 '&foreign=' + (foreign !== '') +
76 '&foreignDb=' + encodeURIComponent(foreignDb) +
77 (foreignTable !== null ?
78 '&foreignTable=' + encodeURIComponent(foreignTable) : ''
80 var $server = $form.find('input[name="server"]');
81 if ($server.length > 0) {
82 url += '&server=' + $form.find('input[name="server"]').val();
87 success: function (data) {
88 PMA_ajaxRemoveMessage($msgbox);
89 if (typeof data !== 'undefined' && data.success) {
90 // if the changed dropdown is a database selector
91 if (foreignTable === null) {
92 // set values for table and column dropdowns
93 setDropdownValues($tableDd, data.tables);
94 setDropdownValues($columnDd, []);
95 } else { // if a table selector
96 // set values for the column dropdown
98 if (typeof data.primary !== 'undefined'
99 && 1 === data.primary.length
101 primary = data.primary[0];
103 setDropdownValues($columnDd, data.columns, primary);
106 PMA_ajaxShowMessage(data.error, false);
113 * Unbind all event handlers before tearing down a page
115 AJAX.registerTeardown('tbl_relation.js', function () {
116 $('body').off('change',
117 'select[name^="destination_db"], ' +
118 'select[name^="destination_table"], ' +
119 'select[name^="destination_foreign_db"], ' +
120 'select[name^="destination_foreign_table"]'
122 $('body').off('click', 'a.add_foreign_key_field');
123 $('body').off('click', 'a.add_foreign_key');
124 $('a.drop_foreign_key_anchor.ajax').off('click');
127 AJAX.registerOnload('tbl_relation.js', function () {
130 * Ajax event handler to fetch table/column dropdown values.
132 $('body').on('change',
133 'select[name^="destination_db"], ' +
134 'select[name^="destination_table"], ' +
135 'select[name^="destination_foreign_db"], ' +
136 'select[name^="destination_foreign_table"]',
138 getDropdownValues($(this));
143 * Ajax event handler to add a column to a foreign key constraint.
145 $('body').on('click', 'a.add_foreign_key_field', function (event) {
146 event.preventDefault();
147 event.stopPropagation();
153 .insertBefore($(this))
157 // Add foreign field.
158 var $source_elem = $('select[name^="destination_foreign_column[' +
159 $(this).attr('data-index') + ']"]:last').parent();
162 .insertAfter($source_elem)
168 * Ajax event handler to add a foreign key constraint.
170 $('body').on('click', 'a.add_foreign_key', function (event) {
171 event.preventDefault();
172 event.stopPropagation();
174 var $prev_row = $(this).closest('tr').prev('tr');
175 var odd_even = ($prev_row.attr('class') == 'odd') ? 'even' : 'odd';
176 var $new_row = $prev_row.clone(true, true).attr('class', odd_even);
178 // Update serial number.
179 var curr_index = $new_row
180 .find('a.add_foreign_key_field')
182 var new_index = parseInt(curr_index) + 1;
183 $new_row.find('a.add_foreign_key_field').attr('data-index', new_index);
185 // Update form parameter names.
186 $new_row.find('select[name^="foreign_key_fields_name"]:not(:first), ' +
187 'select[name^="destination_foreign_column"]:not(:first)'
189 $(this).parent().remove();
191 $new_row.find('input, select').each(function () {
193 $(this).attr('name').replace(/\d/, new_index)
197 // Finally add the row.
198 $new_row.insertAfter($prev_row);
202 * Ajax Event handler for 'Drop Foreign key'
204 $('a.drop_foreign_key_anchor.ajax').on('click', function (event) {
205 event.preventDefault();
206 var $anchor = $(this);
208 // Object containing reference to the current field's row
209 var $curr_row = $anchor.parents('tr');
211 var drop_query = escapeHtml(
212 $curr_row.children('td')
213 .children('.drop_foreign_key_msg')
217 var question = PMA_sprintf(PMA_messages.strDoYouReally, drop_query);
219 $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
220 var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingForeignKey, false);
221 $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function (data) {
222 if (data.success === true) {
223 PMA_ajaxRemoveMessage($msg);
224 PMA_commonActions.refreshMain(false, function () {
228 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
231 }); // end $.PMA_confirm()
232 }); //end Drop Foreign key