Translated using Weblate (Interlingua)
[phpmyadmin.git] / js / tbl_relation.js
blob5c3a2033591a1546b2eb0e49eeb27a5ab81a15a1
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     }
16 /**
17  * Sets dropdown options to values
18  */
19 function setDropdownValues ($dropdown, values, selectedValue) {
20     $dropdown.empty();
21     var optionsAsString = '';
22     // add an empty string to the beginning for empty selection
23     values.unshift('');
24     $.each(values, function () {
25         optionsAsString += '<option value=\'' + escapeHtml(this) + '\'' + (selectedValue === this ? ' selected=\'selected\'' : '') + '>' + escapeHtml(this) + '</option>';
26     });
27     $dropdown.append($(optionsAsString));
30 /**
31  * Retrieves and populates dropdowns to the left based on the selected value
32  *
33  * @param $dropdown the dropdown whose value got changed
34  */
35 function getDropdownValues ($dropdown) {
36     var foreignDb = null;
37     var foreignTable = null;
38     var $databaseDd;
39     var $tableDd;
40     var $columnDd;
41     var foreign = '';
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"]');
47         foreign = '_foreign';
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"]');
52     }
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, []);
61             return;
62         }
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, []);
69             return;
70         }
71     }
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) : ''
81         );
82     var $server = $form.find('input[name="server"]');
83     if ($server.length > 0) {
84         url += '&server=' + $form.find('input[name="server"]').val();
85     }
86     $.ajax({
87         url: url,
88         datatype: 'json',
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
99                     var primary = null;
100                     if (typeof data.primary !== 'undefined'
101                         && 1 === data.primary.length
102                     ) {
103                         primary = data.primary[0];
104                     }
105                     setDropdownValues($columnDd.first(), data.columns, primary);
106                     setDropdownValues($columnDd.slice(1), data.columns);
107                 }
108             } else {
109                 PMA_ajaxShowMessage(data.error, false);
110             }
111         }
112     });
116  * Unbind all event handlers before tearing down a page
117  */
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"]'
124     );
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 () {
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     );
144     /**
145      * Ajax event handler to add a column to a foreign key constraint.
146      */
147     $('body').on('click', 'a.add_foreign_key_field', function (event) {
148         event.preventDefault();
149         event.stopPropagation();
151         // Add field.
152         $(this)
153             .prev('span')
154             .clone(true, true)
155             .insertBefore($(this))
156             .find('select')
157             .val('');
159         // Add foreign field.
160         var $source_elem = $('select[name^="destination_foreign_column[' +
161             $(this).attr('data-index') + ']"]:last').parent();
162         $source_elem
163             .clone(true, true)
164             .insertAfter($source_elem)
165             .find('select')
166             .val('');
167     });
169     /**
170      * Ajax event handler to add a foreign key constraint.
171      */
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')
182             .attr('data-index');
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)'
189         ).each(function () {
190             $(this).parent().remove();
191         });
192         $newRow.find('input, select').each(function () {
193             $(this).attr('name',
194                 $(this).attr('name').replace(/\d/, new_index)
195             );
196         });
197         $newRow.find('input[type="text"]').each(function () {
198             $(this).val('');
199         });
200         // Finally add the row.
201         $newRow.insertAfter($prev_row);
202     });
204     /**
205      * Ajax Event handler for 'Drop Foreign key'
206      */
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')
217                 .val()
218         );
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);
224             var params = {
225                 'is_js_confirmed': 1,
226                 'ajax_request': true
227             };
228             $.post(url, params, function (data) {
229                 if (data.success === true) {
230                     PMA_ajaxRemoveMessage($msg);
231                     PMA_commonActions.refreshMain(false, function () {
232                         // Do nothing
233                     });
234                 } else {
235                     PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false);
236                 }
237             }); // end $.post()
238         }); // end $.PMA_confirm()
239     }); // end Drop Foreign key
241     var windowwidth = $(window).width();
242     $('.jsresponsive').css('max-width', (windowwidth - 35) + 'px');