Blame view

bower_components/Materialize/js/tooltip.js 6.19 KB
74249687   Luigi Serra   Cross browser con...
1
2
3
  (function ($) {
      $.fn.tooltip = function (options) {
          var timeout = null,
c5169e0e   Renato De Donato   a new hope
4
5
6
          counter = null,
          started = false,
          counterInterval = null,
74249687   Luigi Serra   Cross browser con...
7
8
9
10
11
12
13
14
15
16
17
          margin = 5;
  
        // Defaults
        var defaults = {
          delay: 350
        };
  
        // Remove tooltip from the activator
        if (options === "remove") {
          this.each(function(){
            $('#' + $(this).attr('data-tooltip-id')).remove();
74249687   Luigi Serra   Cross browser con...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
          });
          return false;
        }
  
        options = $.extend(defaults, options);
  
  
        return this.each(function(){
          var tooltipId = Materialize.guid();
          var origin = $(this);
          origin.attr('data-tooltip-id', tooltipId);
  
          // Create Text span
          var tooltip_text = $('<span></span>').text(origin.attr('data-tooltip'));
  
          // Create tooltip
          var newTooltip = $('<div></div>');
          newTooltip.addClass('material-tooltip').append(tooltip_text)
            .appendTo($('body'))
            .attr('id', tooltipId);
  
          var backdrop = $('<div></div>').addClass('backdrop');
          backdrop.appendTo(newTooltip);
          backdrop.css({ top: 0, left:0 });
  
  
c5169e0e   Renato De Donato   a new hope
44
         //Destroy previously binded events
74249687   Luigi Serra   Cross browser con...
45
        origin.off('mouseenter.tooltip mouseleave.tooltip');
c5169e0e   Renato De Donato   a new hope
46
          // Mouse In
74249687   Luigi Serra   Cross browser con...
47
48
        origin.on({
          'mouseenter.tooltip': function(e) {
c5169e0e   Renato De Donato   a new hope
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            var tooltip_delay = origin.data("delay");
            tooltip_delay = (tooltip_delay === undefined || tooltip_delay === '') ? options.delay : tooltip_delay;
            counter = 0;
            counterInterval = setInterval(function(){
              counter += 10;
              if (counter >= tooltip_delay && started === false) {
                started = true;
                newTooltip.css({ display: 'block', left: '0px', top: '0px' });
  
                // Set Tooltip text
                newTooltip.children('span').text(origin.attr('data-tooltip'));
  
                // Tooltip positioning
                var originWidth = origin.outerWidth();
                var originHeight = origin.outerHeight();
                var tooltipPosition =  origin.attr('data-position');
                var tooltipHeight = newTooltip.outerHeight();
                var tooltipWidth = newTooltip.outerWidth();
                var tooltipVerticalMovement = '0px';
                var tooltipHorizontalMovement = '0px';
                var scale_factor = 8;
  
                if (tooltipPosition === "top") {
74249687   Luigi Serra   Cross browser con...
72
                // Top Position
c5169e0e   Renato De Donato   a new hope
73
74
75
76
                newTooltip.css({
                  top: origin.offset().top - tooltipHeight - margin,
                  left: origin.offset().left + originWidth/2 - tooltipWidth/2
                });
74249687   Luigi Serra   Cross browser con...
77
78
79
80
81
82
                tooltipVerticalMovement = '-10px';
                backdrop.css({
                  borderRadius: '14px 14px 0 0',
                  transformOrigin: '50% 90%',
                  marginTop: tooltipHeight,
                  marginLeft: (tooltipWidth/2) - (backdrop.width()/2)
74249687   Luigi Serra   Cross browser con...
83
  
74249687   Luigi Serra   Cross browser con...
84
                });
c5169e0e   Renato De Donato   a new hope
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
                }
                // Left Position
                else if (tooltipPosition === "left") {
                  newTooltip.css({
                    top: origin.offset().top + originHeight/2 - tooltipHeight/2,
                    left: origin.offset().left - tooltipWidth - margin
                  });
                  tooltipHorizontalMovement = '-10px';
                  backdrop.css({
                    width: '14px',
                    height: '14px',
                    borderRadius: '14px 0 0 14px',
                    transformOrigin: '95% 50%',
                    marginTop: tooltipHeight/2,
                    marginLeft: tooltipWidth
                  });
                }
                // Right Position
                else if (tooltipPosition === "right") {
                  newTooltip.css({
                    top: origin.offset().top + originHeight/2 - tooltipHeight/2,
                    left: origin.offset().left + originWidth + margin
                  });
                  tooltipHorizontalMovement = '+10px';
                  backdrop.css({
                    width: '14px',
                    height: '14px',
                    borderRadius: '0 14px 14px 0',
                    transformOrigin: '5% 50%',
                    marginTop: tooltipHeight/2,
                    marginLeft: '0px'
                  });
                }
                else {
                  // Bottom Position
                  newTooltip.css({
                    top: origin.offset().top + origin.outerHeight() + margin,
                    left: origin.offset().left + originWidth/2 - tooltipWidth/2
                  });
                  tooltipVerticalMovement = '+10px';
                  backdrop.css({
                    marginLeft: (tooltipWidth/2) - (backdrop.width()/2)
                  });
                }
  
                // Calculate Scale to fill
                scale_factor = tooltipWidth / 8;
                if (scale_factor < 8) {
                  scale_factor = 8;
                }
                if (tooltipPosition === "right" || tooltipPosition === "left") {
                  scale_factor = tooltipWidth / 10;
                  if (scale_factor < 6)
                    scale_factor = 6;
                }
  
                newTooltip.velocity({ marginTop: tooltipVerticalMovement, marginLeft: tooltipHorizontalMovement}, { duration: 350, queue: false })
                  .velocity({opacity: 1}, {duration: 300, delay: 50, queue: false});
                backdrop.css({ display: 'block' })
74249687   Luigi Serra   Cross browser con...
144
145
146
                .velocity({opacity:1},{duration: 55, delay: 0, queue: false})
                .velocity({scale: scale_factor}, {duration: 300, delay: 0, queue: false, easing: 'easeInOutQuad'});
  
c5169e0e   Renato De Donato   a new hope
147
148
              }
            }, 10); // End Interval
74249687   Luigi Serra   Cross browser con...
149
150
151
152
153
  
          // Mouse Out
          },
          'mouseleave.tooltip': function(){
            // Reset State
c5169e0e   Renato De Donato   a new hope
154
155
            clearInterval(counterInterval);
            counter = 0;
74249687   Luigi Serra   Cross browser con...
156
157
  
            // Animate back
c5169e0e   Renato De Donato   a new hope
158
159
160
161
162
163
164
165
166
167
168
            newTooltip.velocity({
              opacity: 0, marginTop: 0, marginLeft: 0}, { duration: 225, queue: false, delay: 225 }
            );
            backdrop.velocity({opacity: 0, scale: 1}, {
              duration:225,
              delay: 275, queue: false,
              complete: function(){
                backdrop.css('display', 'none');
                newTooltip.css('display', 'none');
                started = false;}
            });
74249687   Luigi Serra   Cross browser con...
169
170
171
172
173
          }
          });
      });
    };
  
74249687   Luigi Serra   Cross browser con...
174
175
176
177
    $(document).ready(function(){
       $('.tooltipped').tooltip();
     });
  }( jQuery ));