Browse files

Added destroy method

  • Loading branch information...
1 parent 64bc519 commit 66fb536b105d24f0d0384a522b8f3be122e24f5d @nathggns committed Oct 15, 2011
Showing with 7 additions and 3 deletions.
  1. +1 −1 build/jquery.scrollbars.min.js
  2. +6 −2 src/jquery.scrollbars.js
View
2 build/jquery.scrollbars.min.js
@@ -1,2 +1,2 @@
/*! jQuery Scrollbars | License: https://github.com/nathggns/Scrollbars/blob/master/LICENSE */
-(function(a){a.fn.scrollbars=function(f){var d={};var e={ypadding:"auto",xpadding:"auto",mousewheel:true,mousedrag:false,mousedragcursor:"move",clicktoscroll:true,draggerheight:"auto",draggerwidth:"auto",autohide:false,touch:true,blackberry:true};var c={init:function(p){var h=a.extend(e,p);d[this]={opts:h,ele:this};var o=this;var i=this.css("overflow"),k=this.css("overflow-x"),j=this.css("overflow-y"),m=i=="auto"||i=="scroll";m=m||k=="auto"||k=="scroll";m=m||j=="auto"||j=="scroll";if(!m){return}var l=this.find("img"),n=l.length,g=0;if(n==0){c.prepare.call(this)}else{d[this]["imgs"]={eles:l,length:n,load:g};a.each(l,function(r,q){image=new Image;d[image]=o;a(image).bind("load error",function(){d[d[this]].imgs.load++;if(d[d[this]].imgs.load==d[d[this]].imgs.length){c.prepare.call(d[d[this]].ele)}});image.src=q.src})}},prepare:function(){var j=this;if(this.css("position")=="static"){this.css("position","relative")}id="scroll-"+Math.floor(Math.random()*100000);this.addClass(id).addClass("scrollRoot");d[this].id=id;var i=d[this].opts.xpadding,h=d[this].opts.ypadding;if(i=="auto"){temp=a(document.createElement("div"));temp.addClass("dragConX");a("body").append(temp);i=d[this].opts.xpadding=parseFloat(temp.outerHeight());temp.remove()}if(h=="auto"){temp=a(document.createElement("div"));temp.addClass("dragConY");a("body").append(temp);h=d[this].opts.ypadding=parseFloat(temp.outerWidth());temp.remove()}var k=a(document.createElement("div"));k.addClass(id).addClass("contentWrap");this.wrapInner(k);var k=this.find(".contentWrap");d[this].contentWrap=k;var g=a(document.createElement("div"));g.addClass(id).addClass("rootWrap");this.wrapInner(g);var g=this.find(".rootWrap");d[this].rootWrap=g;g.css({width:this.width()-h,height:this.height()-i});if(g.height()>k.height()&&g.width()>k.width()){this.html(k.html());this.removeClass(id).removeClass("scrollRoot")}if(c.generate.call(this,"X")){c.addEvents.call(this,"X",d[this].id)}if(c.generate.call(this,"Y")){c.addEvents.call(this,"Y",d[this].id)}if(this.find(".dragX").length>0){this.addClass("dragXUsed")}if(this.find(".dragY").length>0){this.addClass("dragYUsed")}},generate:function(i){var j=d[this].contentWrap,g=d[this].rootWrap,h=d[this].id;var p=a(document.createElement("div"));p.addClass(h).addClass("dragCon"+i);this.append(p);d[this][i]={dragCon:p};if(i=="X"){var n=d[this].opts.xpadding;if((g.width()+n)>=j.width()){g.css({height:g.height()+n});p.remove();return false}var l=p.width()/j.width();var o=d[this].opts.draggerwidth;o=o=="auto"?+(p.width()*l):o;o=o<10?10:o;o=o>(p.width()-10)?p.width()-10:o}else{var m=d[this].opts.ypadding;if((g.height()+m)>j.height()){g.css({width:g.width()+m});p.remove();return false}var l=p.height()/j.height();var o=d[this].opts.draggerheight;o=o=="auto"?+(p.height()*l):o;o=o<10?10:o;o=o>(p.height()-10)?p.height()-10:o}var k=a(document.createElement("div"));k.addClass(h).addClass("drag"+i).addClass("drag");d[this][i].drag=k;if(i=="X"){k.css({width:o})}else{k.css({height:o})}p.append(k);if(d[this].opts.autohide){k.fadeTo(0,0)}return true},addEvents:function(j,m){ele=this;drag=d[this][j].drag;dragCon=d[this][j].dragCon;var h={dragMouseDown:function(n){if(j=="X"){a(this).data("move",n.pageX)}else{a(this).data("move",n.pageY)}a(this).addClass("active");a("body").addClass("scrollingActive");n.preventDefault();return false},scrollStarMouseMove:function(n){a(".scrollRoot").each(function(){var o=a(this).find(".drag"+j);if(o.data("move")){if(j=="X"){var p=n.pageX-o.data("move");c.move.call(a(this),p,j);o.data("move",n.pageX)}else{var p=n.pageY-o.data("move");c.move.call(a(this),p,j);o.data("move",n.pageY)}}})},scrollStarMouseUp:function(n){a(".dragX, .dragY").data("move",false)},mousewheel:function(p,q,o,n){if(j=="X"){c.move.call(a(this),o*10,j)}else{c.move.call(a(this),-n*10,j)}return false},clickScrollUp:function(p){var n=a(".dragCon"+j+"."+m);if(a(p.srcElement).hasClass("drag")){return}if(!n.data("mousedown")){return}n.data("mousedown",false);var o=a(this).find(".drag");if(j=="X"){var q=p.pageX;q=q-o.offset().left;q=q-(o.width()/2);c.move.call(a(".scrollRoot."+m),q,j)}else{var q=p.pageY;q=q-o.offset().top;q=q-(o.height()/2);c.move.call(a(".scrollRoot."+m),q,j)}p.preventDefault();return false},clickScrollDown:function(n){if(a(n.srcElement).hasClass("drag")){return}if(!a(this).data("stayOff")){a(this).data("mousedown",true)}else{a(this).data("mousedown",false)}n.preventDefault();return false},hideEnter:function(n){a(this).find(".drag").fadeTo(400,1)},hideOut:function(o){var n=a(this).find(".drag");if(!n.data("move")){n.fadeTo(400,0)}},mouseDragDown:function(n){a("html, body").css({cursor:d[a(this)].opts.mousedragcursor});a(this).data("move",[n.pageX,n.pageY]);n.preventDefault();return false},mouseDragStarMove:function(n){a(".scrollRoot").each(function(){if(a(this).data("move")){x=a(this).data("move")[0];y=a(this).data("move")[1];dX=n.pageX-x;dY=n.pageY-y;c.move.call(a(this),-dX,"X");c.move.call(a(this),-dY,"Y");a(this).data("move",[n.pageX,n.pageY])}})},mouseDragStarUp:function(n){a("html, body").css({cursor:"auto"});a(".scrollRoot").each(function(){a(this).data("move",false)})},onTouchStart:function(n){ele=a(".scrollRoot."+m);if(n.targetTouches.length==1){ele.data("start",[n.targetTouches[0].pageX,n.targetTouches[0].pageY])}else{ele.data("start",false)}},onTouchEnd:function(n){ele=a(".scrollRoot."+m);if(n.targetTouches.length==1){ele.data("start",[n.targetTouches[0].pageX,n.targetTouches[0].pageY])}else{ele.data("start",false)}},onTouchMove:function(n,o){ele=a(".scrollRoot."+m);if(ele.data("start")){startX=ele.data("start")[0];startY=ele.data("start")[1];newX=n.targetTouches[0].pageX;newY=n.targetTouches[0].pageY;difX=newX-startX;difY=newY-startY;ele.data("start",[newX,newY]);if(o){X=c.move.call(a(".scrollRoot."+m),difX,"X");Y=c.move.call(a(".scrollRoot."+m),difY,"Y")}else{X=c.move.call(a(".scrollRoot."+m),-difX,"X");Y=c.move.call(a(".scrollRoot."+m),-difY,"Y")}if(!X&&!Y){n.preventDefault()}}},bbMouseDown:function(n){if(a(this).data("move")){a(this).data("move",false)}else{if(j=="X"){a(this).data("move",n.pageX)}else{a(this).data("move",n.pageY)}}},ignore:function(){}};if(d[this].opts.blackberry&&navigator.userAgent.toLowerCase().search("blackberry")!=-1){h.scrollStarMouseUp=h.ignore;h.dragMouseDown=h.bbMouseDown}var l=navigator.userAgent.toLowerCase();var g=l.search("iphone")>-1||l.search("ipod")>-1||l.search("ipad")>-1||l.search("android")>-1;if(d[this].opts.touch&&g){var i=this.find(".contentWrap").get(0);var k=drag.get(0);i.ontouchstart=h.onTouchStart;i.ontouchend=h.onTouchEnd;i.ontouchmove=h.onTouchMove;k.ontouchstart=function(n){i.ontouchstart(n,true)};k.ontouchend=function(n){i.ontouchend(n,true)};k.ontouchmove=function(n){i.ontouchmove(n,true)}}drag.mousedown(h.dragMouseDown);a("*").mousemove(h.scrollStarMouseMove).mouseup(h.scrollStarMouseUp);if(a().mousewheel&&d[this].opts.mousewheel){this.mousewheel(h.mousewheel)}if(d[this].opts.clicktoscroll){dragCon.mouseup(h.clickScrollUp).mousedown(h.clickScrollDown);drag.get(0).ontouchend=function(n){dragCon.data("stayOff",true);dragCon.data("mousedown",false)}}if(d[this].opts.autohide){this.hover(h.hideEnter,h.hideOut)}if(d[this].opts.mousedrag){this.mousedown(h.mouseDragDown);a("*").mousemove(h.mouseDragStarMove).mouseup(h.mouseDragStarUp);this.css({cursor:d[this].opts.mousedragcursor})}},move:function(n,k){var p=this.find(".drag"+k);var t=this.find(".dragCon"+k);var l=this.find(".contentWrap");var i=this.find(".rootWrap");var s=false;if(k=="X"){var q=p.css("left")}else{var q=p.css("top")}q=(q=="auto")?0:parseFloat(q);var h=q+n;var m=0;if(k=="X"){var r=t.width()-p.width();if(h<m){h=m;s=true}if(h>r){h=r;s=true}p.css({left:h});var o=t.width()-p.width(),j=l.width()-i.width(),g=j/o,h=(h*g)*-1;l.css({left:h})}else{var r=t.height()-p.height();if(h<m){h=m}if(h>r){h=r}p.css({top:h});if(h==r){h=h-1}var o=t.height()-p.height(),j=l.height()-i.height(),g=j/o,h=(h*g)*-1;l.css({top:h})}return s}};var b=arguments;return this.each(function(){if(c[f]){return c[f].apply(a(this),Array.prototype.slice(b,1))}else{if(typeof f==="object"||!f){return c.init.apply(a(this),b)}else{a.error("Method "+f+" does not exist on jQuery.scrollbars")}}})}})(jQuery);
+(function(a){a.fn.scrollbars=function(f){var d={};var e={ypadding:"auto",xpadding:"auto",mousewheel:true,mousedrag:false,mousedragcursor:"move",clicktoscroll:true,draggerheight:"auto",draggerwidth:"auto",autohide:false,touch:true,blackberry:true};var c={init:function(p){var h=a.extend(e,p);d[this]={opts:h,ele:this};var o=this;var i=this.css("overflow"),k=this.css("overflow-x"),j=this.css("overflow-y"),m=i=="auto"||i=="scroll";m=m||k=="auto"||k=="scroll";m=m||j=="auto"||j=="scroll";if(!m){return}var l=this.find("img"),n=l.length,g=0;if(n==0){c.prepare.call(this)}else{d[this]["imgs"]={eles:l,length:n,load:g};a.each(l,function(r,q){image=new Image;d[image]=o;a(image).bind("load error",function(){d[d[this]].imgs.load++;if(d[d[this]].imgs.load==d[d[this]].imgs.length){c.prepare.call(d[d[this]].ele)}});image.src=q.src})}},prepare:function(){var j=this;if(this.css("position")=="static"){this.css("position","relative")}id="scroll-"+Math.floor(Math.random()*100000);this.addClass(id).addClass("scrollRoot");d[this].id=id;var i=d[this].opts.xpadding,h=d[this].opts.ypadding;if(i=="auto"){temp=a(document.createElement("div"));temp.addClass("dragConX");a("body").append(temp);i=d[this].opts.xpadding=parseFloat(temp.outerHeight());temp.remove()}if(h=="auto"){temp=a(document.createElement("div"));temp.addClass("dragConY");a("body").append(temp);h=d[this].opts.ypadding=parseFloat(temp.outerWidth());temp.remove()}var k=a(document.createElement("div"));k.addClass(id).addClass("contentWrap");this.wrapInner(k);var k=this.find(".contentWrap");d[this].contentWrap=k;var g=a(document.createElement("div"));g.addClass(id).addClass("rootWrap");this.wrapInner(g);var g=this.find(".rootWrap");d[this].rootWrap=g;g.css({width:this.width()-h,height:this.height()-i});if(g.height()>k.height()&&g.width()>k.width()){this.html(k.html());this.removeClass(id).removeClass("scrollRoot")}if(c.generate.call(this,"X")){c.addEvents.call(this,"X",d[this].id)}if(c.generate.call(this,"Y")){c.addEvents.call(this,"Y",d[this].id)}if(this.find(".dragX").length>0){this.addClass("dragXUsed")}if(this.find(".dragY").length>0){this.addClass("dragYUsed")}},generate:function(i){var j=d[this].contentWrap,g=d[this].rootWrap,h=d[this].id;var p=a(document.createElement("div"));p.addClass(h).addClass("dragCon"+i);this.append(p);d[this][i]={dragCon:p};if(i=="X"){var n=d[this].opts.xpadding;if((g.width()+n)>=j.width()){g.css({height:g.height()+n});p.remove();return false}var l=p.width()/j.width();var o=d[this].opts.draggerwidth;o=o=="auto"?+(p.width()*l):o;o=o<10?10:o;o=o>(p.width()-10)?p.width()-10:o}else{var m=d[this].opts.ypadding;if((g.height()+m)>j.height()){g.css({width:g.width()+m});p.remove();return false}var l=p.height()/j.height();var o=d[this].opts.draggerheight;o=o=="auto"?+(p.height()*l):o;o=o<10?10:o;o=o>(p.height()-10)?p.height()-10:o}var k=a(document.createElement("div"));k.addClass(h).addClass("drag"+i).addClass("drag");d[this][i].drag=k;if(i=="X"){k.css({width:o})}else{k.css({height:o})}p.append(k);if(d[this].opts.autohide){k.fadeTo(0,0)}return true},addEvents:function(j,m){ele=this;drag=d[this][j].drag;dragCon=d[this][j].dragCon;var h={dragMouseDown:function(n){if(j=="X"){a(this).data("move",n.pageX)}else{a(this).data("move",n.pageY)}a(this).addClass("active");a("body").addClass("scrollingActive");n.preventDefault();return false},scrollStarMouseMove:function(n){a(".scrollRoot").each(function(){var o=a(this).find(".drag"+j);if(o.data("move")){if(j=="X"){var p=n.pageX-o.data("move");c.move.call(a(this),p,j);o.data("move",n.pageX)}else{var p=n.pageY-o.data("move");c.move.call(a(this),p,j);o.data("move",n.pageY)}}})},scrollStarMouseUp:function(n){a(".dragX, .dragY").data("move",false)},mousewheel:function(p,q,o,n){if(j=="X"){c.move.call(a(this),o*10,j)}else{c.move.call(a(this),-n*10,j)}return false},clickScrollUp:function(p){var n=a(".dragCon"+j+"."+m);if(a(p.srcElement).hasClass("drag")){return}if(!n.data("mousedown")){return}n.data("mousedown",false);var o=a(this).find(".drag");if(j=="X"){var q=p.pageX;q=q-o.offset().left;q=q-(o.width()/2);c.move.call(a(".scrollRoot."+m),q,j)}else{var q=p.pageY;q=q-o.offset().top;q=q-(o.height()/2);c.move.call(a(".scrollRoot."+m),q,j)}p.preventDefault();return false},clickScrollDown:function(n){if(a(n.srcElement).hasClass("drag")){return}if(!a(this).data("stayOff")){a(this).data("mousedown",true)}else{a(this).data("mousedown",false)}n.preventDefault();return false},hideEnter:function(n){a(this).find(".drag").fadeTo(400,1)},hideOut:function(o){var n=a(this).find(".drag");if(!n.data("move")){n.fadeTo(400,0)}},mouseDragDown:function(n){a("html, body").css({cursor:d[a(this)].opts.mousedragcursor});a(this).data("move",[n.pageX,n.pageY]);n.preventDefault();return false},mouseDragStarMove:function(n){a(".scrollRoot").each(function(){if(a(this).data("move")){x=a(this).data("move")[0];y=a(this).data("move")[1];dX=n.pageX-x;dY=n.pageY-y;c.move.call(a(this),-dX,"X");c.move.call(a(this),-dY,"Y");a(this).data("move",[n.pageX,n.pageY])}})},mouseDragStarUp:function(n){a("html, body").css({cursor:"auto"});a(".scrollRoot").each(function(){a(this).data("move",false)})},onTouchStart:function(n){ele=a(".scrollRoot."+m);if(n.targetTouches.length==1){ele.data("start",[n.targetTouches[0].pageX,n.targetTouches[0].pageY])}else{ele.data("start",false)}},onTouchEnd:function(n){ele=a(".scrollRoot."+m);if(n.targetTouches.length==1){ele.data("start",[n.targetTouches[0].pageX,n.targetTouches[0].pageY])}else{ele.data("start",false)}},onTouchMove:function(n,o){ele=a(".scrollRoot."+m);if(ele.data("start")){startX=ele.data("start")[0];startY=ele.data("start")[1];newX=n.targetTouches[0].pageX;newY=n.targetTouches[0].pageY;difX=newX-startX;difY=newY-startY;ele.data("start",[newX,newY]);if(o){X=c.move.call(a(".scrollRoot."+m),difX,"X");Y=c.move.call(a(".scrollRoot."+m),difY,"Y")}else{X=c.move.call(a(".scrollRoot."+m),-difX,"X");Y=c.move.call(a(".scrollRoot."+m),-difY,"Y")}if(!X&&!Y){n.preventDefault()}}},bbMouseDown:function(n){if(a(this).data("move")){a(this).data("move",false)}else{if(j=="X"){a(this).data("move",n.pageX)}else{a(this).data("move",n.pageY)}}},ignore:function(){}};if(d[this].opts.blackberry&&navigator.userAgent.toLowerCase().search("blackberry")!=-1){h.scrollStarMouseUp=h.ignore;h.dragMouseDown=h.bbMouseDown}var l=navigator.userAgent.toLowerCase();var g=l.search("iphone")>-1||l.search("ipod")>-1||l.search("ipad")>-1||l.search("android")>-1;if(d[this].opts.touch&&g){var i=this.find(".contentWrap").get(0);var k=drag.get(0);i.ontouchstart=h.onTouchStart;i.ontouchend=h.onTouchEnd;i.ontouchmove=h.onTouchMove;k.ontouchstart=function(n){i.ontouchstart(n,true)};k.ontouchend=function(n){i.ontouchend(n,true)};k.ontouchmove=function(n){i.ontouchmove(n,true)}}drag.mousedown(h.dragMouseDown);a("*").mousemove(h.scrollStarMouseMove).mouseup(h.scrollStarMouseUp);if(a().mousewheel&&d[this].opts.mousewheel){this.mousewheel(h.mousewheel)}if(d[this].opts.clicktoscroll){dragCon.mouseup(h.clickScrollUp).mousedown(h.clickScrollDown);drag.get(0).ontouchend=function(n){dragCon.data("stayOff",true);dragCon.data("mousedown",false)}}if(d[this].opts.autohide){this.hover(h.hideEnter,h.hideOut)}if(d[this].opts.mousedrag){this.mousedown(h.mouseDragDown);a("*").mousemove(h.mouseDragStarMove).mouseup(h.mouseDragStarUp);this.css({cursor:d[this].opts.mousedragcursor})}},move:function(n,k){var p=this.find(".drag"+k);var t=this.find(".dragCon"+k);var l=this.find(".contentWrap");var i=this.find(".rootWrap");var s=false;if(k=="X"){var q=p.css("left")}else{var q=p.css("top")}q=(q=="auto")?0:parseFloat(q);var h=q+n;var m=0;if(k=="X"){var r=t.width()-p.width();if(h<m){h=m;s=true}if(h>r){h=r;s=true}p.css({left:h});var o=t.width()-p.width(),j=l.width()-i.width(),g=j/o,h=(h*g)*-1;l.css({left:h})}else{var r=t.height()-p.height();if(h<m){h=m}if(h>r){h=r}p.css({top:h});if(h==r){h=h-1}var o=t.height()-p.height(),j=l.height()-i.height(),g=j/o,h=(h*g)*-1;l.css({top:h})}return s},destroy:function(){this.html(this.find(".contentWrap").html());this.removeClass("scrollRoot").removeClass("dragXUsed").removeClass("dragYUsed")}};var b=arguments;return this.each(function(){if(c[f]){return c[f].apply(a(this),Array.prototype.slice(b,1))}else{if(typeof f==="object"||!f){return c.init.apply(a(this),b)}else{a.error("Method "+f+" does not exist on jQuery.scrollbars")}}})}})(jQuery);
View
8 src/jquery.scrollbars.js
@@ -15,7 +15,7 @@
'touch': true,
'blackberry': true
}
-
+
var methods = {
init: function(options) {
var opts = $.extend(defaults, options);
@@ -541,6 +541,10 @@
}
return returnV;
+ },
+ destroy: function() {
+ this.html(this.find('.contentWrap').html());
+ this.removeClass('scrollRoot').removeClass('dragXUsed').removeClass('dragYUsed');
}
}
var arg = arguments;
@@ -554,4 +558,4 @@
}
});
}
-})(jQuery);
+})(jQuery);

0 comments on commit 66fb536

Please sign in to comment.