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 "token": PMA_commonParams.get('token'),
24 "db": PMA_commonParams.get('db'),
25 "table": PMA_commonParams.get('table'),
29 if (data.success === true) {
30 $('select[name=makeAtomic]').html(data.message);
35 function goTo3NFStep1(newTables)
37 if (Object.keys(newTables).length === 1) {
38 newTables = [PMA_commonParams.get('table')];
43 "token": PMA_commonParams.get('token'),
45 "db": PMA_commonParams.get('db'),
49 $("#page_content h3").html(PMA_messages.str3NFNormalization);
50 $("#mainContent legend").html(data.legendText);
51 $("#mainContent h4").html(data.headText);
52 $("#mainContent p").html(data.subText);
53 $("#mainContent #extra").html(data.extra);
54 $("#extra form").each(function() {
55 var form_id = $(this).attr('id');
56 var colname = $(this).data('colname');
57 $("#" + form_id + " input[value='" + colname + "']").next().remove();
58 $("#" + form_id + " input[value='" + colname + "']").remove();
60 $("#mainContent #newCols").html('');
61 $('.tblFooters').html('');
62 if (data.subText !== "") {
63 $('.tblFooters').html('<input type="button" onClick="processDependencies(\'\', true);" value="' + PMA_messages.strDone + '"/>');
68 function goTo2NFStep1() {
72 "token": PMA_commonParams.get('token'),
74 "db": PMA_commonParams.get('db'),
75 "table": PMA_commonParams.get('table'),
78 $("#page_content h3").html(PMA_messages.str2NFNormalization);
79 $("#mainContent legend").html(data.legendText);
80 $("#mainContent h4").html(data.headText);
81 $("#mainContent p").html(data.subText);
82 $("#mainContent #extra").html(data.extra);
83 $("#mainContent #newCols").html('');
84 if (data.subText !== '') {
85 $('.tblFooters').html('<input type="submit" value="' + PMA_messages.strDone + '" onclick="processDependencies(\'' + data.primary_key + '\');">');
87 if (normalizeto === '3nf') {
88 $("#mainContent #newCols").html(PMA_messages.strToNextStep);
89 setTimeout(function() {
90 goTo3NFStep1([PMA_commonParams.get('table')]);
97 function goToFinish1NF()
99 if (normalizeto !== '1nf') {
103 $("#mainContent legend").html(PMA_messages.strEndStep);
104 $("#mainContent h4").html(
105 "<h3>" + PMA_sprintf(PMA_messages.strFinishMsg, PMA_commonParams.get('table')) + "</h3>"
107 $("#mainContent p").html('');
108 $("#mainContent #extra").html('');
109 $("#mainContent #newCols").html('');
110 $('.tblFooters').html('');
118 "token": PMA_commonParams.get('token'),
119 "ajax_request": true,
120 "db": PMA_commonParams.get('db'),
121 "table": PMA_commonParams.get('table'),
124 $("#mainContent legend").html(data.legendText);
125 $("#mainContent h4").html(data.headText);
126 $("#mainContent p").html(data.subText);
127 $("#mainContent #extra").html(data.extra);
128 $("#mainContent #newCols").html('');
129 $('.tblFooters').html('');
130 for(var pk in primary_key) {
131 $("#extra input[value='" + primary_key[pk] + "']").attr("disabled","disabled");
142 "token": PMA_commonParams.get('token'),
143 "ajax_request": true,
144 "db": PMA_commonParams.get('db'),
145 "table": PMA_commonParams.get('table'),
148 $("#mainContent legend").html(data.legendText);
149 $("#mainContent h4").html(data.headText);
150 $("#mainContent p").html(data.subText);
151 $("#mainContent #extra").html(data.extra);
152 $("#mainContent #newCols").html('');
153 $('.tblFooters').html('');
154 primary_key = $.parseJSON(data.primary_key);
155 for(var pk in primary_key) {
156 $("#extra input[value='" + primary_key[pk] + "']").attr("disabled","disabled");
162 function goToStep2(extra)
167 "token": PMA_commonParams.get('token'),
168 "ajax_request": true,
169 "db": PMA_commonParams.get('db'),
170 "table": PMA_commonParams.get('table'),
173 $("#mainContent legend").html(data.legendText);
174 $("#mainContent h4").html(data.headText);
175 $("#mainContent p").html(data.subText);
176 $("#mainContent #extra,#mainContent #newCols").html('');
177 $('.tblFooters').html('');
178 if (data.hasPrimaryKey === "1") {
179 if(extra === 'goToStep3') {
180 $("#mainContent h4").html(PMA_messages.strPrimaryKeyAdded);
181 $("#mainContent p").html(PMA_messages.strToNextStep);
183 if(extra === 'goToFinish1NF') {
186 setTimeout(function() {
191 //form to select columns to make primary
192 $("#mainContent #extra").html(data.extra);
198 function goTo2NFFinish(pd)
201 for (var dependson in pd) {
202 tables[dependson] = $('#extra input[name="' + dependson + '"]').val();
204 datastring = {"token": PMA_commonParams.get('token'),
205 "ajax_request": true,
206 "db": PMA_commonParams.get('db'),
207 "table": PMA_commonParams.get('table'),
208 "pd": JSON.stringify(pd),
209 "newTablesName":JSON.stringify(tables),
210 "createNewTables2NF":1};
213 url: "normalization.php",
216 success: function(data) {
217 if (data.success === true) {
218 if(data.queryError === false) {
219 if (normalizeto === '3nf') {
220 $("#pma_navigation_reload").click();
221 goTo3NFStep1(tables);
224 $("#mainContent legend").html(data.legendText);
225 $("#mainContent h4").html(data.headText);
226 $("#mainContent p").html('');
227 $("#mainContent #extra").html('');
228 $('.tblFooters').html('');
230 PMA_ajaxShowMessage(data.extra, false);
232 $("#pma_navigation_reload").click();
234 PMA_ajaxShowMessage(data.error, false);
240 function goTo3NFFinish(newTables)
242 for (var table in newTables) {
243 for (var newtbl in newTables[table]) {
244 var updatedname = $('#extra input[name="' + newtbl + '"]').val();
245 newTables[table][updatedname] = newTables[table][newtbl];
246 if (updatedname !== newtbl) {
247 delete newTables[table][newtbl];
251 datastring = {"token": PMA_commonParams.get('token'),
252 "ajax_request": true,
253 "db": PMA_commonParams.get('db'),
254 "newTables":JSON.stringify(newTables),
255 "createNewTables3NF":1};
258 url: "normalization.php",
261 success: function(data) {
262 if (data.success === true) {
263 if(data.queryError === false) {
264 $("#mainContent legend").html(data.legendText);
265 $("#mainContent h4").html(data.headText);
266 $("#mainContent p").html('');
267 $("#mainContent #extra").html('');
268 $('.tblFooters').html('');
270 PMA_ajaxShowMessage(data.extra, false);
272 $("#pma_navigation_reload").click();
274 PMA_ajaxShowMessage(data.error, false);
280 function goTo2NFStep2(pd, primary_key)
282 $("#newCols").html('');
283 $("#mainContent legend").html(PMA_messages.strStep + ' 2.2 ' + PMA_messages.strConfirmPd);
284 $("#mainContent h4").html(PMA_messages.strSelectedPd);
285 $("#mainContent p").html(PMA_messages.strPdHintNote);
286 var extra = '<div class="dependencies_box">';
288 for (var dependson in pd) {
289 if (dependson !== primary_key) {
291 extra += '<p class="displayblock desc">' + escapeHtml(dependson) + " -> " + escapeHtml(pd[dependson].toString()) + '</p>';
295 extra += '<p class="displayblock desc">' + PMA_messages.strNoPdSelected + '</p>';
299 datastring = {"token": PMA_commonParams.get('token'),
300 "ajax_request": true,
301 "db": PMA_commonParams.get('db'),
302 "table": PMA_commonParams.get('table'),
303 "pd": JSON.stringify(pd),
304 "getNewTables2NF":1};
307 url: "normalization.php",
310 success: function(data) {
311 if (data.success === true) {
312 extra += data.message;
314 PMA_ajaxShowMessage(data.error, false);
319 $("#mainContent #extra").html(extra);
320 $('.tblFooters').html('<input type="button" value="' + PMA_messages.strBack + '" id="backEditPd"/><input type="button" id="goTo2NFFinish" value="' + PMA_messages.strGo + '"/>');
321 $("#goTo2NFFinish").click(function(){
326 function goTo3NFStep2(pd, tablesTds)
328 $("#newCols").html('');
329 $("#mainContent legend").html(PMA_messages.strStep + ' 3.2 ' + PMA_messages.strConfirmTd);
330 $("#mainContent h4").html(PMA_messages.strSelectedTd);
331 $("#mainContent p").html(PMA_messages.strPdHintNote);
332 var extra = '<div class="dependencies_box">';
334 for (var table in tablesTds) {
335 for (var i in tablesTds[table]) {
336 dependson = tablesTds[table][i];
337 if (dependson !== '' && dependson !== table) {
339 extra += '<p class="displayblock desc">' + escapeHtml(dependson) + " -> " + escapeHtml(pd[dependson].toString()) + '</p>';
344 extra += '<p class="displayblock desc">' + PMA_messages.strNoTdSelected + '</p>';
348 datastring = {"token": PMA_commonParams.get('token'),
349 "ajax_request": true,
350 "db": PMA_commonParams.get('db'),
351 "tables": JSON.stringify(tablesTds),
352 "pd": JSON.stringify(pd),
353 "getNewTables3NF":1};
356 url: "normalization.php",
359 success: function(data) {
360 data_parsed = $.parseJSON(data.message);
361 if (data.success === true) {
362 extra += data_parsed.html;
364 PMA_ajaxShowMessage(data.error, false);
369 $("#mainContent #extra").html(extra);
370 $('.tblFooters').html('<input type="button" value="' + PMA_messages.strBack + '" id="backEditPd"/><input type="button" id="goTo3NFFinish" value="' + PMA_messages.strGo + '"/>');
371 $("#goTo3NFFinish").click(function(){
375 goTo3NFFinish(data_parsed.newTables);
379 function processDependencies(primary_key, isTransitive)
384 pd[primary_key] = [];
385 $("#extra form").each(function() {
387 if (isTransitive === true) {
388 tblname = $(this).data('tablename');
389 primary_key = tblname;
390 if (!(tblname in tablesTds)) {
391 tablesTds[tblname] = [];
393 tablesTds[tblname].push(primary_key);
395 var form_id = $(this).attr('id');
396 $('#' + form_id + ' input[type=checkbox]:not(:checked)').removeAttr('checked');
398 $('#' + form_id + ' input[type=checkbox]:checked').each(function(){
399 dependsOn += $(this).val() + ', ';
400 $(this).attr("checked","checked");
402 if (dependsOn === '') {
403 dependsOn = primary_key;
405 dependsOn = dependsOn.slice(0, -2);
407 if (! (dependsOn in pd)) {
410 pd[dependsOn].push($(this).data('colname'));
411 if (isTransitive === true) {
412 if (!(tblname in tablesTds)) {
413 tablesTds[tblname] = [];
415 if ($.inArray(dependsOn, tablesTds[tblname]) === -1) {
416 tablesTds[tblname].push(dependsOn);
420 backup = $("#mainContent").html();
421 if (isTransitive === true) {
422 goTo3NFStep2(pd, tablesTds);
424 goTo2NFStep2(pd, primary_key);
429 function moveRepeatingGroup(repeatingCols) {
430 var newTable = $("input[name=repeatGroupTable]").val();
431 var newColumn = $("input[name=repeatGroupColumn]").val();
433 $("input[name=repeatGroupTable]").focus();
437 $("input[name=repeatGroupColumn]").focus();
440 datastring = {"token": PMA_commonParams.get('token'),
441 "ajax_request": true,
442 "db": PMA_commonParams.get('db'),
443 "table": PMA_commonParams.get('table'),
444 "repeatingColumns": repeatingCols,
446 "newColumn":newColumn,
447 "primary_columns":primary_key.toString()
451 url: "normalization.php",
454 success: function(data) {
455 if (data.success === true) {
456 if(data.queryError === false) {
459 PMA_ajaxShowMessage(data.message, false);
460 $("#pma_navigation_reload").click();
462 PMA_ajaxShowMessage(data.error, false);
467 AJAX.registerTeardown('normalization.js', function () {
468 $("#extra").off("click", "#selectNonAtomicCol");
469 $("#splitGo").unbind('click');
470 $('.tblFooters').off("click", "#saveSplit");
471 $("#extra").off("click", "#addNewPrimary");
472 $(".tblFooters").off("click", "#saveNewPrimary");
473 $("#extra").off("click", "#removeRedundant");
474 $("#mainContent p").off("click", "#createPrimaryKey");
475 $("#mainContent").off("click", "#backEditPd");
476 $("#mainContent").off("click", "#showPossiblePd");
477 $("#mainContent").off("click", ".pickPd");
480 AJAX.registerOnload('normalization.js', function() {
482 normalizeto = $("#mainContent").data('normalizeto');
483 $("#extra").on("click", "#selectNonAtomicCol", function() {
484 if ($(this).val() === 'no_such_col') {
487 selectedCol = $(this).val();
491 $("#splitGo").click(function() {
492 if(!selectedCol || selectedCol === '') {
495 var numField = $("#numField").val();
499 "token": PMA_commonParams.get('token'),
500 "ajax_request": true,
501 "db": PMA_commonParams.get('db'),
502 "table": PMA_commonParams.get('table'),
504 "numFields": numField
507 if (data.success === true) {
508 $('#newCols').html(data.message);
509 $('.default_value').hide();
510 $('.enum_notice').hide();
511 $('.tblFooters').html("<input type='submit' id='saveSplit' value='" + PMA_messages.strSave + "'/>" +
512 "<input type='submit' id='cancelSplit' value='" + PMA_messages.strCancel + "' " +
513 "onclick=\"$('#newCols').html('');$(this).parent().html('')\"/>");
519 $('.tblFooters').on("click","#saveSplit", function() {
520 central_column_list = [];
521 if ($("#newCols #field_0_1").val() === '') {
522 $("#newCols #field_0_1").focus();
525 datastring = $('#newCols :input').serialize();
526 datastring += "&ajax_request=1&do_save_data=1&field_where=last";
527 $.post("tbl_addfield.php", datastring, function(data) {
532 "token": PMA_commonParams.get('token'),
533 "ajax_request": true,
534 "db": PMA_commonParams.get('db'),
535 "table": PMA_commonParams.get('table'),
536 "dropped_column": selectedCol,
537 "sql_query": 'ALTER TABLE `' + PMA_commonParams.get('table') + '` DROP `' + selectedCol + '`;',
541 if (data.success === true) {
542 appendHtmlColumnsList();
543 $('#newCols').html('');
544 $('.tblFooters').html('');
546 PMA_ajaxShowMessage(data.error, false);
552 PMA_ajaxShowMessage(data.error, false);
557 $("#extra").on("click", "#addNewPrimary", function() {
561 "token": PMA_commonParams.get('token'),
562 "ajax_request": true,
563 "db": PMA_commonParams.get('db'),
564 "table": PMA_commonParams.get('table'),
565 "addNewPrimary": true
568 if (data.success === true) {
569 $('#newCols').html(data.message);
570 $('.default_value').hide();
571 $('.enum_notice').hide();
572 $('.tblFooters').html("<input type='submit' id='saveNewPrimary' value='" + PMA_messages.strSave + "'/>" +
573 "<input type='submit' id='cancelSplit' value='" + PMA_messages.strCancel + "' " +
574 "onclick=\"$('#newCols').html('');$(this).parent().html('')\"/>");
576 PMA_ajaxShowMessage(data.error, false);
582 $(".tblFooters").on("click", "#saveNewPrimary", function() {
583 var datastring = $('#newCols :input').serialize();
584 datastring += "&field_key[0]=primary_0&ajax_request=1&do_save_data=1&field_where=last";
585 $.post("tbl_addfield.php", datastring, function(data) {
586 if (data.success === true) {
587 $("#mainContent h4").html(PMA_messages.strPrimaryKeyAdded);
588 $("#mainContent p").html(PMA_messages.strToNextStep);
589 $("#mainContent #extra").html('');
590 $("#mainContent #newCols").html('');
591 $('.tblFooters').html('');
592 setTimeout(function() {
596 PMA_ajaxShowMessage(data.error, false);
600 $("#extra").on("click", "#removeRedundant", function() {
601 var dropQuery = 'ALTER TABLE `' + PMA_commonParams.get('table') + '` ';
602 $("#extra input[type=checkbox]:checked").each(function() {
603 dropQuery += 'DROP `' + $(this).val() + '`, ';
605 dropQuery = dropQuery.slice(0, -2);
609 "token": PMA_commonParams.get('token'),
610 "ajax_request": true,
611 "db": PMA_commonParams.get('db'),
612 "table": PMA_commonParams.get('table'),
613 "sql_query": dropQuery,
617 if (data.success === true) {
618 goToStep2('goToFinish1NF');
620 PMA_ajaxShowMessage(data.error, false);
625 $("#extra").on("click", "#moveRepeatingGroup", function() {
626 var repeatingCols = '';
627 $("#extra input[type=checkbox]:checked").each(function() {
628 repeatingCols += $(this).val() + ', ';
631 if (repeatingCols !== '') {
632 var newColName = $("#extra input[type=checkbox]:checked:first").val();
633 repeatingCols = repeatingCols.slice(0, -2);
634 var confirmStr = PMA_sprintf(PMA_messages.strMoveRepeatingGroup, escapeHtml(repeatingCols), escapeHtml(PMA_commonParams.get('table')));
635 confirmStr += '<input type="text" name="repeatGroupTable" placeholder="' + PMA_messages.strNewTablePlaceholder + '"/>' +
636 '( ' + escapeHtml(primary_key.toString()) + ', <input type="text" name="repeatGroupColumn" placeholder="' + PMA_messages.strNewColumnPlaceholder + '" value="' + escapeHtml(newColName) + '">)' +
638 $("#newCols").html(confirmStr);
639 $('.tblFooters').html('<input type="submit" value="' + PMA_messages.strCancel + '" onclick="$(\'#newCols\').html(\'\');$(\'#extra input[type=checkbox]\').removeAttr(\'checked\')"/>' +
640 '<input type="submit" value="' + PMA_messages.strGo + '" onclick="moveRepeatingGroup(\'' + repeatingCols + '\')"/>');
643 $("#mainContent p").on("click", "#createPrimaryKey", function(event) {
644 event.preventDefault();
645 var url = { create_index: 1,
646 server: PMA_commonParams.get('server'),
647 db: PMA_commonParams.get('db'),
648 table: PMA_commonParams.get('table'),
649 token: PMA_commonParams.get('token'),
652 index: {Key_name:'PRIMARY'},
655 var title = PMA_messages.strAddPrimaryKey;
656 indexEditorDialog(url, title, function(){
658 $(".sqlqueryresults").remove();
659 $('.result_query').remove();
660 $('.tblFooters').html('');
661 goToStep2('goToStep3');
665 $("#mainContent").on("click", "#backEditPd", function(){
666 $("#mainContent").html(backup);
668 $("#mainContent").on("click", "#showPossiblePd", function(){
669 if($(this).hasClass('hideList')) {
670 $(this).html('+ ' + PMA_messages.strShowPossiblePd);
671 $(this).removeClass('hideList');
672 $("#newCols").slideToggle("slow");
675 if($("#newCols").html() !== '') {
676 $("#showPossiblePd").html('- ' + PMA_messages.strHidePd);
677 $("#showPossiblePd").addClass('hideList');
678 $("#newCols").slideToggle("slow");
681 $("#newCols").insertAfter("#mainContent h4");
682 $("#newCols").html('<div class="center">' + PMA_messages.strLoading + '<br/>' + PMA_messages.strWaitForPd + '</div>');
686 "token": PMA_commonParams.get('token'),
687 "ajax_request": true,
688 "db": PMA_commonParams.get('db'),
689 "table": PMA_commonParams.get('table'),
692 $("#showPossiblePd").html('- ' + PMA_messages.strHidePd);
693 $("#showPossiblePd").addClass('hideList');
694 $("#newCols").html(data.message);
697 $("#mainContent").on("click", ".pickPd", function(){
698 var strColsLeft = $(this).next('.determinants').html();
699 var colsLeft = strColsLeft.split(',');
700 var strColsRight = $(this).next().next().html();
701 var colsRight = strColsRight.split(',');
702 for (var i in colsRight) {
703 $('form[data-colname="' + colsRight[i].trim() + '"] input[type="checkbox"]').prop('checked', false);
704 for (var j in colsLeft) {
705 $('form[data-colname="' + colsRight[i].trim() + '"] input[value="' + colsLeft[j].trim() + '"]').prop('checked', true);