common.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * Functionality for communicating with the querywindow
  4. */
  5. $(function () {
  6. /**
  7. * Event handler for click on the open query window link
  8. * in the top menu of the navigation panel
  9. */
  10. $('#pma_open_querywindow').click(function (event) {
  11. event.preventDefault();
  12. PMA_querywindow.focus();
  13. });
  14. });
  15. /**
  16. * Holds common parameters such as server, db, table, etc
  17. *
  18. * The content for this is normally loaded from Header.class.php or
  19. * Response.class.php and executed by ajax.js
  20. */
  21. var PMA_commonParams = (function () {
  22. /**
  23. * @var hash params An associative array of key value pairs
  24. * @access private
  25. */
  26. var params = {};
  27. // The returned object is the public part of the module
  28. return {
  29. /**
  30. * Saves all the key value pair that
  31. * are provided in the input array
  32. *
  33. * @param hash obj The input array
  34. *
  35. * @return void
  36. */
  37. setAll: function (obj) {
  38. var reload = false;
  39. var updateNavigation = false;
  40. for (var i in obj) {
  41. if (params[i] !== undefined && params[i] !== obj[i]) {
  42. reload = true;
  43. if (i == 'db' || i == 'table') {
  44. updateNavigation = true;
  45. }
  46. }
  47. params[i] = obj[i];
  48. }
  49. if (updateNavigation) {
  50. PMA_showCurrentNavigation();
  51. }
  52. if (reload) {
  53. PMA_querywindow.refresh();
  54. }
  55. },
  56. /**
  57. * Retrieves a value given its key
  58. * Returns empty string for undefined values
  59. *
  60. * @param string name The key
  61. *
  62. * @return string
  63. */
  64. get: function (name) {
  65. return params[name] || '';
  66. },
  67. /**
  68. * Saves a single key value pair
  69. *
  70. * @param string name The key
  71. * @param string value The value
  72. *
  73. * @return self For chainability
  74. */
  75. set: function (name, value) {
  76. var updateNavigation = false;
  77. if (params[name] !== undefined && params[name] !== value) {
  78. PMA_querywindow.refresh();
  79. if (name == 'db' || name == 'table') {
  80. updateNavigation = true;
  81. }
  82. }
  83. params[name] = value;
  84. if (updateNavigation) {
  85. PMA_showCurrentNavigation();
  86. }
  87. return this;
  88. },
  89. /**
  90. * Returns the url query string using the saved parameters
  91. *
  92. * @return string
  93. */
  94. getUrlQuery: function () {
  95. return $.sprintf(
  96. '?%s&server=%s&db=%s&table=%s',
  97. this.get('common_query'),
  98. encodeURIComponent(this.get('server')),
  99. encodeURIComponent(this.get('db')),
  100. encodeURIComponent(this.get('table'))
  101. );
  102. }
  103. };
  104. })();
  105. /**
  106. * Holds common parameters such as server, db, table, etc
  107. *
  108. * The content for this is normally loaded from Header.class.php or
  109. * Response.class.php and executed by ajax.js
  110. */
  111. var PMA_commonActions = {
  112. /**
  113. * Saves the database name when it's changed
  114. * and reloads the query window, if necessary
  115. *
  116. * @param string new_db The name of the new database
  117. *
  118. * @return void
  119. */
  120. setDb: function (new_db) {
  121. if (new_db != PMA_commonParams.get('db')) {
  122. PMA_commonParams.setAll({'db': new_db, 'table': ''});
  123. }
  124. },
  125. /**
  126. * Opens a database in the main part of the page
  127. *
  128. * @param string new_db The name of the new database
  129. *
  130. * @return void
  131. */
  132. openDb: function (new_db) {
  133. PMA_commonParams
  134. .set('db', new_db)
  135. .set('table', '');
  136. PMA_querywindow.refresh();
  137. this.refreshMain(
  138. PMA_commonParams.get('opendb_url')
  139. );
  140. },
  141. /**
  142. * Refreshes the main frame
  143. *
  144. * @param mixed url Undefined to refresh to the same page
  145. * String to go to a different page, e.g: 'index.php'
  146. *
  147. * @return void
  148. */
  149. refreshMain: function (url, callback) {
  150. if (! url) {
  151. url = $('#selflink a').attr('href');
  152. url = url.substring(0, url.indexOf('?'));
  153. }
  154. url += PMA_commonParams.getUrlQuery();
  155. $('<a />', {href: url})
  156. .appendTo('body')
  157. .click()
  158. .remove();
  159. AJAX._callback = callback;
  160. }
  161. };
  162. /**
  163. * Common functions used for communicating with the querywindow
  164. */
  165. var PMA_querywindow = (function ($, window) {
  166. /**
  167. * @var Object querywindow Reference to the window
  168. * object of the querywindow
  169. * @access private
  170. */
  171. var querywindow = {};
  172. /**
  173. * @var string queryToLoad Stores the SQL query that is to be displayed
  174. * in the querywindow when it is ready
  175. * @access private
  176. */
  177. var queryToLoad = '';
  178. // The returned object is the public part of the module
  179. return {
  180. /**
  181. * Opens the query window
  182. *
  183. * @param mixed url Undefined to open the default page
  184. * String to go to a different
  185. *
  186. * @return void
  187. */
  188. open: function (url, sql_query) {
  189. if (! url) {
  190. url = 'querywindow.php' + PMA_commonParams.getUrlQuery();
  191. }
  192. if (sql_query) {
  193. url += '&sql_query=' + encodeURIComponent(sql_query);
  194. }
  195. if (! querywindow.closed && querywindow.location) {
  196. var href = querywindow.location.href;
  197. if (href != url
  198. && href != PMA_commonParams.get('pma_absolute_uri') + url
  199. ) {
  200. if (PMA_commonParams.get('safari_browser')) {
  201. querywindow.location.href = targeturl;
  202. } else {
  203. querywindow.location.replace(targeturl);
  204. }
  205. querywindow.focus();
  206. }
  207. } else {
  208. querywindow = window.open(
  209. url + '&init=1',
  210. '',
  211. 'toolbar=0,location=0,directories=0,status=1,'
  212. + 'menubar=0,scrollbars=yes,resizable=yes,'
  213. + 'width=' + PMA_commonParams.get('querywindow_width') + ','
  214. + 'height=' + PMA_commonParams.get('querywindow_height')
  215. );
  216. }
  217. if (! querywindow.opener) {
  218. querywindow.opener = window.window;
  219. }
  220. if (window.focus) {
  221. querywindow.focus();
  222. }
  223. },
  224. /**
  225. * Opens, if necessary, focuses the query window
  226. * and displays an SQL query.
  227. *
  228. * @param string sql_query The SQL query to display in
  229. * the query window
  230. *
  231. * @return void
  232. */
  233. focus: function (sql_query) {
  234. if (! querywindow || querywindow.closed || ! querywindow.location) {
  235. // we need first to open the window and cannot pass the query with it
  236. // as we dont know if the query exceeds max url length
  237. queryToLoad = sql_query;
  238. this.open(false, sql_query);
  239. } else {
  240. //var querywindow = querywindow;
  241. var hiddenqueryform = querywindow
  242. .document
  243. .getElementById('hiddenqueryform');
  244. if (hiddenqueryform.querydisplay_tab != 'sql' ) {
  245. hiddenqueryform.querydisplay_tab.value = "sql";
  246. hiddenqueryform.sql_query.value = sql_query;
  247. $(hiddenqueryform).addClass('disableAjax');
  248. hiddenqueryform.submit();
  249. querywindow.focus();
  250. } else {
  251. querywindow.focus();
  252. }
  253. }
  254. },
  255. /**
  256. * Refreshes the query window given a url
  257. *
  258. * @param string url Where to go to
  259. *
  260. * @return void
  261. */
  262. refresh: function (url) {
  263. if (! querywindow.closed && querywindow.location) {
  264. var $form = $(querywindow.document).find('#sqlqueryform');
  265. if ($form.find('#checkbox_lock:checked').length == 0) {
  266. PMA_querywindow.open(url);
  267. }
  268. }
  269. },
  270. /**
  271. * Reloads the query window given the details
  272. * of a db, a table and an sql_query
  273. *
  274. * @param string db The name of the database
  275. * @param string table The name of the table
  276. * @param string sql_query The SQL query to be displayed
  277. *
  278. * @return void
  279. */
  280. reload: function (db, table, sql_query) {
  281. if (! querywindow.closed && querywindow.location) {
  282. var $form = $(querywindow.document).find('#sqlqueryform');
  283. if ($form.find('#checkbox_lock:checked').length == 0) {
  284. var $hiddenform = $(querywindow.document)
  285. .find('#hiddenqueryform');
  286. $hiddenform.find('input[name=db]').val(db);
  287. $hiddenform.find('input[name=table]').val(table);
  288. if (sql_query) {
  289. $hiddenform.find('input[name=sql_query]').val(sql_query);
  290. }
  291. $hiddenform.addClass('disableAjax').submit();
  292. }
  293. }
  294. }
  295. };
  296. })(jQuery, window);