Blame view

bower_components/Materialize/js/sideNav.js 10.9 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
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  (function ($) {
  
    var methods = {
      init : function(options) {
        var defaults = {
          menuWidth: 240,
          edge: 'left',
          closeOnClick: false
        };
        options = $.extend(defaults, options);
  
        $(this).each(function(){
          var $this = $(this);
          var menu_id = $("#"+ $this.attr('data-activates'));
  
          // Set to width
          if (options.menuWidth != 240) {
            menu_id.css('width', options.menuWidth);
          }
  
          // Add Touch Area
          var dragTarget = $('<div class="drag-target"></div>');
          $('body').append(dragTarget);
  
          if (options.edge == 'left') {
            menu_id.css('left', -1 * (options.menuWidth + 10));
            dragTarget.css({'left': 0}); // Add Touch Area
          }
          else {
            menu_id.addClass('right-aligned') // Change text-alignment to right
              .css('right', -1 * (options.menuWidth + 10))
              .css('left', '');
            dragTarget.css({'right': 0}); // Add Touch Area
          }
  
          // If fixed sidenav, bring menu out
          if (menu_id.hasClass('fixed')) {
              if (window.innerWidth > 992) {
                menu_id.css('left', 0);
              }
            }
  
          // Window resize to reset on large screens fixed
          if (menu_id.hasClass('fixed')) {
            $(window).resize( function() {
              if (window.innerWidth > 992) {
                // Close menu if window is resized bigger than 992 and user has fixed sidenav
                if ($('#sidenav-overlay').css('opacity') !== 0 && menuOut) {
                  removeMenu(true);
                }
                else {
                  menu_id.removeAttr('style');
                  menu_id.css('width', options.menuWidth);
                }
              }
              else if (menuOut === false){
                if (options.edge === 'left')
                  menu_id.css('left', -1 * (options.menuWidth + 10));
                else
                  menu_id.css('right', -1 * (options.menuWidth + 10));
              }
  
            });
          }
  
          // if closeOnClick, then add close event for all a tags in side sideNav
          if (options.closeOnClick === true) {
            menu_id.on("click.itemclick", "a:not(.collapsible-header)", function(){
              removeMenu();
            });
          }
  
          function removeMenu(restoreNav) {
            panning = false;
            menuOut = false;
  
            // Reenable scrolling
            $('body').css('overflow', '');
  
            $('#sidenav-overlay').velocity({opacity: 0}, {duration: 200, queue: false, easing: 'easeOutQuad',
              complete: function() {
                $(this).remove();
              } });
            if (options.edge === 'left') {
              // Reset phantom div
              dragTarget.css({width: '', right: '', left: '0'});
              menu_id.velocity(
                {left: -1 * (options.menuWidth + 10)},
                { duration: 200,
                  queue: false,
                  easing: 'easeOutCubic',
                  complete: function() {
                    if (restoreNav === true) {
                      // Restore Fixed sidenav
                      menu_id.removeAttr('style');
                      menu_id.css('width', options.menuWidth);
                    }
                  }
  
              });
            }
            else {
              // Reset phantom div
              dragTarget.css({width: '', right: '0', left: ''});
              menu_id.velocity(
                {right: -1 * (options.menuWidth + 10)},
                { duration: 200,
                  queue: false,
                  easing: 'easeOutCubic',
                  complete: function() {
                    if (restoreNav === true) {
                      // Restore Fixed sidenav
                      menu_id.removeAttr('style');
                      menu_id.css('width', options.menuWidth);
                    }
                  }
                });
            }
          }
  
  
  
          // Touch Event
          var panning = false;
          var menuOut = false;
  
          dragTarget.on('click', function(){
            removeMenu();
          });
  
          dragTarget.hammer({
            prevent_default: false
          }).bind('pan', function(e) {
  
            if (e.gesture.pointerType == "touch") {
  
              var direction = e.gesture.direction;
              var x = e.gesture.center.x;
              var y = e.gesture.center.y;
              var velocityX = e.gesture.velocityX;
  
              // Disable Scrolling
              $('body').css('overflow', 'hidden');
  
              // If overlay does not exist, create one and if it is clicked, close menu
              if ($('#sidenav-overlay').length === 0) {
                var overlay = $('<div id="sidenav-overlay"></div>');
                overlay.css('opacity', 0).click( function(){
                  removeMenu();
                });
                $('body').append(overlay);
              }
  
              // Keep within boundaries
              if (options.edge === 'left') {
                if (x > options.menuWidth) { x = options.menuWidth; }
                else if (x < 0) { x = 0; }
              }
  
              if (options.edge === 'left') {
                // Left Direction
                if (x < (options.menuWidth / 2)) { menuOut = false; }
                // Right Direction
                else if (x >= (options.menuWidth / 2)) { menuOut = true; }
  
                menu_id.css('left', (x - options.menuWidth));
              }
              else {
                // Left Direction
                if (x < (window.innerWidth - options.menuWidth / 2)) {
                  menuOut = true;
                }
                // Right Direction
                else if (x >= (window.innerWidth - options.menuWidth / 2)) {
                 menuOut = false;
               }
                var rightPos = -1 *(x - options.menuWidth / 2);
                if (rightPos > 0) {
                  rightPos = 0;
                }
  
                menu_id.css('right', rightPos);
              }
  
  
  
  
              // Percentage overlay
              var overlayPerc;
              if (options.edge === 'left') {
                overlayPerc = x / options.menuWidth;
                $('#sidenav-overlay').velocity({opacity: overlayPerc }, {duration: 50, queue: false, easing: 'easeOutQuad'});
              }
              else {
                overlayPerc = Math.abs((x - window.innerWidth) / options.menuWidth);
                $('#sidenav-overlay').velocity({opacity: overlayPerc }, {duration: 50, queue: false, easing: 'easeOutQuad'});
              }
            }
  
          }).bind('panend', function(e) {
  
            if (e.gesture.pointerType == "touch") {
              var velocityX = e.gesture.velocityX;
              panning = false;
              if (options.edge === 'left') {
                // If velocityX <= 0.3 then the user is flinging the menu closed so ignore menuOut
                if ((menuOut && velocityX <= 0.3) || velocityX < -0.5) {
                  menu_id.velocity({left: 0}, {duration: 300, queue: false, easing: 'easeOutQuad'});
                  $('#sidenav-overlay').velocity({opacity: 1 }, {duration: 50, queue: false, easing: 'easeOutQuad'});
                  dragTarget.css({width: '50%', right: 0, left: ''});
                }
                else if (!menuOut || velocityX > 0.3) {
                  // Enable Scrolling
                  $('body').css('overflow', '');
                  // Slide menu closed
                  menu_id.velocity({left: -1 * (options.menuWidth + 10)}, {duration: 200, queue: false, easing: 'easeOutQuad'});
                  $('#sidenav-overlay').velocity({opacity: 0 }, {duration: 200, queue: false, easing: 'easeOutQuad',
                    complete: function () {
                      $(this).remove();
                    }});
                  dragTarget.css({width: '10px', right: '', left: 0});
                }
              }
              else {
                if ((menuOut && velocityX >= -0.3) || velocityX > 0.5) {
                  menu_id.velocity({right: 0}, {duration: 300, queue: false, easing: 'easeOutQuad'});
                  $('#sidenav-overlay').velocity({opacity: 1 }, {duration: 50, queue: false, easing: 'easeOutQuad'});
                  dragTarget.css({width: '50%', right: '', left: 0});
                }
                else if (!menuOut || velocityX < -0.3) {
                  // Enable Scrolling
                  $('body').css('overflow', '');
                  // Slide menu closed
                  menu_id.velocity({right: -1 * (options.menuWidth + 10)}, {duration: 200, queue: false, easing: 'easeOutQuad'});
                  $('#sidenav-overlay').velocity({opacity: 0 }, {duration: 200, queue: false, easing: 'easeOutQuad',
                    complete: function () {
                      $(this).remove();
                    }});
                  dragTarget.css({width: '10px', right: 0, left: ''});
                }
              }
  
            }
          });
  
            $this.click(function() {
              if (menuOut === true) {
                menuOut = false;
                panning = false;
                removeMenu();
              }
              else {
  
                // Disable Scrolling
                $('body').css('overflow', 'hidden');
                // Push current drag target on top of DOM tree
                $('body').append(dragTarget);
                
                if (options.edge === 'left') {
                  dragTarget.css({width: '50%', right: 0, left: ''});
                  menu_id.velocity({left: 0}, {duration: 300, queue: false, easing: 'easeOutQuad'});
                }
                else {
                  dragTarget.css({width: '50%', right: '', left: 0});
                  menu_id.velocity({right: 0}, {duration: 300, queue: false, easing: 'easeOutQuad'});
                  menu_id.css('left','');
                }
  
                var overlay = $('<div id="sidenav-overlay"></div>');
                overlay.css('opacity', 0)
                .click(function(){
                  menuOut = false;
                  panning = false;
                  removeMenu();
                  overlay.velocity({opacity: 0}, {duration: 300, queue: false, easing: 'easeOutQuad',
                    complete: function() {
                      $(this).remove();
                    } });
  
                });
                $('body').append(overlay);
                overlay.velocity({opacity: 1}, {duration: 300, queue: false, easing: 'easeOutQuad',
                  complete: function () {
                    menuOut = true;
                    panning = false;
                  }
                });
              }
  
              return false;
            });
        });
  
  
      },
      show : function() {
        this.trigger('click');
      },
      hide : function() {
        $('#sidenav-overlay').trigger('click');
      }
    };
  
  
      $.fn.sideNav = function(methodOrOptions) {
        if ( methods[methodOrOptions] ) {
          return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
          // Default to "init"
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  methodOrOptions + ' does not exist on jQuery.sideNav' );
        }
      }; // Plugin end
  }( jQuery ));