Merge pull request #16208 from mauriciofauth/lock-action
[phpmyadmin.git] / js / common.js
blob45bad3038c0c3475538b7f7a0c848a19e94d916a
2 $(function () {
3     Functions.checkNumberOfFields();
4 });
6 /**
7  * Holds common parameters such as server, db, table, etc
8  *
9  * The content for this is normally loaded from Header.php or
10  * Response.php and executed by ajax.js
11  */
12 var CommonParams = (function () {
13     /**
14      * @var hash params An associative array of key value pairs
15      * @access private
16      */
17     var params = {};
18     // The returned object is the public part of the module
19     return {
20         /**
21          * Saves all the key value pair that
22          * are provided in the input array
23          *
24          * @param obj hash The input array
25          *
26          * @return void
27          */
28         setAll: function (obj) {
29             var updateNavigation = false;
30             for (var i in obj) {
31                 if (params[i] !== undefined && params[i] !== obj[i]) {
32                     if (i === 'db' || i === 'table') {
33                         updateNavigation = true;
34                     }
35                 }
36                 params[i] = obj[i];
37             }
38             if (updateNavigation &&
39                     $('#pma_navigation_tree').hasClass('synced')
40             ) {
41                 Navigation.showCurrent();
42             }
43         },
44         /**
45          * Retrieves a value given its key
46          * Returns empty string for undefined values
47          *
48          * @param name string The key
49          *
50          * @return string
51          */
52         get: function (name) {
53             return params[name];
54         },
55         /**
56          * Saves a single key value pair
57          *
58          * @param name  string The key
59          * @param value string The value
60          *
61          * @return self For chainability
62          */
63         set: function (name, value) {
64             var updateNavigation = false;
65             if (name === 'db' || name === 'table' &&
66                 params[name] !== value
67             ) {
68                 updateNavigation = true;
69             }
70             params[name] = value;
71             if (updateNavigation &&
72                     $('#pma_navigation_tree').hasClass('synced')
73             ) {
74                 Navigation.showCurrent();
75             }
76             return this;
77         },
78         /**
79          * Returns the url query string using the saved parameters
80          *
81          * @param {string} separator New separator
82          *
83          * @return string
84          */
85         getUrlQuery: function (separator) {
86             var sep = (typeof separator !== 'undefined') ? separator : '?';
87             var common = this.get('common_query');
88             var argsep = CommonParams.get('arg_separator');
89             return Functions.sprintf(
90                 '%s%sserver=%s' + argsep + 'db=%s' + argsep + 'table=%s',
91                 sep,
92                 common,
93                 encodeURIComponent(this.get('server')),
94                 encodeURIComponent(this.get('db')),
95                 encodeURIComponent(this.get('table'))
96             );
97         }
98     };
99 }());
102  * Holds common parameters such as server, db, table, etc
104  * The content for this is normally loaded from Header.php or
105  * Response.php and executed by ajax.js
106  */
107 // eslint-disable-next-line no-unused-vars
108 var CommonActions = {
109     /**
110      * Saves the database name when it's changed
111      * and reloads the query window, if necessary
112      *
113      * @param newDb string new_db The name of the new database
114      *
115      * @return void
116      */
117     setDb: function (newDb) {
118         if (newDb !== CommonParams.get('db')) {
119             CommonParams.setAll({ 'db': newDb, 'table': '' });
120         }
121     },
122     /**
123      * Opens a database in the main part of the page
124      *
125      * @param newDb string The name of the new database
126      *
127      * @return void
128      */
129     openDb: function (newDb) {
130         CommonParams
131             .set('db', newDb)
132             .set('table', '');
133         this.refreshMain(
134             CommonParams.get('opendb_url')
135         );
136     },
137     /**
138      * Refreshes the main frame
139      *
140      * @param mixed url Undefined to refresh to the same page
141      *                  String to go to a different page, e.g: 'index.php'
142      *
143      * @return void
144      */
145     refreshMain: function (url, callback) {
146         var newUrl = url;
147         if (! newUrl) {
148             newUrl = $('#selflink').find('a').attr('href') || window.location.pathname;
149             newUrl = newUrl.substring(0, newUrl.indexOf('?'));
150         }
151         if (newUrl.indexOf('?') !== -1) {
152             newUrl += CommonParams.getUrlQuery(CommonParams.get('arg_separator'));
153         } else {
154             newUrl += CommonParams.getUrlQuery('?');
155         }
156         $('<a></a>', { href: newUrl })
157             .appendTo('body')
158             .trigger('click')
159             .remove();
160         AJAX.callback = callback;
161     }