Added buttons to scroll toolbar buttons. Fixes #606.
[redakcja.git] / redakcja / static / js / wiki / toolbar.js
1 /*
2  * Toolbar plugin.
3  */
4 (function($) {
5     
6     $.fn.toolbarize = function(options) {
7         var $toolbar = $(this);
8         var $container = $('.button_group_container', $toolbar);
9         
10         $('.button_group button', $toolbar).click(function(event){            
11             event.preventDefault();
12             
13             var params = eval("(" + $(this).attr('data-ui-action-params') + ")");
14             
15             scriptletCenter.callInteractive({
16                 action: $(this).attr('data-ui-action'),
17                 context: options.actionContext,
18                 extra: params
19             });
20         });
21                 
22         $toolbar.children().filter('select').change(function(event){
23             var slug = $(this).val();            
24             $container.scrollLeft(0);
25             $('*[data-group]').hide().filter('[data-group=' + slug + ']').show();            
26         }).change();
27         
28         $('button.next', $toolbar).click(function() {
29             var $current_group = $('.button_group:visible', $toolbar);            
30             var scroll = $container.scrollLeft();            
31                        
32             var $hidden = $("li", $current_group).filter(function() {
33                 return ($(this).position().left + $(this).outerWidth()) > $container.width();
34             }).first(); 
35             
36             if($hidden.length > 0) {
37                 scroll = $hidden.position().left + scroll + $hidden.outerWidth() - $container.width() + 1;          
38                 $container.scrollLeft(scroll);
39             };
40         });
41         
42         $('button.prev', $toolbar).click(function() {
43             var $current_group = $('.button_group:visible', $toolbar);            
44             var scroll = $container.scrollLeft();
45             
46             /* first not visible element is: */          
47             var $hidden = $("li", $current_group).filter(function() {
48                 return $(this).position().left < 0;
49             }).last();
50             
51             if( $hidden.length > 0)
52             {
53                 scroll = scroll + $hidden.position().left;           
54                 $container.scrollLeft(scroll);
55             };
56         });
57         
58     };           
59                     
60 })(jQuery);