UPDATE 4.4.0.0
[phpmyadmin.git] / js / tbl_relation.js
blobfd35443d401df152cc5c57757b55bc61d3671939
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='" + this + "'" + (selectedValue == this ? " selected='selected'" : "") + ">" + 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 $tableDd, $columnDd;
39     var foreign = '';
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"]');
44         foreign = '_foreign';
45     } else { // internal relations
46         $tableDd  = $dropdown.parent().find('select[name^="destination_table"]');
47         $columnDd = $dropdown.parent().find('select[name^="destination_column"]');
48     }
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, []);
57             return;
58         }
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, []);
66             return;
67         }
68     }
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) : ''
79         );
80     var $server = $form.find('input[name="server"]');
81     if ($server.length > 0) {
82         url += '&server=' + $form.find('input[name="server"]').val();
83     }
84     $.ajax({
85         url: url,
86         datatype: 'json',
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
97                     var primary = null;
98                     if (typeof data.primary !== 'undefined'
99                         && 1 === data.primary.length
100                     ) {
101                         primary = data.primary[0];
102                     }
103                     setDropdownValues($columnDd, data.columns, primary);
104                 }
105             } else {
106                 PMA_ajaxShowMessage(data.error, false);
107             }
108         }
109     });
113  * Unbind all event handlers before tearing down a page
114  */
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"]'
121     );
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 () {
129     /**
130      * Ajax event handler to fetch table/column dropdown values.
131      */
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"]',
137         function () {
138             getDropdownValues($(this));
139         }
140     );
142     /**
143      * Ajax event handler to add a column to a foreign key constraint.
144      */
145     $('body').on('click', 'a.add_foreign_key_field', function (event) {
146         event.preventDefault();
147         event.stopPropagation();
149         // Add field.
150         $(this)
151         .prev('span')
152         .clone(true, true)
153         .insertBefore($(this))
154         .find('select')
155         .val('');
157         // Add foreign field.
158         var $source_elem = $('select[name^="destination_foreign_column[' +
159             $(this).attr('data-index') + ']"]:last').parent();
160         $source_elem
161         .clone(true, true)
162         .insertAfter($source_elem)
163         .find('select')
164         .val('');
165     });
167     /**
168      * Ajax event handler to add a foreign key constraint.
169      */
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')
181             .attr('data-index');
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)'
188         ).each(function () {
189             $(this).parent().remove();
190         });
191         $new_row.find('input, select').each(function () {
192             $(this).attr('name',
193                 $(this).attr('name').replace(/\d/, new_index)
194             );
195         });
197         // Finally add the row.
198         $new_row.insertAfter($prev_row);
199     });
201     /**
202      * Ajax Event handler for 'Drop Foreign key'
203      */
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')
214                 .val()
215         );
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 () {
225                         // Do nothing
226                     });
227                 } else {
228                     PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
229                 }
230             }); // end $.get()
231         }); // end $.PMA_confirm()
232     }); //end Drop Foreign key