Blame view

bower_components/Materialize/js/materialbox.js 7.99 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
  (function ($) {
  
    $.fn.materialbox = function () {
  
      return this.each(function() {
  
        if ($(this).hasClass('initialized')) {
          return;
        }
  
        $(this).addClass('initialized');
  
        var overlayActive = false;
        var doneAnimating = true;
        var inDuration = 275;
        var outDuration = 200;
        var origin = $(this);
        var placeholder = $('<div></div>').addClass('material-placeholder');
        var originalWidth = 0;
        var originalHeight = 0;
a1a3bc73   Luigi Serra   graphs updates
21
22
        var ancestorsChanged;
        var ancestor;
74249687   Luigi Serra   Cross browser con...
23
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
        origin.wrap(placeholder);
  
  
        origin.on('click', function(){
          var placeholder = origin.parent('.material-placeholder');
          var windowWidth = window.innerWidth;
          var windowHeight = window.innerHeight;
          var originalWidth = origin.width();
          var originalHeight = origin.height();
  
  
          // If already modal, return to original
          if (doneAnimating === false) {
            returnToOriginal();
            return false;
          }
          else if (overlayActive && doneAnimating===true) {
            returnToOriginal();
            return false;
          }
  
  
          // Set states
          doneAnimating = false;
          origin.addClass('active');
          overlayActive = true;
  
          // Set positioning for placeholder
74249687   Luigi Serra   Cross browser con...
51
52
53
54
55
56
57
58
          placeholder.css({
            width: placeholder[0].getBoundingClientRect().width,
            height: placeholder[0].getBoundingClientRect().height,
            position: 'relative',
            top: 0,
            left: 0
          });
  
a1a3bc73   Luigi Serra   graphs updates
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
          // Find ancestor with overflow: hidden; and remove it
          ancestorsChanged = undefined;
          ancestor = placeholder[0].parentNode;
          var count = 0;
          while (ancestor !== null && !$(ancestor).is(document)) {
            var curr = $(ancestor);
            if (curr.css('overflow') === 'hidden') {
              curr.css('overflow', 'visible');
              if (ancestorsChanged === undefined) {
                ancestorsChanged = curr;
              }
              else {
                ancestorsChanged = ancestorsChanged.add(curr);
              }
            }
            ancestor = ancestor.parentNode;
          }
74249687   Luigi Serra   Cross browser con...
76
77
78
79
80
81
82
83
84
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  
          // Set css on origin
          origin.css({position: 'absolute', 'z-index': 1000})
          .data('width', originalWidth)
          .data('height', originalHeight);
  
          // Add overlay
          var overlay = $('<div id="materialbox-overlay"></div>')
            .css({
              opacity: 0
            })
            .click(function(){
              if (doneAnimating === true)
              returnToOriginal();
            });
            // Animate Overlay
            $('body').append(overlay);
            overlay.velocity({opacity: 1}, {duration: inDuration, queue: false, easing: 'easeOutQuad'}
              );
  
  
          // Add and animate caption if it exists
          if (origin.data('caption') !== "") {
            var $photo_caption = $('<div class="materialbox-caption"></div>');
            $photo_caption.text(origin.data('caption'));
            $('body').append($photo_caption);
            $photo_caption.css({ "display": "inline" });
            $photo_caption.velocity({opacity: 1}, {duration: inDuration, queue: false, easing: 'easeOutQuad'});
          }
  
  
  
          // Resize Image
          var ratio = 0;
          var widthPercent = originalWidth / windowWidth;
          var heightPercent = originalHeight / windowHeight;
          var newWidth = 0;
          var newHeight = 0;
  
          if (widthPercent > heightPercent) {
            ratio = originalHeight / originalWidth;
            newWidth = windowWidth * 0.9;
            newHeight = windowWidth * 0.9 * ratio;
          }
          else {
            ratio = originalWidth / originalHeight;
            newWidth = (windowHeight * 0.9) * ratio;
            newHeight = windowHeight * 0.9;
          }
  
          // Animate image + set z-index
          if(origin.hasClass('responsive-img')) {
            origin.velocity({'max-width': newWidth, 'width': originalWidth}, {duration: 0, queue: false,
              complete: function(){
                origin.css({left: 0, top: 0})
                .velocity(
                  {
                    height: newHeight,
                    width: newWidth,
                    left: $(document).scrollLeft() + windowWidth/2 - origin.parent('.material-placeholder').offset().left - newWidth/2,
                    top: $(document).scrollTop() + windowHeight/2 - origin.parent('.material-placeholder').offset().top - newHeight/ 2
                  },
                  {
                    duration: inDuration,
                    queue: false,
                    easing: 'easeOutQuad',
                    complete: function(){doneAnimating = true;}
                  }
                );
              } // End Complete
            }); // End Velocity
          }
          else {
            origin.css('left', 0)
            .css('top', 0)
            .velocity(
              {
                height: newHeight,
                width: newWidth,
                left: $(document).scrollLeft() + windowWidth/2 - origin.parent('.material-placeholder').offset().left - newWidth/2,
                top: $(document).scrollTop() + windowHeight/2 - origin.parent('.material-placeholder').offset().top - newHeight/ 2
              },
              {
                duration: inDuration,
                queue: false,
                easing: 'easeOutQuad',
                complete: function(){doneAnimating = true;}
              }
              ); // End Velocity
          }
  
      }); // End origin on click
  
  
        // Return on scroll
        $(window).scroll(function() {
          if (overlayActive ) {
            returnToOriginal();
          }
        });
  
        // Return on ESC
        $(document).keyup(function(e) {
  
          if (e.keyCode === 27 && doneAnimating === true) {   // ESC key
            if (overlayActive) {
              returnToOriginal();
            }
          }
        });
  
  
        // This function returns the modaled image to the original spot
        function returnToOriginal() {
  
            doneAnimating = false;
  
            var placeholder = origin.parent('.material-placeholder');
            var windowWidth = window.innerWidth;
            var windowHeight = window.innerHeight;
            var originalWidth = origin.data('width');
            var originalHeight = origin.data('height');
  
            origin.velocity("stop", true);
            $('#materialbox-overlay').velocity("stop", true);
            $('.materialbox-caption').velocity("stop", true);
  
  
            $('#materialbox-overlay').velocity({opacity: 0}, {
              duration: outDuration, // Delay prevents animation overlapping
              queue: false, easing: 'easeOutQuad',
              complete: function(){
                // Remove Overlay
                overlayActive = false;
                $(this).remove();
              }
            });
  
            // Resize Image
            origin.velocity(
              {
                width: originalWidth,
                height: originalHeight,
                left: 0,
                top: 0
              },
              {
                duration: outDuration,
                queue: false, easing: 'easeOutQuad'
              }
            );
  
            // Remove Caption + reset css settings on image
            $('.materialbox-caption').velocity({opacity: 0}, {
              duration: outDuration, // Delay prevents animation overlapping
              queue: false, easing: 'easeOutQuad',
              complete: function(){
                placeholder.css({
                  height: '',
                  width: '',
                  position: '',
                  top: '',
                  left: ''
                });
  
                origin.css({
                  height: '',
                  top: '',
                  left: '',
                  width: '',
                  'max-width': '',
                  position: '',
                  'z-index': ''
                });
  
                // Remove class
                origin.removeClass('active');
                doneAnimating = true;
                $(this).remove();
a1a3bc73   Luigi Serra   graphs updates
255
256
257
  
                // Remove overflow overrides on ancestors
                ancestorsChanged.css('overflow', '');
74249687   Luigi Serra   Cross browser con...
258
259
260
261
262
263
264
265
266
267
268
269
              }
            });
  
          }
          });
  };
  
  $(document).ready(function(){
    $('.materialboxed').materialbox();
  });
  
  }( jQuery ));