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 $databaseDd, $tableDd, $columnDd;
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"]');
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"]');
52 // if the changed dropdown is a database selector
53 if ($dropdown.is('select[name^="destination' + foreign + '_db"]')) {
54 foreignDb = $dropdown.val();
55 // if no database is selected empty table and column dropdowns
56 if (foreignDb === '') {
57 setDropdownValues($tableDd, []);
58 setDropdownValues($columnDd, []);
61 } else { // if a table selector
62 foreignDb = $databaseDd.val();
63 foreignTable = $dropdown.val();
64 // if no table is selected empty the column dropdown
65 if (foreignTable === '') {
66 setDropdownValues($columnDd, []);
70 var $msgbox = PMA_ajaxShowMessage();
71 var $form = $dropdown.parents('form');
72 var url = 'tbl_relation.php?getDropdownValues=true&ajax_request=true' +
73 '&token=' + $form.find('input[name="token"]').val() +
74 '&db=' + $form.find('input[name="db"]').val() +
75 '&table=' + $form.find('input[name="table"]').val() +
76 '&foreign=' + (foreign !== '') +
77 '&foreignDb=' + encodeURIComponent(foreignDb) +
78 (foreignTable !== null ?
79 '&foreignTable=' + encodeURIComponent(foreignTable) : ''
81 var $server = $form.find('input[name="server"]');
82 if ($server.length > 0) {
83 url += '&server=' + $form.find('input[name="server"]').val();
88 success: function (data) {
89 PMA_ajaxRemoveMessage($msgbox);
90 if (typeof data !== 'undefined' && data.success) {
91 // if the changed dropdown is a database selector
92 if (foreignTable === null) {
93 // set values for table and column dropdowns
94 setDropdownValues($tableDd, data.tables);
95 setDropdownValues($columnDd, []);
96 } else { // if a table selector
97 // set values for the column dropdown
99 if (typeof data.primary !== 'undefined'
100 && 1 === data.primary.length
102 primary = data.primary[0];
104 setDropdownValues($columnDd, data.columns, primary);
107 PMA_ajaxShowMessage(data.error, false);
114 * Unbind all event handlers before tearing down a page
116 AJAX.registerTeardown('tbl_relation.js', function () {
117 $('body').off('change',
118 'select[name^="destination_db"], ' +
119 'select[name^="destination_table"], ' +
120 'select[name^="destination_foreign_db"], ' +
121 'select[name^="destination_foreign_table"]'
123 $('body').off('click', 'a.add_foreign_key_field');
124 $('body').off('click', 'a.add_foreign_key');
125 $('a.drop_foreign_key_anchor.ajax').off('click');
128 AJAX.registerOnload('tbl_relation.js', function () {
131 * Ajax event handler to fetch table/column dropdown values.
133 $('body').on('change',
134 'select[name^="destination_db"], ' +
135 'select[name^="destination_table"], ' +
136 'select[name^="destination_foreign_db"], ' +
137 'select[name^="destination_foreign_table"]',
139 getDropdownValues($(this));
144 * Ajax event handler to add a column to a foreign key constraint.
146 $('body').on('click', 'a.add_foreign_key_field', function (event) {
147 event.preventDefault();
148 event.stopPropagation();
154 .insertBefore($(this))
158 // Add foreign field.
159 var $source_elem = $('select[name^="destination_foreign_column[' +
160 $(this).attr('data-index') + ']"]:last').parent();
163 .insertAfter($source_elem)
169 * Ajax event handler to add a foreign key constraint.
171 $('body').on('click', 'a.add_foreign_key', function (event) {
172 event.preventDefault();
173 event.stopPropagation();
175 var $prev_row = $(this).closest('tr').prev('tr');
176 var odd_even = ($prev_row.attr('class') == 'odd') ? 'even' : 'odd';
177 var $new_row = $prev_row.clone(true, true).attr('class', odd_even);
179 // Update serial number.
180 var curr_index = $new_row
181 .find('a.add_foreign_key_field')
183 var new_index = parseInt(curr_index) + 1;
184 $new_row.find('a.add_foreign_key_field').attr('data-index', new_index);
186 // Update form parameter names.
187 $new_row.find('select[name^="foreign_key_fields_name"]:not(:first), ' +
188 'select[name^="destination_foreign_column"]:not(:first)'
190 $(this).parent().remove();
192 $new_row.find('input, select').each(function () {
194 $(this).attr('name').replace(/\d/, new_index)
198 // Finally add the row.
199 $new_row.insertAfter($prev_row);
203 * Ajax Event handler for 'Drop Foreign key'
205 $('a.drop_foreign_key_anchor.ajax').on('click', function (event) {
206 event.preventDefault();
207 var $anchor = $(this);
209 // Object containing reference to the current field's row
210 var $curr_row = $anchor.parents('tr');
212 var drop_query = escapeHtml(
213 $curr_row.children('td')
214 .children('.drop_foreign_key_msg')
218 var question = PMA_sprintf(PMA_messages.strDoYouReally, drop_query);
220 $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
221 var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingForeignKey, false);
222 $.get(url, {'is_js_confirmed': 1, 'ajax_request': true}, function (data) {
223 if (data.success === true) {
224 PMA_ajaxRemoveMessage($msg);
225 PMA_commonActions.refreshMain(false, function () {
229 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
232 }); // end $.PMA_confirm()
233 }); //end Drop Foreign key