1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * function used in or for navigation panel
5 * @package phpMyAdmin-Navigation
9 * updates the tree state in sessionStorage
13 function navTreeStateUpdate () {
14 // update if session storage is supported
15 if (isStorageSupported('sessionStorage')) {
16 var storage = window.sessionStorage;
17 // try catch necessary here to detect whether
18 // content to be stored exceeds storage capacity
20 storage.setItem('navTreePaths', JSON.stringify(traverseNavigationForPaths()));
21 storage.setItem('server', PMA_commonParams.get('server'));
22 storage.setItem('token', PMA_commonParams.get('token'));
24 // storage capacity exceeded & old navigation tree
25 // state is no more valid, so remove it
26 storage.removeItem('navTreePaths');
27 storage.removeItem('server');
28 storage.removeItem('token');
35 * updates the filter state in sessionStorage
39 function navFilterStateUpdate (filterName, filterValue) {
40 if (isStorageSupported('sessionStorage')) {
41 var storage = window.sessionStorage;
43 var currentFilter = $.extend({}, JSON.parse(storage.getItem('navTreeSearchFilters')));
45 filter[filterName] = filterValue;
46 currentFilter = $.extend(currentFilter, filter);
47 storage.setItem('navTreeSearchFilters', JSON.stringify(currentFilter));
49 storage.removeItem('navTreeSearchFilters');
56 * restores the filter state on navigation reload
60 function navFilterStateRestore () {
61 if (isStorageSupported('sessionStorage')
62 && typeof window.sessionStorage.navTreeSearchFilters !== 'undefined'
64 var searchClauses = JSON.parse(window.sessionStorage.navTreeSearchFilters);
65 if (Object.keys(searchClauses).length < 1) {
68 // restore database filter if present and not empty
69 if (searchClauses.hasOwnProperty('dbFilter')
70 && searchClauses.dbFilter.length
72 $obj = $('#pma_navigation_tree');
73 if (! $obj.data('fastFilter')) {
76 new PMA_fastFilter.filter($obj, '')
79 $obj.find('li.fast_filter.db_fast_filter input.searchClause')
80 .val(searchClauses.dbFilter)
83 // find all table filters present in the tree
84 $tableFilters = $('#pma_navigation_tree li.database')
85 .children('div.list_container')
86 .find('li.fast_filter input.searchClause');
87 // restore table filters
88 $tableFilters.each(function () {
89 $obj = $(this).closest('div.list_container');
90 // aPath associated with this filter
91 var filterName = $(this).siblings('input[name=aPath]').val();
92 // if this table's filter has a state stored in storage
93 if (searchClauses.hasOwnProperty(filterName)
94 && searchClauses[filterName].length
96 // clear state if item is not visible,
97 // happens when table filter becomes invisible
98 // as db filter has already been applied
99 if (! $obj.is(':visible')) {
100 navFilterStateUpdate(filterName, '');
103 if (! $obj.data('fastFilter')) {
106 new PMA_fastFilter.filter($obj, '')
109 $(this).val(searchClauses[filterName])
117 * Loads child items of a node and executes a given callback
120 * @param $expandElem expander
121 * @param callback callback function
125 function loadChildNodes (isNode, $expandElem, callback) {
126 var $destination = null;
130 if (!$expandElem.hasClass('expander')) {
133 $destination = $expandElem.closest('li');
135 aPath: $expandElem.find('span.aPath').text(),
136 vPath: $expandElem.find('span.vPath').text(),
137 pos: $expandElem.find('span.pos').text(),
138 pos2_name: $expandElem.find('span.pos2_name').text(),
139 pos2_value: $expandElem.find('span.pos2_value').text(),
143 if ($expandElem.closest('ul').hasClass('search_results')) {
144 params.searchClause = PMA_fastFilter.getSearchClause();
145 params.searchClause2 = PMA_fastFilter.getSearchClause2($expandElem);
148 $destination = $('#pma_navigation_tree_content');
150 aPath: $expandElem.attr('aPath'),
151 vPath: $expandElem.attr('vPath'),
152 pos: $expandElem.attr('pos'),
160 var url = $('#pma_navigation').find('a.navigation_url').attr('href');
161 $.get(url, params, function (data) {
162 if (typeof data !== 'undefined' && data.success === true) {
163 $destination.find('div.list_container').remove(); // FIXME: Hack, there shouldn't be a list container there
165 $destination.append(data.message);
166 $expandElem.addClass('loaded');
168 $destination.html(data.message);
169 $destination.children()
179 var $errors = $(data._errors);
180 if ($errors.children().length > 0) {
181 $('#pma_errors').replaceWith(data._errors);
184 if (callback && typeof callback === 'function') {
187 } else if (data.redirect_flag === '1') {
188 if (window.location.href.indexOf('?') === -1) {
189 window.location.href += '?session_expired=1';
191 window.location.href += PMA_commonParams.get('arg_separator') + 'session_expired=1';
193 window.location.reload();
195 var $throbber = $expandElem.find('img.throbber');
197 var $icon = $expandElem.find('img.ic_b_plus');
199 PMA_ajaxShowMessage(data.error, false);
205 * Collapses a node in navigation tree.
207 * @param $expandElem expander
211 function collapseTreeNode ($expandElem) {
212 var $children = $expandElem.closest('li').children('div.list_container');
213 var $icon = $expandElem.find('img');
214 if ($expandElem.hasClass('loaded')) {
215 if ($icon.is('.ic_b_minus')) {
216 $icon.removeClass('ic_b_minus').addClass('ic_b_plus');
217 $children.slideUp('fast');
221 $children.promise().done(navTreeStateUpdate);
225 * Traverse the navigation tree backwards to generate all the actual
226 * and virtual paths, as well as the positions in the pagination at
227 * various levels, if necessary.
231 function traverseNavigationForPaths () {
233 pos: $('#pma_navigation_tree').find('div.dbselector select').val()
235 if ($('#navi_db_select').length) {
239 $('#pma_navigation_tree').find('a.expander:visible').each(function () {
240 if ($(this).find('img').is('.ic_b_minus') &&
241 $(this).closest('li').find('div.list_container .ic_b_minus').length === 0
243 params['n' + count + '_aPath'] = $(this).find('span.aPath').text();
244 params['n' + count + '_vPath'] = $(this).find('span.vPath').text();
246 var pos2_name = $(this).find('span.pos2_name').text();
251 .find('span.pos2_name:last')
254 var pos2_value = $(this).find('span.pos2_value').text();
259 .find('span.pos2_value:last')
263 params['n' + count + '_pos2_name'] = pos2_name;
264 params['n' + count + '_pos2_value'] = pos2_value;
266 params['n' + count + '_pos3_name'] = $(this).find('span.pos3_name').text();
267 params['n' + count + '_pos3_value'] = $(this).find('span.pos3_value').text();
275 * Executed on page load
278 if (! $('#pma_navigation').length) {
279 // Don't bother running any code if the navigation is not even on the page
283 // Do not let the page reload on submitting the fast filter
284 $(document).on('submit', '.fast_filter', function (event) {
285 event.preventDefault();
288 // Fire up the resize handlers
292 * opens/closes (hides/shows) tree elements
293 * loads data via ajax
295 $(document).on('click', '#pma_navigation_tree a.expander', function (event) {
296 event.preventDefault();
297 event.stopImmediatePropagation();
298 var $icon = $(this).find('img');
299 if ($icon.is('.ic_b_plus')) {
300 expandTreeNode($(this));
302 collapseTreeNode($(this));
307 * Register event handler for click on the reload
308 * navigation icon at the top of the panel
310 $(document).on('click', '#pma_navigation_reload', function (event) {
311 event.preventDefault();
313 // Find the loading symbol and show it
314 var $icon_throbber_src = $('#pma_navigation').find('.throbber');
315 $icon_throbber_src.show();
316 // TODO Why is a loading symbol both hidden, and invisible?
317 $icon_throbber_src.css('visibility', '');
319 // Callback to be used to hide the loading symbol when done reloading
320 function hideNav () {
321 $icon_throbber_src.hide();
324 // Reload the navigation
325 PMA_reloadNavigation(hideNav);
328 $(document).on('change', '#navi_db_select', function (event) {
329 if (! $(this).val()) {
330 PMA_commonParams.set('db', '');
331 PMA_reloadNavigation();
333 $(this).closest('form').trigger('submit');
337 * Register event handler for click on the collapse all
338 * navigation icon at the top of the navigation tree
340 $(document).on('click', '#pma_navigation_collapse', function (event) {
341 event.preventDefault();
342 $('#pma_navigation_tree').find('a.expander').each(function () {
343 var $icon = $(this).find('img');
344 if ($icon.is('.ic_b_minus')) {
351 * Register event handler to toggle
352 * the 'link with main panel' icon on mouseenter.
354 $(document).on('mouseenter', '#pma_navigation_sync', function (event) {
355 event.preventDefault();
356 var synced = $('#pma_navigation_tree').hasClass('synced');
357 var $img = $('#pma_navigation_sync').children('img');
359 $img.removeClass('ic_s_link').addClass('ic_s_unlink');
361 $img.removeClass('ic_s_unlink').addClass('ic_s_link');
366 * Register event handler to toggle
367 * the 'link with main panel' icon on mouseout.
369 $(document).on('mouseout', '#pma_navigation_sync', function (event) {
370 event.preventDefault();
371 var synced = $('#pma_navigation_tree').hasClass('synced');
372 var $img = $('#pma_navigation_sync').children('img');
374 $img.removeClass('ic_s_unlink').addClass('ic_s_link');
376 $img.removeClass('ic_s_link').addClass('ic_s_unlink');
381 * Register event handler to toggle
382 * the linking with main panel behavior
384 $(document).on('click', '#pma_navigation_sync', function (event) {
385 event.preventDefault();
386 var synced = $('#pma_navigation_tree').hasClass('synced');
387 var $img = $('#pma_navigation_sync').children('img');
390 .removeClass('ic_s_unlink')
391 .addClass('ic_s_link')
392 .attr('alt', PMA_messages.linkWithMain)
393 .attr('title', PMA_messages.linkWithMain);
394 $('#pma_navigation_tree')
395 .removeClass('synced')
397 .removeClass('selected');
400 .removeClass('ic_s_link')
401 .addClass('ic_s_unlink')
402 .attr('alt', PMA_messages.unlinkWithMain)
403 .attr('title', PMA_messages.unlinkWithMain);
404 $('#pma_navigation_tree').addClass('synced');
405 PMA_showCurrentNavigation();
410 * Bind all "fast filter" events
412 $(document).on('click', '#pma_navigation_tree li.fast_filter span', PMA_fastFilter.events.clear);
413 $(document).on('focus', '#pma_navigation_tree li.fast_filter input.searchClause', PMA_fastFilter.events.focus);
414 $(document).on('blur', '#pma_navigation_tree li.fast_filter input.searchClause', PMA_fastFilter.events.blur);
415 $(document).on('keyup', '#pma_navigation_tree li.fast_filter input.searchClause', PMA_fastFilter.events.keyup);
418 * Ajax handler for pagination
420 $(document).on('click', '#pma_navigation_tree div.pageselector a.ajax', function (event) {
421 event.preventDefault();
422 PMA_navigationTreePagination($(this));
430 '#pma_navigation_tree.highlight li:not(.fast_filter)',
432 if ($('li:visible', this).length === 0) {
433 $(this).addClass('activePointer');
439 '#pma_navigation_tree.highlight li:not(.fast_filter)',
441 $(this).removeClass('activePointer');
445 /** Create a Routine, Trigger or Event */
446 $(document).on('click', 'li.new_procedure a.ajax, li.new_function a.ajax', function (event) {
447 event.preventDefault();
448 var dialog = new RTE.object('routine');
449 dialog.editorDialog(1, $(this));
451 $(document).on('click', 'li.new_trigger a.ajax', function (event) {
452 event.preventDefault();
453 var dialog = new RTE.object('trigger');
454 dialog.editorDialog(1, $(this));
456 $(document).on('click', 'li.new_event a.ajax', function (event) {
457 event.preventDefault();
458 var dialog = new RTE.object('event');
459 dialog.editorDialog(1, $(this));
462 /** Edit Routines, Triggers or Events */
463 $(document).on('click', 'li.procedure > a.ajax, li.function > a.ajax', function (event) {
464 event.preventDefault();
465 var dialog = new RTE.object('routine');
466 dialog.editorDialog(0, $(this));
468 $(document).on('click', 'li.trigger > a.ajax', function (event) {
469 event.preventDefault();
470 var dialog = new RTE.object('trigger');
471 dialog.editorDialog(0, $(this));
473 $(document).on('click', 'li.event > a.ajax', function (event) {
474 event.preventDefault();
475 var dialog = new RTE.object('event');
476 dialog.editorDialog(0, $(this));
479 /** Execute Routines */
480 $(document).on('click', 'li.procedure div a.ajax img,' +
481 ' li.function div a.ajax img', function (event) {
482 event.preventDefault();
483 var dialog = new RTE.object('routine');
484 dialog.executeDialog($(this).parent());
486 /** Export Triggers and Events */
487 $(document).on('click', 'li.trigger div:eq(1) a.ajax img,' +
488 ' li.event div:eq(1) a.ajax img', function (event) {
489 event.preventDefault();
490 var dialog = new RTE.object();
491 dialog.exportDialog($(this).parent());
495 $(document).on('click', '#pma_navigation_tree li.new_index a.ajax', function (event) {
496 event.preventDefault();
497 var url = $(this).attr('href').substr(
498 $(this).attr('href').indexOf('?') + 1
499 ) + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
500 var title = PMA_messages.strAddIndex;
501 indexEditorDialog(url, title);
505 $(document).on('click', 'li.index a.ajax', function (event) {
506 event.preventDefault();
507 var url = $(this).attr('href').substr(
508 $(this).attr('href').indexOf('?') + 1
509 ) + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
510 var title = PMA_messages.strEditIndex;
511 indexEditorDialog(url, title);
515 $(document).on('click', 'li.new_view a.ajax', function (event) {
516 event.preventDefault();
517 PMA_createViewDialog($(this));
520 /** Hide navigation tree item */
521 $(document).on('click', 'a.hideNavItem.ajax', function (event) {
522 event.preventDefault();
523 var argSep = PMA_commonParams.get('arg_separator');
524 var params = $(this).getPostData();
525 params += argSep + 'ajax_request=true' + argSep + 'server=' + PMA_commonParams.get('server');
529 url: $(this).attr('href'),
530 success: function (data) {
531 if (typeof data !== 'undefined' && data.success === true) {
532 PMA_reloadNavigation();
534 PMA_ajaxShowMessage(data.error);
540 /** Display a dialog to choose hidden navigation items to show */
541 $(document).on('click', 'a.showUnhide.ajax', function (event) {
542 event.preventDefault();
543 var $msg = PMA_ajaxShowMessage();
544 var argSep = PMA_commonParams.get('arg_separator');
545 var params = $(this).getPostData();
546 params += argSep + 'ajax_request=true';
547 $.post($(this).attr('href'), params, function (data) {
548 if (typeof data !== 'undefined' && data.success === true) {
549 PMA_ajaxRemoveMessage($msg);
550 var buttonOptions = {};
551 buttonOptions[PMA_messages.strClose] = function () {
552 $(this).dialog('close');
555 .attr('id', 'unhideNavItemDialog')
556 .append(data.message)
561 buttons: buttonOptions,
562 title: PMA_messages.strUnhideNavItem,
568 PMA_ajaxShowMessage(data.error);
573 /** Show a hidden navigation tree item */
574 $(document).on('click', 'a.unhideNavItem.ajax', function (event) {
575 event.preventDefault();
576 var $tr = $(this).parents('tr');
577 var $msg = PMA_ajaxShowMessage();
578 var argSep = PMA_commonParams.get('arg_separator');
579 var params = $(this).getPostData();
580 params += argSep + 'ajax_request=true' + argSep + 'server=' + PMA_commonParams.get('server');
584 url: $(this).attr('href'),
585 success: function (data) {
586 PMA_ajaxRemoveMessage($msg);
587 if (typeof data !== 'undefined' && data.success === true) {
589 PMA_reloadNavigation();
591 PMA_ajaxShowMessage(data.error);
597 // Add/Remove favorite table using Ajax.
598 $(document).on('click', '.favorite_table_anchor', function (event) {
599 event.preventDefault();
601 var anchor_id = $self.attr('id');
602 if ($self.data('favtargetn') !== null) {
603 if ($('a[data-favtargets="' + $self.data('favtargetn') + '"]').length > 0) {
604 $('a[data-favtargets="' + $self.data('favtargetn') + '"]').trigger('click');
610 url: $self.attr('href'),
614 favorite_tables: (isStorageSupported('localStorage') && typeof window.localStorage.favorite_tables !== 'undefined')
615 ? window.localStorage.favorite_tables
617 server: PMA_commonParams.get('server'),
619 success: function (data) {
621 $('#pma_favorite_list').html(data.list);
622 $('#' + anchor_id).parent().html(data.anchor);
626 $('#' + anchor_id).attr('title')
628 // Update localStorage.
629 if (isStorageSupported('localStorage')) {
630 window.localStorage.favorite_tables = data.favorite_tables;
633 PMA_ajaxShowMessage(data.message);
638 // Check if session storage is supported
639 if (isStorageSupported('sessionStorage')) {
640 var storage = window.sessionStorage;
641 // remove tree from storage if Navi_panel config form is submitted
642 $(document).on('submit', 'form.config-form', function (event) {
643 storage.removeItem('navTreePaths');
645 // Initialize if no previous state is defined
646 if ($('#pma_navigation_tree_content').length &&
647 typeof storage.navTreePaths === 'undefined'
649 PMA_reloadNavigation();
650 } else if (PMA_commonParams.get('server') === storage.server &&
651 PMA_commonParams.get('token') === storage.token
653 // Reload the tree to the state before page refresh
654 PMA_reloadNavigation(navFilterStateRestore, JSON.parse(storage.navTreePaths));
656 // If the user is different
657 navTreeStateUpdate();
663 * Expands a node in navigation tree.
665 * @param $expandElem expander
666 * @param callback callback function
670 function expandTreeNode ($expandElem, callback) {
671 var $children = $expandElem.closest('li').children('div.list_container');
672 var $icon = $expandElem.find('img');
673 if ($expandElem.hasClass('loaded')) {
674 if ($icon.is('.ic_b_plus')) {
675 $icon.removeClass('ic_b_plus').addClass('ic_b_minus');
676 $children.slideDown('fast');
678 if (callback && typeof callback === 'function') {
681 $children.promise().done(navTreeStateUpdate);
683 var $throbber = $('#pma_navigation').find('.throbber')
686 .css({ visibility: 'visible', display: 'block' })
689 $throbber.insertBefore($icon);
691 loadChildNodes(true, $expandElem, function (data) {
692 if (typeof data !== 'undefined' && data.success === true) {
693 var $destination = $expandElem.closest('li');
694 $icon.removeClass('ic_b_plus').addClass('ic_b_minus');
695 $children = $destination.children('div.list_container');
696 $children.slideDown('fast');
697 if ($destination.find('ul > li').length === 1) {
698 $destination.find('ul > li')
699 .find('a.expander.container')
702 if (callback && typeof callback === 'function') {
705 PMA_showFullName($destination);
707 PMA_ajaxShowMessage(data.error, false);
711 $children.promise().done(navTreeStateUpdate);
718 * Auto-scrolls the newly chosen database
720 * @param object $element The element to set to view
721 * @param boolean $forceToTop Whether to force scroll to top
724 function scrollToView ($element, $forceToTop) {
725 navFilterStateRestore();
726 var $container = $('#pma_navigation_tree_content');
727 var elemTop = $element.offset().top - $container.offset().top;
729 var scrollPadding = 20; // extra padding from top of bottom when scrolling to view
730 if (elemTop < 0 || $forceToTop) {
731 $container.stop().animate({
732 scrollTop: elemTop + $container.scrollTop() - scrollPadding
734 } else if (elemTop + textHeight > $container.height()) {
735 $container.stop().animate({
736 scrollTop: elemTop + textHeight - $container.height() + $container.scrollTop() + scrollPadding
742 * Expand the navigation and highlight the current database or table/view
746 function PMA_showCurrentNavigation () {
747 var db = PMA_commonParams.get('db');
748 var table = PMA_commonParams.get('table');
749 $('#pma_navigation_tree')
751 .removeClass('selected');
753 var $dbItem = findLoadedItem(
754 $('#pma_navigation_tree').find('> div'), db, 'database', !table
756 if ($('#navi_db_select').length &&
757 $('option:selected', $('#navi_db_select')).length
759 if (! PMA_selectCurrentDb()) {
762 // If loaded database in navigation is not same as current one
763 if ($('#pma_navigation_tree_content').find('span.loaded_db:first').text()
764 !== $('#navi_db_select').val()
766 loadChildNodes(false, $('option:selected', $('#navi_db_select')), function (data) {
767 handleTableOrDb(table, $('#pma_navigation_tree_content'));
768 var $children = $('#pma_navigation_tree_content').children('div.list_container');
769 $children.promise().done(navTreeStateUpdate);
772 handleTableOrDb(table, $('#pma_navigation_tree_content'));
774 } else if ($dbItem) {
775 var $expander = $dbItem.children('div:first').children('a.expander');
776 // if not loaded or loaded but collapsed
777 if (! $expander.hasClass('loaded') ||
778 $expander.find('img').is('.ic_b_plus')
780 expandTreeNode($expander, function () {
781 handleTableOrDb(table, $dbItem);
784 handleTableOrDb(table, $dbItem);
787 } else if ($('#navi_db_select').length && $('#navi_db_select').val()) {
788 $('#navi_db_select').val('').hide().trigger('change');
790 PMA_showFullName($('#pma_navigation_tree'));
792 function handleTableOrDb (table, $dbItem) {
794 loadAndHighlightTableOrView($dbItem, table);
796 var $container = $dbItem.children('div.list_container');
797 var $tableContainer = $container.children('ul').children('li.tableContainer');
798 if ($tableContainer.length > 0) {
799 var $expander = $tableContainer.children('div:first').children('a.expander');
800 $tableContainer.addClass('selected');
801 expandTreeNode($expander, function () {
802 scrollToView($dbItem, true);
805 scrollToView($dbItem, true);
810 function findLoadedItem ($container, name, clazz, doSelect) {
812 $container.children('ul').children('li').each(function () {
814 // this is a navigation group, recurse
815 if ($li.is('.navGroup')) {
816 var $container = $li.children('div.list_container');
817 var $childRet = findLoadedItem(
818 $container, name, clazz, doSelect
824 } else { // this is a real navigation item
825 // name and class matches
826 if (((clazz && $li.is('.' + clazz)) || ! clazz) &&
827 $li.children('a').text() === name) {
829 $li.addClass('selected');
831 // taverse up and expand and parent navigation groups
832 $li.parents('.navGroup').each(function () {
833 var $cont = $(this).children('div.list_container');
834 if (! $cont.is(':visible')) {
836 .children('div:first')
837 .children('a.expander')
849 function loadAndHighlightTableOrView ($dbItem, itemName) {
850 var $container = $dbItem.children('div.list_container');
852 var $whichItem = isItemInContainer($container, itemName, 'li.table, li.view');
853 // If item already there in some container
855 // get the relevant container while may also be a subcontainer
856 var $relatedContainer = $whichItem.closest('li.subContainer').length
857 ? $whichItem.closest('li.subContainer')
859 $whichItem = findLoadedItem(
860 $relatedContainer.children('div.list_container'),
864 showTableOrView($whichItem, $relatedContainer.children('div:first').children('a.expander'));
865 // else if item not there, try loading once
867 var $sub_containers = $dbItem.find('.subContainer');
868 // If there are subContainers i.e. tableContainer or viewContainer
869 if ($sub_containers.length > 0) {
870 var $containers = [];
871 $sub_containers.each(function (index) {
872 $containers[index] = $(this);
873 $expander = $containers[index]
874 .children('div:first')
875 .children('a.expander');
876 if (! $expander.hasClass('loaded')) {
877 loadAndShowTableOrView($expander, $containers[index], itemName);
880 // else if no subContainers
883 .children('div:first')
884 .children('a.expander');
885 if (! $expander.hasClass('loaded')) {
886 loadAndShowTableOrView($expander, $dbItem, itemName);
892 function loadAndShowTableOrView ($expander, $relatedContainer, itemName) {
893 loadChildNodes(true, $expander, function (data) {
894 var $whichItem = findLoadedItem(
895 $relatedContainer.children('div.list_container'),
899 showTableOrView($whichItem, $expander);
904 function showTableOrView ($whichItem, $expander) {
905 expandTreeNode($expander, function (data) {
907 scrollToView($whichItem, false);
912 function isItemInContainer ($container, name, clazz) {
913 var $whichItem = null;
914 $items = $container.find(clazz);
916 $items.each(function () {
917 if ($(this).children('a').text() === name) {
918 $whichItem = $(this);
927 * Disable navigation panel settings
931 function PMA_disableNaviSettings () {
932 $('#pma_navigation_settings_icon').addClass('hide');
933 $('#pma_navigation_settings').remove();
937 * Ensure that navigation panel settings is properly setup.
942 function PMA_ensureNaviSettings (selflink) {
943 $('#pma_navigation_settings_icon').removeClass('hide');
945 if (!$('#pma_navigation_settings').length) {
947 getNaviSettings: true,
948 server: PMA_commonParams.get('server'),
950 var url = $('#pma_navigation').find('a.navigation_url').attr('href');
951 $.post(url, params, function (data) {
952 if (typeof data !== 'undefined' && data.success) {
953 $('#pma_navi_settings_container').html(data.message);
957 $('#pma_navigation_settings').find('form').attr('action', selflink);
959 PMA_ajaxShowMessage(data.error);
963 $('#pma_navigation_settings').find('form').attr('action', selflink);
968 * Reloads the whole navigation tree while preserving its state
970 * @param function the callback function
971 * @param Object stored navigation paths
975 function PMA_reloadNavigation (callback, paths) {
979 server: PMA_commonParams.get('server'),
981 paths = paths || traverseNavigationForPaths();
982 $.extend(params, paths);
983 if ($('#navi_db_select').length) {
984 params.db = PMA_commonParams.get('db');
985 requestNaviReload(params);
988 requestNaviReload(params);
990 function requestNaviReload (params) {
991 var url = $('#pma_navigation').find('a.navigation_url').attr('href');
992 $.post(url, params, function (data) {
993 if (typeof data !== 'undefined' && data.success) {
994 $('#pma_navigation_tree').html(data.message).children('div').show();
995 if ($('#pma_navigation_tree').hasClass('synced')) {
996 PMA_selectCurrentDb();
997 PMA_showCurrentNavigation();
999 // Fire the callback, if any
1000 if (typeof callback === 'function') {
1003 navTreeStateUpdate();
1005 PMA_ajaxShowMessage(data.error);
1011 function PMA_selectCurrentDb () {
1012 var $naviDbSelect = $('#navi_db_select');
1014 if (!$naviDbSelect.length) {
1018 if (PMA_commonParams.get('db')) { // db selected
1019 $naviDbSelect.show();
1022 $naviDbSelect.val(PMA_commonParams.get('db'));
1023 return $naviDbSelect.val() === PMA_commonParams.get('db');
1027 * Handles any requests to change the page in a branch of a tree
1029 * This can be called from link click or select change event handlers
1031 * @param object $this A jQuery object that points to the element that
1032 * initiated the action of changing the page
1036 function PMA_navigationTreePagination ($this) {
1037 var $msgbox = PMA_ajaxShowMessage();
1038 var isDbSelector = $this.closest('div.pageselector').is('.dbselector');
1041 if ($this[0].tagName === 'A') {
1042 url = $this.attr('href');
1043 params = 'ajax_request=true';
1044 } else { // tagName === 'SELECT'
1045 url = 'navigation.php';
1046 params = $this.closest('form').serialize() + PMA_commonParams.get('arg_separator') + 'ajax_request=true';
1048 var searchClause = PMA_fastFilter.getSearchClause();
1050 params += PMA_commonParams.get('arg_separator') + 'searchClause=' + encodeURIComponent(searchClause);
1053 params += PMA_commonParams.get('arg_separator') + 'full=true';
1055 var searchClause2 = PMA_fastFilter.getSearchClause2($this);
1056 if (searchClause2) {
1057 params += PMA_commonParams.get('arg_separator') + 'searchClause2=' + encodeURIComponent(searchClause2);
1060 $.post(url, params, function (data) {
1061 if (typeof data !== 'undefined' && data.success) {
1062 PMA_ajaxRemoveMessage($msgbox);
1064 var val = PMA_fastFilter.getSearchClause();
1065 $('#pma_navigation_tree')
1070 $('#pma_navigation_tree')
1071 .find('li.fast_filter input.searchClause')
1075 var $parent = $this.closest('div.list_container').parent();
1076 var val = PMA_fastFilter.getSearchClause2($this);
1077 $this.closest('div.list_container').html(
1078 $(data.message).children().show()
1081 $parent.find('li.fast_filter input.searchClause').val(val);
1083 $parent.find('span.pos2_value:first').text(
1084 $parent.find('span.pos2_value:last').text()
1086 $parent.find('span.pos3_value:first').text(
1087 $parent.find('span.pos3_value:last').text()
1091 PMA_ajaxShowMessage(data.error);
1092 PMA_handleRedirectAndReload(data);
1094 navTreeStateUpdate();
1099 * @var ResizeHandler Custom object that manages the resizing of the navigation
1101 * XXX: Must only be ever instanciated once
1102 * XXX: Inside event handlers the 'this' object is accessed as 'event.data.resize_handler'
1104 var ResizeHandler = function () {
1106 * @var int panel_width Used by the collapser to know where to go
1107 * back to when uncollapsing the panel
1109 this.panel_width = 0;
1111 * @var string left Used to provide support for RTL languages
1113 this.left = $('html').attr('dir') === 'ltr' ? 'left' : 'right';
1115 * Adjusts the width of the navigation panel to the specified value
1117 * @param int pos Navigation width in pixels
1121 this.setWidth = function (pos) {
1122 if (typeof pos !== 'number') {
1125 var $resizer = $('#pma_navigation_resizer');
1126 var resizer_width = $resizer.width();
1127 var $collapser = $('#pma_navigation_collapser');
1128 var windowWidth = $(window).width();
1129 $('#pma_navigation').width(pos);
1130 $('body').css('margin-' + this.left, pos + 'px');
1131 // Issue #15127 : Adding fixed positioning to menubar
1132 // Issue #15570 : Panels on homescreen go underneath of floating menubar
1133 $('#floating_menubar')
1134 .css('margin-' + this.left, $('#pma_navigation').width() + $('#pma_navigation_resizer').width())
1137 'position': 'fixed',
1142 .append($('#serverinfo'))
1143 .append($('#topmenucontainer'));
1144 // Allow the DOM to render, then adjust the padding on the body
1145 setTimeout(function () {
1148 $('#floating_menubar').outerHeight(true)
1152 .css('margin-' + this.left, (pos + resizer_width) + 'px');
1153 $resizer.css(this.left, pos + 'px');
1156 .css(this.left, pos + resizer_width)
1157 .html(this.getSymbol(pos))
1158 .prop('title', PMA_messages.strShowPanel);
1159 } else if (windowWidth > 768) {
1161 .css(this.left, pos)
1162 .html(this.getSymbol(pos))
1163 .prop('title', PMA_messages.strHidePanel);
1164 $('#pma_navigation_resizer').css({ 'width': '3px' });
1167 .css(this.left, windowWidth - 22)
1168 .html(this.getSymbol(100))
1169 .prop('title', PMA_messages.strHidePanel);
1170 $('#pma_navigation').width(windowWidth);
1171 $('body').css('margin-' + this.left, '0px');
1172 $('#pma_navigation_resizer').css({ 'width': '0px' });
1174 setTimeout(function () {
1175 $(window).trigger('resize');
1179 * Returns the horizontal position of the mouse,
1180 * relative to the outer side of the navigation panel
1182 * @param int pos Navigation width in pixels
1186 this.getPos = function (event) {
1187 var pos = event.pageX;
1188 var windowWidth = $(window).width();
1189 var windowScroll = $(window).scrollLeft();
1190 pos = pos - windowScroll;
1191 if (this.left !== 'left') {
1192 pos = windowWidth - event.pageX;
1196 } else if (pos + 100 >= windowWidth) {
1197 pos = windowWidth - 100;
1199 this.panel_width = 0;
1204 * Returns the HTML code for the arrow symbol used in the collapser
1206 * @param int width The width of the panel
1210 this.getSymbol = function (width) {
1211 if (this.left === 'left') {
1226 * Event handler for initiating a resize of the panel
1228 * @param object e Event data (contains a reference to resizeHandler)
1232 this.mousedown = function (event) {
1233 event.preventDefault();
1235 .on('mousemove', { 'resize_handler': event.data.resize_handler },
1236 $.throttle(event.data.resize_handler.mousemove, 4))
1237 .on('mouseup', { 'resize_handler': event.data.resize_handler },
1238 event.data.resize_handler.mouseup);
1239 $('body').css('cursor', 'col-resize');
1242 * Event handler for terminating a resize of the panel
1244 * @param object e Event data (contains a reference to resizeHandler)
1248 this.mouseup = function (event) {
1249 $('body').css('cursor', '');
1250 configSet('NavigationWidth', event.data.resize_handler.getPos(event));
1251 $('#topmenu').menuResizer('resize');
1257 * Event handler for updating the panel during a resize operation
1259 * @param object e Event data (contains a reference to resizeHandler)
1263 this.mousemove = function (event) {
1264 event.preventDefault();
1265 var pos = event.data.resize_handler.getPos(event);
1266 event.data.resize_handler.setWidth(pos);
1267 if ($('.sticky_columns').length !== 0) {
1268 handleAllStickyColumns();
1272 * Event handler for collapsing the panel
1274 * @param object e Event data (contains a reference to resizeHandler)
1278 this.collapse = function (event) {
1279 event.preventDefault();
1280 var panel_width = event.data.resize_handler.panel_width;
1281 var width = $('#pma_navigation').width();
1282 if (width === 0 && panel_width === 0) {
1285 configSet('NavigationWidth', panel_width);
1286 event.data.resize_handler.setWidth(panel_width);
1287 event.data.resize_handler.panel_width = width;
1290 * Event handler for resizing the navigation tree height on window resize
1294 this.treeResize = function (event) {
1295 var $nav = $('#pma_navigation');
1296 var $nav_tree = $('#pma_navigation_tree');
1297 var $nav_header = $('#pma_navigation_header');
1298 var $nav_tree_content = $('#pma_navigation_tree_content');
1299 $nav_tree.height($nav.height() - $nav_header.height());
1300 if ($nav_tree_content.length > 0) {
1301 $nav_tree_content.height($nav_tree.height() - $nav_tree_content.position().top);
1303 // TODO: in fast filter search response there is no #pma_navigation_tree_content, needs to be added in php
1305 'overflow-y': 'auto'
1308 // Set content bottom space beacuse of console
1309 $('body').css('margin-bottom', $('#pma_console').height() + 'px');
1311 // Hide the pma_navigation initially when loaded on mobile
1312 if ($(window).width() < 768) {
1315 this.setWidth(configGet('NavigationWidth', false));
1316 $('#topmenu').menuResizer('resize');
1318 // Register the events for the resizer and the collapser
1319 $(document).on('mousedown', '#pma_navigation_resizer', { 'resize_handler': this }, this.mousedown);
1320 $(document).on('click', '#pma_navigation_collapser', { 'resize_handler': this }, this.collapse);
1322 // Add the correct arrow symbol to the collapser
1323 $('#pma_navigation_collapser').html(this.getSymbol($('#pma_navigation').width()));
1324 // Fix navigation tree height
1325 $(window).on('resize', this.treeResize);
1326 // need to call this now and then, browser might decide
1327 // to show/hide horizontal scrollbars depending on page content width
1328 setInterval(this.treeResize, 2000);
1330 }; // End of ResizeHandler
1333 * @var object PMA_fastFilter Handles the functionality that allows filtering
1334 * of the items in a branch of the navigation tree
1336 var PMA_fastFilter = {
1338 * Construct for the asynchronous fast filter functionality
1340 * @param object $this A jQuery object pointing to the list container
1341 * which is the nearest parent of the fast filter
1342 * @param string searchClause The query string for the filter
1344 * @return new PMA_fastFilter.filter object
1346 filter: function ($this, searchClause) {
1348 * @var object $this A jQuery object pointing to the list container
1349 * which is the nearest parent of the fast filter
1353 * @var bool searchClause The query string for the filter
1355 this.searchClause = searchClause;
1357 * @var object $clone A clone of the original contents
1358 * of the navigation branch before
1359 * the fast filter was applied
1361 this.$clone = $this.clone();
1363 * @var object xhr A reference to the ajax request that is currently running
1367 * @var int timeout Used to delay the request for asynchronous search
1369 this.timeout = null;
1371 var $filterInput = $this.find('li.fast_filter input.searchClause');
1372 if ($filterInput.length !== 0 &&
1373 $filterInput.val() !== '' &&
1374 $filterInput.val() !== $filterInput[0].defaultValue
1380 * Gets the query string from the database fast filter form
1384 getSearchClause: function () {
1386 var $input = $('#pma_navigation_tree')
1387 .find('li.fast_filter.db_fast_filter input.searchClause');
1388 if ($input.length && $input.val() !== $input[0].defaultValue) {
1389 retval = $input.val();
1394 * Gets the query string from a second level item's fast filter form
1395 * The retrieval is done by trasversing the navigation tree backwards
1399 getSearchClause2: function ($this) {
1400 var $filterContainer = $this.closest('div.list_container');
1401 var $filterInput = $([]);
1402 if ($filterContainer
1403 .find('li.fast_filter:not(.db_fast_filter) input.searchClause')
1405 $filterInput = $filterContainer
1406 .find('li.fast_filter:not(.db_fast_filter) input.searchClause');
1408 var searchClause2 = '';
1409 if ($filterInput.length !== 0 &&
1410 $filterInput.first().val() !== $filterInput[0].defaultValue
1412 searchClause2 = $filterInput.val();
1414 return searchClause2;
1417 * @var hash events A list of functions that are bound to DOM events
1418 * at the top of this file
1421 focus: function (event) {
1422 var $obj = $(this).closest('div.list_container');
1423 if (! $obj.data('fastFilter')) {
1426 new PMA_fastFilter.filter($obj, $(this).val())
1429 if ($(this).val() === this.defaultValue) {
1435 blur: function (event) {
1436 if ($(this).val() === '') {
1437 $(this).val(this.defaultValue);
1439 var $obj = $(this).closest('div.list_container');
1440 if ($(this).val() === this.defaultValue && $obj.data('fastFilter')) {
1441 $obj.data('fastFilter').restore();
1444 keyup: function (event) {
1445 var $obj = $(this).closest('div.list_container');
1447 if ($(this).val() !== this.defaultValue && $(this).val() !== '') {
1448 $obj.find('div.pageselector').hide();
1449 str = $(this).val();
1453 * FIXME at the server level a value match is done while on
1454 * the client side it is a regex match. These two should be aligned
1457 // regex used for filtering.
1460 regex = new RegExp(str, 'i');
1465 // this is the div that houses the items to be filtered by this filter.
1467 if ($(this).closest('li.fast_filter').is('.db_fast_filter')) {
1468 outerContainer = $('#pma_navigation_tree_content');
1470 outerContainer = $obj;
1473 // filters items that are directly under the div as well as grouped in
1474 // groups. Does not filter child items (i.e. a database search does
1475 // not filter tables)
1476 var item_filter = function ($curr) {
1477 $curr.children('ul').children('li.navGroup').each(function () {
1478 $(this).children('div.list_container').each(function () {
1479 item_filter($(this)); // recursive
1482 $curr.children('ul').children('li').children('a').not('.container').each(function () {
1483 if (regex.test($(this).text())) {
1484 $(this).parent().show().removeClass('hidden');
1486 $(this).parent().hide().addClass('hidden');
1490 item_filter(outerContainer);
1492 // hides containers that does not have any visible children
1493 var container_filter = function ($curr) {
1494 $curr.children('ul').children('li.navGroup').each(function () {
1495 var $group = $(this);
1496 $group.children('div.list_container').each(function () {
1497 container_filter($(this)); // recursive
1499 $group.show().removeClass('hidden');
1500 if ($group.children('div.list_container').children('ul')
1501 .children('li').not('.hidden').length === 0) {
1502 $group.hide().addClass('hidden');
1506 container_filter(outerContainer);
1508 if ($(this).val() !== this.defaultValue && $(this).val() !== '') {
1509 if (! $obj.data('fastFilter')) {
1512 new PMA_fastFilter.filter($obj, $(this).val())
1515 if (event.keyCode === 13) {
1516 $obj.data('fastFilter').update($(this).val());
1519 } else if ($obj.data('fastFilter')) {
1520 $obj.data('fastFilter').restore(true);
1522 // update filter state
1524 if ($(this).attr('name') === 'searchClause2') {
1525 filterName = $(this).siblings('input[name=aPath]').val();
1527 filterName = 'dbFilter';
1529 navFilterStateUpdate(filterName, $(this).val());
1531 clear: function (event) {
1532 event.stopPropagation();
1533 // Clear the input and apply the fast filter with empty input
1534 var filter = $(this).closest('div.list_container').data('fastFilter');
1538 var value = $(this).prev()[0].defaultValue;
1539 $(this).prev().val(value).trigger('keyup');
1544 * Handles a change in the search clause
1546 * @param string searchClause The query string for the filter
1550 PMA_fastFilter.filter.prototype.update = function (searchClause) {
1551 if (this.searchClause !== searchClause) {
1552 this.searchClause = searchClause;
1557 * After a delay of 250mS, initiates a request to retrieve search results
1558 * Multiple calls to this function will always abort the previous request
1562 PMA_fastFilter.filter.prototype.request = function () {
1564 if (self.$this.find('li.fast_filter').find('img.throbber').length === 0) {
1565 self.$this.find('li.fast_filter').append(
1566 $('<div class="throbber"></div>').append(
1567 $('#pma_navigation_content')
1568 .find('img.throbber')
1570 .css({ visibility: 'visible', display: 'block' })
1577 var url = $('#pma_navigation').find('a.navigation_url').attr('href');
1578 var params = self.$this.find('> ul > li > form.fast_filter').first().serialize();
1580 if (self.$this.find('> ul > li > form.fast_filter:first input[name=searchClause]').length === 0) {
1581 var $input = $('#pma_navigation_tree').find('li.fast_filter.db_fast_filter input.searchClause');
1582 if ($input.length && $input.val() !== $input[0].defaultValue) {
1583 params += PMA_commonParams.get('arg_separator') + 'searchClause=' + encodeURIComponent($input.val());
1591 complete: function (jqXHR, status) {
1592 if (status !== 'abort') {
1593 var data = JSON.parse(jqXHR.responseText);
1594 self.$this.find('li.fast_filter').find('div.throbber').remove();
1595 if (data && data.results) {
1596 self.swap.apply(self, [data.message]);
1603 * Replaces the contents of the navigation branch with the search results
1605 * @param string list The search results
1609 PMA_fastFilter.filter.prototype.swap = function (list) {
1611 .html($(list).html())
1615 .find('li.fast_filter input.searchClause')
1616 .val(this.searchClause);
1617 this.$this.data('fastFilter', this);
1620 * Restores the navigation to the original state after the fast filter is cleared
1622 * @param bool focus Whether to also focus the input box of the fast filter
1626 PMA_fastFilter.filter.prototype.restore = function (focus) {
1627 if (this.$this.children('ul').first().hasClass('search_results')) {
1628 this.$this.html(this.$clone.html()).children().show();
1629 this.$this.data('fastFilter', this);
1631 this.$this.find('li.fast_filter input.searchClause').focus();
1634 this.searchClause = '';
1635 this.$this.find('div.pageselector').show();
1636 this.$this.find('div.throbber').remove();
1640 * Show full name when cursor hover and name not shown completely
1642 * @param object $containerELem Container element
1646 function PMA_showFullName ($containerELem) {
1647 $containerELem.find('.hover_show_full').mouseenter(function () {
1649 var $this = $(this);
1650 var thisOffset = $this.offset();
1651 if ($this.text() === '') {
1654 var $parent = $this.parent();
1655 if (($parent.offset().left + $parent.outerWidth())
1656 < (thisOffset.left + $this.outerWidth())) {
1657 var $fullNameLayer = $('#full_name_layer');
1658 if ($fullNameLayer.length === 0) {
1659 $('body').append('<div id="full_name_layer" class="hide"></div>');
1660 $('#full_name_layer').mouseleave(function () {
1662 $(this).addClass('hide')
1663 .removeClass('hovering');
1664 }).mouseenter(function () {
1666 $(this).addClass('hovering');
1668 $fullNameLayer = $('#full_name_layer');
1670 $fullNameLayer.removeClass('hide');
1671 $fullNameLayer.css({ left: thisOffset.left, top: thisOffset.top });
1672 $fullNameLayer.html($this.clone());
1673 setTimeout(function () {
1674 if (! $fullNameLayer.hasClass('hovering')) {
1675 $fullNameLayer.trigger('mouseleave');