PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/js/common.js

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