/scripts/custom-scrollbar-plugin/js/uncompressed/jquery.mCustomScrollbar.js

http://github.com/simogeo/Filemanager · JavaScript · 2199 lines · 1427 code · 225 blank · 547 comment · 392 complexity · ee6db1e09e68d8a6fb574ceb6fdc984c MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. == malihu jquery custom scrollbar plugin ==
  3. Version: 3.0.5
  4. Plugin URI: http://manos.malihu.gr/jquery-custom-content-scroller
  5. Author: malihu
  6. Author URI: http://manos.malihu.gr
  7. License: MIT License (MIT)
  8. */
  9. /*
  10. Copyright 2010 Manos Malihutsakis (email: manos@malihu.gr)
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26. */
  27. /*
  28. The code below is fairly long, fully commented and should be normally used in development.
  29. For production, use either the minified jquery.mCustomScrollbar.min.js script or
  30. the production-ready jquery.mCustomScrollbar.concat.min.js which contains the plugin
  31. and dependencies (minified).
  32. */
  33. ;(function($,window,document,undefined){
  34. (function(init){
  35. var _rjs=typeof define==="function" && define.amd, /* RequireJS */
  36. _dlp=("https:"==document.location.protocol) ? "https:" : "http:", /* location protocol */
  37. _url="cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.11/jquery.mousewheel.min.js";
  38. if(!_rjs){
  39. /* load jquery-mousewheel plugin (via CDN) if it's not present or not loaded via RequireJS
  40. (works when mCustomScrollbar fn is called on window load) */
  41. $.event.special.mousewheel || $("head").append(decodeURI("%3Cscript src="+_dlp+"//"+_url+"%3E%3C/script%3E"));
  42. }
  43. init();
  44. }(function(){
  45. /*
  46. ----------------------------------------
  47. PLUGIN NAMESPACE, PREFIX, DEFAULT SELECTOR(S)
  48. ----------------------------------------
  49. */
  50. var pluginNS="mCustomScrollbar",
  51. pluginPfx="mCS",
  52. defaultSelector=".mCustomScrollbar",
  53. /*
  54. ----------------------------------------
  55. DEFAULT OPTIONS
  56. ----------------------------------------
  57. */
  58. defaults={
  59. /*
  60. set element/content width programmatically
  61. values: boolean, pixels, percentage
  62. */
  63. setWidth:false,
  64. /*
  65. set element/content height programmatically
  66. values: boolean, pixels, percentage
  67. */
  68. setHeight:false,
  69. /*
  70. set the initial css top property of content
  71. values: string (e.g. "-100px", "10%" etc.)
  72. */
  73. setTop:0,
  74. /*
  75. set the initial css left property of content
  76. values: string (e.g. "-100px", "10%" etc.)
  77. */
  78. setLeft:0,
  79. /*
  80. scrollbar axis (vertical and/or horizontal scrollbars)
  81. values (string): "y", "x", "yx"
  82. */
  83. axis:"y",
  84. /*
  85. position of scrollbar relative to content
  86. values (string): "inside", "outside" ("outside" requires elements with position:relative)
  87. */
  88. scrollbarPosition:"inside",
  89. /*
  90. scrolling inertia
  91. values: integer (milliseconds)
  92. */
  93. scrollInertia:950,
  94. /*
  95. auto-adjust scrollbar dragger length
  96. values: boolean
  97. */
  98. autoDraggerLength:true,
  99. /*
  100. auto-hide scrollbar when idle
  101. values: boolean
  102. */
  103. autoHideScrollbar:false,
  104. /*
  105. auto-expands scrollbar on mouse-over and dragging
  106. */
  107. autoExpandScrollbar:false,
  108. /*
  109. always show scrollbar, even when there's nothing to scroll
  110. values: integer (0=disable, 1=always show dragger rail, 2=always show dragger rail, dragger and buttons), boolean
  111. */
  112. alwaysShowScrollbar:0,
  113. /*
  114. scrolling always snaps to a multiple of this number in pixels
  115. values: integer
  116. */
  117. snapAmount:null,
  118. /*
  119. when snapping, snap with this number in pixels as an offset
  120. values: integer
  121. */
  122. snapOffset:0,
  123. /*
  124. mouse-wheel scrolling
  125. */
  126. mouseWheel:{
  127. /*
  128. enable mouse-wheel scrolling
  129. values: boolean
  130. */
  131. enable:true,
  132. /*
  133. scrolling amount in pixels
  134. values: "auto", integer
  135. */
  136. scrollAmount:"auto",
  137. /*
  138. mouse-wheel scrolling axis
  139. the default scrolling direction when both vertical and horizontal scrollbars are present
  140. values (string): "y", "x"
  141. */
  142. axis:"y",
  143. /*
  144. prevent the default behaviour which automatically scrolls the parent element(s)
  145. when end of scrolling is reached
  146. values: boolean
  147. */
  148. preventDefault:false,
  149. /*
  150. the reported mouse-wheel delta value. The number of lines (translated to pixels) one wheel notch scrolls.
  151. values: "auto", integer
  152. "auto" uses the default OS/browser value
  153. */
  154. deltaFactor:"auto",
  155. /*
  156. normalize mouse-wheel delta to -1 or 1 (disables mouse-wheel acceleration)
  157. values: boolean
  158. */
  159. normalizeDelta:false,
  160. /*
  161. invert mouse-wheel scrolling direction
  162. values: boolean
  163. */
  164. invert:false,
  165. /*
  166. the tags that disable mouse-wheel when cursor is over them
  167. */
  168. disableOver:["select","option","keygen","datalist","textarea"]
  169. },
  170. /*
  171. scrollbar buttons
  172. */
  173. scrollButtons:{
  174. /*
  175. enable scrollbar buttons
  176. values: boolean
  177. */
  178. enable:false,
  179. /*
  180. scrollbar buttons scrolling type
  181. values (string): "stepless", "stepped"
  182. */
  183. scrollType:"stepless",
  184. /*
  185. scrolling amount in pixels
  186. values: "auto", integer
  187. */
  188. scrollAmount:"auto"
  189. },
  190. /*
  191. keyboard scrolling
  192. */
  193. keyboard:{
  194. /*
  195. enable scrolling via keyboard
  196. values: boolean
  197. */
  198. enable:true,
  199. /*
  200. keyboard scrolling type
  201. values (string): "stepless", "stepped"
  202. */
  203. scrollType:"stepless",
  204. /*
  205. scrolling amount in pixels
  206. values: "auto", integer
  207. */
  208. scrollAmount:"auto"
  209. },
  210. /*
  211. enable content touch-swipe scrolling
  212. values: boolean, integer, string (number)
  213. integer values define the axis-specific minimum amount required for scrolling momentum
  214. */
  215. contentTouchScroll:25,
  216. /*
  217. advanced option parameters
  218. */
  219. advanced:{
  220. /*
  221. auto-expand content horizontally (for "x" or "yx" axis)
  222. values: boolean
  223. */
  224. autoExpandHorizontalScroll:false,
  225. /*
  226. auto-scroll to elements with focus
  227. */
  228. autoScrollOnFocus:"input,textarea,select,button,datalist,keygen,a[tabindex],area,object,[contenteditable='true']",
  229. /*
  230. auto-update scrollbars on content, element or viewport resize
  231. should be true for fluid layouts/elements, adding/removing content dynamically, hiding/showing elements, content with images etc.
  232. values: boolean
  233. */
  234. updateOnContentResize:true,
  235. /*
  236. auto-update scrollbars each time each image inside the element is fully loaded
  237. values: boolean
  238. */
  239. updateOnImageLoad:true,
  240. /*
  241. auto-update scrollbars based on the amount and size changes of specific selectors
  242. useful when you need to update the scrollbar(s) automatically, each time a type of element is added, removed or changes its size
  243. values: boolean, string (e.g. "ul li" will auto-update scrollbars each time list-items inside the element are changed)
  244. a value of true (boolean) will auto-update scrollbars each time any element is changed
  245. */
  246. updateOnSelectorChange:false,
  247. /*
  248. extra selectors that'll release scrollbar dragging upon mouseup, pointerup, touchend etc. (e.g. "selector-1, selector-2")
  249. */
  250. releaseDraggableSelectors:false
  251. },
  252. /*
  253. scrollbar theme
  254. values: string
  255. ready-to-use themes: "light", "dark", "light-2", "dark-2", "light-3", "dark-3", "light-thick", "dark-thick", "light-thin", "dark-thin",
  256. "rounded", "rounded-dark", "rounded-dots", "rounded-dots-dark", "3d", "3d-dark", "3d-thick", "3d-thick-dark", "minimal", "minimal-dark",
  257. "inset", "inset-dark", "inset-2", "inset-2-dark", "inset-3", "inset-3-dark"
  258. */
  259. theme:"light",
  260. /*
  261. user defined callback functions
  262. */
  263. callbacks:{
  264. /*
  265. function to call when the scrollbars have initialized
  266. values (function): function(){}
  267. */
  268. onInit:false,
  269. /*
  270. function to call when a scroll event starts
  271. values (function): function(){}
  272. */
  273. onScrollStart:false,
  274. /*
  275. function to call when a scroll event is complete
  276. values (function): function(){}
  277. */
  278. onScroll:false,
  279. /*
  280. function to call when a scroll event is complete and content is scrolled all the way to the end (bottom/right)
  281. values (function): function(){}
  282. */
  283. onTotalScroll:false,
  284. /*
  285. function to call when a scroll event is complete and content is scrolled back to the beginning (top/left)
  286. values (function): function(){}
  287. */
  288. onTotalScrollBack:false,
  289. /*
  290. function to call when a scroll event is running
  291. values (function): function(){}
  292. */
  293. whileScrolling:false,
  294. /*
  295. onTotalScroll offset value
  296. values: integer (pixels)
  297. */
  298. onTotalScrollOffset:0,
  299. /*
  300. onTotalScrollBack offset value
  301. values: integer (pixels)
  302. */
  303. onTotalScrollBackOffset:0,
  304. /*
  305. callback offsets will trigger even if content is already scrolled to the end or beginning
  306. values: boolean
  307. */
  308. alwaysTriggerOffsets:true,
  309. /*
  310. function to call when content becomes long enough and vertical scrollbar is added
  311. values (function): function(){}
  312. */
  313. onOverflowY:false,
  314. /*
  315. function to call when content becomes wide enough and horizontal scrollbar is added
  316. values (function): function(){}
  317. */
  318. onOverflowX:false,
  319. /*
  320. function to call when content becomes short enough and vertical scrollbar is removed
  321. values (function): function(){}
  322. */
  323. onOverflowYNone:false,
  324. /*
  325. function to call when content becomes narrow enough and horizontal scrollbar is removed
  326. values (function): function(){}
  327. */
  328. onOverflowXNone:false
  329. },
  330. /*
  331. add scrollbar(s) on all elements matching the current selector, now and in the future
  332. values: boolean, string
  333. string values: "on" (enable), "once" (disable after first invocation), "off" (disable)
  334. */
  335. live:false,
  336. /*
  337. the matching set of elements (instead of the current selector) to add scrollbar(s), now and in the future
  338. values: string (selector)
  339. */
  340. liveSelector:null
  341. },
  342. /*
  343. ----------------------------------------
  344. VARS, CONSTANTS
  345. ----------------------------------------
  346. */
  347. totalInstances=0, /* plugin instances amount */
  348. liveTimers={}, /* live option timers */
  349. /* live option timers removal */
  350. removeLiveTimers=function(selector){
  351. if(liveTimers[selector]){
  352. clearTimeout(liveTimers[selector]);
  353. functions._delete.call(null,liveTimers[selector]);
  354. }
  355. },
  356. oldIE=(window.attachEvent && !window.addEventListener) ? 1 : 0, /* detect IE < 9 */
  357. touchActive=false, /* global touch state (for touch and pointer events) */
  358. /*
  359. ----------------------------------------
  360. METHODS
  361. ----------------------------------------
  362. */
  363. methods={
  364. /*
  365. plugin initialization method
  366. creates the scrollbar(s), plugin data object and options
  367. ----------------------------------------
  368. */
  369. init:function(options){
  370. var options=$.extend(true,{},defaults,options),
  371. selector=functions._selector.call(this); /* validate selector */
  372. /*
  373. if live option is enabled, monitor for elements matching the current selector and
  374. apply scrollbar(s) when found (now and in the future)
  375. */
  376. if(options.live){
  377. var liveSelector=options.liveSelector || this.selector || defaultSelector, /* live selector(s) */
  378. $liveSelector=$(liveSelector); /* live selector(s) as jquery object */
  379. if(options.live==="off"){
  380. /*
  381. disable live if requested
  382. usage: $(selector).mCustomScrollbar({live:"off"});
  383. */
  384. removeLiveTimers(liveSelector);
  385. return;
  386. }
  387. liveTimers[liveSelector]=setTimeout(function(){
  388. /* call mCustomScrollbar fn on live selector(s) every half-second */
  389. $liveSelector.mCustomScrollbar(options);
  390. if(options.live==="once" && $liveSelector.length){
  391. /* disable live after first invocation */
  392. removeLiveTimers(liveSelector);
  393. }
  394. },500);
  395. }else{
  396. removeLiveTimers(liveSelector);
  397. }
  398. /* options backward compatibility (for versions < 3.0.0) and normalization */
  399. options.setWidth=(options.set_width) ? options.set_width : options.setWidth;
  400. options.setHeight=(options.set_height) ? options.set_height : options.setHeight;
  401. options.axis=(options.horizontalScroll) ? "x" : functions._findAxis.call(null,options.axis);
  402. options.scrollInertia=options.scrollInertia>0 && options.scrollInertia<17 ? 17 : options.scrollInertia;
  403. if(typeof options.mouseWheel!=="object" && options.mouseWheel==true){ /* old school mouseWheel option (non-object) */
  404. options.mouseWheel={enable:true,scrollAmount:"auto",axis:"y",preventDefault:false,deltaFactor:"auto",normalizeDelta:false,invert:false}
  405. }
  406. options.mouseWheel.scrollAmount=!options.mouseWheelPixels ? options.mouseWheel.scrollAmount : options.mouseWheelPixels;
  407. options.mouseWheel.normalizeDelta=!options.advanced.normalizeMouseWheelDelta ? options.mouseWheel.normalizeDelta : options.advanced.normalizeMouseWheelDelta;
  408. options.scrollButtons.scrollType=functions._findScrollButtonsType.call(null,options.scrollButtons.scrollType);
  409. functions._theme.call(null,options); /* theme-specific options */
  410. /* plugin constructor */
  411. return $(selector).each(function(){
  412. var $this=$(this);
  413. if(!$this.data(pluginPfx)){ /* prevent multiple instantiations */
  414. /* store options and create objects in jquery data */
  415. $this.data(pluginPfx,{
  416. idx:++totalInstances, /* instance index */
  417. opt:options, /* options */
  418. scrollRatio:{y:null,x:null}, /* scrollbar to content ratio */
  419. overflowed:null, /* overflowed axis */
  420. contentReset:{y:null,x:null}, /* object to check when content resets */
  421. bindEvents:false, /* object to check if events are bound */
  422. tweenRunning:false, /* object to check if tween is running */
  423. sequential:{}, /* sequential scrolling object */
  424. langDir:$this.css("direction"), /* detect/store direction (ltr or rtl) */
  425. cbOffsets:null, /* object to check whether callback offsets always trigger */
  426. /*
  427. object to check how scrolling events where last triggered
  428. "internal" (default - triggered by this script), "external" (triggered by other scripts, e.g. via scrollTo method)
  429. usage: object.data("mCS").trigger
  430. */
  431. trigger:null
  432. });
  433. /* HTML data attributes */
  434. var o=$this.data(pluginPfx).opt,
  435. htmlDataAxis=$this.data("mcs-axis"),htmlDataSbPos=$this.data("mcs-scrollbar-position"),htmlDataTheme=$this.data("mcs-theme");
  436. if(htmlDataAxis){o.axis=htmlDataAxis;} /* usage example: data-mcs-axis="y" */
  437. if(htmlDataSbPos){o.scrollbarPosition=htmlDataSbPos;} /* usage example: data-mcs-scrollbar-position="outside" */
  438. if(htmlDataTheme){ /* usage example: data-mcs-theme="minimal" */
  439. o.theme=htmlDataTheme;
  440. functions._theme.call(null,o); /* theme-specific options */
  441. }
  442. functions._pluginMarkup.call(this); /* add plugin markup */
  443. methods.update.call(null,$this); /* call the update method */
  444. }
  445. });
  446. },
  447. /* ---------------------------------------- */
  448. /*
  449. plugin update method
  450. updates content and scrollbar(s) values, events and status
  451. ----------------------------------------
  452. usage: $(selector).mCustomScrollbar("update");
  453. */
  454. update:function(el){
  455. var selector=el || functions._selector.call(this); /* validate selector */
  456. return $(selector).each(function(){
  457. var $this=$(this);
  458. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  459. var d=$this.data(pluginPfx),o=d.opt,
  460. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  461. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
  462. if(!mCSB_container.length){return;}
  463. if(d.tweenRunning){functions._stop.call(null,$this);} /* stop any running tweens while updating */
  464. /* if element was disabled or destroyed, remove class(es) */
  465. if($this.hasClass("mCS_disabled")){$this.removeClass("mCS_disabled");}
  466. if($this.hasClass("mCS_destroyed")){$this.removeClass("mCS_destroyed");}
  467. functions._maxHeight.call(this); /* detect/set css max-height value */
  468. functions._expandContentHorizontally.call(this); /* expand content horizontally */
  469. if(o.axis!=="y" && !o.advanced.autoExpandHorizontalScroll){
  470. mCSB_container.css("width",functions._contentWidth(mCSB_container.children()));
  471. }
  472. d.overflowed=functions._overflowed.call(this); /* determine if scrolling is required */
  473. functions._scrollbarVisibility.call(this); /* show/hide scrollbar(s) */
  474. /* auto-adjust scrollbar dragger length analogous to content */
  475. if(o.autoDraggerLength){functions._setDraggerLength.call(this);}
  476. functions._scrollRatio.call(this); /* calculate and store scrollbar to content ratio */
  477. functions._bindEvents.call(this); /* bind scrollbar events */
  478. /* reset scrolling position and/or events */
  479. var to=[Math.abs(mCSB_container[0].offsetTop),Math.abs(mCSB_container[0].offsetLeft)];
  480. if(o.axis!=="x"){ /* y/yx axis */
  481. if(!d.overflowed[0]){ /* y scrolling is not required */
  482. functions._resetContentPosition.call(this); /* reset content position */
  483. if(o.axis==="y"){
  484. functions._unbindEvents.call(this);
  485. }else if(o.axis==="yx" && d.overflowed[1]){
  486. functions._scrollTo.call(this,$this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
  487. }
  488. }else if(mCSB_dragger[0].height()>mCSB_dragger[0].parent().height()){
  489. functions._resetContentPosition.call(this); /* reset content position */
  490. }else{ /* y scrolling is required */
  491. functions._scrollTo.call(this,$this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
  492. d.contentReset.y=null;
  493. }
  494. }
  495. if(o.axis!=="y"){ /* x/yx axis */
  496. if(!d.overflowed[1]){ /* x scrolling is not required */
  497. functions._resetContentPosition.call(this); /* reset content position */
  498. if(o.axis==="x"){
  499. functions._unbindEvents.call(this);
  500. }else if(o.axis==="yx" && d.overflowed[0]){
  501. functions._scrollTo.call(this,$this,to[0].toString(),{dir:"y",dur:0,overwrite:"none"});
  502. }
  503. }else if(mCSB_dragger[1].width()>mCSB_dragger[1].parent().width()){
  504. functions._resetContentPosition.call(this); /* reset content position */
  505. }else{ /* x scrolling is required */
  506. functions._scrollTo.call(this,$this,to[1].toString(),{dir:"x",dur:0,overwrite:"none"});
  507. d.contentReset.x=null;
  508. }
  509. }
  510. functions._autoUpdate.call(this); /* initialize automatic updating (for dynamic content, fluid layouts etc.) */
  511. }
  512. });
  513. },
  514. /* ---------------------------------------- */
  515. /*
  516. plugin scrollTo method
  517. triggers a scrolling event to a specific value
  518. ----------------------------------------
  519. usage: $(selector).mCustomScrollbar("scrollTo",value,options);
  520. */
  521. scrollTo:function(val,options){
  522. /* prevent silly things like $(selector).mCustomScrollbar("scrollTo",undefined); */
  523. if(typeof val=="undefined" || val==null){return;}
  524. var selector=functions._selector.call(this); /* validate selector */
  525. return $(selector).each(function(){
  526. var $this=$(this);
  527. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  528. var d=$this.data(pluginPfx),o=d.opt,
  529. /* method default options */
  530. methodDefaults={
  531. trigger:"external", /* method is by default triggered externally (e.g. from other scripts) */
  532. scrollInertia:o.scrollInertia, /* scrolling inertia (animation duration) */
  533. scrollEasing:"mcsEaseInOut", /* animation easing */
  534. moveDragger:false, /* move dragger instead of content */
  535. timeout:60, /* scroll-to delay */
  536. callbacks:true, /* enable/disable callbacks */
  537. onStart:true,
  538. onUpdate:true,
  539. onComplete:true
  540. },
  541. methodOptions=$.extend(true,{},methodDefaults,options),
  542. to=functions._arr.call(this,val),dur=methodOptions.scrollInertia>0 && methodOptions.scrollInertia<17 ? 17 : methodOptions.scrollInertia;
  543. /* translate yx values to actual scroll-to positions */
  544. to[0]=functions._to.call(this,to[0],"y");
  545. to[1]=functions._to.call(this,to[1],"x");
  546. /*
  547. check if scroll-to value moves the dragger instead of content.
  548. Only pixel values apply on dragger (e.g. 100, "100px", "-=100" etc.)
  549. */
  550. if(methodOptions.moveDragger){
  551. to[0]*=d.scrollRatio.y;
  552. to[1]*=d.scrollRatio.x;
  553. }
  554. methodOptions.dur=dur;
  555. setTimeout(function(){
  556. /* do the scrolling */
  557. if(to[0]!==null && typeof to[0]!=="undefined" && o.axis!=="x" && d.overflowed[0]){ /* scroll y */
  558. methodOptions.dir="y";
  559. methodOptions.overwrite="all";
  560. functions._scrollTo.call(this,$this,to[0].toString(),methodOptions);
  561. }
  562. if(to[1]!==null && typeof to[1]!=="undefined" && o.axis!=="y" && d.overflowed[1]){ /* scroll x */
  563. methodOptions.dir="x";
  564. methodOptions.overwrite="none";
  565. functions._scrollTo.call(this,$this,to[1].toString(),methodOptions);
  566. }
  567. },methodOptions.timeout);
  568. }
  569. });
  570. },
  571. /* ---------------------------------------- */
  572. /*
  573. plugin stop method
  574. stops scrolling animation
  575. ----------------------------------------
  576. usage: $(selector).mCustomScrollbar("stop");
  577. */
  578. stop:function(){
  579. var selector=functions._selector.call(this); /* validate selector */
  580. return $(selector).each(function(){
  581. var $this=$(this);
  582. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  583. functions._stop.call(null,$this);
  584. }
  585. });
  586. },
  587. /* ---------------------------------------- */
  588. /*
  589. plugin disable method
  590. temporarily disables the scrollbar(s)
  591. ----------------------------------------
  592. usage: $(selector).mCustomScrollbar("disable",reset);
  593. reset (boolean): resets content position to 0
  594. */
  595. disable:function(r){
  596. var selector=functions._selector.call(this); /* validate selector */
  597. return $(selector).each(function(){
  598. var $this=$(this);
  599. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  600. var d=$this.data(pluginPfx),o=d.opt;
  601. functions._autoUpdate.call(this,"remove"); /* remove automatic updating */
  602. functions._unbindEvents.call(this); /* unbind events */
  603. if(r){functions._resetContentPosition.call(this);} /* reset content position */
  604. functions._scrollbarVisibility.call(this,true); /* show/hide scrollbar(s) */
  605. $this.addClass("mCS_disabled"); /* add disable class */
  606. }
  607. });
  608. },
  609. /* ---------------------------------------- */
  610. /*
  611. plugin destroy method
  612. completely removes the scrollbar(s) and returns the element to its original state
  613. ----------------------------------------
  614. usage: $(selector).mCustomScrollbar("destroy");
  615. */
  616. destroy:function(){
  617. var selector=functions._selector.call(this); /* validate selector */
  618. return $(selector).each(function(){
  619. var $this=$(this);
  620. if($this.data(pluginPfx)){ /* check if plugin has initialized */
  621. var d=$this.data(pluginPfx),o=d.opt,
  622. mCustomScrollBox=$("#mCSB_"+d.idx),
  623. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  624. scrollbar=$(".mCSB_"+d.idx+"_scrollbar");
  625. if(o.live){removeLiveTimers(selector);} /* remove live timer */
  626. functions._autoUpdate.call(this,"remove"); /* remove automatic updating */
  627. functions._unbindEvents.call(this); /* unbind events */
  628. functions._resetContentPosition.call(this); /* reset content position */
  629. $this.removeData(pluginPfx); /* remove plugin data object */
  630. functions._delete.call(null,this.mcs); /* delete callbacks object */
  631. /* remove plugin markup */
  632. scrollbar.remove(); /* remove scrollbar(s) first (those can be either inside or outside plugin's inner wrapper) */
  633. mCustomScrollBox.replaceWith(mCSB_container.contents()); /* replace plugin's inner wrapper with the original content */
  634. /* remove plugin classes from the element and add destroy class */
  635. $this.removeClass(pluginNS+" _"+pluginPfx+"_"+d.idx+" mCS-autoHide mCS-dir-rtl mCS_no_scrollbar mCS_disabled").addClass("mCS_destroyed");
  636. }
  637. });
  638. }
  639. /* ---------------------------------------- */
  640. },
  641. /*
  642. ----------------------------------------
  643. FUNCTIONS
  644. ----------------------------------------
  645. */
  646. functions={
  647. /* validates selector (if selector is invalid or undefined uses the default one) */
  648. _selector:function(){
  649. return (typeof $(this)!=="object" || $(this).length<1) ? defaultSelector : this;
  650. },
  651. /* -------------------- */
  652. /* changes options according to theme */
  653. _theme:function(obj){
  654. var fixedSizeScrollbarThemes=["rounded","rounded-dark","rounded-dots","rounded-dots-dark"],
  655. nonExpandedScrollbarThemes=["rounded-dots","rounded-dots-dark","3d","3d-dark","3d-thick","3d-thick-dark","inset","inset-dark","inset-2","inset-2-dark","inset-3","inset-3-dark"],
  656. disabledScrollButtonsThemes=["minimal","minimal-dark"],
  657. enabledAutoHideScrollbarThemes=["minimal","minimal-dark"],
  658. scrollbarPositionOutsideThemes=["minimal","minimal-dark"];
  659. obj.autoDraggerLength=$.inArray(obj.theme,fixedSizeScrollbarThemes) > -1 ? false : obj.autoDraggerLength;
  660. obj.autoExpandScrollbar=$.inArray(obj.theme,nonExpandedScrollbarThemes) > -1 ? false : obj.autoExpandScrollbar;
  661. obj.scrollButtons.enable=$.inArray(obj.theme,disabledScrollButtonsThemes) > -1 ? false : obj.scrollButtons.enable;
  662. obj.autoHideScrollbar=$.inArray(obj.theme,enabledAutoHideScrollbarThemes) > -1 ? true : obj.autoHideScrollbar;
  663. obj.scrollbarPosition=$.inArray(obj.theme,scrollbarPositionOutsideThemes) > -1 ? "outside" : obj.scrollbarPosition;
  664. },
  665. /* -------------------- */
  666. /* normalizes axis option to valid values: "y", "x", "yx" */
  667. _findAxis:function(val){
  668. return (val==="yx" || val==="xy" || val==="auto") ? "yx" : (val==="x" || val==="horizontal") ? "x" : "y";
  669. },
  670. /* -------------------- */
  671. /* normalizes scrollButtons.scrollType option to valid values: "stepless", "stepped" */
  672. _findScrollButtonsType:function(val){
  673. return (val==="stepped" || val==="pixels" || val==="step" || val==="click") ? "stepped" : "stepless";
  674. },
  675. /* -------------------- */
  676. /* generates plugin markup */
  677. _pluginMarkup:function(){
  678. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  679. expandClass=o.autoExpandScrollbar ? " mCSB_scrollTools_onDrag_expand" : "",
  680. scrollbar=["<div id='mCSB_"+d.idx+"_scrollbar_vertical' class='mCSB_scrollTools mCSB_"+d.idx+"_scrollbar mCS-"+o.theme+" mCSB_scrollTools_vertical"+expandClass+"'><div class='mCSB_draggerContainer'><div id='mCSB_"+d.idx+"_dragger_vertical' class='mCSB_dragger' style='position:absolute;' oncontextmenu='return false;'><div class='mCSB_dragger_bar' /></div><div class='mCSB_draggerRail' /></div></div>","<div id='mCSB_"+d.idx+"_scrollbar_horizontal' class='mCSB_scrollTools mCSB_"+d.idx+"_scrollbar mCS-"+o.theme+" mCSB_scrollTools_horizontal"+expandClass+"'><div class='mCSB_draggerContainer'><div id='mCSB_"+d.idx+"_dragger_horizontal' class='mCSB_dragger' style='position:absolute;' oncontextmenu='return false;'><div class='mCSB_dragger_bar' /></div><div class='mCSB_draggerRail' /></div></div>"],
  681. wrapperClass=o.axis==="yx" ? "mCSB_vertical_horizontal" : o.axis==="x" ? "mCSB_horizontal" : "mCSB_vertical",
  682. scrollbars=o.axis==="yx" ? scrollbar[0]+scrollbar[1] : o.axis==="x" ? scrollbar[1] : scrollbar[0],
  683. contentWrapper=o.axis==="yx" ? "<div id='mCSB_"+d.idx+"_container_wrapper' class='mCSB_container_wrapper' />" : "",
  684. autoHideClass=o.autoHideScrollbar ? " mCS-autoHide" : "",
  685. scrollbarDirClass=(o.axis!=="x" && d.langDir==="rtl") ? " mCS-dir-rtl" : "";
  686. if(o.setWidth){$this.css("width",o.setWidth);} /* set element width */
  687. if(o.setHeight){$this.css("height",o.setHeight);} /* set element height */
  688. o.setLeft=(o.axis!=="y" && d.langDir==="rtl") ? "989999px" : o.setLeft; /* adjust left position for rtl direction */
  689. $this.addClass(pluginNS+" _"+pluginPfx+"_"+d.idx+autoHideClass+scrollbarDirClass).wrapInner("<div id='mCSB_"+d.idx+"' class='mCustomScrollBox mCS-"+o.theme+" "+wrapperClass+"'><div id='mCSB_"+d.idx+"_container' class='mCSB_container' style='position:relative; top:"+o.setTop+"; left:"+o.setLeft+";' dir="+d.langDir+" /></div>");
  690. var mCustomScrollBox=$("#mCSB_"+d.idx),
  691. mCSB_container=$("#mCSB_"+d.idx+"_container");
  692. if(o.axis!=="y" && !o.advanced.autoExpandHorizontalScroll){
  693. mCSB_container.css("width",functions._contentWidth(mCSB_container.children()));
  694. }
  695. if(o.scrollbarPosition==="outside"){
  696. if($this.css("position")==="static"){ /* requires elements with non-static position */
  697. $this.css("position","relative");
  698. }
  699. $this.css("overflow","visible");
  700. mCustomScrollBox.addClass("mCSB_outside").after(scrollbars);
  701. }else{
  702. mCustomScrollBox.addClass("mCSB_inside").append(scrollbars);
  703. mCSB_container.wrap(contentWrapper);
  704. }
  705. functions._scrollButtons.call(this); /* add scrollbar buttons */
  706. /* minimum dragger length */
  707. var mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
  708. mCSB_dragger[0].css("min-height",mCSB_dragger[0].height());
  709. mCSB_dragger[1].css("min-width",mCSB_dragger[1].width());
  710. },
  711. /* -------------------- */
  712. /* calculates content width */
  713. _contentWidth:function(el){
  714. return Math.max.apply(Math,el.map(function(){return $(this).outerWidth(true);}).get());
  715. },
  716. /* -------------------- */
  717. /* expands content horizontally */
  718. _expandContentHorizontally:function(){
  719. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  720. mCSB_container=$("#mCSB_"+d.idx+"_container");
  721. if(o.advanced.autoExpandHorizontalScroll && o.axis!=="y"){
  722. /*
  723. wrap content with an infinite width div and set its position to absolute and width to auto.
  724. Setting width to auto before calculating the actual width is important!
  725. We must let the browser set the width as browser zoom values are impossible to calculate.
  726. */
  727. mCSB_container.css({"position":"absolute","width":"auto"})
  728. .wrap("<div class='mCSB_h_wrapper' style='position:relative; left:0; width:999999px;' />")
  729. .css({ /* set actual width, original position and un-wrap */
  730. /*
  731. get the exact width (with decimals) and then round-up.
  732. Using jquery outerWidth() will round the width value which will mess up with inner elements that have non-integer width
  733. */
  734. "width":(Math.ceil(mCSB_container[0].getBoundingClientRect().right+0.4)-Math.floor(mCSB_container[0].getBoundingClientRect().left)),
  735. "position":"relative"
  736. }).unwrap();
  737. }
  738. },
  739. /* -------------------- */
  740. /* adds scrollbar buttons */
  741. _scrollButtons:function(){
  742. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  743. mCSB_scrollTools=$(".mCSB_"+d.idx+"_scrollbar:first"),
  744. btnHTML=[
  745. "<a href='#' class='mCSB_buttonUp' oncontextmenu='return false;' />","<a href='#' class='mCSB_buttonDown' oncontextmenu='return false;' />",
  746. "<a href='#' class='mCSB_buttonLeft' oncontextmenu='return false;' />","<a href='#' class='mCSB_buttonRight' oncontextmenu='return false;' />"
  747. ],
  748. btn=[(o.axis==="x" ? btnHTML[2] : btnHTML[0]),(o.axis==="x" ? btnHTML[3] : btnHTML[1]),btnHTML[2],btnHTML[3]];
  749. if(o.scrollButtons.enable){
  750. mCSB_scrollTools.prepend(btn[0]).append(btn[1]).next(".mCSB_scrollTools").prepend(btn[2]).append(btn[3]);
  751. }
  752. },
  753. /* -------------------- */
  754. /* detects/sets css max-height value */
  755. _maxHeight:function(){
  756. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  757. mCustomScrollBox=$("#mCSB_"+d.idx),
  758. mh=$this.css("max-height"),pct=mh.indexOf("%")!==-1,
  759. bs=$this.css("box-sizing");
  760. if(mh!=="none"){
  761. var val=pct ? $this.parent().height()*parseInt(mh)/100 : parseInt(mh);
  762. /* if element's css box-sizing is "border-box", subtract any paddings and/or borders from max-height value */
  763. if(bs==="border-box"){val-=(($this.innerHeight()-$this.height())+($this.outerHeight()-$this.innerHeight()));}
  764. mCustomScrollBox.css("max-height",Math.round(val));
  765. }
  766. },
  767. /* -------------------- */
  768. /* auto-adjusts scrollbar dragger length */
  769. _setDraggerLength:function(){
  770. var $this=$(this),d=$this.data(pluginPfx),
  771. mCustomScrollBox=$("#mCSB_"+d.idx),
  772. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  773. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  774. ratio=[mCustomScrollBox.height()/mCSB_container.outerHeight(false),mCustomScrollBox.width()/mCSB_container.outerWidth(false)],
  775. l=[
  776. parseInt(mCSB_dragger[0].css("min-height")),Math.round(ratio[0]*mCSB_dragger[0].parent().height()),
  777. parseInt(mCSB_dragger[1].css("min-width")),Math.round(ratio[1]*mCSB_dragger[1].parent().width())
  778. ],
  779. h=oldIE && (l[1]<l[0]) ? l[0] : l[1],w=oldIE && (l[3]<l[2]) ? l[2] : l[3];
  780. mCSB_dragger[0].css({
  781. "height":h,"max-height":(mCSB_dragger[0].parent().height()-10)
  782. }).find(".mCSB_dragger_bar").css({"line-height":l[0]+"px"});
  783. mCSB_dragger[1].css({
  784. "width":w,"max-width":(mCSB_dragger[1].parent().width()-10)
  785. });
  786. },
  787. /* -------------------- */
  788. /* calculates scrollbar to content ratio */
  789. _scrollRatio:function(){
  790. var $this=$(this),d=$this.data(pluginPfx),
  791. mCustomScrollBox=$("#mCSB_"+d.idx),
  792. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  793. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  794. scrollAmount=[mCSB_container.outerHeight(false)-mCustomScrollBox.height(),mCSB_container.outerWidth(false)-mCustomScrollBox.width()],
  795. ratio=[
  796. scrollAmount[0]/(mCSB_dragger[0].parent().height()-mCSB_dragger[0].height()),
  797. scrollAmount[1]/(mCSB_dragger[1].parent().width()-mCSB_dragger[1].width())
  798. ];
  799. d.scrollRatio={y:ratio[0],x:ratio[1]};
  800. },
  801. /* -------------------- */
  802. /* toggles scrolling classes */
  803. _onDragClasses:function(el,action,xpnd){
  804. var expandClass=xpnd ? "mCSB_dragger_onDrag_expanded" : "",classes=["mCSB_dragger_onDrag","mCSB_scrollTools_onDrag"],
  805. scrollbar=el.closest(".mCSB_scrollTools");
  806. if(action==="active"){
  807. el.toggleClass(classes[0]+" "+expandClass); scrollbar.toggleClass(classes[1]);
  808. el[0]._draggable=el[0]._draggable ? 0 : 1;
  809. }else{
  810. if(!el[0]._draggable){
  811. if(action==="hide"){
  812. el.removeClass(classes[0]); scrollbar.removeClass(classes[1]);
  813. }else{
  814. el.addClass(classes[0]); scrollbar.addClass(classes[1]);
  815. }
  816. }
  817. }
  818. },
  819. /* -------------------- */
  820. /* checks if content overflows its container to determine if scrolling is required */
  821. _overflowed:function(){
  822. var $this=$(this),d=$this.data(pluginPfx),
  823. mCustomScrollBox=$("#mCSB_"+d.idx),
  824. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  825. contentHeight=d.overflowed==null ? mCSB_container.height() : mCSB_container.outerHeight(false),
  826. contentWidth=d.overflowed==null ? mCSB_container.width() : mCSB_container.outerWidth(false);
  827. return [contentHeight>mCustomScrollBox.height(),contentWidth>mCustomScrollBox.width()];
  828. },
  829. /* -------------------- */
  830. /* resets content position to 0 */
  831. _resetContentPosition:function(){
  832. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  833. mCustomScrollBox=$("#mCSB_"+d.idx),
  834. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  835. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")];
  836. functions._stop($this); /* stop any current scrolling before resetting */
  837. if((o.axis!=="x" && !d.overflowed[0]) || (o.axis==="y" && d.overflowed[0])){ /* reset y */
  838. mCSB_dragger[0].add(mCSB_container).css("top",0);
  839. functions._scrollTo($this,"_resetY");
  840. }
  841. if((o.axis!=="y" && !d.overflowed[1]) || (o.axis==="x" && d.overflowed[1])){ /* reset x */
  842. var cx=dx=0;
  843. if(d.langDir==="rtl"){ /* adjust left position for rtl direction */
  844. cx=mCustomScrollBox.width()-mCSB_container.outerWidth(false);
  845. dx=Math.abs(cx/d.scrollRatio.x);
  846. }
  847. mCSB_container.css("left",cx);
  848. mCSB_dragger[1].css("left",dx);
  849. functions._scrollTo($this,"_resetX");
  850. }
  851. },
  852. /* -------------------- */
  853. /* binds scrollbar events */
  854. _bindEvents:function(){
  855. var $this=$(this),d=$this.data(pluginPfx),o=d.opt;
  856. if(!d.bindEvents){ /* check if events are already bound */
  857. functions._draggable.call(this);
  858. if(o.contentTouchScroll){functions._contentDraggable.call(this);}
  859. if(o.mouseWheel.enable){ /* bind mousewheel fn when plugin is available */
  860. function _mwt(){
  861. mousewheelTimeout=setTimeout(function(){
  862. if(!$.event.special.mousewheel){
  863. _mwt();
  864. }else{
  865. clearTimeout(mousewheelTimeout);
  866. functions._mousewheel.call($this[0]);
  867. }
  868. },1000);
  869. }
  870. var mousewheelTimeout;
  871. _mwt();
  872. }
  873. functions._draggerRail.call(this);
  874. functions._wrapperScroll.call(this);
  875. if(o.advanced.autoScrollOnFocus){functions._focus.call(this);}
  876. if(o.scrollButtons.enable){functions._buttons.call(this);}
  877. if(o.keyboard.enable){functions._keyboard.call(this);}
  878. d.bindEvents=true;
  879. }
  880. },
  881. /* -------------------- */
  882. /* unbinds scrollbar events */
  883. _unbindEvents:function(){
  884. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  885. namespace=pluginPfx+"_"+d.idx,
  886. sb=".mCSB_"+d.idx+"_scrollbar",
  887. sel=$("#mCSB_"+d.idx+",#mCSB_"+d.idx+"_container,#mCSB_"+d.idx+"_container_wrapper,"+sb+" .mCSB_draggerContainer,#mCSB_"+d.idx+"_dragger_vertical,#mCSB_"+d.idx+"_dragger_horizontal,"+sb+">a"),
  888. mCSB_container=$("#mCSB_"+d.idx+"_container");
  889. if(o.advanced.releaseDraggableSelectors){sel.add($(o.advanced.releaseDraggableSelectors));}
  890. if(d.bindEvents){ /* check if events are bound */
  891. /* unbind namespaced events from document/selectors */
  892. $(document).unbind("."+namespace);
  893. sel.each(function(){
  894. $(this).unbind("."+namespace);
  895. });
  896. /* clear and delete timeouts/objects */
  897. clearTimeout($this[0]._focusTimeout); functions._delete.call(null,$this[0]._focusTimeout);
  898. clearTimeout(d.sequential.step); functions._delete.call(null,d.sequential.step);
  899. clearTimeout(mCSB_container[0].onCompleteTimeout); functions._delete.call(null,mCSB_container[0].onCompleteTimeout);
  900. d.bindEvents=false;
  901. }
  902. },
  903. /* -------------------- */
  904. /* toggles scrollbar visibility */
  905. _scrollbarVisibility:function(disabled){
  906. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  907. contentWrapper=$("#mCSB_"+d.idx+"_container_wrapper"),
  908. content=contentWrapper.length ? contentWrapper : $("#mCSB_"+d.idx+"_container"),
  909. scrollbar=[$("#mCSB_"+d.idx+"_scrollbar_vertical"),$("#mCSB_"+d.idx+"_scrollbar_horizontal")],
  910. mCSB_dragger=[scrollbar[0].find(".mCSB_dragger"),scrollbar[1].find(".mCSB_dragger")];
  911. if(o.axis!=="x"){
  912. if(d.overflowed[0] && !disabled){
  913. scrollbar[0].add(mCSB_dragger[0]).add(scrollbar[0].children("a")).css("display","block");
  914. content.removeClass("mCS_no_scrollbar_y mCS_y_hidden");
  915. }else{
  916. if(o.alwaysShowScrollbar){
  917. if(o.alwaysShowScrollbar!==2){mCSB_dragger[0].add(scrollbar[0].children("a")).css("display","none");}
  918. content.removeClass("mCS_y_hidden");
  919. }else{
  920. scrollbar[0].css("display","none");
  921. content.addClass("mCS_y_hidden");
  922. }
  923. content.addClass("mCS_no_scrollbar_y");
  924. }
  925. }
  926. if(o.axis!=="y"){
  927. if(d.overflowed[1] && !disabled){
  928. scrollbar[1].add(mCSB_dragger[1]).add(scrollbar[1].children("a")).css("display","block");
  929. content.removeClass("mCS_no_scrollbar_x mCS_x_hidden");
  930. }else{
  931. if(o.alwaysShowScrollbar){
  932. if(o.alwaysShowScrollbar!==2){mCSB_dragger[1].add(scrollbar[1].children("a")).css("display","none");}
  933. content.removeClass("mCS_x_hidden");
  934. }else{
  935. scrollbar[1].css("display","none");
  936. content.addClass("mCS_x_hidden");
  937. }
  938. content.addClass("mCS_no_scrollbar_x");
  939. }
  940. }
  941. if(!d.overflowed[0] && !d.overflowed[1]){
  942. $this.addClass("mCS_no_scrollbar");
  943. }else{
  944. $this.removeClass("mCS_no_scrollbar");
  945. }
  946. },
  947. /* -------------------- */
  948. /* returns input coordinates of pointer, touch and mouse events (relative to document) */
  949. _coordinates:function(e){
  950. var t=e.type;
  951. switch(t){
  952. case "pointerdown": case "MSPointerDown": case "pointermove": case "MSPointerMove": case "pointerup": case "MSPointerUp":
  953. return [e.originalEvent.pageY,e.originalEvent.pageX,false];
  954. break;
  955. case "touchstart": case "touchmove": case "touchend":
  956. var touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0],
  957. touches=e.originalEvent.touches.length || e.originalEvent.changedTouches.length;
  958. return [touch.pageY,touch.pageX,touches>1];
  959. break;
  960. default:
  961. return [e.pageY,e.pageX,false];
  962. }
  963. },
  964. /* -------------------- */
  965. /*
  966. SCROLLBAR DRAG EVENTS
  967. scrolls content via scrollbar dragging
  968. */
  969. _draggable:function(){
  970. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  971. namespace=pluginPfx+"_"+d.idx,
  972. draggerId=["mCSB_"+d.idx+"_dragger_vertical","mCSB_"+d.idx+"_dragger_horizontal"],
  973. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  974. mCSB_dragger=$("#"+draggerId[0]+",#"+draggerId[1]),
  975. draggable,dragY,dragX,
  976. rds=o.advanced.releaseDraggableSelectors ? mCSB_dragger.add($(o.advanced.releaseDraggableSelectors)) : mCSB_dragger;
  977. mCSB_dragger.bind("mousedown."+namespace+" touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  978. e.stopImmediatePropagation();
  979. e.preventDefault();
  980. if(!functions._mouseBtnLeft(e)){return;} /* left mouse button only */
  981. touchActive=true;
  982. if(oldIE){document.onselectstart=function(){return false;}} /* disable text selection for IE < 9 */
  983. _iframe(false); /* enable scrollbar dragging over iframes by disabling their events */
  984. functions._stop($this);
  985. draggable=$(this);
  986. var offset=draggable.offset(),y=functions._coordinates(e)[0]-offset.top,x=functions._coordinates(e)[1]-offset.left,
  987. h=draggable.height()+offset.top,w=draggable.width()+offset.left;
  988. if(y<h && y>0 && x<w && x>0){
  989. dragY=y;
  990. dragX=x;
  991. }
  992. functions._onDragClasses(draggable,"active",o.autoExpandScrollbar);
  993. }).bind("touchmove."+namespace,function(e){
  994. e.stopImmediatePropagation();
  995. e.preventDefault();
  996. var offset=draggable.offset(),y=functions._coordinates(e)[0]-offset.top,x=functions._coordinates(e)[1]-offset.left;
  997. _drag(dragY,dragX,y,x);
  998. });
  999. $(document).bind("mousemove."+namespace+" pointermove."+namespace+" MSPointerMove."+namespace,function(e){
  1000. if(draggable){
  1001. var offset=draggable.offset(),y=functions._coordinates(e)[0]-offset.top,x=functions._coordinates(e)[1]-offset.left;
  1002. if(dragY===y){return;} /* has it really moved? */
  1003. _drag(dragY,dragX,y,x);
  1004. }
  1005. }).add(rds).bind("mouseup."+namespace+" touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace,function(e){
  1006. if(draggable){
  1007. functions._onDragClasses(draggable,"active",o.autoExpandScrollbar);
  1008. draggable=null;
  1009. }
  1010. touchActive=false;
  1011. if(oldIE){document.onselectstart=null;} /* enable text selection for IE < 9 */
  1012. _iframe(true); /* enable iframes events */
  1013. });
  1014. function _iframe(evt){
  1015. var el=mCSB_container.find("iframe");
  1016. if(!el.length){return;} /* check if content contains iframes */
  1017. var val=!evt ? "none" : "auto";
  1018. el.css("pointer-events",val); /* for IE11, iframe's display property should not be "block" */
  1019. }
  1020. function _drag(dragY,dragX,y,x){
  1021. mCSB_container[0].idleTimer=o.scrollInertia<233 ? 250 : 0;
  1022. if(draggable.attr("id")===draggerId[1]){
  1023. var dir="x",to=((draggable[0].offsetLeft-dragX)+x)*d.scrollRatio.x;
  1024. }else{
  1025. var dir="y",to=((draggable[0].offsetTop-dragY)+y)*d.scrollRatio.y;
  1026. }
  1027. functions._scrollTo($this,to.toString(),{dir:dir,drag:true});
  1028. }
  1029. },
  1030. /* -------------------- */
  1031. /*
  1032. TOUCH SWIPE EVENTS
  1033. scrolls content via touch swipe
  1034. Emulates the native touch-swipe scrolling with momentum found in iOS, Android and WP devices
  1035. */
  1036. _contentDraggable:function(){
  1037. var $this=$(this),d=$this.data(pluginPfx),o=d.opt,
  1038. namespace=pluginPfx+"_"+d.idx,
  1039. mCustomScrollBox=$("#mCSB_"+d.idx),
  1040. mCSB_container=$("#mCSB_"+d.idx+"_container"),
  1041. mCSB_dragger=[$("#mCSB_"+d.idx+"_dragger_vertical"),$("#mCSB_"+d.idx+"_dragger_horizontal")],
  1042. dragY,dragX,touchStartY,touchStartX,touchMoveY=[],touchMoveX=[],startTime,runningTime,endTime,distance,speed,amount,
  1043. durA=0,durB,overwrite=o.axis==="yx" ? "none" : "all",touchIntent=[];
  1044. mCSB_container.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  1045. if(!functions._pointerTouch(e) || touchActive || functions._coordinates(e)[2]){return;}
  1046. var offset=mCSB_container.offset();
  1047. dragY=functions._coordinates(e)[0]-offset.top;
  1048. dragX=functions._coordinates(e)[1]-offset.left;
  1049. touchIntent=[functions._coordinates(e)[0],functions._coordinates(e)[1]];
  1050. }).bind("touchmove."+namespace+" pointermove."+namespace+" MSPointerMove."+namespace,function(e){
  1051. if(!functions._pointerTouch(e) || touchActive || functions._coordinates(e)[2]){return;}
  1052. e.stopImmediatePropagation();
  1053. runningTime=functions._getTime();
  1054. var offset=mCustomScrollBox.offset(),y=functions._coordinates(e)[0]-offset.top,x=functions._coordinates(e)[1]-offset.left,
  1055. easing="mcsLinearOut";
  1056. touchMoveY.push(y);
  1057. touchMoveX.push(x);
  1058. touchIntent[2]=Math.abs(functions._coordinates(e)[0]-touchIntent[0]); touchIntent[3]=Math.abs(functions._coordinates(e)[1]-touchIntent[1]);
  1059. if(d.overflowed[0]){
  1060. var limit=mCSB_dragger[0].parent().height()-mCSB_dragger[0].height(),
  1061. prevent=((dragY-y)>0 && (y-dragY)>-(limit*d.scrollRatio.y) && (touchIntent[3]*2<touchIntent[2] || o.axis==="yx"));
  1062. }
  1063. if(d.overflowed[1]){
  1064. var limitX=mCSB_dragger[1].parent().width()-mCSB_dragger[1].width(),
  1065. preventX=((dragX-x)>0 && (x-dragX)>-(limitX*d.scrollRatio.x) && (touchIntent[2]*2<touchIntent[3] || o.axis==="yx"));
  1066. }
  1067. if(prevent || preventX){e.preventDefault();} /* prevent native document scrolling */
  1068. amount=o.axis==="yx" ? [(dragY-y),(dragX-x)] : o.axis==="x" ? [null,(dragX-x)] : [(dragY-y),null];
  1069. mCSB_container[0].idleTimer=250;
  1070. if(d.overflowed[0]){_drag(amount[0],durA,easing,"y","all",true);}
  1071. if(d.overflowed[1]){_drag(amount[1],durA,easing,"x",overwrite,true);}
  1072. });
  1073. mCustomScrollBox.bind("touchstart."+namespace+" pointerdown."+namespace+" MSPointerDown."+namespace,function(e){
  1074. if(!functions._pointerTouch(e) || touchActive || functions._coordinates(e)[2]){return;}
  1075. e.stopImmediatePropagation();
  1076. functions._stop($this);
  1077. startTime=functions._getTime();
  1078. var offset=mCustomScrollBox.offset();
  1079. touchStartY=functions._coordinates(e)[0]-offset.top;
  1080. touchStartX=functions._coordinates(e)[1]-offset.left;
  1081. touchMoveY=[]; touchMoveX=[];
  1082. }).bind("touchend."+namespace+" pointerup."+namespace+" MSPointerUp."+namespace,function(e){
  1083. if(!functions._pointerTouch(e) || touchActive || functions._coordinates(e)[2]){return;}
  1084. e.stopImmediatePropagation();
  1085. endTime=functions._getTime();
  1086. var offset=mCustomScrollBox.offset(),y=functions._coordinates(e)[0]-offset.top,x=functions._coordinates(e)[1]-offset.left;
  1087. if((endTime-runningTime)>30){return;}
  1088. speed=1000/(endTime-startTime);
  1089. var easing="mcsEaseOut",slow=speed<2.5,
  1090. diff=slow ? [touchMoveY[touchMoveY.length-2],touchMoveX[touchMoveX.length-2]] : [0,0];
  1091. distance=slow ? [(y-diff[0]),(x-diff[1])] : [y-touchStartY,x-touchStartX];
  1092. var absDistance=[Math.abs(distance[0]),Math.abs(distance[1])];
  1093. speed=slow ? [Math.abs(distance[0]/4),Math.abs(distance[1]/4)] : [speed,speed];
  1094. var a=[
  1095. Math.abs(mCSB_container[0].offsetTop)-(distance[0]*_m((absDistance[0]/speed[0]),speed[0])),
  1096. Math.abs(mCSB_container[0].offsetLeft)-(distance[1]*_m((absDistance[1]/speed[1]),speed[1]))
  1097. ];
  1098. amount=o.axis==="yx" ? [a[0],a[1]] : o.axis==="x" ? [null