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 === escapeHtml(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 $db = $form.find('input[name="db"]').val();
75 var $table = $form.find('input[name="table"]').val();
76 var argsep = PMA_commonParams.get('arg_separator');
77 var params = 'getDropdownValues=true' + argsep + 'ajax_request=true' +
78 argsep + 'db=' + encodeURIComponent($db) +
79 argsep + 'table=' + encodeURIComponent($table) +
80 argsep + 'foreign=' + (foreign !== '') +
81 argsep + 'foreignDb=' + encodeURIComponent(foreignDb) +
82 (foreignTable !== null ?
83 argsep + 'foreignTable=' + encodeURIComponent(foreignTable) : ''
85 var $server = $form.find('input[name="server"]');
86 if ($server.length > 0) {
87 params += argsep + 'server=' + $form.find('input[name="server"]').val();
91 url: 'tbl_relation.php',
94 success: function (data) {
95 PMA_ajaxRemoveMessage($msgbox);
96 if (typeof data !== 'undefined' && data.success) {
97 // if the changed dropdown is a database selector
98 if (foreignTable === null) {
99 // set values for table and column dropdowns
100 setDropdownValues($tableDd, data.tables);
101 setDropdownValues($columnDd, []);
102 } else { // if a table selector
103 // set values for the column dropdown
105 if (typeof data.primary !== 'undefined'
106 && 1 === data.primary.length
108 primary = data.primary[0];
110 setDropdownValues($columnDd.first(), data.columns, primary);
111 setDropdownValues($columnDd.slice(1), data.columns);
114 PMA_ajaxShowMessage(data.error, false);
121 * Unbind all event handlers before tearing down a page
123 AJAX.registerTeardown('tbl_relation.js', function () {
124 $('body').off('change',
125 'select[name^="destination_db"], ' +
126 'select[name^="destination_table"], ' +
127 'select[name^="destination_foreign_db"], ' +
128 'select[name^="destination_foreign_table"]'
130 $('body').off('click', 'a.add_foreign_key_field');
131 $('body').off('click', 'a.add_foreign_key');
132 $('a.drop_foreign_key_anchor.ajax').off('click');
135 AJAX.registerOnload('tbl_relation.js', function () {
137 * Ajax event handler to fetch table/column dropdown values.
139 $('body').on('change',
140 'select[name^="destination_db"], ' +
141 'select[name^="destination_table"], ' +
142 'select[name^="destination_foreign_db"], ' +
143 'select[name^="destination_foreign_table"]',
145 getDropdownValues($(this));
150 * Ajax event handler to add a column to a foreign key constraint.
152 $('body').on('click', 'a.add_foreign_key_field', function (event) {
153 event.preventDefault();
154 event.stopPropagation();
160 .insertBefore($(this))
164 // Add foreign field.
165 var $source_elem = $('select[name^="destination_foreign_column[' +
166 $(this).attr('data-index') + ']"]:last').parent();
169 .insertAfter($source_elem)
175 * Ajax event handler to add a foreign key constraint.
177 $('body').on('click', 'a.add_foreign_key', function (event) {
178 event.preventDefault();
179 event.stopPropagation();
181 var $prev_row = $(this).closest('tr').prev('tr');
182 var $newRow = $prev_row.clone(true, true);
184 // Update serial number.
185 var curr_index = $newRow
186 .find('a.add_foreign_key_field')
188 var new_index = parseInt(curr_index) + 1;
189 $newRow.find('a.add_foreign_key_field').attr('data-index', new_index);
191 // Update form parameter names.
192 $newRow.find('select[name^="foreign_key_fields_name"]:not(:first), ' +
193 'select[name^="destination_foreign_column"]:not(:first)'
195 $(this).parent().remove();
197 $newRow.find('input, select').each(function () {
199 $(this).attr('name').replace(/\d/, new_index)
202 $newRow.find('input[type="text"]').each(function () {
205 // Finally add the row.
206 $newRow.insertAfter($prev_row);
210 * Ajax Event handler for 'Drop Foreign key'
212 $('a.drop_foreign_key_anchor.ajax').on('click', function (event) {
213 event.preventDefault();
214 var $anchor = $(this);
216 // Object containing reference to the current field's row
217 var $curr_row = $anchor.parents('tr');
219 var drop_query = escapeHtml(
220 $curr_row.children('td')
221 .children('.drop_foreign_key_msg')
225 var question = PMA_sprintf(PMA_messages.strDoYouReally, drop_query);
227 $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) {
228 var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingForeignKey, false);
229 var params = getJSConfirmCommonParam(this, $anchor.getPostData());
230 $.post(url, params, function (data) {
231 if (data.success === true) {
232 PMA_ajaxRemoveMessage($msg);
233 PMA_commonActions.refreshMain(false, function () {
237 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false);
240 }); // end $.PMA_confirm()
241 }); // end Drop Foreign key
243 var windowwidth = $(window).width();
244 $('.jsresponsive').css('max-width', (windowwidth - 35) + 'px');