Projet

Général

Profil

Paste
Télécharger (3,11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / sweaver / plugins / sweaver_plugin_themeswitch / sweaver_plugin_themeswitch.js @ 651307cd

1

    
2
/**
3
 * Add scroller on the theme switcher.
4
 * Inspiration from http://jqueryui.com/demos/slider/side-scroll.html
5
 */
6

    
7
Drupal.Sweaver = Drupal.Sweaver || {};
8

    
9
$(document).ready(function() {
10
  Drupal.Sweaver.themeSwitch_init();
11
  $('#tab-sweaver_plugin_themeswitch a').click(function() {
12
    Drupal.Sweaver.themeSwitch_init();
13
  });
14
});
15

    
16

    
17
Drupal.Sweaver.themeSwitch_init = function() {
18
    // Set content width
19
    var width = 0;
20
    $('#themeswitch-content .selected-image').each(function () {
21
      width += $(this).outerWidth(true);
22
    });
23
    $('#themeswitch-content').css('width', width +'px');
24

    
25
    // scrollpane parts
26
    var scrollPane = $('#themeswitch-pane');
27
    var scrollContent = $('#themeswitch-content');
28

    
29
    // build slider
30
    var scrollbar = $("#sweaver .scroll-bar").slider({
31
      slide:function(e, ui){
32
        if (scrollContent.width() > scrollPane.width()) {
33
          //scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px');
34
          scrollContent.css('cssText', 'margin-left: ' + Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px !important; width: ' + width + 'px');
35
        }
36
        else {
37
          //scrollContent.css('margin-left', 0);
38
          scrollContent.css('cssText', 'margin-left: 0px !important; width: ' + width + 'px');
39
        }
40
      }
41
    });
42

    
43
    // append icon to handle.
44
    var handleHelper = scrollbar.find('.ui-slider-handle')
45
    .mousedown(function(){
46
      scrollbar.width( handleHelper.width() );
47
    })
48
    .mouseup(function(){
49
      scrollbar.width( '100%' );
50
    })
51
    .append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
52
    .wrap('<div class="ui-handle-helper-parent"></div>').parent();
53

    
54
    // change overflow to hidden now that slider handles the scrolling.
55
    scrollPane.css('overflow','hidden');
56

    
57
    // size scrollbar and handle proportionally to scroll distance.
58
    function sizeScrollbar(){
59
      if (scrollContent.width() > scrollPane.width()) {
60
        handleHelper.parents('.ui-slider').show();
61
        var remainder = scrollContent.width() - scrollPane.width();
62
        var proportion = remainder / scrollContent.width();
63
        var handleSize = scrollPane.width() - (proportion * scrollPane.width());
64
        scrollbar.find('.ui-slider-handle').css({
65
          width: handleSize
66
        });
67
        handleHelper.width(scrollbar.width());
68
      }
69
      else {
70
        handleHelper.parents('.ui-slider').hide();
71
      }
72
    }
73

    
74
    // reset slider value based on scroll content position
75
    function resetValue(){
76
      var remainder = scrollPane.width() - scrollContent.width();
77
      var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
78
      var percentage = Math.round(leftVal / remainder * 100);
79
      scrollbar.slider("value", percentage);
80
    }
81

    
82
    // change handle position on window resize
83
    $(window)
84
    .resize(function(){
85
      if (scrollPane.parent().is(':visible')) {
86
        resetValue();
87
        sizeScrollbar();
88
      }
89
    });
90

    
91
    // init scrollbar size
92
    setTimeout(sizeScrollbar,10); // safari wants a timeout
93
};