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 | } |
||
14 | } |
||
15 | |||
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)); |
||
28 | } |
||
29 | |||
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 | } |
||
53 | |||
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 argsep = PMA_commonParams.get('arg_separator'); |
||
75 | var url = 'tbl_relation.php?getDropdownValues=true' + argsep + 'ajax_request=true' + |
||
76 | argsep + 'db=' + $form.find('input[name="db"]').val() + |
||
77 | argsep + 'table=' + $form.find('input[name="table"]').val() + |
||
78 | argsep + 'foreign=' + (foreign !== '') + |
||
79 | argsep + 'foreignDb=' + encodeURIComponent(foreignDb) + |
||
80 | (foreignTable !== null ? |
||
81 | argsep + 'foreignTable=' + encodeURIComponent(foreignTable) : '' |
||
82 | ); |
||
83 | var $server = $form.find('input[name="server"]'); |
||
84 | if ($server.length > 0) { |
||
85 | url += argsep + 'server=' + $form.find('input[name="server"]').val(); |
||
86 | } |
||
87 | $.ajax({ |
||
88 | url: url, |
||
89 | datatype: 'json', |
||
90 | success: function (data) { |
||
91 | PMA_ajaxRemoveMessage($msgbox); |
||
92 | if (typeof data !== 'undefined' && data.success) { |
||
93 | // if the changed dropdown is a database selector |
||
94 | if (foreignTable === null) { |
||
95 | // set values for table and column dropdowns |
||
96 | setDropdownValues($tableDd, data.tables); |
||
97 | setDropdownValues($columnDd, []); |
||
98 | } else { // if a table selector |
||
99 | // set values for the column dropdown |
||
100 | var primary = null; |
||
101 | if (typeof data.primary !== 'undefined' |
||
102 | && 1 === data.primary.length |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
103 | ) { |
||
104 | primary = data.primary[0]; |
||
105 | } |
||
106 | setDropdownValues($columnDd.first(), data.columns, primary); |
||
107 | setDropdownValues($columnDd.slice(1), data.columns); |
||
108 | } |
||
109 | } else { |
||
110 | PMA_ajaxShowMessage(data.error, false); |
||
111 | } |
||
112 | } |
||
113 | }); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Unbind all event handlers before tearing down a page |
||
118 | */ |
||
119 | AJAX.registerTeardown('tbl_relation.js', function () { |
||
120 | $('body').off('change', |
||
121 | 'select[name^="destination_db"], ' + |
||
122 | 'select[name^="destination_table"], ' + |
||
123 | 'select[name^="destination_foreign_db"], ' + |
||
124 | 'select[name^="destination_foreign_table"]' |
||
125 | ); |
||
126 | $('body').off('click', 'a.add_foreign_key_field'); |
||
127 | $('body').off('click', 'a.add_foreign_key'); |
||
128 | $('a.drop_foreign_key_anchor.ajax').off('click'); |
||
129 | }); |
||
130 | |||
131 | AJAX.registerOnload('tbl_relation.js', function () { |
||
132 | /** |
||
133 | * Ajax event handler to fetch table/column dropdown values. |
||
134 | */ |
||
135 | $('body').on('change', |
||
136 | 'select[name^="destination_db"], ' + |
||
137 | 'select[name^="destination_table"], ' + |
||
138 | 'select[name^="destination_foreign_db"], ' + |
||
139 | 'select[name^="destination_foreign_table"]', |
||
140 | function () { |
||
141 | getDropdownValues($(this)); |
||
142 | } |
||
143 | ); |
||
144 | |||
145 | /** |
||
146 | * Ajax event handler to add a column to a foreign key constraint. |
||
147 | */ |
||
148 | $('body').on('click', 'a.add_foreign_key_field', function (event) { |
||
149 | event.preventDefault(); |
||
150 | event.stopPropagation(); |
||
151 | |||
152 | // Add field. |
||
153 | $(this) |
||
154 | .prev('span') |
||
155 | .clone(true, true) |
||
156 | .insertBefore($(this)) |
||
157 | .find('select') |
||
158 | .val(''); |
||
159 | |||
160 | // Add foreign field. |
||
161 | var $source_elem = $('select[name^="destination_foreign_column[' + |
||
162 | $(this).attr('data-index') + ']"]:last').parent(); |
||
163 | $source_elem |
||
164 | .clone(true, true) |
||
165 | .insertAfter($source_elem) |
||
166 | .find('select') |
||
167 | .val(''); |
||
168 | }); |
||
169 | |||
170 | /** |
||
171 | * Ajax event handler to add a foreign key constraint. |
||
172 | */ |
||
173 | $('body').on('click', 'a.add_foreign_key', function (event) { |
||
174 | event.preventDefault(); |
||
175 | event.stopPropagation(); |
||
176 | |||
177 | var $prev_row = $(this).closest('tr').prev('tr'); |
||
178 | var $newRow = $prev_row.clone(true, true); |
||
179 | |||
180 | // Update serial number. |
||
181 | var curr_index = $newRow |
||
182 | .find('a.add_foreign_key_field') |
||
183 | .attr('data-index'); |
||
184 | var new_index = parseInt(curr_index) + 1; |
||
185 | $newRow.find('a.add_foreign_key_field').attr('data-index', new_index); |
||
186 | |||
187 | // Update form parameter names. |
||
188 | $newRow.find('select[name^="foreign_key_fields_name"]:not(:first), ' + |
||
189 | 'select[name^="destination_foreign_column"]:not(:first)' |
||
190 | ).each(function () { |
||
191 | $(this).parent().remove(); |
||
192 | }); |
||
193 | $newRow.find('input, select').each(function () { |
||
194 | $(this).attr('name', |
||
195 | $(this).attr('name').replace(/\d/, new_index) |
||
196 | ); |
||
197 | }); |
||
198 | $newRow.find('input[type="text"]').each(function () { |
||
199 | $(this).val(''); |
||
200 | }); |
||
201 | // Finally add the row. |
||
202 | $newRow.insertAfter($prev_row); |
||
203 | }); |
||
204 | |||
205 | /** |
||
206 | * Ajax Event handler for 'Drop Foreign key' |
||
207 | */ |
||
208 | $('a.drop_foreign_key_anchor.ajax').on('click', function (event) { |
||
209 | event.preventDefault(); |
||
210 | var $anchor = $(this); |
||
211 | |||
212 | // Object containing reference to the current field's row |
||
213 | var $curr_row = $anchor.parents('tr'); |
||
214 | |||
215 | var drop_query = escapeHtml( |
||
216 | $curr_row.children('td') |
||
217 | .children('.drop_foreign_key_msg') |
||
218 | .val() |
||
219 | ); |
||
220 | |||
221 | var question = PMA_sprintf(PMA_messages.strDoYouReally, drop_query); |
||
222 | |||
223 | $anchor.PMA_confirm(question, $anchor.attr('href'), function (url) { |
||
224 | var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingForeignKey, false); |
||
225 | var params = getJSConfirmCommonParam(this, $anchor.getPostData()); |
||
226 | $.post(url, params, function (data) { |
||
227 | if (data.success === true) { |
||
228 | PMA_ajaxRemoveMessage($msg); |
||
229 | PMA_commonActions.refreshMain(false, function () { |
||
230 | // Do nothing |
||
231 | }); |
||
232 | } else { |
||
233 | PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + ' : ' + data.error, false); |
||
234 | } |
||
235 | }); // end $.post() |
||
236 | }); // end $.PMA_confirm() |
||
237 | }); // end Drop Foreign key |
||
238 | |||
239 | var windowwidth = $(window).width(); |
||
240 | $('.jsresponsive').css('max-width', (windowwidth - 35) + 'px'); |
||
241 | }); |
||
242 |