1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * Handles the resizing of a menu according to the available screen width
5 * Uses themes/original/css/resizable-menu.css.php
8 * $('#myMenu').menuResizer(function () {
9 * // This function will be called to find out how much
10 * // available horizontal space there is for the menu
11 * return $('body').width() - 5; // Some extra margin for good measure
14 * To trigger a resize operation:
15 * $('#myMenu').menuResizer('resize'); // Bind this to $(window).resize()
17 * To restore the menu to a state like before it was initialized:
18 * $('#myMenu').menuResizer('destroy');
23 function MenuResizer ($container, widthCalculator) {
25 self.$container = $container;
26 self.widthCalculator = widthCalculator;
27 var windowWidth = $(window).width();
29 if (windowWidth < 768) {
30 $('#pma_navigation_resizer').css({ 'width': '0px' });
32 // Sets the image for the left and right scroll indicator
33 $('.scrollindicator--left').html($(Functions.getImage('b_left').toString()));
34 $('.scrollindicator--right').html($(Functions.getImage('b_right').toString()));
36 // Set the width of the navigation bar without scroll indicator
37 $('.navigationbar').css({ 'width': widthCalculator.call($container) - 60 });
39 // Scroll the navigation bar on click
40 $('.scrollindicator--right').on('click', function () {
41 $('.navigationbar').scrollLeft($('.navigationbar').scrollLeft() + 70);
43 $('.scrollindicator--left').on('click', function () {
44 $('.navigationbar').scrollLeft($('.navigationbar').scrollLeft() - 70);
47 // create submenu container
48 var link = $('<a></a>', { href: '#', 'class': 'tab nowrap' })
49 .text(Messages.strMore)
50 .on('click', false); // same as event.preventDefault()
51 var img = $container.find('li img');
53 $(Functions.getImage('b_more').toString()).prependTo(link);
55 var $submenu = $('<li></li>', { 'class': 'submenu' })
57 .append($('<ul></ul>'))
58 .on('mouseenter', function () {
59 if ($(this).find('ul .tabactive').length === 0) {
61 .addClass('submenuhover')
63 .addClass('tabactive');
66 .on('mouseleave', function () {
67 if ($(this).find('ul .tabactive').length === 0) {
69 .removeClass('submenuhover')
71 .removeClass('tabactive');
74 $container.children('.clearfloat').remove();
75 $container.append($submenu).append('<div class=\'clearfloat\'></div>');
76 setTimeout(function () {
80 MenuResizer.prototype.resize = function () {
81 var wmax = this.widthCalculator.call(this.$container);
82 var windowWidth = $(window).width();
83 var $submenu = this.$container.find('.submenu:last');
84 var submenuW = $submenu.outerWidth(true);
85 var $submenuUl = $submenu.find('ul');
86 var $li = this.$container.find('> li');
87 var $li2 = $submenuUl.find('li');
88 var moreShown = $li2.length > 0;
89 // Calculate the total width used by all the shown tabs
90 var totalLen = moreShown ? submenuW : 0;
91 var l = $li.length - 1;
93 for (i = 0; i < l; i++) {
94 totalLen += $($li[i]).outerWidth(true);
97 var hasVScroll = document.body.scrollHeight > document.body.clientHeight;
101 if (windowWidth < 768) {
105 // Now hide menu elements that don't fit into the menubar
106 var hidden = false; // Whether we have hidden any tabs
107 while (totalLen >= wmax && --l >= 0) { // Process the tabs backwards
110 var elWidth = el.outerWidth(true);
111 el.data('width', elWidth);
114 el.prependTo($submenuUl);
115 totalLen += submenuW;
119 el.prependTo($submenuUl);
122 // If we didn't hide any tabs, then there might be some space to show some
124 // Show menu elements that do fit into the menubar
125 for (i = 0, l = $li2.length; i < l; i++) {
126 totalLen += $($li2[i]).data('width');
127 // item fits or (it is the last item
128 // and it would fit if More got removed)
129 if (totalLen < wmax ||
130 (i === $li2.length - 1 && totalLen - submenuW < wmax)
132 $($li2[i]).insertBefore($submenu);
138 // Show/hide the "More" tab as needed
139 if (windowWidth < 768) {
140 $('.navigationbar').css({ 'width': windowWidth - 80 - $('#pma_navigation').width() });
141 $submenu.removeClass('shown');
142 $('.navigationbar').css({ 'overflow': 'hidden' });
144 $('.navigationbar').css({ 'width': 'auto' });
145 $('.navigationbar').css({ 'overflow': 'visible' });
146 if ($submenuUl.find('li').length > 0) {
147 $submenu.addClass('shown');
149 $submenu.removeClass('shown');
152 if (this.$container.find('> li').length === 1) {
153 // If there is only the "More" tab left, then we need
154 // to align the submenu to the left edge of the tab
155 $submenuUl.removeClass().addClass('only');
157 // Otherwise we align the submenu to the right edge of the tab
158 $submenuUl.removeClass().addClass('notonly');
160 if ($submenu.find('.tabactive').length) {
165 .addClass('tabactive');
168 .removeClass('active')
171 .removeClass('tabactive');
174 MenuResizer.prototype.destroy = function () {
175 var $submenu = this.$container.find('li.submenu').removeData();
176 $submenu.find('li').appendTo(this.$container);
182 init: function (widthCalculator) {
183 return this.each(function () {
185 if (! $this.data('menuResizer')) {
188 new MenuResizer($this, widthCalculator)
193 resize: function () {
194 return this.each(function () {
195 var self = $(this).data('menuResizer');
201 destroy: function () {
202 return this.each(function () {
203 var self = $(this).data('menuResizer');
212 $.fn.menuResizer = function (method) {
213 if (methods[method]) {
214 return methods[method].call(this);
215 } else if (typeof method === 'function') {
216 return methods.init.apply(this, [method]);
218 $.error('Method ' + method + ' does not exist on jQuery.menuResizer');