ソースを参照

clean the nicescroll array on destroy

Gianluca Guarini 12 年 前
コミット
ea30eca307
1 ファイル変更69 行追加50 行削除
  1. 69 50
      jquery.nicescroll.js

+ 69 - 50
jquery.nicescroll.js

@@ -109,8 +109,8 @@
 
     var d = {};
     
-		d.haspointerlock = "pointerLockElement" in document || "mozPointerLockElement" in document || "webkitPointerLockElement" in document;
-		
+    d.haspointerlock = "pointerLockElement" in document || "mozPointerLockElement" in document || "webkitPointerLockElement" in document;
+    
     d.isopera = ("opera" in window);
     d.isopera12 = (d.isopera&&("getUserMedia" in navigator));
     
@@ -126,16 +126,16 @@
     d.isie7mobile = (!d.isie9mobile&&d.isie7) && /iemobile/i.test(navigator.userAgent);  //wp 7.0
     
     d.ismozilla = ("MozAppearance" in domtest.style);
-		
+    
     d.iswebkit = ("WebkitAppearance" in domtest.style);
     
     d.ischrome = ("chrome" in window);
-		d.ischrome22 = (d.ischrome&&d.haspointerlock);
+    d.ischrome22 = (d.ischrome&&d.haspointerlock);
     d.ischrome26 = (d.ischrome&&("transition" in domtest.style));  // issue with transform detection (maintain prefix)
     
     d.cantouch = ("ontouchstart" in document.documentElement)||("ontouchstart" in window);  // detection for Chrome Touch Emulation
     d.hasmstouch = (window.navigator.msPointerEnabled||false);  // IE10+ pointer events
-		
+    
     d.ismac = /^mac$/i.test(navigator.platform);
     
     d.isios = (d.cantouch && /iphone|ipad|ipod/i.test(navigator.platform));
@@ -614,20 +614,20 @@
           if (self.rail.align&&off.left) pos.left+=off.left;
         }
         
-				if (!self.locked) self.rail.css({top:pos.top,left:pos.left,height:(len)?len.h:self.win.innerHeight()});
-				
-				if (self.zoom) {				  
-				  self.zoom.css({top:pos.top+1,left:(self.rail.align==1) ? pos.left-20 : pos.left+self.rail.width+4});
-			  }
-				
-				if (self.railh&&!self.locked) {
-					var pos = {top:wpos.top,left:wpos.left};
-					var y = (self.railh.align) ? pos.top + getWidthToPixel(self.win,'border-top-width',true) + self.win.innerHeight() - self.railh.height : pos.top + getWidthToPixel(self.win,'border-top-width',true);
-					var x = pos.left + getWidthToPixel(self.win,'border-left-width');
-					self.railh.css({top:y,left:x,width:self.railh.width});
-				}
-		
-				
+        if (!self.locked) self.rail.css({top:pos.top,left:pos.left,height:(len)?len.h:self.win.innerHeight()});
+        
+        if (self.zoom) {          
+          self.zoom.css({top:pos.top+1,left:(self.rail.align==1) ? pos.left-20 : pos.left+self.rail.width+4});
+        }
+        
+        if (self.railh&&!self.locked) {
+          var pos = {top:wpos.top,left:wpos.left};
+          var y = (self.railh.align) ? pos.top + getWidthToPixel(self.win,'border-top-width',true) + self.win.innerHeight() - self.railh.height : pos.top + getWidthToPixel(self.win,'border-top-width',true);
+          var x = pos.left + getWidthToPixel(self.win,'border-left-width');
+          self.railh.css({top:y,left:x,width:self.railh.width});
+        }
+    
+        
       }
     };
     
@@ -859,8 +859,8 @@
           
           if (cap.isios) self.css(self.win,{'-webkit-tap-highlight-color':'rgba(0,0,0,0)','-webkit-touch-callout':'none'});  // prevent grey layer on click
           
-					if (cap.isie&&self.opt.disableoutline) self.win.attr("hideFocus","true");  // IE, prevent dotted rectangle on focused div
-					if (cap.iswebkit&&self.opt.disableoutline) self.win.css({"outline":"none"});
+          if (cap.isie&&self.opt.disableoutline) self.win.attr("hideFocus","true");  // IE, prevent dotted rectangle on focused div
+          if (cap.iswebkit&&self.opt.disableoutline) self.win.css({"outline":"none"});
           
         }
         
@@ -1394,7 +1394,7 @@
               return self.cancelEvent(e);
             });
           }
-		
+    
           if (self.opt.cursordragontouch||!cap.cantouch&&!self.opt.touchbehavior) {
 
             self.rail.css({"cursor":"default"});
@@ -1451,16 +1451,16 @@
               self.bind(document,"mousemove",self.onselectiondrag);
             }
 
-						if (self.zoom) {
-							self.jqbind(self.zoom,"mouseenter",function() {
-								if (self.canshowonmouseevent) self.showCursor();
-								self.rail.active = true;
-							});          
-							self.jqbind(self.zoom,"mouseleave",function() { 
-								self.rail.active = false;
-								if (!self.rail.drag) self.hideCursor();
-							});
-						}
+            if (self.zoom) {
+              self.jqbind(self.zoom,"mouseenter",function() {
+                if (self.canshowonmouseevent) self.showCursor();
+                self.rail.active = true;
+              });          
+              self.jqbind(self.zoom,"mouseleave",function() { 
+                self.rail.active = false;
+                if (!self.rail.drag) self.hideCursor();
+              });
+            }
 
           } else {
             
@@ -1476,13 +1476,13 @@
             }
           
           }
-						
-					if (self.opt.enablemousewheel) {
-						if (!self.isiframe) self.bind((cap.isie&&self.ispage) ? document : self.docscroll,"mousewheel",self.onmousewheel);
-						self.bind(self.rail,"mousewheel",self.onmousewheel);
-						if (self.railh) self.bind(self.railh,"mousewheel",self.onmousewheelhr);
-					}						
-						
+            
+          if (self.opt.enablemousewheel) {
+            if (!self.isiframe) self.bind((cap.isie&&self.ispage) ? document : self.docscroll,"mousewheel",self.onmousewheel);
+            self.bind(self.rail,"mousewheel",self.onmousewheel);
+            if (self.railh) self.bind(self.railh,"mousewheel",self.onmousewheelhr);
+          }           
+            
           if (!self.ispage&&!cap.cantouch&&!(/HTML|BODY/.test(self.win[0].nodeName))) {
             if (!self.win.attr("tabindex")) self.win.attr({"tabindex":tabindexcounter++});
             
@@ -1598,10 +1598,10 @@
         self.bind(window,'orientationchange',self.lazyResize);
         
         self.bind(window,"load",self.lazyResize);
-		
+    
         if (cap.ischrome&&!self.ispage&&!self.haswrapper) { //chrome void scrollbar bug - it persists in version 26
           var tmp=self.win.attr("style");
-					var ww = parseFloat(self.win.css("width"))+1;
+          var ww = parseFloat(self.win.css("width"))+1;
           self.win.css('width',ww);
           self.synched("chromefix",function(){self.win.attr("style",tmp)});
         }
@@ -1800,8 +1800,8 @@
   
     this.onResize = function(e,page) {
     
-	  if (!self.win) return false;
-	
+    if (!self.win) return false;
+  
       if (!self.haswrapper&&!self.ispage) {
         if (self.win.css('display')=='none') {
           if (self.visibility) self.hideRail().hideRailHr();
@@ -1866,9 +1866,9 @@
   
       self.locked = (self.page.maxh==0)&&(self.page.maxw==0);
       if (self.locked) {
-				if (!self.ispage) self.updateScrollBar(self.view);
-			  return false;
-		  }
+        if (!self.ispage) self.updateScrollBar(self.view);
+        return false;
+      }
 
       if (!self.hidden&&!self.visibility) {
         self.showRail().showRailHr();
@@ -2130,11 +2130,15 @@
       }
       self.saved = false;      
       self.me.data('__nicescroll',''); //erase all traces
-  	  for (var i in self) {
+      // remove the current nicescroll from the scrollbars array
+      $.nicescroll.remove(self)
+
+      for (var i in self) {
         self[i] = null;
         delete self[i];
       }
-      delete this;
+
+      self = null;
       
     };
     
@@ -3054,7 +3058,8 @@
     this.name = "nicescrollarray";
   
     this.each = function(fn) {
-      for(var a=0;a<self.length;a++) fn.call(self[a]);
+      var i = 0;
+      for(var a=0;a<self.length;a++) fn.call(self[a],i++);
       return self;
     };
     
@@ -3062,6 +3067,20 @@
       self[self.length]=nice;
       self.length++;
     };
+    /**
+     * 
+     * Remove a scrollbar from this array when it is destroyed
+     * @param  { object } nice: the scrollbar to remove 
+     *
+     */
+    this.remove = function ( nice ) {
+      self.each(function(i){
+        if(this.id === nice.id) {
+          delete self[i];
+          self.length --;
+        }
+      });
+    };
     
     this.eq = function(idx) {
       return self[idx];
@@ -3152,4 +3171,4 @@
   }
   
 })( jQuery );
-  
+