Fix some Selenium tests
[phpmyadmin.git] / js / common.js
blobfd6f3e434a40f1a9557fabcf2642d67a62c7e379
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             if (common.length > 0) {
90                 sep = argsep;
91             }
92             return Functions.sprintf(
93                 '%s%sserver=%s' + argsep + 'db=%s' + argsep + 'table=%s',
94                 this.get('common_query'),
95                 sep,
96                 encodeURIComponent(this.get('server')),
97                 encodeURIComponent(this.get('db')),
98                 encodeURIComponent(this.get('table'))
99             );
100         }
101     };
102 }());
105  * Holds common parameters such as server, db, table, etc
107  * The content for this is normally loaded from Header.php or
108  * Response.php and executed by ajax.js
109  */
110 // eslint-disable-next-line no-unused-vars
111 var CommonActions = {
112     /**
113      * Saves the database name when it's changed
114      * and reloads the query window, if necessary
115      *
116      * @param newDb string new_db The name of the new database
117      *
118      * @return void
119      */
120     setDb: function (newDb) {
121         if (newDb !== CommonParams.get('db')) {
122             CommonParams.setAll({ 'db': newDb, 'table': '' });
123         }
124     },
125     /**
126      * Opens a database in the main part of the page
127      *
128      * @param newDb string The name of the new database
129      *
130      * @return void
131      */
132     openDb: function (newDb) {
133         CommonParams
134             .set('db', newDb)
135             .set('table', '');
136         this.refreshMain(
137             CommonParams.get('opendb_url')
138         );
139     },
140     /**
141      * Refreshes the main frame
142      *
143      * @param mixed url Undefined to refresh to the same page
144      *                  String to go to a different page, e.g: 'index.php'
145      *
146      * @return void
147      */
148     refreshMain: function (url, callback) {
149         var newUrl = url;
150         if (! newUrl) {
151             newUrl = $('#selflink').find('a').attr('href') || window.location.pathname;
152             newUrl = newUrl.substring(0, newUrl.indexOf('?'));
153         }
154         if (newUrl.indexOf('?') !== -1) {
155             newUrl += CommonParams.getUrlQuery('&');
156         } else {
157             newUrl += CommonParams.getUrlQuery();
158         }
159         $('<a></a>', { href: newUrl })
160             .appendTo('body')
161             .trigger('click')
162             .remove();
163         AJAX.callback = callback;
164     }