Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / js / db_operations.js
blob2e59173086f47bcf4f748de56aa0f39b82483596
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    function used in server privilege pages
4  * @name            Database Operations
5  *
6  * @requires    jQuery
7  * @requires    jQueryUI
8  * @requires    js/functions.js
9  *
10  */
12 /**
13  * Ajax event handlers here for db_operations.php
14  *
15  * Actions Ajaxified here:
16  * Rename Database
17  * Copy Database
18  * Change Charset
19  * Drop Database
20  */
22 /**
23  * Unbind all event handlers before tearing down a page
24  */
25 AJAX.registerTeardown('db_operations.js', function () {
26     $(document).off('submit', "#rename_db_form.ajax");
27     $(document).off('submit', "#copy_db_form.ajax");
28     $(document).off('submit', "#change_db_charset_form.ajax");
29     $(document).off('click', "#drop_db_anchor.ajax");
30 });
32 AJAX.registerOnload('db_operations.js', function () {
34     /**
35      * Ajax event handlers for 'Rename Database'
36      */
37     $(document).on('submit', "#rename_db_form.ajax", function (event) {
38         event.preventDefault();
40         var old_db_name = PMA_commonParams.get('db');
41         var new_db_name = $('#new_db_name').val();
43         if (new_db_name == old_db_name) {
44             PMA_ajaxShowMessage(PMA_messages.strDropDatabaseStrongWarning);
45             return false;
46         }
48         var $form = $(this);
50         var question = escapeHtml('CREATE DATABASE ' + new_db_name + ' / DROP DATABASE ' + old_db_name);
52         PMA_prepareForAjaxRequest($form);
54         $form.PMA_confirm(question, $form.attr('action'), function (url) {
55             PMA_ajaxShowMessage(PMA_messages.strRenamingDatabases, false);
56             $.get(url, $("#rename_db_form").serialize() + '&is_js_confirmed=1', function (data) {
57                 if (typeof data !== 'undefined' && data.success === true) {
58                     PMA_ajaxShowMessage(data.message);
59                     PMA_commonParams.set('db', data.newname);
61                     PMA_reloadNavigation(function () {
62                         $('#pma_navigation_tree')
63                             .find("a:not('.expander')")
64                             .each(function (index) {
65                                 var $thisAnchor = $(this);
66                                 if ($thisAnchor.text() == data.newname) {
67                                     // simulate a click on the new db name
68                                     // in navigation
69                                     $thisAnchor.trigger('click');
70                                 }
71                             });
72                     });
73                 } else {
74                     PMA_ajaxShowMessage(data.error, false);
75                 }
76             }); // end $.get()
77         });
78     }); // end Rename Database
80     /**
81      * Ajax Event Handler for 'Copy Database'
82      */
83     $(document).on('submit', "#copy_db_form.ajax", function (event) {
84         event.preventDefault();
85         PMA_ajaxShowMessage(PMA_messages.strCopyingDatabase, false);
86         var $form = $(this);
87         PMA_prepareForAjaxRequest($form);
88         $.get($form.attr('action'), $form.serialize(), function (data) {
89             // use messages that stay on screen
90             $('div.success, div.error').fadeOut();
91             if (typeof data !== 'undefined' && data.success === true) {
92                 if ($("#checkbox_switch").is(":checked")) {
93                     PMA_commonParams.set('db', data.newname);
94                     PMA_commonActions.refreshMain(false, function () {
95                         PMA_ajaxShowMessage(data.message);
96                     });
97                 } else {
98                     PMA_commonParams.set('db', data.db);
99                     PMA_ajaxShowMessage(data.message);
100                 }
101                 PMA_reloadNavigation();
102             } else {
103                 PMA_ajaxShowMessage(data.error, false);
104             }
105         }); // end $.get
106     }); // end copy database
108     /**
109      * Ajax Event handler for 'Change Charset' of the database
110      */
111     $(document).on('submit', "#change_db_charset_form.ajax", function (event) {
112         event.preventDefault();
113         var $form = $(this);
114         PMA_prepareForAjaxRequest($form);
115         PMA_ajaxShowMessage(PMA_messages.strChangingCharset);
116         $.get($form.attr('action'), $form.serialize() + "&submitcollation=1", function (data) {
117             if (typeof data !== 'undefined' && data.success === true) {
118                 PMA_ajaxShowMessage(data.message);
119             } else {
120                 PMA_ajaxShowMessage(data.error, false);
121             }
122         }); // end $.get()
123     }); // end change charset
125     /**
126      * Ajax event handlers for Drop Database
127      */
128     $(document).on('click', "#drop_db_anchor.ajax", function (event) {
129         event.preventDefault();
130         /**
131          * @var question    String containing the question to be asked for confirmation
132          */
133         var question = PMA_messages.strDropDatabaseStrongWarning + ' ';
134         question += PMA_sprintf(
135             PMA_messages.strDoYouReally,
136             'DROP DATABASE ' + escapeHtml(PMA_commonParams.get('db'))
137         );
138         $(this).PMA_confirm(question, $(this).attr('href'), function (url) {
139             PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
140             $.get(url, {'is_js_confirmed': '1', 'ajax_request': true}, function (data) {
141                 if (typeof data !== 'undefined' && data.success) {
142                     //Database deleted successfully, refresh both the frames
143                     PMA_reloadNavigation();
144                     PMA_commonParams.set('db', '');
145                     PMA_commonActions.refreshMain(
146                         'server_databases.php',
147                         function () {
148                             PMA_ajaxShowMessage(data.message);
149                         }
150                     );
151                 } else {
152                     PMA_ajaxShowMessage(data.error, false);
153                 }
154             });
155         });
156     });