1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used on the table structure page
4 * @name Table Structure
8 * @required js/functions.js
12 * AJAX scripts for tbl_structure.php
14 * Actions ajaxified here:
17 * Drop Primary Key/Index
22 * This function returns the horizontal space available for the menu in pixels.
23 * To calculate this value we start we the width of the main panel, then we
24 * substract the margin of the page content, then we substract any cellspacing
25 * that the table may have (original theme only) and finally we substract the
26 * width of all columns of the table except for the last one (which is where
27 * the menu will go). What we should end up with is the distance between the
28 * start of the last column on the table and the edge of the page, again this
29 * is the space available for the menu.
31 * In the case where the table cell where the menu will be displayed is already
32 * off-screen (the table is wider than the page), a negative value will be returned,
33 * but this will be treated as a zero by the menuResizer plugin.
37 function PMA_tbl_structure_menu_resizer_callback() {
38 var pagewidth = $('body').width();
39 var $page = $('#page_content');
40 pagewidth -= $page.outerWidth(true) - $page.outerWidth();
42 var $columns = $('#tablestructure').find('tr:eq(1)').find('td,th');
43 $columns.not(':last').each(function () {
44 columnsWidth += $(this).outerWidth(true);
46 var totalCellSpacing = $('#tablestructure').width();
47 $columns.each(function () {
48 totalCellSpacing -= $(this).outerWidth(true);
50 return pagewidth - columnsWidth - totalCellSpacing - 15; // 15px extra margin
56 function reloadFieldForm() {
57 $.post($("#fieldsForm").attr('action'), $("#fieldsForm").serialize() + "&ajax_request=true", function (form_data) {
58 var $temp_div = $("<div id='temp_div'><div>").append(form_data.message);
59 $("#fieldsForm").replaceWith($temp_div.find("#fieldsForm"));
60 $("#addColumns").replaceWith($temp_div.find("#addColumns"));
61 $('#move_columns_dialog').find('ul').replaceWith($temp_div.find("#move_columns_dialog ul"));
62 $("#moveColumns").removeClass("move-active");
63 /* reinitialise the more options in table */
64 if ($('#fieldsForm').hasClass('HideStructureActions')) {
65 $('#fieldsForm').find('ul.table-structure-actions').menuResizer(PMA_tbl_structure_menu_resizer_callback);
68 $('#page_content').show();
71 function checkFirst() {
72 if ($("select[name=after_field] option:selected").data('pos') === 'first') {
73 $("input[name=field_where]").val('first');
75 $("input[name=field_where]").val('after');
79 * Unbind all event handlers before tearing down a page
81 AJAX.registerTeardown('tbl_structure.js', function () {
82 $(document).off('click', "a.drop_column_anchor.ajax");
83 $(document).off('click', "a.add_key.ajax");
84 $(document).off('click', "#move_columns_anchor");
85 $(document).off('click', "#printView");
86 $(document).off('submit', ".append_fields_form.ajax");
87 $('body').off('click', '#fieldsForm.ajax button[name="submit_mult"], #fieldsForm.ajax input[name="submit_mult"]');
88 $(document).off('click', 'a[name^=partition_action].ajax');
91 AJAX.registerOnload('tbl_structure.js', function () {
93 // Re-initialize variables.
97 fulltext_indexes = [];
101 *Ajax action for submitting the "Column Change" and "Add Column" form
103 $(".append_fields_form.ajax").off();
104 $(document).on('submit', ".append_fields_form.ajax", function (event) {
105 event.preventDefault();
107 * @var the_form object referring to the export form
110 var field_cnt = $form.find('input[name=orig_num_fields]').val();
113 function submitForm(){
114 $msg = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
115 $.post($form.attr('action'), $form.serialize() + '&do_save_data=1', function (data) {
116 if ($(".sqlqueryresults").length !== 0) {
117 $(".sqlqueryresults").remove();
118 } else if ($(".error:not(.tab)").length !== 0) {
119 $(".error:not(.tab)").remove();
121 if (typeof data.success != 'undefined' && data.success === true) {
124 .append(data.message)
126 PMA_highlightSQL($('#page_content'));
127 $(".result_query .notice").remove();
130 PMA_ajaxRemoveMessage($msg);
132 PMA_reloadNavigation();
134 PMA_ajaxShowMessage(data.error, false);
139 function checkIfConfirmRequired($form, $field_cnt) {
140 var i = 0, id, elm, val, name_orig, elm_orig, val_orig;
141 var checkRequired = false;
142 for (i = 0; i < field_cnt; i++) {
143 id = "#field_" + i + "_5";
147 name_orig = "input[name=field_collation_orig\\[" + i + "\\]]";
148 elm_orig = $form.find(name_orig);
149 val_orig = elm_orig.val();
151 if (val && val_orig && val !== val_orig){
152 checkRequired = true;
156 return checkRequired;
160 * First validate the form; if there is a problem, avoid submitting it
162 * checkTableEditForm() needs a pure element and not a jQuery object,
163 * this is why we pass $form[0] as a parameter (the jQuery object
164 * is actually an array of DOM elements)
166 if (checkTableEditForm($form[0], field_cnt)) {
167 // OK, form passed validation step
169 PMA_prepareForAjaxRequest($form);
170 if (PMA_checkReservedWordColumns($form)) {
171 //User wants to submit the form
173 // If Collation is changed, Warn and Confirm
174 if (checkIfConfirmRequired($form, field_cnt)){
175 var question = sprintf(
176 PMA_messages.strChangeColumnCollation, 'http://wiki.phpmyadmin.net/pma/Garbled_data'
178 $form.PMA_confirm(question, $form.attr('action'), function (url) {
186 }); // end change table button "do_save_data"
189 * Attach Event Handler for 'Drop Column'
191 $(document).on('click', "a.drop_column_anchor.ajax", function (event) {
192 event.preventDefault();
194 * @var curr_table_name String containing the name of the current table
196 var curr_table_name = $(this).closest('form').find('input[name=table]').val();
198 * @var curr_row Object reference to the currently selected row (i.e. field in the table)
200 var $curr_row = $(this).parents('tr');
202 * @var curr_column_name String containing name of the field referred to by {@link curr_row}
204 var curr_column_name = $curr_row.children('th').children('label').text();
205 curr_column_name = escapeHtml(curr_column_name);
207 * @var $after_field_item Corresponding entry in the 'After' field.
209 var $after_field_item = $("select[name='after_field'] option[value='" + curr_column_name + "']");
211 * @var question String containing the question to be asked for confirmation
213 var question = PMA_sprintf(PMA_messages.strDoYouReally, 'ALTER TABLE `' + escapeHtml(curr_table_name) + '` DROP `' + escapeHtml(curr_column_name) + '`;');
214 $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
215 var $msg = PMA_ajaxShowMessage(PMA_messages.strDroppingColumn, false);
216 $.get(url, {'is_js_confirmed' : 1, 'ajax_request' : true, 'ajax_page_request' : true}, function (data) {
217 if (typeof data !== 'undefined' && data.success === true) {
218 PMA_ajaxRemoveMessage($msg);
219 if ($('.result_query').length) {
220 $('.result_query').remove();
222 if (data.sql_query) {
223 $('<div class="result_query"></div>')
224 .html(data.sql_query)
225 .prependTo('#structure_content');
226 PMA_highlightSQL($('#page_content'));
228 toggleRowColors($curr_row.next());
229 // Adjust the row numbers
230 for (var $row = $curr_row.next(); $row.length > 0; $row = $row.next()) {
231 var new_val = parseInt($row.find('td:nth-child(2)').text(), 10) - 1;
232 $row.find('td:nth-child(2)').text(new_val);
234 $after_field_item.remove();
235 $curr_row.hide("medium").remove();
236 //by default select the last option to add new column (in case last column is dropped)
237 $("select[name=after_field] option:last").attr("selected","selected");
238 //refresh table stats
239 if (data.tableStat) {
240 $('#tablestatistics').html(data.tableStat);
242 // refresh the list of indexes (comes from sql.php)
243 $('.index_info').replaceWith(data.indexes_list);
244 PMA_reloadNavigation();
246 PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest + " : " + data.error, false);
249 }); // end $.PMA_confirm()
250 }); //end of Drop Column Anchor action
253 * Attach Event Handler for 'Print View'
255 $(document).on('click', "#printView", function (event) {
256 event.preventDefault();
260 }); //end of Print View action
263 * Ajax Event handler for adding keys
265 $(document).on('click', "a.add_key.ajax", function (event) {
266 event.preventDefault();
269 var curr_table_name = $this.closest('form').find('input[name=table]').val();
270 var curr_column_name = $this.parents('tr').children('th').children('label').text();
273 if ($this.is('.add_primary_key_anchor')) {
274 add_clause = 'ADD PRIMARY KEY';
275 } else if ($this.is('.add_index_anchor')) {
276 add_clause = 'ADD INDEX';
277 } else if ($this.is('.add_unique_anchor')) {
278 add_clause = 'ADD UNIQUE';
279 } else if ($this.is('.add_spatial_anchor')) {
280 add_clause = 'ADD SPATIAL';
281 } else if ($this.is('.add_fulltext_anchor')) {
282 add_clause = 'ADD FULLTEXT';
284 var question = PMA_sprintf(PMA_messages.strDoYouReally, 'ALTER TABLE `' +
285 escapeHtml(curr_table_name) + '` ' + add_clause + '(`' + escapeHtml(curr_column_name) + '`);');
287 $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
288 PMA_ajaxShowMessage();
290 $.get(url, {'ajax_request' : true, 'ajax_page_request' : true}, AJAX.responseHandler);
291 }); // end $.PMA_confirm()
295 * Inline move columns
297 $(document).on('click', "#move_columns_anchor", function (e) {
300 if ($(this).hasClass("move-active")) {
305 * @var button_options Object that stores the options passed to jQueryUI
308 var button_options = {};
310 button_options[PMA_messages.strGo] = function (event) {
311 event.preventDefault();
312 var $msgbox = PMA_ajaxShowMessage();
314 var $form = $this.find("form");
315 var serialized = $form.serialize();
317 // check if any columns were moved at all
318 if (serialized == $form.data("serialized-unmoved")) {
319 PMA_ajaxRemoveMessage($msgbox);
320 $this.dialog('close');
324 $.post($form.prop("action"), serialized + "&ajax_request=true", function (data) {
325 if (data.success === false) {
326 PMA_ajaxRemoveMessage($msgbox);
331 title: $(this).prop("title"),
335 buttons: button_options_error
336 }); // end dialog options
338 if ($('#fieldsForm').hasClass('HideStructureActions')) {
339 $('#fieldsForm').find('ul.table-structure-actions').menuResizer('destroy');
341 // sort the fields table
342 var $fields_table = $("table#tablestructure tbody");
343 // remove all existing rows and remember them
344 var $rows = $fields_table.find("tr").remove();
345 // loop through the correct order
346 for (var i in data.columns) {
347 var the_column = data.columns[i];
349 .find("input:checkbox[value=" + the_column + "]")
351 // append the row for this column to the table
352 $fields_table.append($the_row);
354 var $firstrow = $fields_table.find("tr").eq(0);
355 // Adjust the row numbers and colors
356 for (var $row = $firstrow; $row.length > 0; $row = $row.next()) {
358 .find('td:nth-child(2)')
359 .text($row.index() + 1)
361 .removeClass("odd even")
362 .addClass($row.index() % 2 === 0 ? "odd" : "even");
364 PMA_ajaxShowMessage(data.message);
365 $this.dialog('close');
366 if ($('#fieldsForm').hasClass('HideStructureActions')) {
367 $('#fieldsForm').find('ul.table-structure-actions').menuResizer(PMA_tbl_structure_menu_resizer_callback);
372 button_options[PMA_messages.strCancel] = function () {
373 $(this).dialog('close');
376 var button_options_error = {};
377 button_options_error[PMA_messages.strOK] = function () {
378 $(this).dialog('close').remove();
383 $("#tablestructure").find("tbody tr").each(function () {
384 var col_name = $(this).find("input:checkbox").eq(0).val();
385 var hidden_input = $("<input/>")
387 name: "move_columns[]",
391 columns[columns.length] = $("<li/>")
392 .addClass("placeholderDrag")
394 .append(hidden_input);
397 var col_list = $("#move_columns_dialog").find("ul")
398 .find("li").remove().end();
399 for (var i in columns) {
400 col_list.append(columns[i]);
404 containment: $("#move_columns_dialog").find("div"),
406 }).disableSelection();
407 var $form = $("#move_columns_dialog").find("form");
408 $form.data("serialized-unmoved", $form.serialize());
410 $("#move_columns_dialog").dialog({
412 buttons: button_options,
414 if ($('#move_columns_dialog').parents('.ui-dialog').height() > $(window).height()) {
415 $('#move_columns_dialog').dialog("option", "height", $(window).height());
418 beforeClose: function () {
419 $("#move_columns_anchor").removeClass("move-active");
425 * Handles multi submits in table structure page such as change, browse, drop, primary etc.
427 $('body').on('click', '#fieldsForm.ajax button[name="submit_mult"], #fieldsForm.ajax input[name="submit_mult"]', function (e) {
429 var $button = $(this);
430 var $form = $button.parents('form');
431 var submitData = $form.serialize() + '&ajax_request=true&ajax_page_request=true&submit_mult=' + $button.val();
432 PMA_ajaxShowMessage();
434 $.post($form.attr('action'), submitData, AJAX.responseHandler);
438 * Handles clicks on Action links in partition table
440 $(document).on('click', 'a[name^=partition_action].ajax', function (e) {
444 function submitPartitionAction(url) {
445 var submitData = '&ajax_request=true&ajax_page_request=true';
446 PMA_ajaxShowMessage();
448 $.post(url, submitData, AJAX.responseHandler);
451 if ($link.is('#partition_action_DROP')) {
452 var question = PMA_messages.strDropPartitionWarning;
453 $link.PMA_confirm(question, $link.attr('href'), function (url) {
454 submitPartitionAction(url);
456 } else if ($link.is('#partition_action_TRUNCATE')) {
457 var question = PMA_messages.strTruncatePartitionWarning;
458 $link.PMA_confirm(question, $link.attr('href'), function (url) {
459 submitPartitionAction(url);
462 submitPartitionAction($link.attr('href'));
467 /** Handler for "More" dropdown in structure table rows */
468 AJAX.registerOnload('tbl_structure.js', function () {
469 if ($('#fieldsForm').hasClass('HideStructureActions')) {
470 $('#fieldsForm').find('ul.table-structure-actions').menuResizer(PMA_tbl_structure_menu_resizer_callback);
472 $('.table-structure-actions').width(function () {
474 $(this).find('li').each(function () {
475 width += $(this).outerWidth(true);
481 AJAX.registerTeardown('tbl_structure.js', function () {
482 if ($('#fieldsForm').hasClass('HideStructureActions')) {
483 $('#fieldsForm').find('ul.table-structure-actions').menuResizer('destroy');
487 $(window).resize($.throttle(function () {
488 if ($('#fieldsForm').length && $('#fieldsForm').hasClass('HideStructureActions')) {
489 $('#fieldsForm').find('ul.table-structure-actions').menuResizer('resize');