Edit js/src/functions.js to add functions required for create table in db_structure.php.

Signed-Off-By: Piyush Vijay <piyushvijay.1997@gmail.com>
This commit is contained in:
Piyush Vijay 2018-07-27 18:23:53 +05:30
parent 6e790e72e5
commit 5e10b2c6b7

View File

@ -2,6 +2,10 @@ import { AJAX } from './ajax';
import { $ } from './utils/JqueryExtended';
import CommonParams from './variables/common_params';
import { PMA_Messages as PMA_messages } from './variables/export_variables';
import { PMA_prepareForAjaxRequest } from './functions/AjaxRequest';
import { PMA_ajaxShowMessage, PMA_ajaxRemoveMessage } from './utils/show_ajax_messages';
import PMA_MicroHistory from './classes/MicroHistory';
import { PMA_commonActions } from './classes/CommonActions';
// Sql based imports
import { PMA_getSQLEditor, bindCodeMirrorToInlineEditor } from './functions/Sql/SqlEditor';
@ -9,6 +13,12 @@ import { sqlQueryOptions, updateQueryParameters, PMA_highlightSQL } from './util
import { PMA_handleSimulateQueryButton, insertQuery, checkSqlQuery } from './functions/Sql/SqlQuery';
import { escapeHtml } from './utils/Sanitise';
import { getForeignKeyCheckboxLoader, loadForeignKeyCheckbox } from './functions/Sql/ForeignKey';
import { PMA_previewSQL } from './functions/Sql/PreviewSql';
// Create Table based imports
import { checkTableEditForm, PMA_checkReservedWordColumns } from './functions/Table/CreateTable';
import { PMA_adjustTotals } from './functions/Database/Structure';
import { PMA_verifyColumnsProperties, PMA_hideShowConnection } from './functions/Table/TableColumns';
/**
* Here we register a function that will remove the onsubmit event from all
@ -117,6 +127,7 @@ export function onload1 () {
});
}
/* *************************************** CODEMIRROR EDITOR START *************************************** */
/**
* Attach CodeMirror2 editor to SQL edit area.
*/
@ -256,6 +267,7 @@ export function onloadSqlInlineEditor () {
}
}
}
/* *************************************** CODEMIRROR EDITOR ENDS *************************************** */
/**
* Unbind all event handlers before tearing down a page
@ -279,3 +291,205 @@ export function onloadCtrlEnterFormSubmit () {
}
});
}
/* *************************************** CREATE TABLE STARTS *************************************** */
/**
* Unbind all event handlers before tearing down a page
*/
export function teardownCreateTable () {
$(document).off('submit', '#create_table_form_minimal.ajax');
$(document).off('submit', 'form.create_table_form.ajax');
$(document).off('click', 'form.create_table_form.ajax input[name=submit_num_fields]');
$(document).off('keyup', 'form.create_table_form.ajax input');
$(document).off('change', 'input[name=partition_count],input[name=subpartition_count],select[name=partition_by]');
}
/**
* jQuery coding for 'Create Table'. Used on db_operations.php,
* db_structure.php and db_tracking.php (i.e., wherever
* PhpMyAdmin\Display\CreateTable is used)
*
* Attach Ajax Event handlers for Create Table
*/
export function onloadCreateTable () {
/**
* Attach event handler for submission of create table form (save)
*/
$(document).on('submit', 'form.create_table_form.ajax', function (event) {
event.preventDefault();
/**
* @var the_form object referring to the create table form
*/
var $form = $(this);
/*
* First validate the form; if there is a problem, avoid submitting it
*
* checkTableEditForm() needs a pure element and not a jQuery object,
* this is why we pass $form[0] as a parameter (the jQuery object
* is actually an array of DOM elements)
*/
if (checkTableEditForm($form[0], $form.find('input[name=orig_num_fields]').val())) {
PMA_prepareForAjaxRequest($form);
if (PMA_checkReservedWordColumns($form)) {
PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
// User wants to submit the form
$.post($form.attr('action'), $form.serialize() + CommonParams.get('arg_separator') + 'do_save_data=1', function (data) {
if (typeof data !== 'undefined' && data.success === true) {
$('#properties_message')
.removeClass('error')
.html('');
PMA_ajaxShowMessage(data.message);
// Only if the create table dialog (distinct panel) exists
var $createTableDialog = $('#create_table_dialog');
if ($createTableDialog.length > 0) {
$createTableDialog.dialog('close').remove();
}
$('#tableslistcontainer').before(data.formatted_sql);
/**
* @var tables_table Object referring to the <tbody> element that holds the list of tables
*/
var tables_table = $('#tablesForm').find('tbody').not('#tbl_summary_row');
// this is the first table created in this db
if (tables_table.length === 0) {
PMA_commonActions.refreshMain(
CommonParams.get('opendb_url')
);
} else {
/**
* @var curr_last_row Object referring to the last <tr> element in {@link tables_table}
*/
var curr_last_row = $(tables_table).find('tr:last');
/**
* @var curr_last_row_index_string String containing the index of {@link curr_last_row}
*/
var curr_last_row_index_string = $(curr_last_row).find('input:checkbox').attr('id').match(/\d+/)[0];
/**
* @var curr_last_row_index Index of {@link curr_last_row}
*/
var curr_last_row_index = parseFloat(curr_last_row_index_string);
/**
* @var new_last_row_index Index of the new row to be appended to {@link tables_table}
*/
var new_last_row_index = curr_last_row_index + 1;
/**
* @var new_last_row_id String containing the id of the row to be appended to {@link tables_table}
*/
var new_last_row_id = 'checkbox_tbl_' + new_last_row_index;
data.new_table_string = data.new_table_string.replace(/checkbox_tbl_/, new_last_row_id);
// append to table
$(data.new_table_string)
.appendTo(tables_table);
// Sort the table
$(tables_table).PMA_sort_table('th');
// Adjust summary row
PMA_adjustTotals();
}
// Refresh navigation as a new table has been added
PMA_reloadNavigation();
// Redirect to table structure page on creation of new table
var argsep = CommonParams.get('arg_separator');
var params_12 = 'ajax_request=true' + argsep + 'ajax_page_request=true';
if (! (history && history.pushState)) {
params_12 += PMA_MicroHistory.menus.getRequestParam();
}
var tblStruct_url = 'tbl_structure.php?server=' + data._params.server +
argsep + 'db=' + data._params.db + argsep + 'token=' + data._params.token +
argsep + 'goto=db_structure.php' + argsep + 'table=' + data._params.table + '';
$.get(tblStruct_url, params_12, AJAX.responseHandler);
} else {
PMA_ajaxShowMessage(
'<div class="error">' + data.error + '</div>',
false
);
}
}); // end $.post()
}
} // end if (checkTableEditForm() )
}); // end create table form (save)
/**
* Submits the intermediate changes in the table creation form
* to refresh the UI accordingly
*/
function submitChangesInCreateTableForm (actionParam) {
/**
* @var the_form object referring to the create table form
*/
var $form = $('form.create_table_form.ajax');
var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
PMA_prepareForAjaxRequest($form);
// User wants to add more fields to the table
$.post($form.attr('action'), $form.serialize() + '&' + actionParam, function (data) {
if (typeof data !== 'undefined' && data.success) {
var $pageContent = $('#page_content');
$pageContent.html(data.message);
PMA_highlightSQL($pageContent);
PMA_verifyColumnsProperties();
PMA_hideShowConnection($('.create_table_form select[name=tbl_storage_engine]'));
PMA_ajaxRemoveMessage($msgbox);
} else {
PMA_ajaxShowMessage(data.error);
}
}); // end $.post()
}
/**
* Attach event handler for create table form (add fields)
*/
$(document).on('click', 'form.create_table_form.ajax input[name=submit_num_fields]', function (event) {
event.preventDefault();
submitChangesInCreateTableForm('submit_num_fields=1');
}); // end create table form (add fields)
$(document).on('keydown', 'form.create_table_form.ajax input[name=added_fields]', function (event) {
if (event.keyCode === 13) {
event.preventDefault();
event.stopImmediatePropagation();
$(this)
.closest('form')
.find('input[name=submit_num_fields]')
.trigger('click');
}
});
/**
* Attach event handler to manage changes in number of partitions and subpartitions
*/
$(document).on('change', 'input[name=partition_count],input[name=subpartition_count],select[name=partition_by]', function (event) {
var $this = $(this);
var $form = $this.parents('form');
if ($form.is('.create_table_form.ajax')) {
submitChangesInCreateTableForm('submit_partition_change=1');
} else {
$form.submit();
}
});
$(document).on('change', 'input[value=AUTO_INCREMENT]', function () {
if (this.checked) {
var col = /\d/.exec($(this).attr('name'));
col = col[0];
var $selectFieldKey = $('select[name="field_key[' + col + ']"]');
if ($selectFieldKey.val() === 'none_' + col) {
$selectFieldKey.val('primary_' + col).trigger('change');
}
}
});
$('body')
.off('click', 'input.preview_sql')
.on('click', 'input.preview_sql', function () {
var $form = $(this).closest('form');
PMA_previewSQL($form);
});
}
/* *************************************** CREATE TABLE STARTS *************************************** */