Translated using Weblate (Interlingua)
[phpmyadmin.git] / js / tbl_relation.js
blob2d0ec5dc37ba20fac6d065ae6a423402ea043d29
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * for tbl_relation.php
4  *
5  */
6 function show_hide_clauses($thisDropdown)
8     if ($thisDropdown.val() === '') {
9         $thisDropdown.parent().nextAll('span').hide();
10     } else {
11         if ($thisDropdown.is('select[name^="destination_foreign_column"]')) {
12             $thisDropdown.parent().nextAll('span').show();
13         }
14     }
17 /**
18  * Sets dropdown options to values
19  */
20 function setDropdownValues($dropdown, values, selectedValue) {
21     $dropdown.empty();
22     var optionsAsString = '';
23     // add an empty string to the beginning for empty selection
24     values.unshift('');
25     $.each(values, function () {
26         optionsAsString += "<option value='" + escapeHtml(this) + "'" + (selectedValue == this ? " selected='selected'" : "") + ">" + escapeHtml(this) + "</option>";
27     });
28     $dropdown.append($(optionsAsString));
31 /**
32  * Retrieves and populates dropdowns to the left based on the selected value
33  *
34  * @param $dropdown the dropdown whose value got changed
35  */
36 function getDropdownValues($dropdown) {
37     var foreignDb = null, foreignTable = null;
38     var $databaseDd, $tableDd, $columnDd;
39     var foreign = '';
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"]');
45         foreign = '_foreign';
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"]');
50     }
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, []);
59             return;
60         }
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, []);
67             return;
68         }
69     }
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) : ''
80         );
81     var $server = $form.find('input[name="server"]');
82     if ($server.length > 0) {
83         url += '&server=' + $form.find('input[name="server"]').val();
84     }
85     $.ajax({
86         url: url,
87         datatype: 'json',
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
98                     var primary = null;
99                     if (typeof data.primary !== 'undefined'
100                         && 1 === data.primary.length
101                     ) {
102                         primary = data.primary[0];
103                     }
104                     setDropdownValues($columnDd, data.columns, primary);
105                 }
106             } else {
107                 PMA_ajaxShowMessage(data.error, false);
108             }
109         }
110     });
114  * Unbind all event handlers before tearing down a page
115  */
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"]'
122     );
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 () {
130     /**
131      * Ajax event handler to fetch table/column dropdown values.
132      */
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"]',
138         function () {
139             getDropdownValues($(this));
140         }
141     );
143     /**
144      * Ajax event handler to add a column to a foreign key constraint.
145      */
146     $('body').on('click', 'a.add_foreign_key_field', function (event) {
147         event.preventDefault();
148         event.stopPropagation();
150         // Add field.
151         $(this)
152         .prev('span')
153         .clone(true, true)
154         .insertBefore($(this))
155         .find('select')
156         .val('');
158         // Add foreign field.
159         var $source_elem = $('select[name^="destination_foreign_column[' +
160             $(this).attr('data-index') + ']"]:last').parent();
161         $source_elem
162         .clone(true, true)
163         .insertAfter($source_elem)
164         .find('select')
165         .val('');
166     });
168     /**
169      * Ajax event handler to add a foreign key constraint.
170      */
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')
182             .attr('data-index');
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)'
189         ).each(function () {
190             $(this).parent().remove();
191         });
192         $new_row.find('input, select').each(function () {
193             $(this).attr('name',
194                 $(this).attr('name').replace(/\d/, new_index)
195             );
196         });
198         // Finally add the row.
199         $new_row.insertAfter($prev_row);
200     });
202     /**
203      * Ajax Event handler for 'Drop Foreign key'
204      */
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')
215                 .val()
216         );
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             var params = {
223                 'is_js_confirmed': 1,
224                 'ajax_request': true,
225                 'token': PMA_commonParams.get('token')
226             };
227             $.post(url, params, function (data) {
228                 if (data.success === true) {
229                     PMA_ajaxRemoveMessage($msg);
230                     PMA_commonActions.refreshMain(false, function () {
231                         // Do nothing
232                     });
233                 } else {
234                     PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
235                 }
236             }); // end $.post()
237         }); // end $.PMA_confirm()
238     }); //end Drop Foreign key