1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview events handling from normalization page
10 * AJAX scripts for normalization.php
14 var normalizeto = '1nf';
16 var data_parsed = null;
17 function appendHtmlColumnsList () {
22 'db': PMA_commonParams.get('db'),
23 'table': PMA_commonParams.get('table'),
24 'server': PMA_commonParams.get('server'),
28 if (data.success === true) {
29 $('select[name=makeAtomic]').html(data.message);
34 function goTo3NFStep1 (newTables) {
35 if (Object.keys(newTables).length === 1) {
36 newTables = [PMA_commonParams.get('table')];
42 'db': PMA_commonParams.get('db'),
43 'server': PMA_commonParams.get('server'),
47 $('#page_content').find('h3').html(PMA_messages.str3NFNormalization);
48 $('#mainContent').find('legend').html(data.legendText);
49 $('#mainContent').find('h4').html(data.headText);
50 $('#mainContent').find('p').html(data.subText);
51 $('#mainContent').find('#extra').html(data.extra);
52 $('#extra').find('form').each(function () {
53 var form_id = $(this).attr('id');
54 var colname = $(this).data('colname');
55 $('#' + form_id + ' input[value=\'' + colname + '\']').next().remove();
56 $('#' + form_id + ' input[value=\'' + colname + '\']').remove();
58 $('#mainContent').find('#newCols').html('');
59 $('.tblFooters').html('');
61 if (data.subText !== '') {
63 .attr({ type: 'button', value: PMA_messages.strDone })
64 .on('click', function () {
65 processDependencies('', true);
67 .appendTo('.tblFooters');
72 function goTo2NFStep1 () {
77 'db': PMA_commonParams.get('db'),
78 'table': PMA_commonParams.get('table'),
79 'server': PMA_commonParams.get('server'),
82 $('#page_content h3').html(PMA_messages.str2NFNormalization);
83 $('#mainContent legend').html(data.legendText);
84 $('#mainContent h4').html(data.headText);
85 $('#mainContent p').html(data.subText);
86 $('#mainContent #extra').html(data.extra);
87 $('#mainContent #newCols').html('');
88 if (data.subText !== '') {
89 var doneButton = $('<input />')
90 .attr({ type: 'submit', value: PMA_messages.strDone, })
91 .on('click', function () {
92 processDependencies(data.primary_key);
94 .appendTo('.tblFooters');
96 if (normalizeto === '3nf') {
97 $('#mainContent #newCols').html(PMA_messages.strToNextStep);
98 setTimeout(function () {
99 goTo3NFStep1([PMA_commonParams.get('table')]);
106 function goToFinish1NF () {
107 if (normalizeto !== '1nf') {
111 $('#mainContent legend').html(PMA_messages.strEndStep);
112 $('#mainContent h4').html(
113 '<h3>' + PMA_sprintf(PMA_messages.strFinishMsg, escapeHtml(PMA_commonParams.get('table'))) + '</h3>'
115 $('#mainContent p').html('');
116 $('#mainContent #extra').html('');
117 $('#mainContent #newCols').html('');
118 $('.tblFooters').html('');
121 function goToStep4 () {
125 'ajax_request': true,
126 'db': PMA_commonParams.get('db'),
127 'table': PMA_commonParams.get('table'),
128 'server': PMA_commonParams.get('server'),
131 $('#mainContent legend').html(data.legendText);
132 $('#mainContent h4').html(data.headText);
133 $('#mainContent p').html(data.subText);
134 $('#mainContent #extra').html(data.extra);
135 $('#mainContent #newCols').html('');
136 $('.tblFooters').html('');
137 for (var pk in primary_key) {
138 $('#extra input[value=\'' + escapeJsString(primary_key[pk]) + '\']').attr('disabled','disabled');
144 function goToStep3 () {
148 'ajax_request': true,
149 'db': PMA_commonParams.get('db'),
150 'table': PMA_commonParams.get('table'),
151 'server': PMA_commonParams.get('server'),
154 $('#mainContent legend').html(data.legendText);
155 $('#mainContent h4').html(data.headText);
156 $('#mainContent p').html(data.subText);
157 $('#mainContent #extra').html(data.extra);
158 $('#mainContent #newCols').html('');
159 $('.tblFooters').html('');
160 primary_key = JSON.parse(data.primary_key);
161 for (var pk in primary_key) {
162 $('#extra input[value=\'' + escapeJsString(primary_key[pk]) + '\']').attr('disabled','disabled');
168 function goToStep2 (extra) {
172 'ajax_request': true,
173 'db': PMA_commonParams.get('db'),
174 'table': PMA_commonParams.get('table'),
175 'server': PMA_commonParams.get('server'),
178 $('#mainContent legend').html(data.legendText);
179 $('#mainContent h4').html(data.headText);
180 $('#mainContent p').html(data.subText);
181 $('#mainContent #extra,#mainContent #newCols').html('');
182 $('.tblFooters').html('');
183 if (data.hasPrimaryKey === '1') {
184 if (extra === 'goToStep3') {
185 $('#mainContent h4').html(PMA_messages.strPrimaryKeyAdded);
186 $('#mainContent p').html(PMA_messages.strToNextStep);
188 if (extra === 'goToFinish1NF') {
191 setTimeout(function () {
196 // form to select columns to make primary
197 $('#mainContent #extra').html(data.extra);
203 function goTo2NFFinish (pd) {
205 for (var dependson in pd) {
206 tables[dependson] = $('#extra input[name="' + dependson + '"]').val();
209 'ajax_request': true,
210 'db': PMA_commonParams.get('db'),
211 'table': PMA_commonParams.get('table'),
212 'server': PMA_commonParams.get('server'),
213 'pd': JSON.stringify(pd),
214 'newTablesName':JSON.stringify(tables),
215 'createNewTables2NF':1 };
218 url: 'normalization.php',
221 success: function (data) {
222 if (data.success === true) {
223 if (data.queryError === false) {
224 if (normalizeto === '3nf') {
225 $('#pma_navigation_reload').click();
226 goTo3NFStep1(tables);
229 $('#mainContent legend').html(data.legendText);
230 $('#mainContent h4').html(data.headText);
231 $('#mainContent p').html('');
232 $('#mainContent #extra').html('');
233 $('.tblFooters').html('');
235 PMA_ajaxShowMessage(data.extra, false);
237 $('#pma_navigation_reload').click();
239 PMA_ajaxShowMessage(data.error, false);
245 function goTo3NFFinish (newTables) {
246 for (var table in newTables) {
247 for (var newtbl in newTables[table]) {
248 var updatedname = $('#extra input[name="' + newtbl + '"]').val();
249 newTables[table][updatedname] = newTables[table][newtbl];
250 if (updatedname !== newtbl) {
251 delete newTables[table][newtbl];
256 'ajax_request': true,
257 'db': PMA_commonParams.get('db'),
258 'server': PMA_commonParams.get('server'),
259 'newTables':JSON.stringify(newTables),
260 'createNewTables3NF':1 };
263 url: 'normalization.php',
266 success: function (data) {
267 if (data.success === true) {
268 if (data.queryError === false) {
269 $('#mainContent legend').html(data.legendText);
270 $('#mainContent h4').html(data.headText);
271 $('#mainContent p').html('');
272 $('#mainContent #extra').html('');
273 $('.tblFooters').html('');
275 PMA_ajaxShowMessage(data.extra, false);
277 $('#pma_navigation_reload').click();
279 PMA_ajaxShowMessage(data.error, false);
285 function goTo2NFStep2 (pd, primary_key) {
286 $('#newCols').html('');
287 $('#mainContent legend').html(PMA_messages.strStep + ' 2.2 ' + PMA_messages.strConfirmPd);
288 $('#mainContent h4').html(PMA_messages.strSelectedPd);
289 $('#mainContent p').html(PMA_messages.strPdHintNote);
290 var extra = '<div class="dependencies_box">';
292 for (var dependson in pd) {
293 if (dependson !== primary_key) {
295 extra += '<p class="displayblock desc">' + escapeHtml(dependson) + ' -> ' + escapeHtml(pd[dependson].toString()) + '</p>';
299 extra += '<p class="displayblock desc">' + PMA_messages.strNoPdSelected + '</p>';
304 'ajax_request': true,
305 'db': PMA_commonParams.get('db'),
306 'table': PMA_commonParams.get('table'),
307 'server': PMA_commonParams.get('server'),
308 'pd': JSON.stringify(pd),
309 'getNewTables2NF':1 };
312 url: 'normalization.php',
315 success: function (data) {
316 if (data.success === true) {
317 extra += data.message;
319 PMA_ajaxShowMessage(data.error, false);
324 $('#mainContent #extra').html(extra);
325 $('.tblFooters').html('<input type="button" value="' + PMA_messages.strBack + '" id="backEditPd"/><input type="button" id="goTo2NFFinish" value="' + PMA_messages.strGo + '"/>');
326 $('#goTo2NFFinish').click(function () {
331 function goTo3NFStep2 (pd, tablesTds) {
332 $('#newCols').html('');
333 $('#mainContent legend').html(PMA_messages.strStep + ' 3.2 ' + PMA_messages.strConfirmTd);
334 $('#mainContent h4').html(PMA_messages.strSelectedTd);
335 $('#mainContent p').html(PMA_messages.strPdHintNote);
336 var extra = '<div class="dependencies_box">';
338 for (var table in tablesTds) {
339 for (var i in tablesTds[table]) {
340 dependson = tablesTds[table][i];
341 if (dependson !== '' && dependson !== table) {
343 extra += '<p class="displayblock desc">' + escapeHtml(dependson) + ' -> ' + escapeHtml(pd[dependson].toString()) + '</p>';
348 extra += '<p class="displayblock desc">' + PMA_messages.strNoTdSelected + '</p>';
353 'ajax_request': true,
354 'db': PMA_commonParams.get('db'),
355 'tables': JSON.stringify(tablesTds),
356 'server': PMA_commonParams.get('server'),
357 'pd': JSON.stringify(pd),
358 'getNewTables3NF':1 };
361 url: 'normalization.php',
364 success: function (data) {
366 if (data.success === true) {
367 extra += data_parsed.html;
369 PMA_ajaxShowMessage(data.error, false);
374 $('#mainContent #extra').html(extra);
375 $('.tblFooters').html('<input type="button" value="' + PMA_messages.strBack + '" id="backEditPd"/><input type="button" id="goTo3NFFinish" value="' + PMA_messages.strGo + '"/>');
376 $('#goTo3NFFinish').click(function () {
380 goTo3NFFinish(data_parsed.newTables);
384 function processDependencies (primary_key, isTransitive) {
388 pd[primary_key] = [];
389 $('#extra form').each(function () {
391 if (isTransitive === true) {
392 tblname = $(this).data('tablename');
393 primary_key = tblname;
394 if (!(tblname in tablesTds)) {
395 tablesTds[tblname] = [];
397 tablesTds[tblname].push(primary_key);
399 var form_id = $(this).attr('id');
400 $('#' + form_id + ' input[type=checkbox]:not(:checked)').prop('checked', false);
402 $('#' + form_id + ' input[type=checkbox]:checked').each(function () {
403 dependsOn += $(this).val() + ', ';
404 $(this).attr('checked','checked');
406 if (dependsOn === '') {
407 dependsOn = primary_key;
409 dependsOn = dependsOn.slice(0, -2);
411 if (! (dependsOn in pd)) {
414 pd[dependsOn].push($(this).data('colname'));
415 if (isTransitive === true) {
416 if (!(tblname in tablesTds)) {
417 tablesTds[tblname] = [];
419 if ($.inArray(dependsOn, tablesTds[tblname]) === -1) {
420 tablesTds[tblname].push(dependsOn);
424 backup = $('#mainContent').html();
425 if (isTransitive === true) {
426 goTo3NFStep2(pd, tablesTds);
428 goTo2NFStep2(pd, primary_key);
433 function moveRepeatingGroup (repeatingCols) {
434 var newTable = $('input[name=repeatGroupTable]').val();
435 var newColumn = $('input[name=repeatGroupColumn]').val();
437 $('input[name=repeatGroupTable]').focus();
441 $('input[name=repeatGroupColumn]').focus();
445 'ajax_request': true,
446 'db': PMA_commonParams.get('db'),
447 'table': PMA_commonParams.get('table'),
448 'server': PMA_commonParams.get('server'),
449 'repeatingColumns': repeatingCols,
451 'newColumn':newColumn,
452 'primary_columns':primary_key.toString()
456 url: 'normalization.php',
459 success: function (data) {
460 if (data.success === true) {
461 if (data.queryError === false) {
464 PMA_ajaxShowMessage(data.message, false);
465 $('#pma_navigation_reload').click();
467 PMA_ajaxShowMessage(data.error, false);
472 AJAX.registerTeardown('normalization.js', function () {
473 $('#extra').off('click', '#selectNonAtomicCol');
474 $('#splitGo').off('click');
475 $('.tblFooters').off('click', '#saveSplit');
476 $('#extra').off('click', '#addNewPrimary');
477 $('.tblFooters').off('click', '#saveNewPrimary');
478 $('#extra').off('click', '#removeRedundant');
479 $('#mainContent p').off('click', '#createPrimaryKey');
480 $('#mainContent').off('click', '#backEditPd');
481 $('#mainContent').off('click', '#showPossiblePd');
482 $('#mainContent').off('click', '.pickPd');
485 AJAX.registerOnload('normalization.js', function () {
487 normalizeto = $('#mainContent').data('normalizeto');
488 $('#extra').on('click', '#selectNonAtomicCol', function () {
489 if ($(this).val() === 'no_such_col') {
492 selectedCol = $(this).val();
496 $('#splitGo').click(function () {
497 if (!selectedCol || selectedCol === '') {
500 var numField = $('#numField').val();
504 'ajax_request': true,
505 'db': PMA_commonParams.get('db'),
506 'table': PMA_commonParams.get('table'),
507 'server': PMA_commonParams.get('server'),
509 'numFields': numField
512 if (data.success === true) {
513 $('#newCols').html(data.message);
514 $('.default_value').hide();
515 $('.enum_notice').hide();
518 .attr({ type: 'submit', id: 'saveSplit', value: PMA_messages.strSave })
519 .appendTo('.tblFooters');
521 var cancelSplitButton = $('<input />')
522 .attr({ type: 'submit', id: 'cancelSplit', value: PMA_messages.strCancel })
523 .on('click', function () {
524 $('#newCols').html('');
525 $(this).parent().html('');
527 .appendTo('.tblFooters');
533 $('.tblFooters').on('click','#saveSplit', function () {
534 central_column_list = [];
535 if ($('#newCols #field_0_1').val() === '') {
536 $('#newCols #field_0_1').focus();
539 var argsep = PMA_commonParams.get('arg_separator');
540 datastring = $('#newCols :input').serialize();
541 datastring += argsep + 'ajax_request=1' + argsep + 'do_save_data=1' + argsep + 'field_where=last';
542 $.post('tbl_addfield.php', datastring, function (data) {
547 'ajax_request': true,
548 'db': PMA_commonParams.get('db'),
549 'table': PMA_commonParams.get('table'),
550 'server': PMA_commonParams.get('server'),
551 'dropped_column': selectedCol,
553 'sql_query': 'ALTER TABLE `' + PMA_commonParams.get('table') + '` DROP `' + selectedCol + '`;',
557 if (data.success === true) {
558 appendHtmlColumnsList();
559 $('#newCols').html('');
560 $('.tblFooters').html('');
562 PMA_ajaxShowMessage(data.error, false);
568 PMA_ajaxShowMessage(data.error, false);
573 $('#extra').on('click', '#addNewPrimary', function () {
577 'ajax_request': true,
578 'db': PMA_commonParams.get('db'),
579 'table': PMA_commonParams.get('table'),
580 'server': PMA_commonParams.get('server'),
581 'addNewPrimary': true
584 if (data.success === true) {
585 $('#newCols').html(data.message);
586 $('.default_value').hide();
587 $('.enum_notice').hide();
590 .attr({ type: 'submit', id: 'saveNewPrimary', value: PMA_messages.strSave })
591 .appendTo('.tblFooters');
593 .attr({ type: 'submit', id: 'cancelSplit', value: PMA_messages.strCancel })
594 .on('click', function () {
595 $('#newCols').html('');
596 $(this).parent().html('');
598 .appendTo('.tblFooters');
600 PMA_ajaxShowMessage(data.error, false);
606 $('.tblFooters').on('click', '#saveNewPrimary', function () {
607 var datastring = $('#newCols :input').serialize();
608 var argsep = PMA_commonParams.get('arg_separator');
609 datastring += argsep + 'field_key[0]=primary_0' + argsep + 'ajax_request=1' + argsep + 'do_save_data=1' + argsep + 'field_where=last';
610 $.post('tbl_addfield.php', datastring, function (data) {
611 if (data.success === true) {
612 $('#mainContent h4').html(PMA_messages.strPrimaryKeyAdded);
613 $('#mainContent p').html(PMA_messages.strToNextStep);
614 $('#mainContent #extra').html('');
615 $('#mainContent #newCols').html('');
616 $('.tblFooters').html('');
617 setTimeout(function () {
621 PMA_ajaxShowMessage(data.error, false);
625 $('#extra').on('click', '#removeRedundant', function () {
626 var dropQuery = 'ALTER TABLE `' + PMA_commonParams.get('table') + '` ';
627 $('#extra input[type=checkbox]:checked').each(function () {
628 dropQuery += 'DROP `' + $(this).val() + '`, ';
630 dropQuery = dropQuery.slice(0, -2);
634 'ajax_request': true,
635 'db': PMA_commonParams.get('db'),
636 'table': PMA_commonParams.get('table'),
637 'server': PMA_commonParams.get('server'),
638 'sql_query': dropQuery,
642 if (data.success === true) {
643 goToStep2('goToFinish1NF');
645 PMA_ajaxShowMessage(data.error, false);
650 $('#extra').on('click', '#moveRepeatingGroup', function () {
651 var repeatingCols = '';
652 $('#extra input[type=checkbox]:checked').each(function () {
653 repeatingCols += $(this).val() + ', ';
656 if (repeatingCols !== '') {
657 var newColName = $('#extra input[type=checkbox]:checked:first').val();
658 repeatingCols = repeatingCols.slice(0, -2);
659 var confirmStr = PMA_sprintf(PMA_messages.strMoveRepeatingGroup, escapeHtml(repeatingCols), escapeHtml(PMA_commonParams.get('table')));
660 confirmStr += '<input type="text" name="repeatGroupTable" placeholder="' + PMA_messages.strNewTablePlaceholder + '"/>' +
661 '( ' + escapeHtml(primary_key.toString()) + ', <input type="text" name="repeatGroupColumn" placeholder="' + PMA_messages.strNewColumnPlaceholder + '" value="' + escapeHtml(newColName) + '">)' +
663 $('#newCols').html(confirmStr);
666 .attr({ type: 'submit', value: PMA_messages.strCancel })
667 .on('click', function () {
668 $('#newCols').html('');
669 $('#extra input[type=checkbox]').prop('checked', false);
671 .appendTo('.tblFooters');
673 .attr({ type: 'submit', value: PMA_messages.strGo })
674 .on('click', function () {
675 moveRepeatingGroup(repeatingCols);
677 .appendTo('.tblFooters');
680 $('#mainContent p').on('click', '#createPrimaryKey', function (event) {
681 event.preventDefault();
682 var url = { create_index: 1,
683 server: PMA_commonParams.get('server'),
684 db: PMA_commonParams.get('db'),
685 table: PMA_commonParams.get('table'),
688 index: { Key_name:'PRIMARY' },
691 var title = PMA_messages.strAddPrimaryKey;
692 indexEditorDialog(url, title, function () {
694 $('.sqlqueryresults').remove();
695 $('.result_query').remove();
696 $('.tblFooters').html('');
697 goToStep2('goToStep3');
701 $('#mainContent').on('click', '#backEditPd', function () {
702 $('#mainContent').html(backup);
704 $('#mainContent').on('click', '#showPossiblePd', function () {
705 if ($(this).hasClass('hideList')) {
706 $(this).html('+ ' + PMA_messages.strShowPossiblePd);
707 $(this).removeClass('hideList');
708 $('#newCols').slideToggle('slow');
711 if ($('#newCols').html() !== '') {
712 $('#showPossiblePd').html('- ' + PMA_messages.strHidePd);
713 $('#showPossiblePd').addClass('hideList');
714 $('#newCols').slideToggle('slow');
717 $('#newCols').insertAfter('#mainContent h4');
718 $('#newCols').html('<div class="center">' + PMA_messages.strLoading + '<br/>' + PMA_messages.strWaitForPd + '</div>');
722 'ajax_request': true,
723 'db': PMA_commonParams.get('db'),
724 'table': PMA_commonParams.get('table'),
725 'server': PMA_commonParams.get('server'),
728 $('#showPossiblePd').html('- ' + PMA_messages.strHidePd);
729 $('#showPossiblePd').addClass('hideList');
730 $('#newCols').html(data.message);
733 $('#mainContent').on('click', '.pickPd', function () {
734 var strColsLeft = $(this).next('.determinants').html();
735 var colsLeft = strColsLeft.split(',');
736 var strColsRight = $(this).next().next().html();
737 var colsRight = strColsRight.split(',');
738 for (var i in colsRight) {
739 $('form[data-colname="' + colsRight[i].trim() + '"] input[type="checkbox"]').prop('checked', false);
740 for (var j in colsLeft) {
741 $('form[data-colname="' + colsRight[i].trim() + '"] input[value="' + colsLeft[j].trim() + '"]').prop('checked', true);