Merge pull request #15404 from jfcherng/fix-php74-curly-braces
[phpmyadmin.git] / js / server_databases.js
bloba892ca8bfdbf78c91c91989e05ac56600ca4e7bd
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used on the server databases list page
4  * @name            Server Databases
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @required    js/functions.js
9  */
11 /**
12  * Unbind all event handlers before tearing down a page
13  */
14 AJAX.registerTeardown('server_databases.js', function () {
15     $(document).off('submit', '#dbStatsForm');
16     $(document).off('submit', '#create_database_form.ajax');
17 });
19 /**
20  * AJAX scripts for server_databases.php
21  *
22  * Actions ajaxified here:
23  * Drop Databases
24  *
25  */
26 AJAX.registerOnload('server_databases.js', function () {
27     /**
28      * Attach Event Handler for 'Drop Databases'
29      */
30     $(document).on('submit', '#dbStatsForm', function (event) {
31         event.preventDefault();
33         var $form = $(this);
35         /**
36          * @var selected_dbs Array containing the names of the checked databases
37          */
38         var selected_dbs = [];
39         // loop over all checked checkboxes, except the .checkall_box checkbox
40         $form.find('input:checkbox:checked:not(.checkall_box)').each(function () {
41             $(this).closest('tr').addClass('removeMe');
42             selected_dbs[selected_dbs.length] = 'DROP DATABASE `' + escapeHtml($(this).val()) + '`;';
43         });
44         if (! selected_dbs.length) {
45             PMA_ajaxShowMessage(
46                 $('<div class="notice" />').text(
47                     PMA_messages.strNoDatabasesSelected
48                 ),
49                 2000
50             );
51             return;
52         }
53         /**
54          * @var question    String containing the question to be asked for confirmation
55          */
56         var question = PMA_messages.strDropDatabaseStrongWarning + ' ' +
57             PMA_sprintf(PMA_messages.strDoYouReally, selected_dbs.join('<br />'));
59         var argsep = PMA_commonParams.get('arg_separator');
60         $(this).PMA_confirm(
61             question,
62             $form.prop('action') + '?' + $(this).serialize() +
63                 argsep + 'drop_selected_dbs=1',
64             function (url) {
65                 PMA_ajaxShowMessage(PMA_messages.strProcessingRequest, false);
67                 var parts = url.split('?');
68                 var params = getJSConfirmCommonParam(this, parts[1]);
70                 $.post(parts[0], params, function (data) {
71                     if (typeof data !== 'undefined' && data.success === true) {
72                         PMA_ajaxShowMessage(data.message);
74                         var $rowsToRemove = $form.find('tr.removeMe');
75                         var $databasesCount = $('#filter-rows-count');
76                         var newCount = parseInt($databasesCount.text(), 10) - $rowsToRemove.length;
77                         $databasesCount.text(newCount);
79                         $rowsToRemove.remove();
80                         $form.find('tbody').PMA_sort_table('.name');
81                         if ($form.find('tbody').find('tr').length === 0) {
82                             // user just dropped the last db on this page
83                             PMA_commonActions.refreshMain();
84                         }
85                         PMA_reloadNavigation();
86                     } else {
87                         $form.find('tr.removeMe').removeClass('removeMe');
88                         PMA_ajaxShowMessage(data.error, false);
89                     }
90                 }); // end $.post()
91             }
92         ); // end $.PMA_confirm()
93     }); // end of Drop Database action
95     /**
96      * Attach Ajax event handlers for 'Create Database'.
97      */
98     $(document).on('submit', '#create_database_form.ajax', function (event) {
99         event.preventDefault();
101         var $form = $(this);
103         // TODO Remove this section when all browsers support HTML5 "required" property
104         var newDbNameInput = $form.find('input[name=new_db]');
105         if (newDbNameInput.val() === '') {
106             newDbNameInput.focus();
107             alert(PMA_messages.strFormEmpty);
108             return;
109         }
110         // end remove
112         PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
113         PMA_prepareForAjaxRequest($form);
115         $.post($form.attr('action'), $form.serialize(), function (data) {
116             if (typeof data !== 'undefined' && data.success === true) {
117                 PMA_ajaxShowMessage(data.message);
119                 var $databases_count_object = $('#filter-rows-count');
120                 var databases_count = parseInt($databases_count_object.text(), 10) + 1;
121                 $databases_count_object.text(databases_count);
122                 PMA_reloadNavigation();
124                 // make ajax request to load db structure page - taken from ajax.js
125                 var dbStruct_url = data.url_query;
126                 dbStruct_url = dbStruct_url.replace(/amp;/ig, '');
127                 var params = 'ajax_request=true' + PMA_commonParams.get('arg_separator') + 'ajax_page_request=true';
128                 if (! (history && history.pushState)) {
129                     params += PMA_MicroHistory.menus.getRequestParam();
130                 }
131                 $.get(dbStruct_url, params, AJAX.responseHandler);
132             } else {
133                 PMA_ajaxShowMessage(data.error, false);
134             }
135         }); // end $.post()
136     }); // end $(document).on()
138     /* Don't show filter if number of databases are very few */
139     var databasesCount = $('#filter-rows-count').html();
140     if (databasesCount <= 10) {
141         $('#tableFilter').hide();
142     }
144     var tableRows = $('.server_databases');
145     $.each(tableRows, function (index, item) {
146         $(this).click(function () {
147             PMA_commonActions.setDb($(this).attr('data'));
148         });
149     });
150 }); // end $()