Merge #15685 - Fix #15682 - Timestamp method not taking current time
[phpmyadmin.git] / js / tbl_relation.js
blobd7e87857f743805ea4edbaee46e8422e74b43aa3
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 === escapeHtml(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 $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) : ''
84         );
85     var $server = $form.find('input[name="server"]');
86     if ($server.length > 0) {
87         params += argsep + 'server=' + $form.find('input[name="server"]').val();
88     }
89     $.ajax({
90         type: 'POST',
91         url: 'tbl_relation.php',
92         data: params,
93         dataType: 'json',
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
104                     var primary = null;
105                     if (typeof data.primary !== 'undefined'
106                         && 1 === data.primary.length
107                     ) {
108                         primary = data.primary[0];
109                     }
110                     setDropdownValues($columnDd.first(), data.columns, primary);
111                     setDropdownValues($columnDd.slice(1), data.columns);
112                 }
113             } else {
114                 PMA_ajaxShowMessage(data.error, false);
115             }
116         }
117     });
121  * Unbind all event handlers before tearing down a page
122  */
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"]'
129     );
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 () {
136     /**
137      * Ajax event handler to fetch table/column dropdown values.
138      */
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"]',
144         function () {
145             getDropdownValues($(this));
146         }
147     );
149     /**
150      * Ajax event handler to add a column to a foreign key constraint.
151      */
152     $('body').on('click', 'a.add_foreign_key_field', function (event) {
153         event.preventDefault();
154         event.stopPropagation();
156         // Add field.
157         $(this)
158             .prev('span')
159             .clone(true, true)
160             .insertBefore($(this))
161             .find('select')
162             .val('');
164         // Add foreign field.
165         var $source_elem = $('select[name^="destination_foreign_column[' +
166             $(this).attr('data-index') + ']"]:last').parent();
167         $source_elem
168             .clone(true, true)
169             .insertAfter($source_elem)
170             .find('select')
171             .val('');
172     });
174     /**
175      * Ajax event handler to add a foreign key constraint.
176      */
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')
187             .attr('data-index');
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)'
194         ).each(function () {
195             $(this).parent().remove();
196         });
197         $newRow.find('input, select').each(function () {
198             $(this).attr('name',
199                 $(this).attr('name').replace(/\d/, new_index)
200             );
201         });
202         $newRow.find('input[type="text"]').each(function () {
203             $(this).val('');
204         });
205         // Finally add the row.
206         $newRow.insertAfter($prev_row);
207     });
209     /**
210      * Ajax Event handler for 'Drop Foreign key'
211      */
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')
222                 .val()
223         );
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 () {
234                         // Do nothing
235                     });
236                 } else {
237                     PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false);
238                 }
239             }); // end $.post()
240         }); // end $.PMA_confirm()
241     }); // end Drop Foreign key
243     var windowwidth = $(window).width();
244     $('.jsresponsive').css('max-width', (windowwidth - 35) + 'px');