Blame view

bower_components/Materialize/js/dropdown.js 6.1 KB
74249687   Luigi Serra   Cross browser con...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  (function ($) {
  
    // Add posibility to scroll to selected option
    // usefull for select for example
    $.fn.scrollTo = function(elem) {
      $(this).scrollTop($(this).scrollTop() - $(this).offset().top + $(elem).offset().top);
      return this;
    };
  
    $.fn.dropdown = function (option) {
      var defaults = {
        inDuration: 300,
        outDuration: 225,
        constrain_width: true, // Constrains width of dropdown to the activator
        hover: false,
        gutter: 0, // Spacing from edge
        belowOrigin: false,
        alignment: 'left'
      };
  
      this.each(function(){
      var origin = $(this);
      var options = $.extend({}, defaults, option);
74249687   Luigi Serra   Cross browser con...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  
      // Dropdown menu
      var activates = $("#"+ origin.attr('data-activates'));
  
      function updateOptions() {
        if (origin.data('induration') !== undefined)
          options.inDuration = origin.data('inDuration');
        if (origin.data('outduration') !== undefined)
          options.outDuration = origin.data('outDuration');
        if (origin.data('constrainwidth') !== undefined)
          options.constrain_width = origin.data('constrainwidth');
        if (origin.data('hover') !== undefined)
          options.hover = origin.data('hover');
        if (origin.data('gutter') !== undefined)
          options.gutter = origin.data('gutter');
        if (origin.data('beloworigin') !== undefined)
          options.belowOrigin = origin.data('beloworigin');
        if (origin.data('alignment') !== undefined)
          options.alignment = origin.data('alignment');
      }
  
      updateOptions();
  
      // Attach dropdown to its activator
      origin.after(activates);
  
      /*
        Helper function to position and resize dropdown.
        Used in hover and click handler.
      */
c5169e0e   Renato De Donato   a new hope
54
      function placeDropdown() {
74249687   Luigi Serra   Cross browser con...
55
56
57
58
59
        // Check html data attributes
        updateOptions();
  
        // Set Dropdown state
        activates.addClass('active');
74249687   Luigi Serra   Cross browser con...
60
61
62
63
  
        // Constrain width
        if (options.constrain_width === true) {
          activates.css('width', origin.outerWidth());
c5169e0e   Renato De Donato   a new hope
64
65
        }
        else {
74249687   Luigi Serra   Cross browser con...
66
67
          activates.css('white-space', 'nowrap');
        }
c5169e0e   Renato De Donato   a new hope
68
        var offset = 0;
a1a3bc73   Luigi Serra   graphs updates
69
        if (options.belowOrigin === true) {
c5169e0e   Renato De Donato   a new hope
70
          offset = origin.height();
a1a3bc73   Luigi Serra   graphs updates
71
72
        }
  
c5169e0e   Renato De Donato   a new hope
73
74
75
        // Offscreen detection
        var offsetLeft = origin.offset().left;
        var activatesLeft, width_difference, gutter_spacing;
74249687   Luigi Serra   Cross browser con...
76
        if (offsetLeft + activates.innerWidth() > $(window).width()) {
c5169e0e   Renato De Donato   a new hope
77
          options.alignment = 'right';
74249687   Luigi Serra   Cross browser con...
78
        }
c5169e0e   Renato De Donato   a new hope
79
80
        else if (offsetLeft - activates.innerWidth() + origin.innerWidth() < 0) {
          options.alignment = 'left';
74249687   Luigi Serra   Cross browser con...
81
82
83
        }
  
        // Handle edge alignment
c5169e0e   Renato De Donato   a new hope
84
85
86
87
88
89
90
        if (options.alignment === 'left') {
          width_difference = 0;
          gutter_spacing = options.gutter;
          activatesLeft = origin.position().left + width_difference + gutter_spacing;
  
          // Position dropdown
          activates.css({ left: activatesLeft });
74249687   Luigi Serra   Cross browser con...
91
        }
c5169e0e   Renato De Donato   a new hope
92
93
94
95
96
97
98
99
        else if (options.alignment === 'right') {
          var offsetRight = $(window).width() - offsetLeft - origin.innerWidth();
          width_difference = 0;
          gutter_spacing = options.gutter;
          activatesLeft =  ( $(window).width() - origin.position().left - origin.innerWidth() ) + gutter_spacing;
  
          // Position dropdown
          activates.css({ right: activatesLeft });
74249687   Luigi Serra   Cross browser con...
100
        }
74249687   Luigi Serra   Cross browser con...
101
102
103
        // Position dropdown
        activates.css({
          position: 'absolute',
c5169e0e   Renato De Donato   a new hope
104
          top: origin.position().top + offset,
74249687   Luigi Serra   Cross browser con...
105
106
107
        });
  
  
c5169e0e   Renato De Donato   a new hope
108
  
74249687   Luigi Serra   Cross browser con...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        // Show dropdown
        activates.stop(true, true).css('opacity', 0)
          .slideDown({
          queue: false,
          duration: options.inDuration,
          easing: 'easeOutCubic',
          complete: function() {
            $(this).css('height', '');
          }
        })
          .animate( {opacity: 1}, {queue: false, duration: options.inDuration, easing: 'easeOutSine'});
      }
  
      function hideDropdown() {
74249687   Luigi Serra   Cross browser con...
123
124
        activates.fadeOut(options.outDuration);
        activates.removeClass('active');
74249687   Luigi Serra   Cross browser con...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
      }
  
      // Hover
      if (options.hover) {
        var open = false;
        origin.unbind('click.' + origin.attr('id'));
        // Hover handler to show dropdown
        origin.on('mouseenter', function(e){ // Mouse over
          if (open === false) {
            placeDropdown();
            open = true;
          }
        });
        origin.on('mouseleave', function(e){
          // If hover on origin then to something other than dropdown content, then close
          var toEl = e.toElement || e.relatedTarget; // added browser compatibility for target element
          if(!$(toEl).closest('.dropdown-content').is(activates)) {
            activates.stop(true, true);
            hideDropdown();
            open = false;
          }
        });
  
        activates.on('mouseleave', function(e){ // Mouse out
          var toEl = e.toElement || e.relatedTarget;
          if(!$(toEl).closest('.dropdown-button').is(origin)) {
            activates.stop(true, true);
            hideDropdown();
            open = false;
          }
        });
  
      // Click
      } else {
c5169e0e   Renato De Donato   a new hope
159
  
74249687   Luigi Serra   Cross browser con...
160
161
162
        // Click handler to show dropdown
        origin.unbind('click.' + origin.attr('id'));
        origin.bind('click.'+origin.attr('id'), function(e){
c5169e0e   Renato De Donato   a new hope
163
164
165
166
167
168
169
170
171
  
          if ( origin[0] == e.currentTarget && ($(e.target).closest('.dropdown-content').length === 0) ) {
            e.preventDefault(); // Prevents button click from moving window
            placeDropdown();
  
          }
          // If origin is clicked and menu is open, close menu
          else {
            if (origin.hasClass('active')) {
74249687   Luigi Serra   Cross browser con...
172
              hideDropdown();
c5169e0e   Renato De Donato   a new hope
173
              $(document).unbind('click.' + activates.attr('id'));
74249687   Luigi Serra   Cross browser con...
174
            }
74249687   Luigi Serra   Cross browser con...
175
          }
c5169e0e   Renato De Donato   a new hope
176
177
178
179
180
181
182
183
184
          // If menu open, add click close handler to document
          if (activates.hasClass('active')) {
            $(document).bind('click.'+ activates.attr('id'), function (e) {
              if (!activates.is(e.target) && !origin.is(e.target) && (!origin.find(e.target).length > 0) ) {
                hideDropdown();
                $(document).unbind('click.' + activates.attr('id'));
              }
            });
          }
74249687   Luigi Serra   Cross browser con...
185
186
187
188
189
        });
  
      } // End else
  
      // Listen to open and close event - useful for select component
c5169e0e   Renato De Donato   a new hope
190
      origin.on('open', placeDropdown);
74249687   Luigi Serra   Cross browser con...
191
192
193
194
195
196
197
198
199
      origin.on('close', hideDropdown);
  
  
     });
    }; // End dropdown plugin
  
    $(document).ready(function(){
      $('.dropdown-button').dropdown();
    });
c5169e0e   Renato De Donato   a new hope
200
  }( jQuery ));