Translated using Weblate (Turkish)
[phpmyadmin.git] / js / menu_resizer.js
bloba5d4eb75c94d59c3626604019cb24b880c40650a
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * Handles the resizing of a menu according to the available screen width
4  *
5  * Uses themes/original/css/resizable-menu.css.php
6  *
7  * To initialise:
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
12  * });
13  *
14  * To trigger a resize operation:
15  * $('#myMenu').menuResizer('resize'); // Bind this to $(window).resize()
16  *
17  * To restore the menu to a state like before it was initialized:
18  * $('#myMenu').menuResizer('destroy');
19  *
20  * @package PhpMyAdmin
21  */
22 (function ($) {
23     function MenuResizer ($container, widthCalculator) {
24         var self = this;
25         self.$container = $container;
26         self.widthCalculator = widthCalculator;
27         var windowWidth = $(window).width();
29         if (windowWidth < 768) {
30             $('#pma_navigation_resizer').css({ 'width': '0px' });
31         }
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);
42         });
43         $('.scrollindicator--left').on('click', function () {
44             $('.navigationbar').scrollLeft($('.navigationbar').scrollLeft() - 70);
45         });
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');
52         if (img.length) {
53             $(Functions.getImage('b_more').toString()).prependTo(link);
54         }
55         var $submenu = $('<li></li>', { 'class': 'submenu' })
56             .append(link)
57             .append($('<ul></ul>'))
58             .on('mouseenter', function () {
59                 if ($(this).find('ul .tabactive').length === 0) {
60                     $(this)
61                         .addClass('submenuhover')
62                         .find('> a')
63                         .addClass('tabactive');
64                 }
65             })
66             .on('mouseleave', function () {
67                 if ($(this).find('ul .tabactive').length === 0) {
68                     $(this)
69                         .removeClass('submenuhover')
70                         .find('> a')
71                         .removeClass('tabactive');
72                 }
73             });
74         $container.children('.clearfloat').remove();
75         $container.append($submenu).append('<div class=\'clearfloat\'></div>');
76         setTimeout(function () {
77             self.resize();
78         }, 4);
79     }
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;
92         var i;
93         for (i = 0; i < l; i++) {
94             totalLen += $($li[i]).outerWidth(true);
95         }
97         var hasVScroll = document.body.scrollHeight > document.body.clientHeight;
98         if (hasVScroll) {
99             windowWidth += 15;
100         }
101         if (windowWidth < 768) {
102             wmax = 2000;
103         }
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
108             hidden = true;
109             var el = $($li[l]);
110             var elWidth = el.outerWidth(true);
111             el.data('width', elWidth);
112             if (! moreShown) {
113                 totalLen -= elWidth;
114                 el.prependTo($submenuUl);
115                 totalLen += submenuW;
116                 moreShown = true;
117             } else {
118                 totalLen -= elWidth;
119                 el.prependTo($submenuUl);
120             }
121         }
122         // If we didn't hide any tabs, then there might be some space to show some
123         if (! hidden) {
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)
131                 ) {
132                     $($li2[i]).insertBefore($submenu);
133                 } else {
134                     break;
135                 }
136             }
137         }
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' });
143         } else {
144             $('.navigationbar').css({ 'width': 'auto' });
145             $('.navigationbar').css({ 'overflow': 'visible' });
146             if ($submenuUl.find('li').length > 0) {
147                 $submenu.addClass('shown');
148             } else {
149                 $submenu.removeClass('shown');
150             }
151         }
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');
156         } else {
157             // Otherwise we align the submenu to the right edge of the tab
158             $submenuUl.removeClass().addClass('notonly');
159         }
160         if ($submenu.find('.tabactive').length) {
161             $submenu
162                 .addClass('active')
163                 .find('> a')
164                 .removeClass('tab')
165                 .addClass('tabactive');
166         } else {
167             $submenu
168                 .removeClass('active')
169                 .find('> a')
170                 .addClass('tab')
171                 .removeClass('tabactive');
172         }
173     };
174     MenuResizer.prototype.destroy = function () {
175         var $submenu = this.$container.find('li.submenu').removeData();
176         $submenu.find('li').appendTo(this.$container);
177         $submenu.remove();
178     };
180     /** Public API */
181     var methods = {
182         init: function (widthCalculator) {
183             return this.each(function () {
184                 var $this = $(this);
185                 if (! $this.data('menuResizer')) {
186                     $this.data(
187                         'menuResizer',
188                         new MenuResizer($this, widthCalculator)
189                     );
190                 }
191             });
192         },
193         resize: function () {
194             return this.each(function () {
195                 var self = $(this).data('menuResizer');
196                 if (self) {
197                     self.resize();
198                 }
199             });
200         },
201         destroy: function () {
202             return this.each(function () {
203                 var self = $(this).data('menuResizer');
204                 if (self) {
205                     self.destroy();
206                 }
207             });
208         }
209     };
211     /** Extend jQuery */
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]);
217         } else {
218             $.error('Method ' +  method + ' does not exist on jQuery.menuResizer');
219         }
220     };
221 }(jQuery));