navigation.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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. $.ajax({
  490. type: 'POST',
  491. data: {
  492. server: PMA_commonParams.get('server'),
  493. },
  494. url: $(this).attr('href') + PMA_commonParams.get('arg_separator') + 'ajax_request=true',
  495. success: function (data) {
  496. if (typeof data !== 'undefined' && data.success === true) {
  497. PMA_reloadNavigation();
  498. } else {
  499. PMA_ajaxShowMessage(data.error);
  500. }
  501. }
  502. });
  503. });
  504. /** Display a dialog to choose hidden navigation items to show */
  505. $(document).on('click', 'a.showUnhide.ajax', function (event) {
  506. event.preventDefault();
  507. var $msg = PMA_ajaxShowMessage();
  508. $.get($(this).attr('href') + PMA_commonParams.get('arg_separator') + 'ajax_request=1', function (data) {
  509. if (typeof data !== 'undefined' && data.success === true) {
  510. PMA_ajaxRemoveMessage($msg);
  511. var buttonOptions = {};
  512. buttonOptions[PMA_messages.strClose] = function () {
  513. $(this).dialog('close');
  514. };
  515. $('<div/>')
  516. .attr('id', 'unhideNavItemDialog')
  517. .append(data.message)
  518. .dialog({
  519. width: 400,
  520. minWidth: 200,
  521. modal: true,
  522. buttons: buttonOptions,
  523. title: PMA_messages.strUnhideNavItem,
  524. close: function () {
  525. $(this).remove();
  526. }
  527. });
  528. } else {
  529. PMA_ajaxShowMessage(data.error);
  530. }
  531. });
  532. });
  533. /** Show a hidden navigation tree item */
  534. $(document).on('click', 'a.unhideNavItem.ajax', function (event) {
  535. event.preventDefault();
  536. var $tr = $(this).parents('tr');
  537. var $msg = PMA_ajaxShowMessage();
  538. $.ajax({
  539. type: 'POST',
  540. data: {
  541. server: PMA_commonParams.get('server'),
  542. },
  543. url: $(this).attr('href') + PMA_commonParams.get('arg_separator') + 'ajax_request=true',
  544. success: function (data) {
  545. PMA_ajaxRemoveMessage($msg);
  546. if (typeof data !== 'undefined' && data.success === true) {
  547. $tr.remove();
  548. PMA_reloadNavigation();
  549. } else {
  550. PMA_ajaxShowMessage(data.error);
  551. }
  552. }
  553. });
  554. });
  555. // Add/Remove favorite table using Ajax.
  556. $(document).on('click', '.favorite_table_anchor', function (event) {
  557. event.preventDefault();
  558. $self = $(this);
  559. var anchor_id = $self.attr('id');
  560. if ($self.data('favtargetn') !== null) {
  561. if ($('a[data-favtargets="' + $self.data('favtargetn') + '"]').length > 0) {
  562. $('a[data-favtargets="' + $self.data('favtargetn') + '"]').trigger('click');
  563. return;
  564. }
  565. }
  566. $.ajax({
  567. url: $self.attr('href'),
  568. cache: false,
  569. type: 'POST',
  570. data: {
  571. favorite_tables: (isStorageSupported('localStorage') && typeof window.localStorage.favorite_tables !== 'undefined')
  572. ? window.localStorage.favorite_tables
  573. : '',
  574. server: PMA_commonParams.get('server'),
  575. },
  576. success: function (data) {
  577. if (data.changes) {
  578. $('#pma_favorite_list').html(data.list);
  579. $('#' + anchor_id).parent().html(data.anchor);
  580. PMA_tooltip(
  581. $('#' + anchor_id),
  582. 'a',
  583. $('#' + anchor_id).attr('title')
  584. );
  585. // Update localStorage.
  586. if (isStorageSupported('localStorage')) {
  587. window.localStorage.favorite_tables = data.favorite_tables;
  588. }
  589. } else {
  590. PMA_ajaxShowMessage(data.message);
  591. }
  592. }
  593. });
  594. });
  595. // Check if session storage is supported
  596. if (isStorageSupported('sessionStorage')) {
  597. var storage = window.sessionStorage;
  598. // remove tree from storage if Navi_panel config form is submitted
  599. $(document).on('submit', 'form.config-form', function (event) {
  600. storage.removeItem('navTreePaths');
  601. });
  602. // Initialize if no previous state is defined
  603. if ($('#pma_navigation_tree_content').length &&
  604. typeof storage.navTreePaths === 'undefined'
  605. ) {
  606. PMA_reloadNavigation();
  607. } else if (PMA_commonParams.get('server') === storage.server &&
  608. PMA_commonParams.get('token') === storage.token
  609. ) {
  610. // Reload the tree to the state before page refresh
  611. PMA_reloadNavigation(navFilterStateRestore, JSON.parse(storage.navTreePaths));
  612. } else {
  613. // If the user is different
  614. navTreeStateUpdate();
  615. }
  616. }
  617. });
  618. /**
  619. * Expands a node in navigation tree.
  620. *
  621. * @param $expandElem expander
  622. * @param callback callback function
  623. *
  624. * @returns void
  625. */
  626. function expandTreeNode ($expandElem, callback) {
  627. var $children = $expandElem.closest('li').children('div.list_container');
  628. var $icon = $expandElem.find('img');
  629. if ($expandElem.hasClass('loaded')) {
  630. if ($icon.is('.ic_b_plus')) {
  631. $icon.removeClass('ic_b_plus').addClass('ic_b_minus');
  632. $children.slideDown('fast');
  633. }
  634. if (callback && typeof callback === 'function') {
  635. callback.call();
  636. }
  637. $children.promise().done(navTreeStateUpdate);
  638. } else {
  639. var $throbber = $('#pma_navigation').find('.throbber')
  640. .first()
  641. .clone()
  642. .css({ visibility: 'visible', display: 'block' })
  643. .click(false);
  644. $icon.hide();
  645. $throbber.insertBefore($icon);
  646. loadChildNodes(true, $expandElem, function (data) {
  647. if (typeof data !== 'undefined' && data.success === true) {
  648. var $destination = $expandElem.closest('li');
  649. $icon.removeClass('ic_b_plus').addClass('ic_b_minus');
  650. $children = $destination.children('div.list_container');
  651. $children.slideDown('fast');
  652. if ($destination.find('ul > li').length === 1) {
  653. $destination.find('ul > li')
  654. .find('a.expander.container')
  655. .click();
  656. }
  657. if (callback && typeof callback === 'function') {
  658. callback.call();
  659. }
  660. PMA_showFullName($destination);
  661. } else {
  662. PMA_ajaxShowMessage(data.error, false);
  663. }
  664. $icon.show();
  665. $throbber.remove();
  666. $children.promise().done(navTreeStateUpdate);
  667. });
  668. }
  669. $expandElem.blur();
  670. }
  671. /**
  672. * Auto-scrolls the newly chosen database
  673. *
  674. * @param object $element The element to set to view
  675. * @param boolean $forceToTop Whether to force scroll to top
  676. *
  677. */
  678. function scrollToView ($element, $forceToTop) {
  679. navFilterStateRestore();
  680. var $container = $('#pma_navigation_tree_content');
  681. var elemTop = $element.offset().top - $container.offset().top;
  682. var textHeight = 20;
  683. var scrollPadding = 20; // extra padding from top of bottom when scrolling to view
  684. if (elemTop < 0 || $forceToTop) {
  685. $container.stop().animate({
  686. scrollTop: elemTop + $container.scrollTop() - scrollPadding
  687. });
  688. } else if (elemTop + textHeight > $container.height()) {
  689. $container.stop().animate({
  690. scrollTop: elemTop + textHeight - $container.height() + $container.scrollTop() + scrollPadding
  691. });
  692. }
  693. }
  694. /**
  695. * Expand the navigation and highlight the current database or table/view
  696. *
  697. * @returns void
  698. */
  699. function PMA_showCurrentNavigation () {
  700. var db = PMA_commonParams.get('db');
  701. var table = PMA_commonParams.get('table');
  702. $('#pma_navigation_tree')
  703. .find('li.selected')
  704. .removeClass('selected');
  705. if (db) {
  706. var $dbItem = findLoadedItem(
  707. $('#pma_navigation_tree').find('> div'), db, 'database', !table
  708. );
  709. if ($('#navi_db_select').length &&
  710. $('option:selected', $('#navi_db_select')).length
  711. ) {
  712. if (! PMA_selectCurrentDb()) {
  713. return;
  714. }
  715. // If loaded database in navigation is not same as current one
  716. if ($('#pma_navigation_tree_content').find('span.loaded_db:first').text()
  717. !== $('#navi_db_select').val()
  718. ) {
  719. loadChildNodes(false, $('option:selected', $('#navi_db_select')), function (data) {
  720. handleTableOrDb(table, $('#pma_navigation_tree_content'));
  721. var $children = $('#pma_navigation_tree_content').children('div.list_container');
  722. $children.promise().done(navTreeStateUpdate);
  723. });
  724. } else {
  725. handleTableOrDb(table, $('#pma_navigation_tree_content'));
  726. }
  727. } else if ($dbItem) {
  728. var $expander = $dbItem.children('div:first').children('a.expander');
  729. // if not loaded or loaded but collapsed
  730. if (! $expander.hasClass('loaded') ||
  731. $expander.find('img').is('.ic_b_plus')
  732. ) {
  733. expandTreeNode($expander, function () {
  734. handleTableOrDb(table, $dbItem);
  735. });
  736. } else {
  737. handleTableOrDb(table, $dbItem);
  738. }
  739. }
  740. } else if ($('#navi_db_select').length && $('#navi_db_select').val()) {
  741. $('#navi_db_select').val('').hide().trigger('change');
  742. }
  743. PMA_showFullName($('#pma_navigation_tree'));
  744. function handleTableOrDb (table, $dbItem) {
  745. if (table) {
  746. loadAndHighlightTableOrView($dbItem, table);
  747. } else {
  748. var $container = $dbItem.children('div.list_container');
  749. var $tableContainer = $container.children('ul').children('li.tableContainer');
  750. if ($tableContainer.length > 0) {
  751. var $expander = $tableContainer.children('div:first').children('a.expander');
  752. $tableContainer.addClass('selected');
  753. expandTreeNode($expander, function () {
  754. scrollToView($dbItem, true);
  755. });
  756. } else {
  757. scrollToView($dbItem, true);
  758. }
  759. }
  760. }
  761. function findLoadedItem ($container, name, clazz, doSelect) {
  762. var ret = false;
  763. $container.children('ul').children('li').each(function () {
  764. var $li = $(this);
  765. // this is a navigation group, recurse
  766. if ($li.is('.navGroup')) {
  767. var $container = $li.children('div.list_container');
  768. var $childRet = findLoadedItem(
  769. $container, name, clazz, doSelect
  770. );
  771. if ($childRet) {
  772. ret = $childRet;
  773. return false;
  774. }
  775. } else { // this is a real navigation item
  776. // name and class matches
  777. if (((clazz && $li.is('.' + clazz)) || ! clazz) &&
  778. $li.children('a').text() === name) {
  779. if (doSelect) {
  780. $li.addClass('selected');
  781. }
  782. // taverse up and expand and parent navigation groups
  783. $li.parents('.navGroup').each(function () {
  784. var $cont = $(this).children('div.list_container');
  785. if (! $cont.is(':visible')) {
  786. $(this)
  787. .children('div:first')
  788. .children('a.expander')
  789. .click();
  790. }
  791. });
  792. ret = $li;
  793. return false;
  794. }
  795. }
  796. });
  797. return ret;
  798. }
  799. function loadAndHighlightTableOrView ($dbItem, itemName) {
  800. var $container = $dbItem.children('div.list_container');
  801. var $expander;
  802. var $whichItem = isItemInContainer($container, itemName, 'li.table, li.view');
  803. // If item already there in some container
  804. if ($whichItem) {
  805. // get the relevant container while may also be a subcontainer
  806. var $relatedContainer = $whichItem.closest('li.subContainer').length
  807. ? $whichItem.closest('li.subContainer')
  808. : $dbItem;
  809. $whichItem = findLoadedItem(
  810. $relatedContainer.children('div.list_container'),
  811. itemName, null, true
  812. );
  813. // Show directly
  814. showTableOrView($whichItem, $relatedContainer.children('div:first').children('a.expander'));
  815. // else if item not there, try loading once
  816. } else {
  817. var $sub_containers = $dbItem.find('.subContainer');
  818. // If there are subContainers i.e. tableContainer or viewContainer
  819. if ($sub_containers.length > 0) {
  820. var $containers = [];
  821. $sub_containers.each(function (index) {
  822. $containers[index] = $(this);
  823. $expander = $containers[index]
  824. .children('div:first')
  825. .children('a.expander');
  826. if (! $expander.hasClass('loaded')) {
  827. loadAndShowTableOrView($expander, $containers[index], itemName);
  828. }
  829. });
  830. // else if no subContainers
  831. } else {
  832. $expander = $dbItem
  833. .children('div:first')
  834. .children('a.expander');
  835. if (! $expander.hasClass('loaded')) {
  836. loadAndShowTableOrView($expander, $dbItem, itemName);
  837. }
  838. }
  839. }
  840. }
  841. function loadAndShowTableOrView ($expander, $relatedContainer, itemName) {
  842. loadChildNodes(true, $expander, function (data) {
  843. var $whichItem = findLoadedItem(
  844. $relatedContainer.children('div.list_container'),
  845. itemName, null, true
  846. );
  847. if ($whichItem) {
  848. showTableOrView($whichItem, $expander);
  849. }
  850. });
  851. }
  852. function showTableOrView ($whichItem, $expander) {
  853. expandTreeNode($expander, function (data) {
  854. if ($whichItem) {
  855. scrollToView($whichItem, false);
  856. }
  857. });
  858. }
  859. function isItemInContainer ($container, name, clazz) {
  860. var $whichItem = null;
  861. $items = $container.find(clazz);
  862. var found = false;
  863. $items.each(function () {
  864. if ($(this).children('a').text() === name) {
  865. $whichItem = $(this);
  866. return false;
  867. }
  868. });
  869. return $whichItem;
  870. }
  871. }
  872. /**
  873. * Disable navigation panel settings
  874. *
  875. * @return void
  876. */
  877. function PMA_disableNaviSettings () {
  878. $('#pma_navigation_settings_icon').addClass('hide');
  879. $('#pma_navigation_settings').remove();
  880. }
  881. /**
  882. * Ensure that navigation panel settings is properly setup.
  883. * If not, set it up
  884. *
  885. * @return void
  886. */
  887. function PMA_ensureNaviSettings (selflink) {
  888. $('#pma_navigation_settings_icon').removeClass('hide');
  889. if (!$('#pma_navigation_settings').length) {
  890. var params = {
  891. getNaviSettings: true,
  892. server: PMA_commonParams.get('server'),
  893. };
  894. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  895. $.post(url, params, function (data) {
  896. if (typeof data !== 'undefined' && data.success) {
  897. $('#pma_navi_settings_container').html(data.message);
  898. setupRestoreField();
  899. setupValidation();
  900. setupConfigTabs();
  901. $('#pma_navigation_settings').find('form').attr('action', selflink);
  902. } else {
  903. PMA_ajaxShowMessage(data.error);
  904. }
  905. });
  906. } else {
  907. $('#pma_navigation_settings').find('form').attr('action', selflink);
  908. }
  909. }
  910. /**
  911. * Reloads the whole navigation tree while preserving its state
  912. *
  913. * @param function the callback function
  914. * @param Object stored navigation paths
  915. *
  916. * @return void
  917. */
  918. function PMA_reloadNavigation (callback, paths) {
  919. var params = {
  920. reload: true,
  921. no_debug: true,
  922. server: PMA_commonParams.get('server'),
  923. };
  924. paths = paths || traverseNavigationForPaths();
  925. $.extend(params, paths);
  926. if ($('#navi_db_select').length) {
  927. params.db = PMA_commonParams.get('db');
  928. requestNaviReload(params);
  929. return;
  930. }
  931. requestNaviReload(params);
  932. function requestNaviReload (params) {
  933. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  934. $.post(url, params, function (data) {
  935. if (typeof data !== 'undefined' && data.success) {
  936. $('#pma_navigation_tree').html(data.message).children('div').show();
  937. if ($('#pma_navigation_tree').hasClass('synced')) {
  938. PMA_selectCurrentDb();
  939. PMA_showCurrentNavigation();
  940. }
  941. // Fire the callback, if any
  942. if (typeof callback === 'function') {
  943. callback.call();
  944. }
  945. navTreeStateUpdate();
  946. } else {
  947. PMA_ajaxShowMessage(data.error);
  948. }
  949. });
  950. }
  951. }
  952. function PMA_selectCurrentDb () {
  953. var $naviDbSelect = $('#navi_db_select');
  954. if (!$naviDbSelect.length) {
  955. return false;
  956. }
  957. if (PMA_commonParams.get('db')) { // db selected
  958. $naviDbSelect.show();
  959. }
  960. $naviDbSelect.val(PMA_commonParams.get('db'));
  961. return $naviDbSelect.val() === PMA_commonParams.get('db');
  962. }
  963. /**
  964. * Handles any requests to change the page in a branch of a tree
  965. *
  966. * This can be called from link click or select change event handlers
  967. *
  968. * @param object $this A jQuery object that points to the element that
  969. * initiated the action of changing the page
  970. *
  971. * @return void
  972. */
  973. function PMA_navigationTreePagination ($this) {
  974. var $msgbox = PMA_ajaxShowMessage();
  975. var isDbSelector = $this.closest('div.pageselector').is('.dbselector');
  976. var url;
  977. var params;
  978. if ($this[0].tagName === 'A') {
  979. url = $this.attr('href');
  980. params = 'ajax_request=true';
  981. } else { // tagName === 'SELECT'
  982. url = 'navigation.php';
  983. params = $this.closest('form').serialize() + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
  984. }
  985. var searchClause = PMA_fastFilter.getSearchClause();
  986. if (searchClause) {
  987. params += PMA_commonParams.get('arg_separator') + 'searchClause=' + encodeURIComponent(searchClause);
  988. }
  989. if (isDbSelector) {
  990. params += PMA_commonParams.get('arg_separator') + 'full=true';
  991. } else {
  992. var searchClause2 = PMA_fastFilter.getSearchClause2($this);
  993. if (searchClause2) {
  994. params += PMA_commonParams.get('arg_separator') + 'searchClause2=' + encodeURIComponent(searchClause2);
  995. }
  996. }
  997. $.post(url, params, function (data) {
  998. if (typeof data !== 'undefined' && data.success) {
  999. PMA_ajaxRemoveMessage($msgbox);
  1000. if (isDbSelector) {
  1001. var val = PMA_fastFilter.getSearchClause();
  1002. $('#pma_navigation_tree')
  1003. .html(data.message)
  1004. .children('div')
  1005. .show();
  1006. if (val) {
  1007. $('#pma_navigation_tree')
  1008. .find('li.fast_filter input.searchClause')
  1009. .val(val);
  1010. }
  1011. } else {
  1012. var $parent = $this.closest('div.list_container').parent();
  1013. var val = PMA_fastFilter.getSearchClause2($this);
  1014. $this.closest('div.list_container').html(
  1015. $(data.message).children().show()
  1016. );
  1017. if (val) {
  1018. $parent.find('li.fast_filter input.searchClause').val(val);
  1019. }
  1020. $parent.find('span.pos2_value:first').text(
  1021. $parent.find('span.pos2_value:last').text()
  1022. );
  1023. $parent.find('span.pos3_value:first').text(
  1024. $parent.find('span.pos3_value:last').text()
  1025. );
  1026. }
  1027. } else {
  1028. PMA_ajaxShowMessage(data.error);
  1029. PMA_handleRedirectAndReload(data);
  1030. }
  1031. navTreeStateUpdate();
  1032. });
  1033. }
  1034. /**
  1035. * @var ResizeHandler Custom object that manages the resizing of the navigation
  1036. *
  1037. * XXX: Must only be ever instanciated once
  1038. * XXX: Inside event handlers the 'this' object is accessed as 'event.data.resize_handler'
  1039. */
  1040. var ResizeHandler = function () {
  1041. /**
  1042. * @var int panel_width Used by the collapser to know where to go
  1043. * back to when uncollapsing the panel
  1044. */
  1045. this.panel_width = 0;
  1046. /**
  1047. * @var string left Used to provide support for RTL languages
  1048. */
  1049. this.left = $('html').attr('dir') === 'ltr' ? 'left' : 'right';
  1050. /**
  1051. * Adjusts the width of the navigation panel to the specified value
  1052. *
  1053. * @param int pos Navigation width in pixels
  1054. *
  1055. * @return void
  1056. */
  1057. this.setWidth = function (pos) {
  1058. if (typeof pos !== 'number') {
  1059. pos = 240;
  1060. }
  1061. var $resizer = $('#pma_navigation_resizer');
  1062. var resizer_width = $resizer.width();
  1063. var $collapser = $('#pma_navigation_collapser');
  1064. var windowWidth = $(window).width();
  1065. $('#pma_navigation').width(pos);
  1066. $('body').css('margin-' + this.left, pos + 'px');
  1067. $('#floating_menubar, #pma_console')
  1068. .css('margin-' + this.left, (pos + resizer_width) + 'px');
  1069. $resizer.css(this.left, pos + 'px');
  1070. if (pos === 0) {
  1071. $collapser
  1072. .css(this.left, pos + resizer_width)
  1073. .html(this.getSymbol(pos))
  1074. .prop('title', PMA_messages.strShowPanel);
  1075. } else if (windowWidth > 768) {
  1076. $collapser
  1077. .css(this.left, pos)
  1078. .html(this.getSymbol(pos))
  1079. .prop('title', PMA_messages.strHidePanel);
  1080. $('#pma_navigation_resizer').css({ 'width': '3px' });
  1081. } else {
  1082. $collapser
  1083. .css(this.left, windowWidth - 22)
  1084. .html(this.getSymbol(100))
  1085. .prop('title', PMA_messages.strHidePanel);
  1086. $('#pma_navigation').width(windowWidth);
  1087. $('body').css('margin-' + this.left, '0px');
  1088. $('#pma_navigation_resizer').css({ 'width': '0px' });
  1089. }
  1090. setTimeout(function () {
  1091. $(window).trigger('resize');
  1092. }, 4);
  1093. };
  1094. /**
  1095. * Returns the horizontal position of the mouse,
  1096. * relative to the outer side of the navigation panel
  1097. *
  1098. * @param int pos Navigation width in pixels
  1099. *
  1100. * @return void
  1101. */
  1102. this.getPos = function (event) {
  1103. var pos = event.pageX;
  1104. var windowWidth = $(window).width();
  1105. var windowScroll = $(window).scrollLeft();
  1106. pos = pos - windowScroll;
  1107. if (this.left !== 'left') {
  1108. pos = windowWidth - event.pageX;
  1109. }
  1110. if (pos < 0) {
  1111. pos = 0;
  1112. } else if (pos + 100 >= windowWidth) {
  1113. pos = windowWidth - 100;
  1114. } else {
  1115. this.panel_width = 0;
  1116. }
  1117. return pos;
  1118. };
  1119. /**
  1120. * Returns the HTML code for the arrow symbol used in the collapser
  1121. *
  1122. * @param int width The width of the panel
  1123. *
  1124. * @return string
  1125. */
  1126. this.getSymbol = function (width) {
  1127. if (this.left === 'left') {
  1128. if (width === 0) {
  1129. return '&rarr;';
  1130. } else {
  1131. return '&larr;';
  1132. }
  1133. } else {
  1134. if (width === 0) {
  1135. return '&larr;';
  1136. } else {
  1137. return '&rarr;';
  1138. }
  1139. }
  1140. };
  1141. /**
  1142. * Event handler for initiating a resize of the panel
  1143. *
  1144. * @param object e Event data (contains a reference to resizeHandler)
  1145. *
  1146. * @return void
  1147. */
  1148. this.mousedown = function (event) {
  1149. event.preventDefault();
  1150. $(document)
  1151. .on('mousemove', { 'resize_handler': event.data.resize_handler },
  1152. $.throttle(event.data.resize_handler.mousemove, 4))
  1153. .on('mouseup', { 'resize_handler': event.data.resize_handler },
  1154. event.data.resize_handler.mouseup);
  1155. $('body').css('cursor', 'col-resize');
  1156. };
  1157. /**
  1158. * Event handler for terminating a resize of the panel
  1159. *
  1160. * @param object e Event data (contains a reference to resizeHandler)
  1161. *
  1162. * @return void
  1163. */
  1164. this.mouseup = function (event) {
  1165. $('body').css('cursor', '');
  1166. configSet('NavigationWidth', event.data.resize_handler.getPos(event));
  1167. $('#topmenu').menuResizer('resize');
  1168. $(document)
  1169. .off('mousemove')
  1170. .off('mouseup');
  1171. };
  1172. /**
  1173. * Event handler for updating the panel during a resize operation
  1174. *
  1175. * @param object e Event data (contains a reference to resizeHandler)
  1176. *
  1177. * @return void
  1178. */
  1179. this.mousemove = function (event) {
  1180. event.preventDefault();
  1181. var pos = event.data.resize_handler.getPos(event);
  1182. event.data.resize_handler.setWidth(pos);
  1183. if ($('.sticky_columns').length !== 0) {
  1184. handleAllStickyColumns();
  1185. }
  1186. };
  1187. /**
  1188. * Event handler for collapsing the panel
  1189. *
  1190. * @param object e Event data (contains a reference to resizeHandler)
  1191. *
  1192. * @return void
  1193. */
  1194. this.collapse = function (event) {
  1195. event.preventDefault();
  1196. var panel_width = event.data.resize_handler.panel_width;
  1197. var width = $('#pma_navigation').width();
  1198. if (width === 0 && panel_width === 0) {
  1199. panel_width = 240;
  1200. }
  1201. configSet('NavigationWidth', panel_width);
  1202. event.data.resize_handler.setWidth(panel_width);
  1203. event.data.resize_handler.panel_width = width;
  1204. };
  1205. /**
  1206. * Event handler for resizing the navigation tree height on window resize
  1207. *
  1208. * @return void
  1209. */
  1210. this.treeResize = function (event) {
  1211. var $nav = $('#pma_navigation');
  1212. var $nav_tree = $('#pma_navigation_tree');
  1213. var $nav_header = $('#pma_navigation_header');
  1214. var $nav_tree_content = $('#pma_navigation_tree_content');
  1215. $nav_tree.height($nav.height() - $nav_header.height());
  1216. if ($nav_tree_content.length > 0) {
  1217. $nav_tree_content.height($nav_tree.height() - $nav_tree_content.position().top);
  1218. } else {
  1219. // TODO: in fast filter search response there is no #pma_navigation_tree_content, needs to be added in php
  1220. $nav_tree.css({
  1221. 'overflow-y': 'auto'
  1222. });
  1223. }
  1224. // Set content bottom space beacuse of console
  1225. $('body').css('margin-bottom', $('#pma_console').height() + 'px');
  1226. };
  1227. // Hide the pma_navigation initially when loaded on mobile
  1228. if ($(window).width() < 768) {
  1229. this.setWidth(0);
  1230. } else {
  1231. this.setWidth(configGet('NavigationWidth', false));
  1232. $('#topmenu').menuResizer('resize');
  1233. }
  1234. // Register the events for the resizer and the collapser
  1235. $(document).on('mousedown', '#pma_navigation_resizer', { 'resize_handler': this }, this.mousedown);
  1236. $(document).on('click', '#pma_navigation_collapser', { 'resize_handler': this }, this.collapse);
  1237. // Add the correct arrow symbol to the collapser
  1238. $('#pma_navigation_collapser').html(this.getSymbol($('#pma_navigation').width()));
  1239. // Fix navigation tree height
  1240. $(window).on('resize', this.treeResize);
  1241. // need to call this now and then, browser might decide
  1242. // to show/hide horizontal scrollbars depending on page content width
  1243. setInterval(this.treeResize, 2000);
  1244. this.treeResize();
  1245. }; // End of ResizeHandler
  1246. /**
  1247. * @var object PMA_fastFilter Handles the functionality that allows filtering
  1248. * of the items in a branch of the navigation tree
  1249. */
  1250. var PMA_fastFilter = {
  1251. /**
  1252. * Construct for the asynchronous fast filter functionality
  1253. *
  1254. * @param object $this A jQuery object pointing to the list container
  1255. * which is the nearest parent of the fast filter
  1256. * @param string searchClause The query string for the filter
  1257. *
  1258. * @return new PMA_fastFilter.filter object
  1259. */
  1260. filter: function ($this, searchClause) {
  1261. /**
  1262. * @var object $this A jQuery object pointing to the list container
  1263. * which is the nearest parent of the fast filter
  1264. */
  1265. this.$this = $this;
  1266. /**
  1267. * @var bool searchClause The query string for the filter
  1268. */
  1269. this.searchClause = searchClause;
  1270. /**
  1271. * @var object $clone A clone of the original contents
  1272. * of the navigation branch before
  1273. * the fast filter was applied
  1274. */
  1275. this.$clone = $this.clone();
  1276. /**
  1277. * @var object xhr A reference to the ajax request that is currently running
  1278. */
  1279. this.xhr = null;
  1280. /**
  1281. * @var int timeout Used to delay the request for asynchronous search
  1282. */
  1283. this.timeout = null;
  1284. var $filterInput = $this.find('li.fast_filter input.searchClause');
  1285. if ($filterInput.length !== 0 &&
  1286. $filterInput.val() !== '' &&
  1287. $filterInput.val() !== $filterInput[0].defaultValue
  1288. ) {
  1289. this.request();
  1290. }
  1291. },
  1292. /**
  1293. * Gets the query string from the database fast filter form
  1294. *
  1295. * @return string
  1296. */
  1297. getSearchClause: function () {
  1298. var retval = '';
  1299. var $input = $('#pma_navigation_tree')
  1300. .find('li.fast_filter.db_fast_filter input.searchClause');
  1301. if ($input.length && $input.val() !== $input[0].defaultValue) {
  1302. retval = $input.val();
  1303. }
  1304. return retval;
  1305. },
  1306. /**
  1307. * Gets the query string from a second level item's fast filter form
  1308. * The retrieval is done by trasversing the navigation tree backwards
  1309. *
  1310. * @return string
  1311. */
  1312. getSearchClause2: function ($this) {
  1313. var $filterContainer = $this.closest('div.list_container');
  1314. var $filterInput = $([]);
  1315. if ($filterContainer
  1316. .find('li.fast_filter:not(.db_fast_filter) input.searchClause')
  1317. .length !== 0) {
  1318. $filterInput = $filterContainer
  1319. .find('li.fast_filter:not(.db_fast_filter) input.searchClause');
  1320. }
  1321. var searchClause2 = '';
  1322. if ($filterInput.length !== 0 &&
  1323. $filterInput.first().val() !== $filterInput[0].defaultValue
  1324. ) {
  1325. searchClause2 = $filterInput.val();
  1326. }
  1327. return searchClause2;
  1328. },
  1329. /**
  1330. * @var hash events A list of functions that are bound to DOM events
  1331. * at the top of this file
  1332. */
  1333. events: {
  1334. focus: function (event) {
  1335. var $obj = $(this).closest('div.list_container');
  1336. if (! $obj.data('fastFilter')) {
  1337. $obj.data(
  1338. 'fastFilter',
  1339. new PMA_fastFilter.filter($obj, $(this).val())
  1340. );
  1341. }
  1342. if ($(this).val() === this.defaultValue) {
  1343. $(this).val('');
  1344. } else {
  1345. $(this).select();
  1346. }
  1347. },
  1348. blur: function (event) {
  1349. if ($(this).val() === '') {
  1350. $(this).val(this.defaultValue);
  1351. }
  1352. var $obj = $(this).closest('div.list_container');
  1353. if ($(this).val() === this.defaultValue && $obj.data('fastFilter')) {
  1354. $obj.data('fastFilter').restore();
  1355. }
  1356. },
  1357. keyup: function (event) {
  1358. var $obj = $(this).closest('div.list_container');
  1359. var str = '';
  1360. if ($(this).val() !== this.defaultValue && $(this).val() !== '') {
  1361. $obj.find('div.pageselector').hide();
  1362. str = $(this).val();
  1363. }
  1364. /**
  1365. * FIXME at the server level a value match is done while on
  1366. * the client side it is a regex match. These two should be aligned
  1367. */
  1368. // regex used for filtering.
  1369. var regex;
  1370. try {
  1371. regex = new RegExp(str, 'i');
  1372. } catch (err) {
  1373. return;
  1374. }
  1375. // this is the div that houses the items to be filtered by this filter.
  1376. var outerContainer;
  1377. if ($(this).closest('li.fast_filter').is('.db_fast_filter')) {
  1378. outerContainer = $('#pma_navigation_tree_content');
  1379. } else {
  1380. outerContainer = $obj;
  1381. }
  1382. // filters items that are directly under the div as well as grouped in
  1383. // groups. Does not filter child items (i.e. a database search does
  1384. // not filter tables)
  1385. var item_filter = function ($curr) {
  1386. $curr.children('ul').children('li.navGroup').each(function () {
  1387. $(this).children('div.list_container').each(function () {
  1388. item_filter($(this)); // recursive
  1389. });
  1390. });
  1391. $curr.children('ul').children('li').children('a').not('.container').each(function () {
  1392. if (regex.test($(this).text())) {
  1393. $(this).parent().show().removeClass('hidden');
  1394. } else {
  1395. $(this).parent().hide().addClass('hidden');
  1396. }
  1397. });
  1398. };
  1399. item_filter(outerContainer);
  1400. // hides containers that does not have any visible children
  1401. var container_filter = function ($curr) {
  1402. $curr.children('ul').children('li.navGroup').each(function () {
  1403. var $group = $(this);
  1404. $group.children('div.list_container').each(function () {
  1405. container_filter($(this)); // recursive
  1406. });
  1407. $group.show().removeClass('hidden');
  1408. if ($group.children('div.list_container').children('ul')
  1409. .children('li').not('.hidden').length === 0) {
  1410. $group.hide().addClass('hidden');
  1411. }
  1412. });
  1413. };
  1414. container_filter(outerContainer);
  1415. if ($(this).val() !== this.defaultValue && $(this).val() !== '') {
  1416. if (! $obj.data('fastFilter')) {
  1417. $obj.data(
  1418. 'fastFilter',
  1419. new PMA_fastFilter.filter($obj, $(this).val())
  1420. );
  1421. } else {
  1422. if (event.keyCode === 13) {
  1423. $obj.data('fastFilter').update($(this).val());
  1424. }
  1425. }
  1426. } else if ($obj.data('fastFilter')) {
  1427. $obj.data('fastFilter').restore(true);
  1428. }
  1429. // update filter state
  1430. var filterName;
  1431. if ($(this).attr('name') === 'searchClause2') {
  1432. filterName = $(this).siblings('input[name=aPath]').val();
  1433. } else {
  1434. filterName = 'dbFilter';
  1435. }
  1436. navFilterStateUpdate(filterName, $(this).val());
  1437. },
  1438. clear: function (event) {
  1439. event.stopPropagation();
  1440. // Clear the input and apply the fast filter with empty input
  1441. var filter = $(this).closest('div.list_container').data('fastFilter');
  1442. if (filter) {
  1443. filter.restore();
  1444. }
  1445. var value = $(this).prev()[0].defaultValue;
  1446. $(this).prev().val(value).trigger('keyup');
  1447. }
  1448. }
  1449. };
  1450. /**
  1451. * Handles a change in the search clause
  1452. *
  1453. * @param string searchClause The query string for the filter
  1454. *
  1455. * @return void
  1456. */
  1457. PMA_fastFilter.filter.prototype.update = function (searchClause) {
  1458. if (this.searchClause !== searchClause) {
  1459. this.searchClause = searchClause;
  1460. this.request();
  1461. }
  1462. };
  1463. /**
  1464. * After a delay of 250mS, initiates a request to retrieve search results
  1465. * Multiple calls to this function will always abort the previous request
  1466. *
  1467. * @return void
  1468. */
  1469. PMA_fastFilter.filter.prototype.request = function () {
  1470. var self = this;
  1471. if (self.$this.find('li.fast_filter').find('img.throbber').length === 0) {
  1472. self.$this.find('li.fast_filter').append(
  1473. $('<div class="throbber"></div>').append(
  1474. $('#pma_navigation_content')
  1475. .find('img.throbber')
  1476. .clone()
  1477. .css({ visibility: 'visible', display: 'block' })
  1478. )
  1479. );
  1480. }
  1481. if (self.xhr) {
  1482. self.xhr.abort();
  1483. }
  1484. var url = $('#pma_navigation').find('a.navigation_url').attr('href');
  1485. var params = self.$this.find('> ul > li > form.fast_filter').first().serialize();
  1486. if (self.$this.find('> ul > li > form.fast_filter:first input[name=searchClause]').length === 0) {
  1487. var $input = $('#pma_navigation_tree').find('li.fast_filter.db_fast_filter input.searchClause');
  1488. if ($input.length && $input.val() !== $input[0].defaultValue) {
  1489. params += PMA_commonParams.get('arg_separator') + 'searchClause=' + encodeURIComponent($input.val());
  1490. }
  1491. }
  1492. self.xhr = $.ajax({
  1493. url: url,
  1494. type: 'post',
  1495. dataType: 'json',
  1496. data: params,
  1497. complete: function (jqXHR, status) {
  1498. if (status !== 'abort') {
  1499. var data = JSON.parse(jqXHR.responseText);
  1500. self.$this.find('li.fast_filter').find('div.throbber').remove();
  1501. if (data && data.results) {
  1502. self.swap.apply(self, [data.message]);
  1503. }
  1504. }
  1505. }
  1506. });
  1507. };
  1508. /**
  1509. * Replaces the contents of the navigation branch with the search results
  1510. *
  1511. * @param string list The search results
  1512. *
  1513. * @return void
  1514. */
  1515. PMA_fastFilter.filter.prototype.swap = function (list) {
  1516. this.$this
  1517. .html($(list).html())
  1518. .children()
  1519. .show()
  1520. .end()
  1521. .find('li.fast_filter input.searchClause')
  1522. .val(this.searchClause);
  1523. this.$this.data('fastFilter', this);
  1524. };
  1525. /**
  1526. * Restores the navigation to the original state after the fast filter is cleared
  1527. *
  1528. * @param bool focus Whether to also focus the input box of the fast filter
  1529. *
  1530. * @return void
  1531. */
  1532. PMA_fastFilter.filter.prototype.restore = function (focus) {
  1533. if (this.$this.children('ul').first().hasClass('search_results')) {
  1534. this.$this.html(this.$clone.html()).children().show();
  1535. this.$this.data('fastFilter', this);
  1536. if (focus) {
  1537. this.$this.find('li.fast_filter input.searchClause').focus();
  1538. }
  1539. }
  1540. this.searchClause = '';
  1541. this.$this.find('div.pageselector').show();
  1542. this.$this.find('div.throbber').remove();
  1543. };
  1544. /**
  1545. * Show full name when cursor hover and name not shown completely
  1546. *
  1547. * @param object $containerELem Container element
  1548. *
  1549. * @return void
  1550. */
  1551. function PMA_showFullName ($containerELem) {
  1552. $containerELem.find('.hover_show_full').mouseenter(function () {
  1553. /** mouseenter */
  1554. var $this = $(this);
  1555. var thisOffset = $this.offset();
  1556. if ($this.text() === '') {
  1557. return;
  1558. }
  1559. var $parent = $this.parent();
  1560. if (($parent.offset().left + $parent.outerWidth())
  1561. < (thisOffset.left + $this.outerWidth())) {
  1562. var $fullNameLayer = $('#full_name_layer');
  1563. if ($fullNameLayer.length === 0) {
  1564. $('body').append('<div id="full_name_layer" class="hide"></div>');
  1565. $('#full_name_layer').mouseleave(function () {
  1566. /** mouseleave */
  1567. $(this).addClass('hide')
  1568. .removeClass('hovering');
  1569. }).mouseenter(function () {
  1570. /** mouseenter */
  1571. $(this).addClass('hovering');
  1572. });
  1573. $fullNameLayer = $('#full_name_layer');
  1574. }
  1575. $fullNameLayer.removeClass('hide');
  1576. $fullNameLayer.css({ left: thisOffset.left, top: thisOffset.top });
  1577. $fullNameLayer.html($this.clone());
  1578. setTimeout(function () {
  1579. if (! $fullNameLayer.hasClass('hovering')) {
  1580. $fullNameLayer.trigger('mouseleave');
  1581. }
  1582. }, 200);
  1583. }
  1584. });
  1585. }