2 * @fileoverview events handling from normalization page
8 // eslint-disable-next-line no-unused-vars
9 /* global centralColumnList:writable */ // js/functions.js
12 * AJAX scripts for normalization
16 var normalizeto = '1nf';
18 var dataParsed = null;
20 function appendHtmlColumnsList () {
22 'index.php?route=/normalization',
25 'db': CommonParams.get('db'),
26 'table': CommonParams.get('table'),
27 'server': CommonParams.get('server'),
31 if (data.success === true) {
32 $('select[name=makeAtomic]').html(data.message);
38 function goTo3NFStep1 (newTables) {
39 var tables = newTables;
40 if (Object.keys(tables).length === 1) {
41 tables = [CommonParams.get('table')];
44 'index.php?route=/normalization',
47 'db': CommonParams.get('db'),
48 'server': CommonParams.get('server'),
52 $('#page_content').find('h3').html(Messages.str3NFNormalization);
53 $('#mainContent').find('legend').html(data.legendText);
54 $('#mainContent').find('h4').html(data.headText);
55 $('#mainContent').find('p').html(data.subText);
56 $('#mainContent').find('#extra').html(data.extra);
57 $('#extra').find('form').each(function () {
58 var formId = $(this).attr('id');
59 var colName = $(this).data('colname');
60 $('#' + formId + ' input[value=\'' + colName + '\']').next().remove();
61 $('#' + formId + ' input[value=\'' + colName + '\']').remove();
63 $('#mainContent').find('#newCols').html('');
64 $('.tblFooters').html('');
66 if (data.subText !== '') {
68 .attr({ type: 'button', value: Messages.strDone })
69 .on('click', function () {
70 processDependencies('', true);
72 .appendTo('.tblFooters');
78 function goTo2NFStep1 () {
80 'index.php?route=/normalization',
83 'db': CommonParams.get('db'),
84 'table': CommonParams.get('table'),
85 'server': CommonParams.get('server'),
88 $('#page_content h3').html(Messages.str2NFNormalization);
89 $('#mainContent legend').html(data.legendText);
90 $('#mainContent h4').html(data.headText);
91 $('#mainContent p').html(data.subText);
92 $('#mainContent #extra').html(data.extra);
93 $('#mainContent #newCols').html('');
94 if (data.subText !== '') {
96 .attr({ type: 'submit', value: Messages.strDone, })
97 .on('click', function () {
98 processDependencies(data.primary_key);
100 .appendTo('.tblFooters');
102 if (normalizeto === '3nf') {
103 $('#mainContent #newCols').html(Messages.strToNextStep);
104 setTimeout(function () {
105 goTo3NFStep1([CommonParams.get('table')]);
112 function goToFinish1NF () {
113 if (normalizeto !== '1nf') {
117 $('#mainContent legend').html(Messages.strEndStep);
118 $('#mainContent h4').html(
119 '<h3>' + Functions.sprintf(Messages.strFinishMsg, Functions.escapeHtml(CommonParams.get('table'))) + '</h3>'
121 $('#mainContent p').html('');
122 $('#mainContent #extra').html('');
123 $('#mainContent #newCols').html('');
124 $('.tblFooters').html('');
127 // eslint-disable-next-line no-unused-vars
128 function goToStep4 () {
130 'index.php?route=/normalization',
132 'ajax_request': true,
133 'db': CommonParams.get('db'),
134 'table': CommonParams.get('table'),
135 'server': CommonParams.get('server'),
138 $('#mainContent legend').html(data.legendText);
139 $('#mainContent h4').html(data.headText);
140 $('#mainContent p').html(data.subText);
141 $('#mainContent #extra').html(data.extra);
142 $('#mainContent #newCols').html('');
143 $('.tblFooters').html('');
144 for (var pk in primaryKey) {
145 $('#extra input[value=\'' + Functions.escapeJsString(primaryKey[pk]) + '\']').attr('disabled','disabled');
151 function goToStep3 () {
153 'index.php?route=/normalization',
155 'ajax_request': true,
156 'db': CommonParams.get('db'),
157 'table': CommonParams.get('table'),
158 'server': CommonParams.get('server'),
161 $('#mainContent legend').html(data.legendText);
162 $('#mainContent h4').html(data.headText);
163 $('#mainContent p').html(data.subText);
164 $('#mainContent #extra').html(data.extra);
165 $('#mainContent #newCols').html('');
166 $('.tblFooters').html('');
167 primaryKey = JSON.parse(data.primary_key);
168 for (var pk in primaryKey) {
169 $('#extra input[value=\'' + Functions.escapeJsString(primaryKey[pk]) + '\']').attr('disabled','disabled');
175 function goToStep2 (extra) {
177 'index.php?route=/normalization',
179 'ajax_request': true,
180 'db': CommonParams.get('db'),
181 'table': CommonParams.get('table'),
182 'server': CommonParams.get('server'),
185 $('#mainContent legend').html(data.legendText);
186 $('#mainContent h4').html(data.headText);
187 $('#mainContent p').html(data.subText);
188 $('#mainContent #extra,#mainContent #newCols').html('');
189 $('.tblFooters').html('');
190 if (data.hasPrimaryKey === '1') {
191 if (extra === 'goToStep3') {
192 $('#mainContent h4').html(Messages.strPrimaryKeyAdded);
193 $('#mainContent p').html(Messages.strToNextStep);
195 if (extra === 'goToFinish1NF') {
198 setTimeout(function () {
203 // form to select columns to make primary
204 $('#mainContent #extra').html(data.extra);
210 function goTo2NFFinish (pd) {
212 for (var dependson in pd) {
213 tables[dependson] = $('#extra input[name="' + dependson + '"]').val();
216 'ajax_request': true,
217 'db': CommonParams.get('db'),
218 'table': CommonParams.get('table'),
219 'server': CommonParams.get('server'),
220 'pd': JSON.stringify(pd),
221 'newTablesName':JSON.stringify(tables),
222 'createNewTables2NF':1 };
225 url: 'index.php?route=/normalization',
228 success: function (data) {
229 if (data.success === true) {
230 if (data.queryError === false) {
231 if (normalizeto === '3nf') {
232 $('#pma_navigation_reload').trigger('click');
233 goTo3NFStep1(tables);
236 $('#mainContent legend').html(data.legendText);
237 $('#mainContent h4').html(data.headText);
238 $('#mainContent p').html('');
239 $('#mainContent #extra').html('');
240 $('.tblFooters').html('');
242 Functions.ajaxShowMessage(data.extra, false);
244 $('#pma_navigation_reload').trigger('click');
246 Functions.ajaxShowMessage(data.error, false);
252 function goTo3NFFinish (newTables) {
253 for (var table in newTables) {
254 for (var newtbl in newTables[table]) {
255 var updatedname = $('#extra input[name="' + newtbl + '"]').val();
256 newTables[table][updatedname] = newTables[table][newtbl];
257 if (updatedname !== newtbl) {
258 delete newTables[table][newtbl];
263 'ajax_request': true,
264 'db': CommonParams.get('db'),
265 'server': CommonParams.get('server'),
266 'newTables':JSON.stringify(newTables),
267 'createNewTables3NF':1 };
270 url: 'index.php?route=/normalization',
273 success: function (data) {
274 if (data.success === true) {
275 if (data.queryError === false) {
276 $('#mainContent legend').html(data.legendText);
277 $('#mainContent h4').html(data.headText);
278 $('#mainContent p').html('');
279 $('#mainContent #extra').html('');
280 $('.tblFooters').html('');
282 Functions.ajaxShowMessage(data.extra, false);
284 $('#pma_navigation_reload').trigger('click');
286 Functions.ajaxShowMessage(data.error, false);
294 function goTo2NFStep2 (pd, primaryKey) {
295 $('#newCols').html('');
296 $('#mainContent legend').html(Messages.strStep + ' 2.2 ' + Messages.strConfirmPd);
297 $('#mainContent h4').html(Messages.strSelectedPd);
298 $('#mainContent p').html(Messages.strPdHintNote);
299 var extra = '<div class="dependencies_box">';
301 for (var dependson in pd) {
302 if (dependson !== primaryKey) {
304 extra += '<p class="displayblock desc">' + Functions.escapeHtml(dependson) + ' -> ' + Functions.escapeHtml(pd[dependson].toString()) + '</p>';
308 extra += '<p class="displayblock desc">' + Messages.strNoPdSelected + '</p>';
313 'ajax_request': true,
314 'db': CommonParams.get('db'),
315 'table': CommonParams.get('table'),
316 'server': CommonParams.get('server'),
317 'pd': JSON.stringify(pd),
318 'getNewTables2NF':1 };
321 url: 'index.php?route=/normalization',
324 success: function (data) {
325 if (data.success === true) {
326 extra += data.message;
328 Functions.ajaxShowMessage(data.error, false);
333 $('#mainContent #extra').html(extra);
334 $('.tblFooters').html('<input type="button" class="btn btn-primary" value="' + Messages.strBack + '" id="backEditPd"><input type="button" class="btn btn-primary" id="goTo2NFFinish" value="' + Messages.strGo + '">');
335 $('#goTo2NFFinish').on('click', function () {
340 function goTo3NFStep2 (pd, tablesTds) {
341 $('#newCols').html('');
342 $('#mainContent legend').html(Messages.strStep + ' 3.2 ' + Messages.strConfirmTd);
343 $('#mainContent h4').html(Messages.strSelectedTd);
344 $('#mainContent p').html(Messages.strPdHintNote);
345 var extra = '<div class="dependencies_box">';
347 for (var table in tablesTds) {
348 for (var i in tablesTds[table]) {
349 var dependson = tablesTds[table][i];
350 if (dependson !== '' && dependson !== table) {
352 extra += '<p class="displayblock desc">' + Functions.escapeHtml(dependson) + ' -> ' + Functions.escapeHtml(pd[dependson].toString()) + '</p>';
357 extra += '<p class="displayblock desc">' + Messages.strNoTdSelected + '</p>';
362 'ajax_request': true,
363 'db': CommonParams.get('db'),
364 'tables': JSON.stringify(tablesTds),
365 'server': CommonParams.get('server'),
366 'pd': JSON.stringify(pd),
367 'getNewTables3NF':1 };
370 url: 'index.php?route=/normalization',
373 success: function (data) {
375 if (data.success === true) {
376 extra += dataParsed.html;
378 Functions.ajaxShowMessage(data.error, false);
383 $('#mainContent #extra').html(extra);
384 $('.tblFooters').html('<input type="button" class="btn btn-primary" value="' + Messages.strBack + '" id="backEditPd"><input type="button" class="btn btn-primary" id="goTo3NFFinish" value="' + Messages.strGo + '">');
385 $('#goTo3NFFinish').on('click', function () {
389 goTo3NFFinish(dataParsed.newTables);
393 function processDependencies (primaryKey, isTransitive) {
399 $('#extra form').each(function () {
401 if (isTransitive === true) {
402 tblname = $(this).data('tablename');
404 if (!(tblname in tablesTds)) {
405 tablesTds[tblname] = [];
407 tablesTds[tblname].push(pk);
409 var formId = $(this).attr('id');
410 $('#' + formId + ' input[type=checkbox]:not(:checked)').prop('checked', false);
412 $('#' + formId + ' input[type=checkbox]:checked').each(function () {
413 dependsOn += $(this).val() + ', ';
414 $(this).attr('checked','checked');
416 if (dependsOn === '') {
419 dependsOn = dependsOn.slice(0, -2);
421 if (! (dependsOn in pd)) {
424 pd[dependsOn].push($(this).data('colname'));
425 if (isTransitive === true) {
426 if (!(tblname in tablesTds)) {
427 tablesTds[tblname] = [];
429 if ($.inArray(dependsOn, tablesTds[tblname]) === -1) {
430 tablesTds[tblname].push(dependsOn);
434 backup = $('#mainContent').html();
435 if (isTransitive === true) {
436 goTo3NFStep2(pd, tablesTds);
438 goTo2NFStep2(pd, pk);
443 function moveRepeatingGroup (repeatingCols) {
444 var newTable = $('input[name=repeatGroupTable]').val();
445 var newColumn = $('input[name=repeatGroupColumn]').val();
447 $('input[name=repeatGroupTable]').trigger('focus');
451 $('input[name=repeatGroupColumn]').trigger('focus');
455 'ajax_request': true,
456 'db': CommonParams.get('db'),
457 'table': CommonParams.get('table'),
458 'server': CommonParams.get('server'),
459 'repeatingColumns': repeatingCols,
461 'newColumn':newColumn,
462 'primary_columns':primaryKey.toString()
466 url: 'index.php?route=/normalization',
469 success: function (data) {
470 if (data.success === true) {
471 if (data.queryError === false) {
474 Functions.ajaxShowMessage(data.message, false);
475 $('#pma_navigation_reload').trigger('click');
477 Functions.ajaxShowMessage(data.error, false);
482 AJAX.registerTeardown('normalization.js', function () {
483 $('#extra').off('click', '#selectNonAtomicCol');
484 $('#splitGo').off('click');
485 $('.tblFooters').off('click', '#saveSplit');
486 $('#extra').off('click', '#addNewPrimary');
487 $('.tblFooters').off('click', '#saveNewPrimary');
488 $('#extra').off('click', '#removeRedundant');
489 $('#mainContent p').off('click', '#createPrimaryKey');
490 $('#mainContent').off('click', '#backEditPd');
491 $('#mainContent').off('click', '#showPossiblePd');
492 $('#mainContent').off('click', '.pickPd');
495 AJAX.registerOnload('normalization.js', function () {
497 normalizeto = $('#mainContent').data('normalizeto');
498 $('#extra').on('click', '#selectNonAtomicCol', function () {
499 if ($(this).val() === 'no_such_col') {
502 selectedCol = $(this).val();
506 $('#splitGo').on('click', function () {
507 if (!selectedCol || selectedCol === '') {
510 var numField = $('#numField').val();
512 'index.php?route=/normalization',
514 'ajax_request': true,
515 'db': CommonParams.get('db'),
516 'table': CommonParams.get('table'),
517 'server': CommonParams.get('server'),
519 'numFields': numField
522 if (data.success === true) {
523 $('#newCols').html(data.message);
524 $('.default_value').hide();
525 $('.enum_notice').hide();
528 .attr({ type: 'submit', id: 'saveSplit', value: Messages.strSave })
529 .appendTo('.tblFooters');
532 .attr({ type: 'submit', id: 'cancelSplit', value: Messages.strCancel })
533 .on('click', function () {
534 $('#newCols').html('');
535 $(this).parent().html('');
537 .appendTo('.tblFooters');
543 $('.tblFooters').on('click','#saveSplit', function () {
544 centralColumnList = [];
545 if ($('#newCols #field_0_1').val() === '') {
546 $('#newCols #field_0_1').trigger('focus');
549 var argsep = CommonParams.get('arg_separator');
550 var datastring = $('#newCols :input').serialize();
551 datastring += argsep + 'ajax_request=1' + argsep + 'do_save_data=1' + argsep + 'field_where=last';
552 $.post('index.php?route=/table/add-field', datastring, function (data) {
555 'index.php?route=/sql',
557 'ajax_request': true,
558 'db': CommonParams.get('db'),
559 'table': CommonParams.get('table'),
560 'server': CommonParams.get('server'),
561 'dropped_column': selectedCol,
563 'sql_query': 'ALTER TABLE `' + CommonParams.get('table') + '` DROP `' + selectedCol + '`;',
567 if (data.success === true) {
568 appendHtmlColumnsList();
569 $('#newCols').html('');
570 $('.tblFooters').html('');
572 Functions.ajaxShowMessage(data.error, false);
578 Functions.ajaxShowMessage(data.error, false);
583 $('#extra').on('click', '#addNewPrimary', function () {
585 'index.php?route=/normalization',
587 'ajax_request': true,
588 'db': CommonParams.get('db'),
589 'table': CommonParams.get('table'),
590 'server': CommonParams.get('server'),
591 'addNewPrimary': true
594 if (data.success === true) {
595 $('#newCols').html(data.message);
596 $('.default_value').hide();
597 $('.enum_notice').hide();
600 .attr({ type: 'submit', id: 'saveNewPrimary', value: Messages.strSave })
601 .appendTo('.tblFooters');
603 .attr({ type: 'submit', id: 'cancelSplit', value: Messages.strCancel })
604 .on('click', function () {
605 $('#newCols').html('');
606 $(this).parent().html('');
608 .appendTo('.tblFooters');
610 Functions.ajaxShowMessage(data.error, false);
616 $('.tblFooters').on('click', '#saveNewPrimary', function () {
617 var datastring = $('#newCols :input').serialize();
618 var argsep = CommonParams.get('arg_separator');
619 datastring += argsep + 'field_key[0]=primary_0' + argsep + 'ajax_request=1' + argsep + 'do_save_data=1' + argsep + 'field_where=last';
620 $.post('index.php?route=/table/add-field', datastring, function (data) {
621 if (data.success === true) {
622 $('#mainContent h4').html(Messages.strPrimaryKeyAdded);
623 $('#mainContent p').html(Messages.strToNextStep);
624 $('#mainContent #extra').html('');
625 $('#mainContent #newCols').html('');
626 $('.tblFooters').html('');
627 setTimeout(function () {
631 Functions.ajaxShowMessage(data.error, false);
635 $('#extra').on('click', '#removeRedundant', function () {
636 var dropQuery = 'ALTER TABLE `' + CommonParams.get('table') + '` ';
637 $('#extra input[type=checkbox]:checked').each(function () {
638 dropQuery += 'DROP `' + $(this).val() + '`, ';
640 dropQuery = dropQuery.slice(0, -2);
642 'index.php?route=/sql',
644 'ajax_request': true,
645 'db': CommonParams.get('db'),
646 'table': CommonParams.get('table'),
647 'server': CommonParams.get('server'),
648 'sql_query': dropQuery,
652 if (data.success === true) {
653 goToStep2('goToFinish1NF');
655 Functions.ajaxShowMessage(data.error, false);
660 $('#extra').on('click', '#moveRepeatingGroup', function () {
661 var repeatingCols = '';
662 $('#extra input[type=checkbox]:checked').each(function () {
663 repeatingCols += $(this).val() + ', ';
666 if (repeatingCols !== '') {
667 var newColName = $('#extra input[type=checkbox]:checked').first().val();
668 repeatingCols = repeatingCols.slice(0, -2);
669 var confirmStr = Functions.sprintf(Messages.strMoveRepeatingGroup, Functions.escapeHtml(repeatingCols), Functions.escapeHtml(CommonParams.get('table')));
670 confirmStr += '<input type="text" name="repeatGroupTable" placeholder="' + Messages.strNewTablePlaceholder + '">' +
671 '( ' + Functions.escapeHtml(primaryKey.toString()) + ', <input type="text" name="repeatGroupColumn" placeholder="' + Messages.strNewColumnPlaceholder + '" value="' + Functions.escapeHtml(newColName) + '">)' +
673 $('#newCols').html(confirmStr);
676 .attr({ type: 'submit', value: Messages.strCancel })
677 .on('click', function () {
678 $('#newCols').html('');
679 $('#extra input[type=checkbox]').prop('checked', false);
681 .appendTo('.tblFooters');
683 .attr({ type: 'submit', value: Messages.strGo })
684 .on('click', function () {
685 moveRepeatingGroup(repeatingCols);
687 .appendTo('.tblFooters');
690 $('#mainContent p').on('click', '#createPrimaryKey', function (event) {
691 event.preventDefault();
694 'server': CommonParams.get('server'),
695 'db': CommonParams.get('db'),
696 'table': CommonParams.get('table'),
699 'index': { 'Key_name':'PRIMARY' },
702 var title = Messages.strAddPrimaryKey;
703 Functions.indexEditorDialog(url, title, function () {
705 $('.sqlqueryresults').remove();
706 $('.result_query').remove();
707 $('.tblFooters').html('');
708 goToStep2('goToStep3');
712 $('#mainContent').on('click', '#backEditPd', function () {
713 $('#mainContent').html(backup);
715 $('#mainContent').on('click', '#showPossiblePd', function () {
716 if ($(this).hasClass('hideList')) {
717 $(this).html('+ ' + Messages.strShowPossiblePd);
718 $(this).removeClass('hideList');
719 $('#newCols').slideToggle('slow');
722 if ($('#newCols').html() !== '') {
723 $('#showPossiblePd').html('- ' + Messages.strHidePd);
724 $('#showPossiblePd').addClass('hideList');
725 $('#newCols').slideToggle('slow');
728 $('#newCols').insertAfter('#mainContent h4');
729 $('#newCols').html('<div class="text-center">' + Messages.strLoading + '<br>' + Messages.strWaitForPd + '</div>');
731 'index.php?route=/normalization',
733 'ajax_request': true,
734 'db': CommonParams.get('db'),
735 'table': CommonParams.get('table'),
736 'server': CommonParams.get('server'),
739 $('#showPossiblePd').html('- ' + Messages.strHidePd);
740 $('#showPossiblePd').addClass('hideList');
741 $('#newCols').html(data.message);
744 $('#mainContent').on('click', '.pickPd', function () {
745 var strColsLeft = $(this).next('.determinants').html();
746 var colsLeft = strColsLeft.split(',');
747 var strColsRight = $(this).next().next().html();
748 var colsRight = strColsRight.split(',');
749 for (var i in colsRight) {
750 $('form[data-colname="' + colsRight[i].trim() + '"] input[type="checkbox"]').prop('checked', false);
751 for (var j in colsLeft) {
752 $('form[data-colname="' + colsRight[i].trim() + '"] input[value="' + colsLeft[j].trim() + '"]').prop('checked', true);