common.js 9.7 KB

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