navigation.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * function used in or for navigation panel
  4. *
  5. * @package phpMyAdmin-Navigation
  6. */
  7. /**
  8. * updates the tree state in sessionStorage
  9. *
  10. * @returns void
  11. */
  12. function navTreeStateUpdate () {
  13. // update if session storage is supported
  14. if (isStorageSupported('sessionStorage')) {
  15. var storage = window.sessionStorage;
  16. // try catch necessary here to detect whether
  17. // content to be stored exceeds storage capacity
  18. try {
  19. storage.setItem('navTreePaths', JSON.stringify(traverseNavigationForPaths()));
  20. storage.setItem('server', PMA_commonParams.get('server'));
  21. storage.setItem('token', PMA_commonParams.get('token'));
  22. } catch (error) {
  23. // storage capacity exceeded & old navigation tree
  24. // state is no more valid, so remove it
  25. storage.removeItem('navTreePaths');
  26. storage.removeItem('server');
  27. storage.removeItem('token');
  28. }
  29. }
  30. }
  31. /**
  32. * updates the filter state in sessionStorage
  33. *
  34. * @returns void
  35. */
  36. function navFilterStateUpdate (filterName, filterValue) {
  37. if (isStorageSupported('sessionStorage')) {
  38. var storage = window.sessionStorage;
  39. try {
  40. var currentFilter = $.extend({}, JSON.parse(storage.getItem('navTreeSearchFilters')));
  41. var filter = {};
  42. filter[filterName] = filterValue;
  43. currentFilter = $.extend(currentFilter, filter);
  44. storage.setItem('navTreeSearchFilters', JSON.stringify(currentFilter));
  45. } catch (error) {
  46. storage.removeItem('navTreeSearchFilters');
  47. }
  48. }
  49. }
  50. /**
  51. * restores the filter state on navigation reload
  52. *
  53. * @returns void
  54. */
  55. function navFilterStateRestore () {
  56. if (isStorageSupported('sessionStorage')
  57. && typeof window.sessionStorage.navTreeSearchFilters !== 'undefined'
  58. ) {
  59. var searchClauses = JSON.parse(window.sessionStorage.navTreeSearchFilters);
  60. if (Object.keys(searchClauses).length < 1) {
  61. return;
  62. }
  63. // restore database filter if present and not empty
  64. if (searchClauses.hasOwnProperty('dbFilter')
  65. && searchClauses.dbFilter.length
  66. ) {
  67. $obj = $('#pma_navigation_tree');
  68. if (! $obj.data('fastFilter')) {
  69. $obj.data(
  70. 'fastFilter',
  71. new PMA_fastFilter.filter($obj, '')
  72. );
  73. }
  74. $obj.find('li.fast_filter.db_fast_filter input.searchClause')
  75. .val(searchClauses.dbFilter)
  76. .trigger('keyup');
  77. }
  78. // find all table filters present in the tree
  79. $tableFilters = $('#pma_navigation_tree li.database')
  80. .children('div.list_container')
  81. .find('li.fast_filter input.searchClause');
  82. // restore table filters
  83. $tableFilters.each(function () {
  84. $obj = $(this).closest('div.list_container');
  85. // aPath associated with this filter
  86. var filterName = $(this).siblings('input[name=aPath]').val();
  87. // if this table's filter has a state stored in storage
  88. if (searchClauses.hasOwnProperty(filterName)
  89. && searchClauses[filterName].length
  90. ) {
  91. // clear state if item is not visible,
  92. // happens when table filter becomes invisible
  93. // as db filter has already been applied
  94. if (! $obj.is(':visible')) {
  95. navFilterStateUpdate(filterName, '');
  96. return true;
  97. }
  98. if (! $obj.data('fastFilter')) {
  99. $obj.data(
  100. 'fastFilter',
  101. new PMA_fastFilter.filter($obj, '')
  102. );
  103. }
  104. $(this).val(searchClauses[filterName])
  105. .trigger('keyup');
  106. }
  107. });
  108. }
  109. }
  110. /**
  111. * Loads child items of a node and executes a given callback
  112. *
  113. * @param isNode
  114. * @param $expandElem expander
  115. * @param callback callback function
  116. *
  117. * @returns void
  118. */
  119. function loadChildNodes (isNode, $expandElem, callback) {
  120. var $destination = null;
  121. var params = null;
  122. if (isNode) {
  123. if (!$expandElem.hasClass('expander')) {
  124. return;
  125. }
  126. $destination = $expandElem.closest('li');
  127. params = {
  128. aPath: $expandElem.find('span.aPath').text(),
  129. vPath: $expandElem.find('span.vPath').text(),
  130. pos: $expandElem.find('span.pos').text(),
  131. pos2_name: $expandElem.find('span.pos2_name').text(),
  132. pos2_value: $expandElem.find('span.pos2_value').text(),
  133. searchClause: '',
  134. searchClause2: ''
  135. };
  136. if ($expandElem.closest('ul').hasClass('search_results')) {
  137. params.searchClause = PMA_fastFilter.getSearchClause();
  138. params.searchClause2 = PMA_fastFilter.getSearchClause2($expandElem);
  139. }
  140. } else {
  141. $destination = $('#pma_navigation_tree_content');
  142. params = {
  143. aPath: $expandElem.attr('aPath'),
  144. vPath: $expandElem.attr('vPath'),
  145. pos: $expandElem.attr('pos'),
  146. pos2_name: '',
  147. pos2_value: '',
  148. searchClause: '',
  149. searchClause2: ''
  150. };
  151. }
  152. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  153. $.get(url, params, function (data) {
  154. if (typeof data !== 'undefined' && data.success === true) {
  155. $destination.find('div.list_container').remove(); // FIXME: Hack, there shouldn't be a list container there
  156. if (isNode) {
  157. $destination.append(data.message);
  158. $expandElem.addClass('loaded');
  159. } else {
  160. $destination.html(data.message);
  161. $destination.children()
  162. .first()
  163. .css({
  164. border: '0px',
  165. margin: '0em',
  166. padding : '0em'
  167. })
  168. .slideDown('slow');
  169. }
  170. if (data._errors) {
  171. var $errors = $(data._errors);
  172. if ($errors.children().length > 0) {
  173. $('#pma_errors').replaceWith(data._errors);
  174. }
  175. }
  176. if (callback && typeof callback === 'function') {
  177. callback(data);
  178. }
  179. } else if (data.redirect_flag === '1') {
  180. if (window.location.href.indexOf('?') === -1) {
  181. window.location.href += '?session_expired=1';
  182. } else {
  183. window.location.href += PMA_commonParams.get('arg_separator') + 'session_expired=1';
  184. }
  185. window.location.reload();
  186. } else {
  187. var $throbber = $expandElem.find('img.throbber');
  188. $throbber.hide();
  189. var $icon = $expandElem.find('img.ic_b_plus');
  190. $icon.show();
  191. PMA_ajaxShowMessage(data.error, false);
  192. }
  193. });
  194. }
  195. /**
  196. * Collapses a node in navigation tree.
  197. *
  198. * @param $expandElem expander
  199. *
  200. * @returns void
  201. */
  202. function collapseTreeNode ($expandElem) {
  203. var $children = $expandElem.closest('li').children('div.list_container');
  204. var $icon = $expandElem.find('img');
  205. if ($expandElem.hasClass('loaded')) {
  206. if ($icon.is('.ic_b_minus')) {
  207. $icon.removeClass('ic_b_minus').addClass('ic_b_plus');
  208. $children.slideUp('fast');
  209. }
  210. }
  211. $expandElem.blur();
  212. $children.promise().done(navTreeStateUpdate);
  213. }
  214. /**
  215. * Traverse the navigation tree backwards to generate all the actual
  216. * and virtual paths, as well as the positions in the pagination at
  217. * various levels, if necessary.
  218. *
  219. * @return Object
  220. */
  221. function traverseNavigationForPaths () {
  222. var params = {
  223. pos: $('#pma_navigation_tree').find('div.dbselector select').val()
  224. };
  225. if ($('#navi_db_select').length) {
  226. return params;
  227. }
  228. var count = 0;
  229. $('#pma_navigation_tree').find('a.expander:visible').each(function () {
  230. if ($(this).find('img').is('.ic_b_minus') &&
  231. $(this).closest('li').find('div.list_container .ic_b_minus').length === 0
  232. ) {
  233. params['n' + count + '_aPath'] = $(this).find('span.aPath').text();
  234. params['n' + count + '_vPath'] = $(this).find('span.vPath').text();
  235. var pos2_name = $(this).find('span.pos2_name').text();
  236. if (! pos2_name) {
  237. pos2_name = $(this)
  238. .parent()
  239. .parent()
  240. .find('span.pos2_name:last')
  241. .text();
  242. }
  243. var pos2_value = $(this).find('span.pos2_value').text();
  244. if (! pos2_value) {
  245. pos2_value = $(this)
  246. .parent()
  247. .parent()
  248. .find('span.pos2_value:last')
  249. .text();
  250. }
  251. params['n' + count + '_pos2_name'] = pos2_name;
  252. params['n' + count + '_pos2_value'] = pos2_value;
  253. params['n' + count + '_pos3_name'] = $(this).find('span.pos3_name').text();
  254. params['n' + count + '_pos3_value'] = $(this).find('span.pos3_value').text();
  255. count++;
  256. }
  257. });
  258. return params;
  259. }
  260. /**
  261. * Executed on page load
  262. */
  263. $(function () {
  264. if (! $('#pma_navigation').length) {
  265. // Don't bother running any code if the navigation is not even on the page
  266. return;
  267. }
  268. // Do not let the page reload on submitting the fast filter
  269. $(document).on('submit', '.fast_filter', function (event) {
  270. event.preventDefault();
  271. });
  272. // Fire up the resize handlers
  273. new ResizeHandler();
  274. /**
  275. * opens/closes (hides/shows) tree elements
  276. * loads data via ajax
  277. */
  278. $(document).on('click', '#pma_navigation_tree a.expander', function (event) {
  279. event.preventDefault();
  280. event.stopImmediatePropagation();
  281. var $icon = $(this).find('img');
  282. if ($icon.is('.ic_b_plus')) {
  283. expandTreeNode($(this));
  284. } else {
  285. collapseTreeNode($(this));
  286. }
  287. });
  288. /**
  289. * Register event handler for click on the reload
  290. * navigation icon at the top of the panel
  291. */
  292. $(document).on('click', '#pma_navigation_reload', function (event) {
  293. event.preventDefault();
  294. // reload icon object
  295. var $icon = $(this).find('img');
  296. // source of the hidden throbber icon
  297. var icon_throbber_src = $('#pma_navigation').find('.throbber').attr('src');
  298. // source of the reload icon
  299. var icon_reload_src = $icon.attr('src');
  300. // replace the source of the reload icon with the one for throbber
  301. $icon.attr('src', icon_throbber_src);
  302. PMA_reloadNavigation();
  303. // after one second, put back the reload icon
  304. setTimeout(function () {
  305. $icon.attr('src', icon_reload_src);
  306. }, 1000);
  307. });
  308. $(document).on('change', '#navi_db_select', function (event) {
  309. if (! $(this).val()) {
  310. PMA_commonParams.set('db', '');
  311. PMA_reloadNavigation();
  312. }
  313. $(this).closest('form').trigger('submit');
  314. });
  315. /**
  316. * Register event handler for click on the collapse all
  317. * navigation icon at the top of the navigation tree
  318. */
  319. $(document).on('click', '#pma_navigation_collapse', function (event) {
  320. event.preventDefault();
  321. $('#pma_navigation_tree').find('a.expander').each(function () {
  322. var $icon = $(this).find('img');
  323. if ($icon.is('.ic_b_minus')) {
  324. $(this).click();
  325. }
  326. });
  327. });
  328. /**
  329. * Register event handler to toggle
  330. * the 'link with main panel' icon on mouseenter.
  331. */
  332. $(document).on('mouseenter', '#pma_navigation_sync', function (event) {
  333. event.preventDefault();
  334. var synced = $('#pma_navigation_tree').hasClass('synced');
  335. var $img = $('#pma_navigation_sync').children('img');
  336. if (synced) {
  337. $img.removeClass('ic_s_link').addClass('ic_s_unlink');
  338. } else {
  339. $img.removeClass('ic_s_unlink').addClass('ic_s_link');
  340. }
  341. });
  342. /**
  343. * Register event handler to toggle
  344. * the 'link with main panel' icon on mouseout.
  345. */
  346. $(document).on('mouseout', '#pma_navigation_sync', function (event) {
  347. event.preventDefault();
  348. var synced = $('#pma_navigation_tree').hasClass('synced');
  349. var $img = $('#pma_navigation_sync').children('img');
  350. if (synced) {
  351. $img.removeClass('ic_s_unlink').addClass('ic_s_link');
  352. } else {
  353. $img.removeClass('ic_s_link').addClass('ic_s_unlink');
  354. }
  355. });
  356. /**
  357. * Register event handler to toggle
  358. * the linking with main panel behavior
  359. */
  360. $(document).on('click', '#pma_navigation_sync', function (event) {
  361. event.preventDefault();
  362. var synced = $('#pma_navigation_tree').hasClass('synced');
  363. var $img = $('#pma_navigation_sync').children('img');
  364. if (synced) {
  365. $img
  366. .removeClass('ic_s_unlink')
  367. .addClass('ic_s_link')
  368. .attr('alt', PMA_messages.linkWithMain)
  369. .attr('title', PMA_messages.linkWithMain);
  370. $('#pma_navigation_tree')
  371. .removeClass('synced')
  372. .find('li.selected')
  373. .removeClass('selected');
  374. } else {
  375. $img
  376. .removeClass('ic_s_link')
  377. .addClass('ic_s_unlink')
  378. .attr('alt', PMA_messages.unlinkWithMain)
  379. .attr('title', PMA_messages.unlinkWithMain);
  380. $('#pma_navigation_tree').addClass('synced');
  381. PMA_showCurrentNavigation();
  382. }
  383. });
  384. /**
  385. * Bind all "fast filter" events
  386. */
  387. $(document).on('click', '#pma_navigation_tree li.fast_filter span', PMA_fastFilter.events.clear);
  388. $(document).on('focus', '#pma_navigation_tree li.fast_filter input.searchClause', PMA_fastFilter.events.focus);
  389. $(document).on('blur', '#pma_navigation_tree li.fast_filter input.searchClause', PMA_fastFilter.events.blur);
  390. $(document).on('keyup', '#pma_navigation_tree li.fast_filter input.searchClause', PMA_fastFilter.events.keyup);
  391. /**
  392. * Ajax handler for pagination
  393. */
  394. $(document).on('click', '#pma_navigation_tree div.pageselector a.ajax', function (event) {
  395. event.preventDefault();
  396. PMA_navigationTreePagination($(this));
  397. });
  398. /**
  399. * Node highlighting
  400. */
  401. $(document).on(
  402. 'mouseover',
  403. '#pma_navigation_tree.highlight li:not(.fast_filter)',
  404. function () {
  405. if ($('li:visible', this).length === 0) {
  406. $(this).addClass('activePointer');
  407. }
  408. }
  409. );
  410. $(document).on(
  411. 'mouseout',
  412. '#pma_navigation_tree.highlight li:not(.fast_filter)',
  413. function () {
  414. $(this).removeClass('activePointer');
  415. }
  416. );
  417. /** Create a Routine, Trigger or Event */
  418. $(document).on('click', 'li.new_procedure a.ajax, li.new_function a.ajax', function (event) {
  419. event.preventDefault();
  420. var dialog = new RTE.object('routine');
  421. dialog.editorDialog(1, $(this));
  422. });
  423. $(document).on('click', 'li.new_trigger a.ajax', function (event) {
  424. event.preventDefault();
  425. var dialog = new RTE.object('trigger');
  426. dialog.editorDialog(1, $(this));
  427. });
  428. $(document).on('click', 'li.new_event a.ajax', function (event) {
  429. event.preventDefault();
  430. var dialog = new RTE.object('event');
  431. dialog.editorDialog(1, $(this));
  432. });
  433. /** Edit Routines, Triggers or Events */
  434. $(document).on('click', 'li.procedure > a.ajax, li.function > a.ajax', function (event) {
  435. event.preventDefault();
  436. var dialog = new RTE.object('routine');
  437. dialog.editorDialog(0, $(this));
  438. });
  439. $(document).on('click', 'li.trigger > a.ajax', function (event) {
  440. event.preventDefault();
  441. var dialog = new RTE.object('trigger');
  442. dialog.editorDialog(0, $(this));
  443. });
  444. $(document).on('click', 'li.event > a.ajax', function (event) {
  445. event.preventDefault();
  446. var dialog = new RTE.object('event');
  447. dialog.editorDialog(0, $(this));
  448. });
  449. /** Execute Routines */
  450. $(document).on('click', 'li.procedure div a.ajax img,' +
  451. ' li.function div a.ajax img', function (event) {
  452. event.preventDefault();
  453. var dialog = new RTE.object('routine');
  454. dialog.executeDialog($(this).parent());
  455. });
  456. /** Export Triggers and Events */
  457. $(document).on('click', 'li.trigger div:eq(1) a.ajax img,' +
  458. ' li.event div:eq(1) a.ajax img', function (event) {
  459. event.preventDefault();
  460. var dialog = new RTE.object();
  461. dialog.exportDialog($(this).parent());
  462. });
  463. /** New index */
  464. $(document).on('click', '#pma_navigation_tree li.new_index a.ajax', function (event) {
  465. event.preventDefault();
  466. var url = $(this).attr('href').substr(
  467. $(this).attr('href').indexOf('?') + 1
  468. ) + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
  469. var title = PMA_messages.strAddIndex;
  470. indexEditorDialog(url, title);
  471. });
  472. /** Edit index */
  473. $(document).on('click', 'li.index a.ajax', function (event) {
  474. event.preventDefault();
  475. var url = $(this).attr('href').substr(
  476. $(this).attr('href').indexOf('?') + 1
  477. ) + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
  478. var title = PMA_messages.strEditIndex;
  479. indexEditorDialog(url, title);
  480. });
  481. /** New view */
  482. $(document).on('click', 'li.new_view a.ajax', function (event) {
  483. event.preventDefault();
  484. PMA_createViewDialog($(this));
  485. });
  486. /** Hide navigation tree item */
  487. $(document).on('click', 'a.hideNavItem.ajax', function (event) {
  488. event.preventDefault();
  489. var argSep = PMA_commonParams.get('arg_separator');
  490. var params = $(this).getPostData();
  491. params += argSep + 'ajax_request=true' + argSep + 'server=' + PMA_commonParams.get('server');
  492. $.ajax({
  493. type: 'POST',
  494. data: params,
  495. url: $(this).attr('href'),
  496. success: function (data) {
  497. if (typeof data !== 'undefined' && data.success === true) {
  498. PMA_reloadNavigation();
  499. } else {
  500. PMA_ajaxShowMessage(data.error);
  501. }
  502. }
  503. });
  504. });
  505. /** Display a dialog to choose hidden navigation items to show */
  506. $(document).on('click', 'a.showUnhide.ajax', function (event) {
  507. event.preventDefault();
  508. var $msg = PMA_ajaxShowMessage();
  509. var argSep = PMA_commonParams.get('arg_separator');
  510. var params = $(this).getPostData();
  511. params += argSep + 'ajax_request=true';
  512. $.post($(this).attr('href'), params, function (data) {
  513. if (typeof data !== 'undefined' && data.success === true) {
  514. PMA_ajaxRemoveMessage($msg);
  515. var buttonOptions = {};
  516. buttonOptions[PMA_messages.strClose] = function () {
  517. $(this).dialog('close');
  518. };
  519. $('<div/>')
  520. .attr('id', 'unhideNavItemDialog')
  521. .append(data.message)
  522. .dialog({
  523. width: 400,
  524. minWidth: 200,
  525. modal: true,
  526. buttons: buttonOptions,
  527. title: PMA_messages.strUnhideNavItem,
  528. close: function () {
  529. $(this).remove();
  530. }
  531. });
  532. } else {
  533. PMA_ajaxShowMessage(data.error);
  534. }
  535. });
  536. });
  537. /** Show a hidden navigation tree item */
  538. $(document).on('click', 'a.unhideNavItem.ajax', function (event) {
  539. event.preventDefault();
  540. var $tr = $(this).parents('tr');
  541. var $msg = PMA_ajaxShowMessage();
  542. var argSep = PMA_commonParams.get('arg_separator');
  543. var params = $(this).getPostData();
  544. params += argSep + 'ajax_request=true' + argSep + 'server=' + PMA_commonParams.get('server');
  545. $.ajax({
  546. type: 'POST',
  547. data: params,
  548. url: $(this).attr('href'),
  549. success: function (data) {
  550. PMA_ajaxRemoveMessage($msg);
  551. if (typeof data !== 'undefined' && data.success === true) {
  552. $tr.remove();
  553. PMA_reloadNavigation();
  554. } else {
  555. PMA_ajaxShowMessage(data.error);
  556. }
  557. }
  558. });
  559. });
  560. // Add/Remove favorite table using Ajax.
  561. $(document).on('click', '.favorite_table_anchor', function (event) {
  562. event.preventDefault();
  563. $self = $(this);
  564. var anchor_id = $self.attr('id');
  565. if ($self.data('favtargetn') !== null) {
  566. if ($('a[data-favtargets="' + $self.data('favtargetn') + '"]').length > 0) {
  567. $('a[data-favtargets="' + $self.data('favtargetn') + '"]').trigger('click');
  568. return;
  569. }
  570. }
  571. $.ajax({
  572. url: $self.attr('href'),
  573. cache: false,
  574. type: 'POST',
  575. data: {
  576. favorite_tables: (isStorageSupported('localStorage') && typeof window.localStorage.favorite_tables !== 'undefined')
  577. ? window.localStorage.favorite_tables
  578. : '',
  579. server: PMA_commonParams.get('server'),
  580. },
  581. success: function (data) {
  582. if (data.changes) {
  583. $('#pma_favorite_list').html(data.list);
  584. $('#' + anchor_id).parent().html(data.anchor);
  585. PMA_tooltip(
  586. $('#' + anchor_id),
  587. 'a',
  588. $('#' + anchor_id).attr('title')
  589. );
  590. // Update localStorage.
  591. if (isStorageSupported('localStorage')) {
  592. window.localStorage.favorite_tables = data.favorite_tables;
  593. }
  594. } else {
  595. PMA_ajaxShowMessage(data.message);
  596. }
  597. }
  598. });
  599. });
  600. // Check if session storage is supported
  601. if (isStorageSupported('sessionStorage')) {
  602. var storage = window.sessionStorage;
  603. // remove tree from storage if Navi_panel config form is submitted
  604. $(document).on('submit', 'form.config-form', function (event) {
  605. storage.removeItem('navTreePaths');
  606. });
  607. // Initialize if no previous state is defined
  608. if ($('#pma_navigation_tree_content').length &&
  609. typeof storage.navTreePaths === 'undefined'
  610. ) {
  611. PMA_reloadNavigation();
  612. } else if (PMA_commonParams.get('server') === storage.server &&
  613. PMA_commonParams.get('token') === storage.token
  614. ) {
  615. // Reload the tree to the state before page refresh
  616. PMA_reloadNavigation(navFilterStateRestore, JSON.parse(storage.navTreePaths));
  617. } else {
  618. // If the user is different
  619. navTreeStateUpdate();
  620. }
  621. }
  622. });
  623. /**
  624. * Expands a node in navigation tree.
  625. *
  626. * @param $expandElem expander
  627. * @param callback callback function
  628. *
  629. * @returns void
  630. */
  631. function expandTreeNode ($expandElem, callback) {
  632. var $children = $expandElem.closest('li').children('div.list_container');
  633. var $icon = $expandElem.find('img');
  634. if ($expandElem.hasClass('loaded')) {
  635. if ($icon.is('.ic_b_plus')) {
  636. $icon.removeClass('ic_b_plus').addClass('ic_b_minus');
  637. $children.slideDown('fast');
  638. }
  639. if (callback && typeof callback === 'function') {
  640. callback.call();
  641. }
  642. $children.promise().done(navTreeStateUpdate);
  643. } else {
  644. var $throbber = $('#pma_navigation').find('.throbber')
  645. .first()
  646. .clone()
  647. .css({ visibility: 'visible', display: 'block' })
  648. .click(false);
  649. $icon.hide();
  650. $throbber.insertBefore($icon);
  651. loadChildNodes(true, $expandElem, function (data) {
  652. if (typeof data !== 'undefined' && data.success === true) {
  653. var $destination = $expandElem.closest('li');
  654. $icon.removeClass('ic_b_plus').addClass('ic_b_minus');
  655. $children = $destination.children('div.list_container');
  656. $children.slideDown('fast');
  657. if ($destination.find('ul > li').length === 1) {
  658. $destination.find('ul > li')
  659. .find('a.expander.container')
  660. .click();
  661. }
  662. if (callback && typeof callback === 'function') {
  663. callback.call();
  664. }
  665. PMA_showFullName($destination);
  666. } else {
  667. PMA_ajaxShowMessage(data.error, false);
  668. }
  669. $icon.show();
  670. $throbber.remove();
  671. $children.promise().done(navTreeStateUpdate);
  672. });
  673. }
  674. $expandElem.blur();
  675. }
  676. /**
  677. * Auto-scrolls the newly chosen database
  678. *
  679. * @param object $element The element to set to view
  680. * @param boolean $forceToTop Whether to force scroll to top
  681. *
  682. */
  683. function scrollToView ($element, $forceToTop) {
  684. navFilterStateRestore();
  685. var $container = $('#pma_navigation_tree_content');
  686. var elemTop = $element.offset().top - $container.offset().top;
  687. var textHeight = 20;
  688. var scrollPadding = 20; // extra padding from top of bottom when scrolling to view
  689. if (elemTop < 0 || $forceToTop) {
  690. $container.stop().animate({
  691. scrollTop: elemTop + $container.scrollTop() - scrollPadding
  692. });
  693. } else if (elemTop + textHeight > $container.height()) {
  694. $container.stop().animate({
  695. scrollTop: elemTop + textHeight - $container.height() + $container.scrollTop() + scrollPadding
  696. });
  697. }
  698. }
  699. /**
  700. * Expand the navigation and highlight the current database or table/view
  701. *
  702. * @returns void
  703. */
  704. function PMA_showCurrentNavigation () {
  705. var db = PMA_commonParams.get('db');
  706. var table = PMA_commonParams.get('table');
  707. $('#pma_navigation_tree')
  708. .find('li.selected')
  709. .removeClass('selected');
  710. if (db) {
  711. var $dbItem = findLoadedItem(
  712. $('#pma_navigation_tree').find('> div'), db, 'database', !table
  713. );
  714. if ($('#navi_db_select').length &&
  715. $('option:selected', $('#navi_db_select')).length
  716. ) {
  717. if (! PMA_selectCurrentDb()) {
  718. return;
  719. }
  720. // If loaded database in navigation is not same as current one
  721. if ($('#pma_navigation_tree_content').find('span.loaded_db:first').text()
  722. !== $('#navi_db_select').val()
  723. ) {
  724. loadChildNodes(false, $('option:selected', $('#navi_db_select')), function (data) {
  725. handleTableOrDb(table, $('#pma_navigation_tree_content'));
  726. var $children = $('#pma_navigation_tree_content').children('div.list_container');
  727. $children.promise().done(navTreeStateUpdate);
  728. });
  729. } else {
  730. handleTableOrDb(table, $('#pma_navigation_tree_content'));
  731. }
  732. } else if ($dbItem) {
  733. var $expander = $dbItem.children('div:first').children('a.expander');
  734. // if not loaded or loaded but collapsed
  735. if (! $expander.hasClass('loaded') ||
  736. $expander.find('img').is('.ic_b_plus')
  737. ) {
  738. expandTreeNode($expander, function () {
  739. handleTableOrDb(table, $dbItem);
  740. });
  741. } else {
  742. handleTableOrDb(table, $dbItem);
  743. }
  744. }
  745. } else if ($('#navi_db_select').length && $('#navi_db_select').val()) {
  746. $('#navi_db_select').val('').hide().trigger('change');
  747. }
  748. PMA_showFullName($('#pma_navigation_tree'));
  749. function handleTableOrDb (table, $dbItem) {
  750. if (table) {
  751. loadAndHighlightTableOrView($dbItem, table);
  752. } else {
  753. var $container = $dbItem.children('div.list_container');
  754. var $tableContainer = $container.children('ul').children('li.tableContainer');
  755. if ($tableContainer.length > 0) {
  756. var $expander = $tableContainer.children('div:first').children('a.expander');
  757. $tableContainer.addClass('selected');
  758. expandTreeNode($expander, function () {
  759. scrollToView($dbItem, true);
  760. });
  761. } else {
  762. scrollToView($dbItem, true);
  763. }
  764. }
  765. }
  766. function findLoadedItem ($container, name, clazz, doSelect) {
  767. var ret = false;
  768. $container.children('ul').children('li').each(function () {
  769. var $li = $(this);
  770. // this is a navigation group, recurse
  771. if ($li.is('.navGroup')) {
  772. var $container = $li.children('div.list_container');
  773. var $childRet = findLoadedItem(
  774. $container, name, clazz, doSelect
  775. );
  776. if ($childRet) {
  777. ret = $childRet;
  778. return false;
  779. }
  780. } else { // this is a real navigation item
  781. // name and class matches
  782. if (((clazz && $li.is('.' + clazz)) || ! clazz) &&
  783. $li.children('a').text() === name) {
  784. if (doSelect) {
  785. $li.addClass('selected');
  786. }
  787. // taverse up and expand and parent navigation groups
  788. $li.parents('.navGroup').each(function () {
  789. var $cont = $(this).children('div.list_container');
  790. if (! $cont.is(':visible')) {
  791. $(this)
  792. .children('div:first')
  793. .children('a.expander')
  794. .click();
  795. }
  796. });
  797. ret = $li;
  798. return false;
  799. }
  800. }
  801. });
  802. return ret;
  803. }
  804. function loadAndHighlightTableOrView ($dbItem, itemName) {
  805. var $container = $dbItem.children('div.list_container');
  806. var $expander;
  807. var $whichItem = isItemInContainer($container, itemName, 'li.table, li.view');
  808. // If item already there in some container
  809. if ($whichItem) {
  810. // get the relevant container while may also be a subcontainer
  811. var $relatedContainer = $whichItem.closest('li.subContainer').length
  812. ? $whichItem.closest('li.subContainer')
  813. : $dbItem;
  814. $whichItem = findLoadedItem(
  815. $relatedContainer.children('div.list_container'),
  816. itemName, null, true
  817. );
  818. // Show directly
  819. showTableOrView($whichItem, $relatedContainer.children('div:first').children('a.expander'));
  820. // else if item not there, try loading once
  821. } else {
  822. var $sub_containers = $dbItem.find('.subContainer');
  823. // If there are subContainers i.e. tableContainer or viewContainer
  824. if ($sub_containers.length > 0) {
  825. var $containers = [];
  826. $sub_containers.each(function (index) {
  827. $containers[index] = $(this);
  828. $expander = $containers[index]
  829. .children('div:first')
  830. .children('a.expander');
  831. if (! $expander.hasClass('loaded')) {
  832. loadAndShowTableOrView($expander, $containers[index], itemName);
  833. }
  834. });
  835. // else if no subContainers
  836. } else {
  837. $expander = $dbItem
  838. .children('div:first')
  839. .children('a.expander');
  840. if (! $expander.hasClass('loaded')) {
  841. loadAndShowTableOrView($expander, $dbItem, itemName);
  842. }
  843. }
  844. }
  845. }
  846. function loadAndShowTableOrView ($expander, $relatedContainer, itemName) {
  847. loadChildNodes(true, $expander, function (data) {
  848. var $whichItem = findLoadedItem(
  849. $relatedContainer.children('div.list_container'),
  850. itemName, null, true
  851. );
  852. if ($whichItem) {
  853. showTableOrView($whichItem, $expander);
  854. }
  855. });
  856. }
  857. function showTableOrView ($whichItem, $expander) {
  858. expandTreeNode($expander, function (data) {
  859. if ($whichItem) {
  860. scrollToView($whichItem, false);
  861. }
  862. });
  863. }
  864. function isItemInContainer ($container, name, clazz) {
  865. var $whichItem = null;
  866. $items = $container.find(clazz);
  867. var found = false;
  868. $items.each(function () {
  869. if ($(this).children('a').text() === name) {
  870. $whichItem = $(this);
  871. return false;
  872. }
  873. });
  874. return $whichItem;
  875. }
  876. }
  877. /**
  878. * Disable navigation panel settings
  879. *
  880. * @return void
  881. */
  882. function PMA_disableNaviSettings () {
  883. $('#pma_navigation_settings_icon').addClass('hide');
  884. $('#pma_navigation_settings').remove();
  885. }
  886. /**
  887. * Ensure that navigation panel settings is properly setup.
  888. * If not, set it up
  889. *
  890. * @return void
  891. */
  892. function PMA_ensureNaviSettings (selflink) {
  893. $('#pma_navigation_settings_icon').removeClass('hide');
  894. if (!$('#pma_navigation_settings').length) {
  895. var params = {
  896. getNaviSettings: true,
  897. server: PMA_commonParams.get('server'),
  898. };
  899. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  900. $.post(url, params, function (data) {
  901. if (typeof data !== 'undefined' && data.success) {
  902. $('#pma_navi_settings_container').html(data.message);
  903. setupRestoreField();
  904. setupValidation();
  905. setupConfigTabs();
  906. $('#pma_navigation_settings').find('form').attr('action', selflink);
  907. } else {
  908. PMA_ajaxShowMessage(data.error);
  909. }
  910. });
  911. } else {
  912. $('#pma_navigation_settings').find('form').attr('action', selflink);
  913. }
  914. }
  915. /**
  916. * Reloads the whole navigation tree while preserving its state
  917. *
  918. * @param function the callback function
  919. * @param Object stored navigation paths
  920. *
  921. * @return void
  922. */
  923. function PMA_reloadNavigation (callback, paths) {
  924. var params = {
  925. reload: true,
  926. no_debug: true,
  927. server: PMA_commonParams.get('server'),
  928. };
  929. paths = paths || traverseNavigationForPaths();
  930. $.extend(params, paths);
  931. if ($('#navi_db_select').length) {
  932. params.db = PMA_commonParams.get('db');
  933. requestNaviReload(params);
  934. return;
  935. }
  936. requestNaviReload(params);
  937. function requestNaviReload (params) {
  938. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  939. $.post(url, params, function (data) {
  940. if (typeof data !== 'undefined' && data.success) {
  941. $('#pma_navigation_tree').html(data.message).children('div').show();
  942. if ($('#pma_navigation_tree').hasClass('synced')) {
  943. PMA_selectCurrentDb();
  944. PMA_showCurrentNavigation();
  945. }
  946. // Fire the callback, if any
  947. if (typeof callback === 'function') {
  948. callback.call();
  949. }
  950. navTreeStateUpdate();
  951. } else {
  952. PMA_ajaxShowMessage(data.error);
  953. }
  954. });
  955. }
  956. }
  957. function PMA_selectCurrentDb () {
  958. var $naviDbSelect = $('#navi_db_select');
  959. if (!$naviDbSelect.length) {
  960. return false;
  961. }
  962. if (PMA_commonParams.get('db')) { // db selected
  963. $naviDbSelect.show();
  964. }
  965. $naviDbSelect.val(PMA_commonParams.get('db'));
  966. return $naviDbSelect.val() === PMA_commonParams.get('db');
  967. }
  968. /**
  969. * Handles any requests to change the page in a branch of a tree
  970. *
  971. * This can be called from link click or select change event handlers
  972. *
  973. * @param object $this A jQuery object that points to the element that
  974. * initiated the action of changing the page
  975. *
  976. * @return void
  977. */
  978. function PMA_navigationTreePagination ($this) {
  979. var $msgbox = PMA_ajaxShowMessage();
  980. var isDbSelector = $this.closest('div.pageselector').is('.dbselector');
  981. var url;
  982. var params;
  983. if ($this[0].tagName === 'A') {
  984. url = $this.attr('href');
  985. params = 'ajax_request=true';
  986. } else { // tagName === 'SELECT'
  987. url = 'navigation.php';
  988. params = $this.closest('form').serialize() + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
  989. }
  990. var searchClause = PMA_fastFilter.getSearchClause();
  991. if (searchClause) {
  992. params += PMA_commonParams.get('arg_separator') + 'searchClause=' + encodeURIComponent(searchClause);
  993. }
  994. if (isDbSelector) {
  995. params += PMA_commonParams.get('arg_separator') + 'full=true';
  996. } else {
  997. var searchClause2 = PMA_fastFilter.getSearchClause2($this);
  998. if (searchClause2) {
  999. params += PMA_commonParams.get('arg_separator') + 'searchClause2=' + encodeURIComponent(searchClause2);
  1000. }
  1001. }
  1002. $.post(url, params, function (data) {
  1003. if (typeof data !== 'undefined' && data.success) {
  1004. PMA_ajaxRemoveMessage($msgbox);
  1005. if (isDbSelector) {
  1006. var val = PMA_fastFilter.getSearchClause();
  1007. $('#pma_navigation_tree')
  1008. .html(data.message)
  1009. .children('div')
  1010. .show();
  1011. if (val) {
  1012. $('#pma_navigation_tree')
  1013. .find('li.fast_filter input.searchClause')
  1014. .val(val);
  1015. }
  1016. } else {
  1017. var $parent = $this.closest('div.list_container').parent();
  1018. var val = PMA_fastFilter.getSearchClause2($this);
  1019. $this.closest('div.list_container').html(
  1020. $(data.message).children().show()
  1021. );
  1022. if (val) {
  1023. $parent.find('li.fast_filter input.searchClause').val(val);
  1024. }
  1025. $parent.find('span.pos2_value:first').text(
  1026. $parent.find('span.pos2_value:last').text()
  1027. );
  1028. $parent.find('span.pos3_value:first').text(
  1029. $parent.find('span.pos3_value:last').text()
  1030. );
  1031. }
  1032. } else {
  1033. PMA_ajaxShowMessage(data.error);
  1034. PMA_handleRedirectAndReload(data);
  1035. }
  1036. navTreeStateUpdate();
  1037. });
  1038. }
  1039. /**
  1040. * @var ResizeHandler Custom object that manages the resizing of the navigation
  1041. *
  1042. * XXX: Must only be ever instanciated once
  1043. * XXX: Inside event handlers the 'this' object is accessed as 'event.data.resize_handler'
  1044. */
  1045. var ResizeHandler = function () {
  1046. /**
  1047. * @var int panel_width Used by the collapser to know where to go
  1048. * back to when uncollapsing the panel
  1049. */
  1050. this.panel_width = 0;
  1051. /**
  1052. * @var string left Used to provide support for RTL languages
  1053. */
  1054. this.left = $('html').attr('dir') === 'ltr' ? 'left' : 'right';
  1055. /**
  1056. * Adjusts the width of the navigation panel to the specified value
  1057. *
  1058. * @param int pos Navigation width in pixels
  1059. *
  1060. * @return void
  1061. */
  1062. this.setWidth = function (pos) {
  1063. if (typeof pos !== 'number') {
  1064. pos = 240;
  1065. }
  1066. var $resizer = $('#pma_navigation_resizer');
  1067. var resizer_width = $resizer.width();
  1068. var $collapser = $('#pma_navigation_collapser');
  1069. var windowWidth = $(window).width();
  1070. $('#pma_navigation').width(pos);
  1071. $('body').css('margin-' + this.left, pos + 'px');
  1072. $('#floating_menubar, #pma_console')
  1073. .css('margin-' + this.left, (pos + resizer_width) + 'px');
  1074. $resizer.css(this.left, pos + 'px');
  1075. if (pos === 0) {
  1076. $collapser
  1077. .css(this.left, pos + resizer_width)
  1078. .html(this.getSymbol(pos))
  1079. .prop('title', PMA_messages.strShowPanel);
  1080. } else if (windowWidth > 768) {
  1081. $collapser
  1082. .css(this.left, pos)
  1083. .html(this.getSymbol(pos))
  1084. .prop('title', PMA_messages.strHidePanel);
  1085. $('#pma_navigation_resizer').css({ 'width': '3px' });
  1086. } else {
  1087. $collapser
  1088. .css(this.left, windowWidth - 22)
  1089. .html(this.getSymbol(100))
  1090. .prop('title', PMA_messages.strHidePanel);
  1091. $('#pma_navigation').width(windowWidth);
  1092. $('body').css('margin-' + this.left, '0px');
  1093. $('#pma_navigation_resizer').css({ 'width': '0px' });
  1094. }
  1095. setTimeout(function () {
  1096. $(window).trigger('resize');
  1097. }, 4);
  1098. };
  1099. /**
  1100. * Returns the horizontal position of the mouse,
  1101. * relative to the outer side of the navigation panel
  1102. *
  1103. * @param int pos Navigation width in pixels
  1104. *
  1105. * @return void
  1106. */
  1107. this.getPos = function (event) {
  1108. var pos = event.pageX;
  1109. var windowWidth = $(window).width();
  1110. var windowScroll = $(window).scrollLeft();
  1111. pos = pos - windowScroll;
  1112. if (this.left !== 'left') {
  1113. pos = windowWidth - event.pageX;
  1114. }
  1115. if (pos < 0) {
  1116. pos = 0;
  1117. } else if (pos + 100 >= windowWidth) {
  1118. pos = windowWidth - 100;
  1119. } else {
  1120. this.panel_width = 0;
  1121. }
  1122. return pos;
  1123. };
  1124. /**
  1125. * Returns the HTML code for the arrow symbol used in the collapser
  1126. *
  1127. * @param int width The width of the panel
  1128. *
  1129. * @return string
  1130. */
  1131. this.getSymbol = function (width) {
  1132. if (this.left === 'left') {
  1133. if (width === 0) {
  1134. return '&rarr;';
  1135. } else {
  1136. return '&larr;';
  1137. }
  1138. } else {
  1139. if (width === 0) {
  1140. return '&larr;';
  1141. } else {
  1142. return '&rarr;';
  1143. }
  1144. }
  1145. };
  1146. /**
  1147. * Event handler for initiating a resize of the panel
  1148. *
  1149. * @param object e Event data (contains a reference to resizeHandler)
  1150. *
  1151. * @return void
  1152. */
  1153. this.mousedown = function (event) {
  1154. event.preventDefault();
  1155. $(document)
  1156. .on('mousemove', { 'resize_handler': event.data.resize_handler },
  1157. $.throttle(event.data.resize_handler.mousemove, 4))
  1158. .on('mouseup', { 'resize_handler': event.data.resize_handler },
  1159. event.data.resize_handler.mouseup);
  1160. $('body').css('cursor', 'col-resize');
  1161. };
  1162. /**
  1163. * Event handler for terminating a resize of the panel
  1164. *
  1165. * @param object e Event data (contains a reference to resizeHandler)
  1166. *
  1167. * @return void
  1168. */
  1169. this.mouseup = function (event) {
  1170. $('body').css('cursor', '');
  1171. configSet('NavigationWidth', event.data.resize_handler.getPos(event));
  1172. $('#topmenu').menuResizer('resize');
  1173. $(document)
  1174. .off('mousemove')
  1175. .off('mouseup');
  1176. };
  1177. /**
  1178. * Event handler for updating the panel during a resize operation
  1179. *
  1180. * @param object e Event data (contains a reference to resizeHandler)
  1181. *
  1182. * @return void
  1183. */
  1184. this.mousemove = function (event) {
  1185. event.preventDefault();
  1186. var pos = event.data.resize_handler.getPos(event);
  1187. event.data.resize_handler.setWidth(pos);
  1188. if ($('.sticky_columns').length !== 0) {
  1189. handleAllStickyColumns();
  1190. }
  1191. };
  1192. /**
  1193. * Event handler for collapsing the panel
  1194. *
  1195. * @param object e Event data (contains a reference to resizeHandler)
  1196. *
  1197. * @return void
  1198. */
  1199. this.collapse = function (event) {
  1200. event.preventDefault();
  1201. var panel_width = event.data.resize_handler.panel_width;
  1202. var width = $('#pma_navigation').width();
  1203. if (width === 0 && panel_width === 0) {
  1204. panel_width = 240;
  1205. }
  1206. configSet('NavigationWidth', panel_width);
  1207. event.data.resize_handler.setWidth(panel_width);
  1208. event.data.resize_handler.panel_width = width;
  1209. };
  1210. /**
  1211. * Event handler for resizing the navigation tree height on window resize
  1212. *
  1213. * @return void
  1214. */
  1215. this.treeResize = function (event) {
  1216. var $nav = $('#pma_navigation');
  1217. var $nav_tree = $('#pma_navigation_tree');
  1218. var $nav_header = $('#pma_navigation_header');
  1219. var $nav_tree_content = $('#pma_navigation_tree_content');
  1220. $nav_tree.height($nav.height() - $nav_header.height());
  1221. if ($nav_tree_content.length > 0) {
  1222. $nav_tree_content.height($nav_tree.height() - $nav_tree_content.position().top);
  1223. } else {
  1224. // TODO: in fast filter search response there is no #pma_navigation_tree_content, needs to be added in php
  1225. $nav_tree.css({
  1226. 'overflow-y': 'auto'
  1227. });
  1228. }
  1229. // Set content bottom space beacuse of console
  1230. $('body').css('margin-bottom', $('#pma_console').height() + 'px');
  1231. };
  1232. // Hide the pma_navigation initially when loaded on mobile
  1233. if ($(window).width() < 768) {
  1234. this.setWidth(0);
  1235. } else {
  1236. this.setWidth(configGet('NavigationWidth', false));
  1237. $('#topmenu').menuResizer('resize');
  1238. }
  1239. // Register the events for the resizer and the collapser
  1240. $(document).on('mousedown', '#pma_navigation_resizer', { 'resize_handler': this }, this.mousedown);
  1241. $(document).on('click', '#pma_navigation_collapser', { 'resize_handler': this }, this.collapse);
  1242. // Add the correct arrow symbol to the collapser
  1243. $('#pma_navigation_collapser').html(this.getSymbol($('#pma_navigation').width()));
  1244. // Fix navigation tree height
  1245. $(window).on('resize', this.treeResize);
  1246. // need to call this now and then, browser might decide
  1247. // to show/hide horizontal scrollbars depending on page content width
  1248. setInterval(this.treeResize, 2000);
  1249. this.treeResize();
  1250. }; // End of ResizeHandler
  1251. /**
  1252. * @var object PMA_fastFilter Handles the functionality that allows filtering
  1253. * of the items in a branch of the navigation tree
  1254. */
  1255. var PMA_fastFilter = {
  1256. /**
  1257. * Construct for the asynchronous fast filter functionality
  1258. *
  1259. * @param object $this A jQuery object pointing to the list container
  1260. * which is the nearest parent of the fast filter
  1261. * @param string searchClause The query string for the filter
  1262. *
  1263. * @return new PMA_fastFilter.filter object
  1264. */
  1265. filter: function ($this, searchClause) {
  1266. /**
  1267. * @var object $this A jQuery object pointing to the list container
  1268. * which is the nearest parent of the fast filter
  1269. */
  1270. this.$this = $this;
  1271. /**
  1272. * @var bool searchClause The query string for the filter
  1273. */
  1274. this.searchClause = searchClause;
  1275. /**
  1276. * @var object $clone A clone of the original contents
  1277. * of the navigation branch before
  1278. * the fast filter was applied
  1279. */
  1280. this.$clone = $this.clone();
  1281. /**
  1282. * @var object xhr A reference to the ajax request that is currently running
  1283. */
  1284. this.xhr = null;
  1285. /**
  1286. * @var int timeout Used to delay the request for asynchronous search
  1287. */
  1288. this.timeout = null;
  1289. var $filterInput = $this.find('li.fast_filter input.searchClause');
  1290. if ($filterInput.length !== 0 &&
  1291. $filterInput.val() !== '' &&
  1292. $filterInput.val() !== $filterInput[0].defaultValue
  1293. ) {
  1294. this.request();
  1295. }
  1296. },
  1297. /**
  1298. * Gets the query string from the database fast filter form
  1299. *
  1300. * @return string
  1301. */
  1302. getSearchClause: function () {
  1303. var retval = '';
  1304. var $input = $('#pma_navigation_tree')
  1305. .find('li.fast_filter.db_fast_filter input.searchClause');
  1306. if ($input.length && $input.val() !== $input[0].defaultValue) {
  1307. retval = $input.val();
  1308. }
  1309. return retval;
  1310. },
  1311. /**
  1312. * Gets the query string from a second level item's fast filter form
  1313. * The retrieval is done by trasversing the navigation tree backwards
  1314. *
  1315. * @return string
  1316. */
  1317. getSearchClause2: function ($this) {
  1318. var $filterContainer = $this.closest('div.list_container');
  1319. var $filterInput = $([]);
  1320. if ($filterContainer
  1321. .find('li.fast_filter:not(.db_fast_filter) input.searchClause')
  1322. .length !== 0) {
  1323. $filterInput = $filterContainer
  1324. .find('li.fast_filter:not(.db_fast_filter) input.searchClause');
  1325. }
  1326. var searchClause2 = '';
  1327. if ($filterInput.length !== 0 &&
  1328. $filterInput.first().val() !== $filterInput[0].defaultValue
  1329. ) {
  1330. searchClause2 = $filterInput.val();
  1331. }
  1332. return searchClause2;
  1333. },
  1334. /**
  1335. * @var hash events A list of functions that are bound to DOM events
  1336. * at the top of this file
  1337. */
  1338. events: {
  1339. focus: function (event) {
  1340. var $obj = $(this).closest('div.list_container');
  1341. if (! $obj.data('fastFilter')) {
  1342. $obj.data(
  1343. 'fastFilter',
  1344. new PMA_fastFilter.filter($obj, $(this).val())
  1345. );
  1346. }
  1347. if ($(this).val() === this.defaultValue) {
  1348. $(this).val('');
  1349. } else {
  1350. $(this).select();
  1351. }
  1352. },
  1353. blur: function (event) {
  1354. if ($(this).val() === '') {
  1355. $(this).val(this.defaultValue);
  1356. }
  1357. var $obj = $(this).closest('div.list_container');
  1358. if ($(this).val() === this.defaultValue && $obj.data('fastFilter')) {
  1359. $obj.data('fastFilter').restore();
  1360. }
  1361. },
  1362. keyup: function (event) {
  1363. var $obj = $(this).closest('div.list_container');
  1364. var str = '';
  1365. if ($(this).val() !== this.defaultValue && $(this).val() !== '') {
  1366. $obj.find('div.pageselector').hide();
  1367. str = $(this).val();
  1368. }
  1369. /**
  1370. * FIXME at the server level a value match is done while on
  1371. * the client side it is a regex match. These two should be aligned
  1372. */
  1373. // regex used for filtering.
  1374. var regex;
  1375. try {
  1376. regex = new RegExp(str, 'i');
  1377. } catch (err) {
  1378. return;
  1379. }
  1380. // this is the div that houses the items to be filtered by this filter.
  1381. var outerContainer;
  1382. if ($(this).closest('li.fast_filter').is('.db_fast_filter')) {
  1383. outerContainer = $('#pma_navigation_tree_content');
  1384. } else {
  1385. outerContainer = $obj;
  1386. }
  1387. // filters items that are directly under the div as well as grouped in
  1388. // groups. Does not filter child items (i.e. a database search does
  1389. // not filter tables)
  1390. var item_filter = function ($curr) {
  1391. $curr.children('ul').children('li.navGroup').each(function () {
  1392. $(this).children('div.list_container').each(function () {
  1393. item_filter($(this)); // recursive
  1394. });
  1395. });
  1396. $curr.children('ul').children('li').children('a').not('.container').each(function () {
  1397. if (regex.test($(this).text())) {
  1398. $(this).parent().show().removeClass('hidden');
  1399. } else {
  1400. $(this).parent().hide().addClass('hidden');
  1401. }
  1402. });
  1403. };
  1404. item_filter(outerContainer);
  1405. // hides containers that does not have any visible children
  1406. var container_filter = function ($curr) {
  1407. $curr.children('ul').children('li.navGroup').each(function () {
  1408. var $group = $(this);
  1409. $group.children('div.list_container').each(function () {
  1410. container_filter($(this)); // recursive
  1411. });
  1412. $group.show().removeClass('hidden');
  1413. if ($group.children('div.list_container').children('ul')
  1414. .children('li').not('.hidden').length === 0) {
  1415. $group.hide().addClass('hidden');
  1416. }
  1417. });
  1418. };
  1419. container_filter(outerContainer);
  1420. if ($(this).val() !== this.defaultValue && $(this).val() !== '') {
  1421. if (! $obj.data('fastFilter')) {
  1422. $obj.data(
  1423. 'fastFilter',
  1424. new PMA_fastFilter.filter($obj, $(this).val())
  1425. );
  1426. } else {
  1427. if (event.keyCode === 13) {
  1428. $obj.data('fastFilter').update($(this).val());
  1429. }
  1430. }
  1431. } else if ($obj.data('fastFilter')) {
  1432. $obj.data('fastFilter').restore(true);
  1433. }
  1434. // update filter state
  1435. var filterName;
  1436. if ($(this).attr('name') === 'searchClause2') {
  1437. filterName = $(this).siblings('input[name=aPath]').val();
  1438. } else {
  1439. filterName = 'dbFilter';
  1440. }
  1441. navFilterStateUpdate(filterName, $(this).val());
  1442. },
  1443. clear: function (event) {
  1444. event.stopPropagation();
  1445. // Clear the input and apply the fast filter with empty input
  1446. var filter = $(this).closest('div.list_container').data('fastFilter');
  1447. if (filter) {
  1448. filter.restore();
  1449. }
  1450. var value = $(this).prev()[0].defaultValue;
  1451. $(this).prev().val(value).trigger('keyup');
  1452. }
  1453. }
  1454. };
  1455. /**
  1456. * Handles a change in the search clause
  1457. *
  1458. * @param string searchClause The query string for the filter
  1459. *
  1460. * @return void
  1461. */
  1462. PMA_fastFilter.filter.prototype.update = function (searchClause) {
  1463. if (this.searchClause !== searchClause) {
  1464. this.searchClause = searchClause;
  1465. this.request();
  1466. }
  1467. };
  1468. /**
  1469. * After a delay of 250mS, initiates a request to retrieve search results
  1470. * Multiple calls to this function will always abort the previous request
  1471. *
  1472. * @return void
  1473. */
  1474. PMA_fastFilter.filter.prototype.request = function () {
  1475. var self = this;
  1476. if (self.$this.find('li.fast_filter').find('img.throbber').length === 0) {
  1477. self.$this.find('li.fast_filter').append(
  1478. $('<div class="throbber"></div>').append(
  1479. $('#pma_navigation_content')
  1480. .find('img.throbber')
  1481. .clone()
  1482. .css({ visibility: 'visible', display: 'block' })
  1483. )
  1484. );
  1485. }
  1486. if (self.xhr) {
  1487. self.xhr.abort();
  1488. }
  1489. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  1490. var params = self.$this.find('> ul > li > form.fast_filter').first().serialize();
  1491. if (self.$this.find('> ul > li > form.fast_filter:first input[name=searchClause]').length === 0) {
  1492. var $input = $('#pma_navigation_tree').find('li.fast_filter.db_fast_filter input.searchClause');
  1493. if ($input.length && $input.val() !== $input[0].defaultValue) {
  1494. params += PMA_commonParams.get('arg_separator') + 'searchClause=' + encodeURIComponent($input.val());
  1495. }
  1496. }
  1497. self.xhr = $.ajax({
  1498. url: url,
  1499. type: 'post',
  1500. dataType: 'json',
  1501. data: params,
  1502. complete: function (jqXHR, status) {
  1503. if (status !== 'abort') {
  1504. var data = JSON.parse(jqXHR.responseText);
  1505. self.$this.find('li.fast_filter').find('div.throbber').remove();
  1506. if (data && data.results) {
  1507. self.swap.apply(self, [data.message]);
  1508. }
  1509. }
  1510. }
  1511. });
  1512. };
  1513. /**
  1514. * Replaces the contents of the navigation branch with the search results
  1515. *
  1516. * @param string list The search results
  1517. *
  1518. * @return void
  1519. */
  1520. PMA_fastFilter.filter.prototype.swap = function (list) {
  1521. this.$this
  1522. .html($(list).html())
  1523. .children()
  1524. .show()
  1525. .end()
  1526. .find('li.fast_filter input.searchClause')
  1527. .val(this.searchClause);
  1528. this.$this.data('fastFilter', this);
  1529. };
  1530. /**
  1531. * Restores the navigation to the original state after the fast filter is cleared
  1532. *
  1533. * @param bool focus Whether to also focus the input box of the fast filter
  1534. *
  1535. * @return void
  1536. */
  1537. PMA_fastFilter.filter.prototype.restore = function (focus) {
  1538. if (this.$this.children('ul').first().hasClass('search_results')) {
  1539. this.$this.html(this.$clone.html()).children().show();
  1540. this.$this.data('fastFilter', this);
  1541. if (focus) {
  1542. this.$this.find('li.fast_filter input.searchClause').focus();
  1543. }
  1544. }
  1545. this.searchClause = '';
  1546. this.$this.find('div.pageselector').show();
  1547. this.$this.find('div.throbber').remove();
  1548. };
  1549. /**
  1550. * Show full name when cursor hover and name not shown completely
  1551. *
  1552. * @param object $containerELem Container element
  1553. *
  1554. * @return void
  1555. */
  1556. function PMA_showFullName ($containerELem) {
  1557. $containerELem.find('.hover_show_full').mouseenter(function () {
  1558. /** mouseenter */
  1559. var $this = $(this);
  1560. var thisOffset = $this.offset();
  1561. if ($this.text() === '') {
  1562. return;
  1563. }
  1564. var $parent = $this.parent();
  1565. if (($parent.offset().left + $parent.outerWidth())
  1566. < (thisOffset.left + $this.outerWidth())) {
  1567. var $fullNameLayer = $('#full_name_layer');
  1568. if ($fullNameLayer.length === 0) {
  1569. $('body').append('<div id="full_name_layer" class="hide"></div>');
  1570. $('#full_name_layer').mouseleave(function () {
  1571. /** mouseleave */
  1572. $(this).addClass('hide')
  1573. .removeClass('hovering');
  1574. }).mouseenter(function () {
  1575. /** mouseenter */
  1576. $(this).addClass('hovering');
  1577. });
  1578. $fullNameLayer = $('#full_name_layer');
  1579. }
  1580. $fullNameLayer.removeClass('hide');
  1581. $fullNameLayer.css({ left: thisOffset.left, top: thisOffset.top });
  1582. $fullNameLayer.html($this.clone());
  1583. setTimeout(function () {
  1584. if (! $fullNameLayer.hasClass('hovering')) {
  1585. $fullNameLayer.trigger('mouseleave');
  1586. }
  1587. }, 200);
  1588. }
  1589. });
  1590. }