scrollable.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. define("dojox/mobile/scrollable", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/connect",
  4. "dojo/_base/event",
  5. "dojo/_base/lang",
  6. "dojo/_base/window",
  7. "dojo/dom-class",
  8. "dojo/dom-construct",
  9. "dojo/dom-style",
  10. "./sniff"
  11. ], function(dojo, connect, event, lang, win, domClass, domConstruct, domStyle, has){
  12. var dm = lang.getObject("dojox.mobile", true);
  13. /*=====
  14. // summary:
  15. // Utility for enabling touch scrolling capability.
  16. // description:
  17. // Mobile WebKit browsers do not allow scrolling inner DIVs. (You need
  18. // the two-finger operation to scroll them.)
  19. // That means you cannot have fixed-positioned header/footer bars.
  20. // To solve this issue, this module disables the browsers default scrolling
  21. // behavior, and re-builds its own scrolling machinery by handling touch
  22. // events. In this module, this.domNode has height "100%" and is fixed to
  23. // the window, and this.containerNode scrolls. If you place a bar outside
  24. // of this.containerNode, then it will be fixed-positioned while
  25. // this.containerNode is scrollable.
  26. //
  27. // This module has the following features:
  28. // - Scrolls inner DIVs vertically, horizontally, or both.
  29. // - Vertical and horizontal scroll bars.
  30. // - Flashes the scroll bars when a view is shown.
  31. // - Simulates the flick operation using animation.
  32. // - Respects header/footer bars if any.
  33. //
  34. // dojox.mobile.scrollable is a simple function object, which holds
  35. // several properties and functions in it. But if you transform it to a
  36. // dojo class, it can be used as a mix-in class for any custom dojo
  37. // widgets. dojox.mobile._ScrollableMixin is such a class.
  38. //
  39. // Also, it can be used even for non-dojo applications. In such cases,
  40. // several dojo APIs used in this module, such as dojo.connect,
  41. // dojo.create, etc., are re-defined so that the code works without dojo.
  42. // When in dojo, of course those re-defined functions are not necessary.
  43. // So, they are surrounded by the includeStart and includeEnd directives
  44. // so that they can be excluded from the build.
  45. //
  46. // If you use this module for non-dojo application, you need to explicitly
  47. // assign your outer fixed node and inner scrollable node to this.domNode
  48. // and this.containerNode respectively.
  49. //
  50. // Non-dojo application should capture the onorientationchange or
  51. // the onresize event and call resize() in the event handler.
  52. //
  53. // example:
  54. // Use this module from a non-dojo applicatoin:
  55. // | function onLoad(){
  56. // | var scrollable = new dojox.mobile.scrollable(dojo, dojox);
  57. // | scrollable.init({
  58. // | domNode: "outer", // id or node
  59. // | containerNode: "inner" // id or node
  60. // | });
  61. // | }
  62. // | <body onload="onLoad()">
  63. // | <h1 id="hd1" style="position:relative;width:100%;z-index:1;">
  64. // | Fixed Header
  65. // | </h1>
  66. // | <div id="outer" style="position:relative;height:100%;overflow:hidden;">
  67. // | <div id="inner" style="position:absolute;width:100%;">
  68. // | ... content ...
  69. // | </div>
  70. // | </div>
  71. // | </body>
  72. =====*/
  73. var scrollable = function(/*Object?*/dojo, /*Object?*/dojox){
  74. this.fixedHeaderHeight = 0; // height of a fixed header
  75. this.fixedFooterHeight = 0; // height of a fixed footer
  76. this.isLocalFooter = false; // footer is view-local (as opposed to application-wide)
  77. this.scrollBar = true; // show scroll bar or not
  78. this.scrollDir = "v"; // v: vertical, h: horizontal, vh: both, f: flip
  79. this.weight = 0.6; // frictional drag
  80. this.fadeScrollBar = true;
  81. this.disableFlashScrollBar = false;
  82. this.threshold = 4; // drag threshold value in pixels
  83. this.constraint = true; // bounce back to the content area
  84. this.touchNode = null; // a node that will have touch event handlers
  85. this.isNested = false; // this scrollable's parent is also a scrollable
  86. this.dirLock = false; // disable the move handler if scroll starts in the unexpected direction
  87. this.height = ""; // explicitly specified height of this widget (ex. "300px")
  88. this.androidWorkaroud = true; // workaround input field jumping issue
  89. this.init = function(/*Object?*/params){
  90. if(params){
  91. for(var p in params){
  92. if(params.hasOwnProperty(p)){
  93. this[p] = ((p == "domNode" || p == "containerNode") && typeof params[p] == "string") ?
  94. win.doc.getElementById(params[p]) : params[p]; // mix-in params
  95. }
  96. }
  97. }
  98. this.touchNode = this.touchNode || this.containerNode;
  99. this._v = (this.scrollDir.indexOf("v") != -1); // vertical scrolling
  100. this._h = (this.scrollDir.indexOf("h") != -1); // horizontal scrolling
  101. this._f = (this.scrollDir == "f"); // flipping views
  102. this._ch = []; // connect handlers
  103. this._ch.push(connect.connect(this.touchNode,
  104. has("touch") ? "touchstart" : "onmousedown", this, "onTouchStart"));
  105. if(has("webkit")){
  106. this._ch.push(connect.connect(this.domNode, "webkitAnimationEnd", this, "onFlickAnimationEnd"));
  107. this._ch.push(connect.connect(this.domNode, "webkitAnimationStart", this, "onFlickAnimationStart"));
  108. this._aw = this.androidWorkaroud &&
  109. has("android") >= 2.2 && has("android") < 3;
  110. if(this._aw){
  111. this._ch.push(connect.connect(win.global, "onresize", this, "onScreenSizeChanged"));
  112. this._ch.push(connect.connect(win.global, "onfocus", this, function(e){
  113. if(this.containerNode.style.webkitTransform){
  114. this.stopAnimation();
  115. this.toTopLeft();
  116. }
  117. }));
  118. this._sz = this.getScreenSize();
  119. }
  120. // Creation of keyframes takes a little time. If they are created
  121. // in a lazy manner, a slight delay is noticeable when you start
  122. // scrolling for the first time. This is to create keyframes up front.
  123. for(var i = 0; i < 3; i++){
  124. this.setKeyframes(null, null, i);
  125. }
  126. }
  127. // Workaround for iPhone flicker issue
  128. if(has("iphone")){
  129. domStyle.set(this.containerNode, "webkitTransform", "translate3d(0,0,0)");
  130. }
  131. this._speed = {x:0, y:0};
  132. this._appFooterHeight = 0;
  133. if(this.isTopLevel() && !this.noResize){
  134. this.resize();
  135. }
  136. var _this = this;
  137. setTimeout(function(){
  138. _this.flashScrollBar();
  139. }, 600);
  140. };
  141. this.isTopLevel = function(){
  142. // subclass may want to override
  143. return true;
  144. };
  145. this.cleanup = function(){
  146. if(this._ch){
  147. for(var i = 0; i < this._ch.length; i++){
  148. connect.disconnect(this._ch[i]);
  149. }
  150. this._ch = null;
  151. }
  152. };
  153. this.findDisp = function(/*DomNode*/node){
  154. // summary:
  155. // Finds the currently displayed view node from my sibling nodes.
  156. if(!node.parentNode){ return null; }
  157. var nodes = node.parentNode.childNodes;
  158. for(var i = 0; i < nodes.length; i++){
  159. var n = nodes[i];
  160. if(n.nodeType === 1 && domClass.contains(n, "mblView") && n.style.display !== "none"){
  161. return n;
  162. }
  163. }
  164. return node;
  165. };
  166. this.getScreenSize = function(){
  167. // summary:
  168. // Returns the dimensions of the browser window.
  169. return {
  170. h: win.global.innerHeight||win.doc.documentElement.clientHeight||win.doc.documentElement.offsetHeight,
  171. w: win.global.innerWidth||win.doc.documentElement.clientWidth||win.doc.documentElement.offsetWidth
  172. };
  173. };
  174. this.isKeyboardShown = function(e){
  175. // summary:
  176. // Internal function for android workaround.
  177. // description:
  178. // Returns true if a virtual keyboard is shown.
  179. // Indirectly detects whether a virtual keyboard is shown or not by
  180. // examining the screen size.
  181. // TODO: need more reliable detection logic
  182. if(!this._sz){ return false; }
  183. var sz = this.getScreenSize();
  184. return (sz.w * sz.h) / (this._sz.w * this._sz.h) < 0.8;
  185. };
  186. this.disableScroll = function(/*Boolean*/v){
  187. // summary:
  188. // Internal function for android workaround.
  189. // description:
  190. // Disables the touch scrolling and enables the browser's default
  191. // scrolling.
  192. if(this.disableTouchScroll === v || this.domNode.style.display === "none"){ return; }
  193. this.disableTouchScroll = v;
  194. this.scrollBar = !v;
  195. dm.disableHideAddressBar = dm.disableResizeAll = v;
  196. var of = v ? "visible" : "hidden";
  197. domStyle.set(this.domNode, "overflow", of);
  198. domStyle.set(win.doc.documentElement, "overflow", of);
  199. domStyle.set(win.body(), "overflow", of);
  200. var c = this.containerNode;
  201. if(v){
  202. if(!c.style.webkitTransform){
  203. // stop animation when soft keyborad is shown before animation ends.
  204. // TODO: there might be a better way to wait for animation ending.
  205. this.stopAnimation();
  206. this.toTopLeft();
  207. }
  208. var mt = parseInt(c.style.marginTop) || 0;
  209. var h = c.offsetHeight + mt + this.fixedFooterHeight - this._appFooterHeight;
  210. domStyle.set(this.domNode, "height", h + "px");
  211. this._cPos = { // store containerNode's position
  212. x: parseInt(c.style.left) || 0,
  213. y: parseInt(c.style.top) || 0
  214. };
  215. domStyle.set(c, {
  216. top: "0px",
  217. left: "0px"
  218. });
  219. var a = win.doc.activeElement; // focused input field
  220. if(a){ // scrolling to show focused input field
  221. var at = 0; // top position of focused input field
  222. for(var n = a; n.tagName != "BODY"; n = n.offsetParent){
  223. at += n.offsetTop;
  224. }
  225. var st = at + a.clientHeight + 10 - this.getScreenSize().h; // top postion of browser scroll bar
  226. if(st > 0){
  227. win.body().scrollTop = st;
  228. }
  229. }
  230. }else{
  231. if(this._cPos){ // restore containerNode's position
  232. domStyle.set(c, {
  233. top: this._cPos.y + "px",
  234. left: this._cPos.x + "px"
  235. });
  236. this._cPos = null;
  237. }
  238. var tags = this.domNode.getElementsByTagName("*");
  239. for(var i = 0; i < tags.length; i++){
  240. tags[i].blur && tags[i].blur();
  241. }
  242. // Call dojox.mobile.resizeAll if exists.
  243. dm.resizeAll && dm.resizeAll();
  244. }
  245. };
  246. this.onScreenSizeChanged = function(e){
  247. // summary:
  248. // Internal function for android workaround.
  249. var sz = this.getScreenSize();
  250. if(sz.w * sz.h > this._sz.w * this._sz.h){
  251. this._sz = sz; // update the screen size
  252. }
  253. this.disableScroll(this.isKeyboardShown());
  254. };
  255. this.toTransform = function(e){
  256. // summary:
  257. // Internal function for android workaround.
  258. var c = this.containerNode;
  259. if(c.offsetTop === 0 && c.offsetLeft === 0 || !c._webkitTransform){ return; }
  260. domStyle.set(c, {
  261. webkitTransform: c._webkitTransform,
  262. top: "0px",
  263. left: "0px"
  264. });
  265. c._webkitTransform = null;
  266. };
  267. this.toTopLeft = function(){
  268. // summary:
  269. // Internal function for android workaround.
  270. var c = this.containerNode;
  271. if(!c.style.webkitTransform){ return; } // already converted to top/left
  272. c._webkitTransform = c.style.webkitTransform;
  273. var pos = this.getPos();
  274. domStyle.set(c, {
  275. webkitTransform: "",
  276. top: pos.y + "px",
  277. left: pos.x + "px"
  278. });
  279. };
  280. this.resize = function(e){
  281. // summary:
  282. // Adjusts the height of the widget.
  283. // description:
  284. // If the height property is 'inherit', the height is inherited
  285. // from its offset parent. If 'auto', the content height, which
  286. // could be smaller than the entire screen height, is used. If an
  287. // explicit height value (ex. "300px"), it is used as the new
  288. // height. If nothing is specified as the height property, from the
  289. // current top position of the widget to the bottom of the screen
  290. // will be the new height.
  291. // moved from init() to support dynamically added fixed bars
  292. this._appFooterHeight = (this.fixedFooterHeight && !this.isLocalFooter) ?
  293. this.fixedFooterHeight : 0;
  294. if(this.isLocalHeader){
  295. this.containerNode.style.marginTop = this.fixedHeaderHeight + "px";
  296. }
  297. // Get the top position. Same as dojo.position(node, true).y
  298. var top = 0;
  299. for(var n = this.domNode; n && n.tagName != "BODY"; n = n.offsetParent){
  300. n = this.findDisp(n); // find the first displayed view node
  301. if(!n){ break; }
  302. top += n.offsetTop;
  303. }
  304. // adjust the height of this view
  305. var h,
  306. screenHeight = this.getScreenSize().h,
  307. dh = screenHeight - top - this._appFooterHeight; // default height
  308. if(this.height === "inherit"){
  309. if(this.domNode.offsetParent){
  310. h = this.domNode.offsetParent.offsetHeight + "px";
  311. }
  312. }else if(this.height === "auto"){
  313. var parent = this.domNode.offsetParent;
  314. if(parent){
  315. this.domNode.style.height = "0px";
  316. var parentRect = parent.getBoundingClientRect(),
  317. scrollableRect = this.domNode.getBoundingClientRect(),
  318. contentBottom = parentRect.bottom - this._appFooterHeight;
  319. if(scrollableRect.bottom >= contentBottom){ // use entire screen
  320. dh = screenHeight - (scrollableRect.top - parentRect.top) - this._appFooterHeight;
  321. }else{ // stretch to fill predefined area
  322. dh = contentBottom - scrollableRect.bottom;
  323. }
  324. }
  325. // content could be smaller than entire screen height
  326. var contentHeight = Math.max(this.domNode.scrollHeight, this.containerNode.scrollHeight);
  327. h = (contentHeight ? Math.min(contentHeight, dh) : dh) + "px";
  328. }else if(this.height){
  329. h = this.height;
  330. }
  331. if(!h){
  332. h = dh + "px";
  333. }
  334. if(h.charAt(0) !== "-" && // to ensure that h is not negative (e.g. "-10px")
  335. h !== "default"){
  336. this.domNode.style.height = h;
  337. }
  338. // to ensure that the view is within a scrolling area when resized.
  339. this.onTouchEnd();
  340. };
  341. this.onFlickAnimationStart = function(e){
  342. event.stop(e);
  343. };
  344. this.onFlickAnimationEnd = function(e){
  345. var an = e && e.animationName;
  346. if(an && an.indexOf("scrollableViewScroll2") === -1){
  347. if(an.indexOf("scrollableViewScroll0") !== -1){ // scrollBarV
  348. domClass.remove(this._scrollBarNodeV, "mblScrollableScrollTo0");
  349. }else if(an.indexOf("scrollableViewScroll1") !== -1){ // scrollBarH
  350. domClass.remove(this._scrollBarNodeH, "mblScrollableScrollTo1");
  351. }else{ // fade or others
  352. if(this._scrollBarNodeV){ this._scrollBarNodeV.className = ""; }
  353. if(this._scrollBarNodeH){ this._scrollBarNodeH.className = ""; }
  354. }
  355. return;
  356. }
  357. if(e && e.srcElement){
  358. event.stop(e);
  359. }
  360. this.stopAnimation();
  361. if(this._bounce){
  362. var _this = this;
  363. var bounce = _this._bounce;
  364. setTimeout(function(){
  365. _this.slideTo(bounce, 0.3, "ease-out");
  366. }, 0);
  367. _this._bounce = undefined;
  368. }else{
  369. this.hideScrollBar();
  370. this.removeCover();
  371. if(this._aw){ this.toTopLeft(); } // android workaround
  372. }
  373. };
  374. this.isFormElement = function(node){
  375. if(node && node.nodeType !== 1){ node = node.parentNode; }
  376. if(!node || node.nodeType !== 1){ return false; }
  377. var t = node.tagName;
  378. return (t === "SELECT" || t === "INPUT" || t === "TEXTAREA" || t === "BUTTON");
  379. };
  380. this.onTouchStart = function(e){
  381. if(this.disableTouchScroll){ return; }
  382. if(this._conn && (new Date()).getTime() - this.startTime < 500){
  383. return; // ignore successive onTouchStart calls
  384. }
  385. if(!this._conn){
  386. this._conn = [];
  387. this._conn.push(connect.connect(win.doc, has("touch") ? "touchmove" : "onmousemove", this, "onTouchMove"));
  388. this._conn.push(connect.connect(win.doc, has("touch") ? "touchend" : "onmouseup", this, "onTouchEnd"));
  389. }
  390. this._aborted = false;
  391. if(domClass.contains(this.containerNode, "mblScrollableScrollTo2")){
  392. this.abort();
  393. }else{ // reset scrollbar class especially for reseting fade-out animation
  394. if(this._scrollBarNodeV){ this._scrollBarNodeV.className = ""; }
  395. if(this._scrollBarNodeH){ this._scrollBarNodeH.className = ""; }
  396. }
  397. if(this._aw){ this.toTransform(e); } // android workaround
  398. this.touchStartX = e.touches ? e.touches[0].pageX : e.clientX;
  399. this.touchStartY = e.touches ? e.touches[0].pageY : e.clientY;
  400. this.startTime = (new Date()).getTime();
  401. this.startPos = this.getPos();
  402. this._dim = this.getDim();
  403. this._time = [0];
  404. this._posX = [this.touchStartX];
  405. this._posY = [this.touchStartY];
  406. this._locked = false;
  407. if(!this.isFormElement(e.target) && !this.isNested){
  408. event.stop(e);
  409. }
  410. };
  411. this.onTouchMove = function(e){
  412. if(this._locked){ return; }
  413. var x = e.touches ? e.touches[0].pageX : e.clientX;
  414. var y = e.touches ? e.touches[0].pageY : e.clientY;
  415. var dx = x - this.touchStartX;
  416. var dy = y - this.touchStartY;
  417. var to = {x:this.startPos.x + dx, y:this.startPos.y + dy};
  418. var dim = this._dim;
  419. dx = Math.abs(dx);
  420. dy = Math.abs(dy);
  421. if(this._time.length == 1){ // the first TouchMove after TouchStart
  422. if(this.dirLock){
  423. if(this._v && !this._h && dx >= this.threshold && dx >= dy ||
  424. (this._h || this._f) && !this._v && dy >= this.threshold && dy >= dx){
  425. this._locked = true;
  426. return;
  427. }
  428. }
  429. if(this._v && Math.abs(dy) < this.threshold ||
  430. (this._h || this._f) && Math.abs(dx) < this.threshold){
  431. return;
  432. }
  433. this.addCover();
  434. this.showScrollBar();
  435. }
  436. var weight = this.weight;
  437. if(this._v && this.constraint){
  438. if(to.y > 0){ // content is below the screen area
  439. to.y = Math.round(to.y * weight);
  440. }else if(to.y < -dim.o.h){ // content is above the screen area
  441. if(dim.c.h < dim.d.h){ // content is shorter than display
  442. to.y = Math.round(to.y * weight);
  443. }else{
  444. to.y = -dim.o.h - Math.round((-dim.o.h - to.y) * weight);
  445. }
  446. }
  447. }
  448. if((this._h || this._f) && this.constraint){
  449. if(to.x > 0){
  450. to.x = Math.round(to.x * weight);
  451. }else if(to.x < -dim.o.w){
  452. if(dim.c.w < dim.d.w){
  453. to.x = Math.round(to.x * weight);
  454. }else{
  455. to.x = -dim.o.w - Math.round((-dim.o.w - to.x) * weight);
  456. }
  457. }
  458. }
  459. this.scrollTo(to);
  460. var max = 10;
  461. var n = this._time.length; // # of samples
  462. if(n >= 2){
  463. // Check the direction of the finger move.
  464. // If the direction has been changed, discard the old data.
  465. var d0, d1;
  466. if(this._v && !this._h){
  467. d0 = this._posY[n - 1] - this._posY[n - 2];
  468. d1 = y - this._posY[n - 1];
  469. }else if(!this._v && this._h){
  470. d0 = this._posX[n - 1] - this._posX[n - 2];
  471. d1 = x - this._posX[n - 1];
  472. }
  473. if(d0 * d1 < 0){ // direction changed
  474. // leave only the latest data
  475. this._time = [this._time[n - 1]];
  476. this._posX = [this._posX[n - 1]];
  477. this._posY = [this._posY[n - 1]];
  478. n = 1;
  479. }
  480. }
  481. if(n == max){
  482. this._time.shift();
  483. this._posX.shift();
  484. this._posY.shift();
  485. }
  486. this._time.push((new Date()).getTime() - this.startTime);
  487. this._posX.push(x);
  488. this._posY.push(y);
  489. };
  490. this.onTouchEnd = function(e){
  491. if(this._locked){ return; }
  492. var speed = this._speed = {x:0, y:0};
  493. var dim = this._dim;
  494. var pos = this.getPos();
  495. var to = {}; // destination
  496. if(e){
  497. if(!this._conn){ return; } // if we get onTouchEnd without onTouchStart, ignore it.
  498. for(var i = 0; i < this._conn.length; i++){
  499. connect.disconnect(this._conn[i]);
  500. }
  501. this._conn = null;
  502. var n = this._time.length; // # of samples
  503. var clicked = false;
  504. if(!this._aborted){
  505. if(n <= 1){
  506. clicked = true;
  507. }else if(n == 2 && Math.abs(this._posY[1] - this._posY[0]) < 4
  508. && has("touch")){ // for desktop browsers, posY could be the same, since we're using clientY, see onTouchMove()
  509. clicked = true;
  510. }
  511. }
  512. var isFormElem = this.isFormElement(e.target);
  513. if(clicked && !isFormElem){ // clicked, not dragged or flicked
  514. this.hideScrollBar();
  515. this.removeCover();
  516. if(has("touch")){
  517. var elem = e.target;
  518. if(elem.nodeType != 1){
  519. elem = elem.parentNode;
  520. }
  521. var ev = win.doc.createEvent("MouseEvents");
  522. ev.initMouseEvent("click", true, true, win.global, 1, e.screenX, e.screenY, e.clientX, e.clientY);
  523. setTimeout(function(){
  524. elem.dispatchEvent(ev);
  525. }, 0);
  526. }
  527. return;
  528. }else if(this._aw && clicked && isFormElem){ // clicked input fields
  529. this.hideScrollBar();
  530. this.toTopLeft();
  531. return;
  532. }
  533. speed = this._speed = this.getSpeed();
  534. }else{
  535. if(pos.x == 0 && pos.y == 0){ return; } // initializing
  536. dim = this.getDim();
  537. }
  538. if(this._v){
  539. to.y = pos.y + speed.y;
  540. }
  541. if(this._h || this._f){
  542. to.x = pos.x + speed.x;
  543. }
  544. this.adjustDestination(to, pos);
  545. if(this.scrollDir == "v" && dim.c.h < dim.d.h){ // content is shorter than display
  546. this.slideTo({y:0}, 0.3, "ease-out"); // go back to the top
  547. return;
  548. }else if(this.scrollDir == "h" && dim.c.w < dim.d.w){ // content is narrower than display
  549. this.slideTo({x:0}, 0.3, "ease-out"); // go back to the left
  550. return;
  551. }else if(this._v && this._h && dim.c.h < dim.d.h && dim.c.w < dim.d.w){
  552. this.slideTo({x:0, y:0}, 0.3, "ease-out"); // go back to the top-left
  553. return;
  554. }
  555. var duration, easing = "ease-out";
  556. var bounce = {};
  557. if(this._v && this.constraint){
  558. if(to.y > 0){ // going down. bounce back to the top.
  559. if(pos.y > 0){ // started from below the screen area. return quickly.
  560. duration = 0.3;
  561. to.y = 0;
  562. }else{
  563. to.y = Math.min(to.y, 20);
  564. easing = "linear";
  565. bounce.y = 0;
  566. }
  567. }else if(-speed.y > dim.o.h - (-pos.y)){ // going up. bounce back to the bottom.
  568. if(pos.y < -dim.o.h){ // started from above the screen top. return quickly.
  569. duration = 0.3;
  570. to.y = dim.c.h <= dim.d.h ? 0 : -dim.o.h; // if shorter, move to 0
  571. }else{
  572. to.y = Math.max(to.y, -dim.o.h - 20);
  573. easing = "linear";
  574. bounce.y = -dim.o.h;
  575. }
  576. }
  577. }
  578. if((this._h || this._f) && this.constraint){
  579. if(to.x > 0){ // going right. bounce back to the left.
  580. if(pos.x > 0){ // started from right of the screen area. return quickly.
  581. duration = 0.3;
  582. to.x = 0;
  583. }else{
  584. to.x = Math.min(to.x, 20);
  585. easing = "linear";
  586. bounce.x = 0;
  587. }
  588. }else if(-speed.x > dim.o.w - (-pos.x)){ // going left. bounce back to the right.
  589. if(pos.x < -dim.o.w){ // started from left of the screen top. return quickly.
  590. duration = 0.3;
  591. to.x = dim.c.w <= dim.d.w ? 0 : -dim.o.w; // if narrower, move to 0
  592. }else{
  593. to.x = Math.max(to.x, -dim.o.w - 20);
  594. easing = "linear";
  595. bounce.x = -dim.o.w;
  596. }
  597. }
  598. }
  599. this._bounce = (bounce.x !== undefined || bounce.y !== undefined) ? bounce : undefined;
  600. if(duration === undefined){
  601. var distance, velocity;
  602. if(this._v && this._h){
  603. velocity = Math.sqrt(speed.x+speed.x + speed.y*speed.y);
  604. distance = Math.sqrt(Math.pow(to.y - pos.y, 2) + Math.pow(to.x - pos.x, 2));
  605. }else if(this._v){
  606. velocity = speed.y;
  607. distance = to.y - pos.y;
  608. }else if(this._h){
  609. velocity = speed.x;
  610. distance = to.x - pos.x;
  611. }
  612. if(distance === 0 && !e){ return; } // #13154
  613. duration = velocity !== 0 ? Math.abs(distance / velocity) : 0.01; // time = distance / velocity
  614. }
  615. this.slideTo(to, duration, easing);
  616. };
  617. this.adjustDestination = function(to, pos){
  618. // subclass may want to implement
  619. };
  620. this.abort = function(){
  621. this.scrollTo(this.getPos());
  622. this.stopAnimation();
  623. this._aborted = true;
  624. };
  625. this.stopAnimation = function(){
  626. // stop the currently running animation
  627. domClass.remove(this.containerNode, "mblScrollableScrollTo2");
  628. if(has("android")){
  629. domStyle.set(this.containerNode, "webkitAnimationDuration", "0s"); // workaround for android screen flicker problem
  630. }
  631. if(this._scrollBarV){
  632. this._scrollBarV.className = "";
  633. }
  634. if(this._scrollBarH){
  635. this._scrollBarH.className = "";
  636. }
  637. };
  638. this.getSpeed = function(){
  639. var x = 0, y = 0, n = this._time.length;
  640. // if the user holds the mouse or finger more than 0.5 sec, do not move.
  641. if(n >= 2 && (new Date()).getTime() - this.startTime - this._time[n - 1] < 500){
  642. var dy = this._posY[n - (n > 3 ? 2 : 1)] - this._posY[(n - 6) >= 0 ? n - 6 : 0];
  643. var dx = this._posX[n - (n > 3 ? 2 : 1)] - this._posX[(n - 6) >= 0 ? n - 6 : 0];
  644. var dt = this._time[n - (n > 3 ? 2 : 1)] - this._time[(n - 6) >= 0 ? n - 6 : 0];
  645. y = this.calcSpeed(dy, dt);
  646. x = this.calcSpeed(dx, dt);
  647. }
  648. return {x:x, y:y};
  649. };
  650. this.calcSpeed = function(/*Number*/d, /*Number*/t){
  651. return Math.round(d / t * 100) * 4;
  652. };
  653. this.scrollTo = function(/*Object*/to, /*Boolean?*/doNotMoveScrollBar, /*DomNode?*/node){ // to: {x, y}
  654. // summary:
  655. // Scrolls to the given position.
  656. var s = (node || this.containerNode).style;
  657. if(has("webkit")){
  658. s.webkitTransform = this.makeTranslateStr(to);
  659. }else{
  660. if(this._v){
  661. s.top = to.y + "px";
  662. }
  663. if(this._h || this._f){
  664. s.left = to.x + "px";
  665. }
  666. }
  667. if(!doNotMoveScrollBar){
  668. this.scrollScrollBarTo(this.calcScrollBarPos(to));
  669. }
  670. };
  671. this.slideTo = function(/*Object*/to, /*Number*/duration, /*String*/easing){
  672. // summary:
  673. // Scrolls to the given position with slide animation.
  674. this._runSlideAnimation(this.getPos(), to, duration, easing, this.containerNode, 2);
  675. this.slideScrollBarTo(to, duration, easing);
  676. };
  677. this.makeTranslateStr = function(to){
  678. var y = this._v && typeof to.y == "number" ? to.y+"px" : "0px";
  679. var x = (this._h||this._f) && typeof to.x == "number" ? to.x+"px" : "0px";
  680. return dm.hasTranslate3d ?
  681. "translate3d("+x+","+y+",0px)" : "translate("+x+","+y+")";
  682. };
  683. this.getPos = function(){
  684. // summary:
  685. // Get the top position in the midst of animation
  686. if(has("webkit")){
  687. var m = win.doc.defaultView.getComputedStyle(this.containerNode, '')["-webkit-transform"];
  688. if(m && m.indexOf("matrix") === 0){
  689. var arr = m.split(/[,\s\)]+/);
  690. return {y:arr[5] - 0, x:arr[4] - 0};
  691. }
  692. return {x:0, y:0};
  693. }else{
  694. // this.containerNode.offsetTop does not work here,
  695. // because it adds the height of the top margin.
  696. var y = parseInt(this.containerNode.style.top) || 0;
  697. return {y:y, x:this.containerNode.offsetLeft};
  698. }
  699. };
  700. this.getDim = function(){
  701. var d = {};
  702. // content width/height
  703. d.c = {h:this.containerNode.offsetHeight, w:this.containerNode.offsetWidth};
  704. // view width/height
  705. d.v = {h:this.domNode.offsetHeight + this._appFooterHeight, w:this.domNode.offsetWidth};
  706. // display width/height
  707. d.d = {h:d.v.h - this.fixedHeaderHeight - this.fixedFooterHeight, w:d.v.w};
  708. // overflowed width/height
  709. d.o = {h:d.c.h - d.v.h + this.fixedHeaderHeight + this.fixedFooterHeight, w:d.c.w - d.v.w};
  710. return d;
  711. };
  712. this.showScrollBar = function(){
  713. if(!this.scrollBar){ return; }
  714. var dim = this._dim;
  715. if(this.scrollDir == "v" && dim.c.h <= dim.d.h){ return; }
  716. if(this.scrollDir == "h" && dim.c.w <= dim.d.w){ return; }
  717. if(this._v && this._h && dim.c.h <= dim.d.h && dim.c.w <= dim.d.w){ return; }
  718. var createBar = function(self, dir){
  719. var bar = self["_scrollBarNode" + dir];
  720. if(!bar){
  721. var wrapper = domConstruct.create("div", null, self.domNode);
  722. var props = { position: "absolute", overflow: "hidden" };
  723. if(dir == "V"){
  724. props.right = "2px";
  725. props.width = "5px";
  726. }else{
  727. props.bottom = (self.isLocalFooter ? self.fixedFooterHeight : 0) + 2 + "px";
  728. props.height = "5px";
  729. }
  730. domStyle.set(wrapper, props);
  731. wrapper.className = "mblScrollBarWrapper";
  732. self["_scrollBarWrapper"+dir] = wrapper;
  733. bar = domConstruct.create("div", null, wrapper);
  734. domStyle.set(bar, {
  735. opacity: 0.6,
  736. position: "absolute",
  737. backgroundColor: "#606060",
  738. fontSize: "1px",
  739. webkitBorderRadius: "2px",
  740. MozBorderRadius: "2px",
  741. webkitTransformOrigin: "0 0",
  742. zIndex: 2147483647 // max of signed 32-bit integer
  743. });
  744. domStyle.set(bar, dir == "V" ? {width: "5px"} : {height: "5px"});
  745. self["_scrollBarNode" + dir] = bar;
  746. }
  747. return bar;
  748. };
  749. if(this._v && !this._scrollBarV){
  750. this._scrollBarV = createBar(this, "V");
  751. }
  752. if(this._h && !this._scrollBarH){
  753. this._scrollBarH = createBar(this, "H");
  754. }
  755. this.resetScrollBar();
  756. };
  757. this.hideScrollBar = function(){
  758. var fadeRule;
  759. if(this.fadeScrollBar && has("webkit")){
  760. if(!dm._fadeRule){
  761. var node = domConstruct.create("style", null, win.doc.getElementsByTagName("head")[0]);
  762. node.textContent =
  763. ".mblScrollableFadeScrollBar{"+
  764. " -webkit-animation-duration: 1s;"+
  765. " -webkit-animation-name: scrollableViewFadeScrollBar;}"+
  766. "@-webkit-keyframes scrollableViewFadeScrollBar{"+
  767. " from { opacity: 0.6; }"+
  768. " to { opacity: 0; }}";
  769. dm._fadeRule = node.sheet.cssRules[1];
  770. }
  771. fadeRule = dm._fadeRule;
  772. }
  773. if(!this.scrollBar){ return; }
  774. var f = function(bar, self){
  775. domStyle.set(bar, {
  776. opacity: 0,
  777. webkitAnimationDuration: ""
  778. });
  779. if(self._aw){ // android workaround
  780. bar.style.webkitTransform = "";
  781. }else{
  782. bar.className = "mblScrollableFadeScrollBar";
  783. }
  784. };
  785. if(this._scrollBarV){
  786. f(this._scrollBarV, this);
  787. this._scrollBarV = null;
  788. }
  789. if(this._scrollBarH){
  790. f(this._scrollBarH, this);
  791. this._scrollBarH = null;
  792. }
  793. };
  794. this.calcScrollBarPos = function(/*Object*/to){ // to: {x, y}
  795. var pos = {};
  796. var dim = this._dim;
  797. var f = function(wrapperH, barH, t, d, c){
  798. var y = Math.round((d - barH - 8) / (d - c) * t);
  799. if(y < -barH + 5){
  800. y = -barH + 5;
  801. }
  802. if(y > wrapperH - 5){
  803. y = wrapperH - 5;
  804. }
  805. return y;
  806. };
  807. if(typeof to.y == "number" && this._scrollBarV){
  808. pos.y = f(this._scrollBarWrapperV.offsetHeight, this._scrollBarV.offsetHeight, to.y, dim.d.h, dim.c.h);
  809. }
  810. if(typeof to.x == "number" && this._scrollBarH){
  811. pos.x = f(this._scrollBarWrapperH.offsetWidth, this._scrollBarH.offsetWidth, to.x, dim.d.w, dim.c.w);
  812. }
  813. return pos;
  814. };
  815. this.scrollScrollBarTo = function(/*Object*/to){ // to: {x, y}
  816. if(!this.scrollBar){ return; }
  817. if(this._v && this._scrollBarV && typeof to.y == "number"){
  818. if(has("webkit")){
  819. this._scrollBarV.style.webkitTransform = this.makeTranslateStr({y:to.y});
  820. }else{
  821. this._scrollBarV.style.top = to.y + "px";
  822. }
  823. }
  824. if(this._h && this._scrollBarH && typeof to.x == "number"){
  825. if(has("webkit")){
  826. this._scrollBarH.style.webkitTransform = this.makeTranslateStr({x:to.x});
  827. }else{
  828. this._scrollBarH.style.left = to.x + "px";
  829. }
  830. }
  831. };
  832. this.slideScrollBarTo = function(/*Object*/to, /*Number*/duration, /*String*/easing){
  833. if(!this.scrollBar){ return; }
  834. var fromPos = this.calcScrollBarPos(this.getPos());
  835. var toPos = this.calcScrollBarPos(to);
  836. if(this._v && this._scrollBarV){
  837. this._runSlideAnimation({y:fromPos.y}, {y:toPos.y}, duration, easing, this._scrollBarV, 0);
  838. }
  839. if(this._h && this._scrollBarH){
  840. this._runSlideAnimation({x:fromPos.x}, {x:toPos.x}, duration, easing, this._scrollBarH, 1);
  841. }
  842. };
  843. this._runSlideAnimation = function(/*Object*/from, /*Object*/to, /*Number*/duration, /*String*/easing, node, idx){
  844. // idx: 0:scrollbarV, 1:scrollbarH, 2:content
  845. if(has("webkit")){
  846. this.setKeyframes(from, to, idx);
  847. domStyle.set(node, {
  848. webkitAnimationDuration: duration + "s",
  849. webkitAnimationTimingFunction: easing
  850. });
  851. domClass.add(node, "mblScrollableScrollTo"+idx);
  852. if(idx == 2){
  853. this.scrollTo(to, true, node);
  854. }else{
  855. this.scrollScrollBarTo(to);
  856. }
  857. }else if(dojo.fx && dojo.fx.easing && duration){
  858. // If you want to support non-webkit browsers,
  859. // your application needs to load necessary modules as follows:
  860. //
  861. // | dojo.require("dojo.fx");
  862. // | dojo.require("dojo.fx.easing");
  863. //
  864. // This module itself does not make dependency on them.
  865. var s = dojo.fx.slideTo({
  866. node: node,
  867. duration: duration*1000,
  868. left: to.x,
  869. top: to.y,
  870. easing: (easing == "ease-out") ? dojo.fx.easing.quadOut : dojo.fx.easing.linear
  871. }).play();
  872. if(idx == 2){
  873. connect.connect(s, "onEnd", this, "onFlickAnimationEnd");
  874. }
  875. }else{
  876. // directly jump to the destination without animation
  877. if(idx == 2){
  878. this.scrollTo(to, false, node);
  879. this.onFlickAnimationEnd();
  880. }else{
  881. this.scrollScrollBarTo(to);
  882. }
  883. }
  884. };
  885. this.resetScrollBar = function(){
  886. // summary:
  887. // Resets the scroll bar length, position, etc.
  888. var f = function(wrapper, bar, d, c, hd, v){
  889. if(!bar){ return; }
  890. var props = {};
  891. props[v ? "top" : "left"] = hd + 4 + "px"; // +4 is for top or left margin
  892. var t = (d - 8) <= 0 ? 1 : d - 8;
  893. props[v ? "height" : "width"] = t + "px";
  894. domStyle.set(wrapper, props);
  895. var l = Math.round(d * d / c); // scroll bar length
  896. l = Math.min(Math.max(l - 8, 5), t); // -8 is for margin for both ends
  897. bar.style[v ? "height" : "width"] = l + "px";
  898. domStyle.set(bar, {"opacity": 0.6});
  899. };
  900. var dim = this.getDim();
  901. f(this._scrollBarWrapperV, this._scrollBarV, dim.d.h, dim.c.h, this.fixedHeaderHeight, true);
  902. f(this._scrollBarWrapperH, this._scrollBarH, dim.d.w, dim.c.w, 0);
  903. this.createMask();
  904. };
  905. this.createMask = function(){
  906. // summary:
  907. // Creates a mask for a scroll bar edge.
  908. // description:
  909. // This function creates a mask that hides corners of one scroll
  910. // bar edge to make it round edge. The other side of the edge is
  911. // always visible and round shaped with the border-radius style.
  912. if(!has("webkit")){ return; }
  913. var ctx;
  914. if(this._scrollBarWrapperV){
  915. var h = this._scrollBarWrapperV.offsetHeight;
  916. ctx = win.doc.getCSSCanvasContext("2d", "scrollBarMaskV", 5, h);
  917. ctx.fillStyle = "rgba(0,0,0,0.5)";
  918. ctx.fillRect(1, 0, 3, 2);
  919. ctx.fillRect(0, 1, 5, 1);
  920. ctx.fillRect(0, h - 2, 5, 1);
  921. ctx.fillRect(1, h - 1, 3, 2);
  922. ctx.fillStyle = "rgb(0,0,0)";
  923. ctx.fillRect(0, 2, 5, h - 4);
  924. this._scrollBarWrapperV.style.webkitMaskImage = "-webkit-canvas(scrollBarMaskV)";
  925. }
  926. if(this._scrollBarWrapperH){
  927. var w = this._scrollBarWrapperH.offsetWidth;
  928. ctx = win.doc.getCSSCanvasContext("2d", "scrollBarMaskH", w, 5);
  929. ctx.fillStyle = "rgba(0,0,0,0.5)";
  930. ctx.fillRect(0, 1, 2, 3);
  931. ctx.fillRect(1, 0, 1, 5);
  932. ctx.fillRect(w - 2, 0, 1, 5);
  933. ctx.fillRect(w - 1, 1, 2, 3);
  934. ctx.fillStyle = "rgb(0,0,0)";
  935. ctx.fillRect(2, 0, w - 4, 5);
  936. this._scrollBarWrapperH.style.webkitMaskImage = "-webkit-canvas(scrollBarMaskH)";
  937. }
  938. };
  939. this.flashScrollBar = function(){
  940. if(this.disableFlashScrollBar || !this.domNode){ return; }
  941. this._dim = this.getDim();
  942. if(this._dim.d.h <= 0){ return; } // dom is not ready
  943. this.showScrollBar();
  944. var _this = this;
  945. setTimeout(function(){
  946. _this.hideScrollBar();
  947. }, 300);
  948. };
  949. this.addCover = function(){
  950. if(!has("touch") && !this.noCover){
  951. if(!this._cover){
  952. this._cover = domConstruct.create("div", null, win.doc.body);
  953. domStyle.set(this._cover, {
  954. backgroundColor: "#ffff00",
  955. opacity: 0,
  956. position: "absolute",
  957. top: "0px",
  958. left: "0px",
  959. width: "100%",
  960. height: "100%",
  961. zIndex: 2147483647 // max of signed 32-bit integer
  962. });
  963. this._ch.push(connect.connect(this._cover,
  964. has("touch") ? "touchstart" : "onmousedown", this, "onTouchEnd"));
  965. }else{
  966. this._cover.style.display = "";
  967. }
  968. this.setSelectable(this._cover, false);
  969. this.setSelectable(this.domNode, false);
  970. }
  971. };
  972. this.removeCover = function(){
  973. if(!has("touch") && this._cover){
  974. this._cover.style.display = "none";
  975. this.setSelectable(this._cover, true);
  976. this.setSelectable(this.domNode, true);
  977. }
  978. };
  979. this.setKeyframes = function(/*Object*/from, /*Object*/to, /*Number*/idx){
  980. if(!dm._rule){
  981. dm._rule = [];
  982. }
  983. // idx: 0:scrollbarV, 1:scrollbarH, 2:content
  984. if(!dm._rule[idx]){
  985. var node = domConstruct.create("style", null, win.doc.getElementsByTagName("head")[0]);
  986. node.textContent =
  987. ".mblScrollableScrollTo"+idx+"{-webkit-animation-name: scrollableViewScroll"+idx+";}"+
  988. "@-webkit-keyframes scrollableViewScroll"+idx+"{}";
  989. dm._rule[idx] = node.sheet.cssRules[1];
  990. }
  991. var rule = dm._rule[idx];
  992. if(rule){
  993. if(from){
  994. rule.deleteRule("from");
  995. rule.insertRule("from { -webkit-transform: "+this.makeTranslateStr(from)+"; }");
  996. }
  997. if(to){
  998. if(to.x === undefined){ to.x = from.x; }
  999. if(to.y === undefined){ to.y = from.y; }
  1000. rule.deleteRule("to");
  1001. rule.insertRule("to { -webkit-transform: "+this.makeTranslateStr(to)+"; }");
  1002. }
  1003. }
  1004. };
  1005. this.setSelectable = function(node, selectable){
  1006. // dojo.setSelectable has dependency on dojo.query. Re-define our own.
  1007. node.style.KhtmlUserSelect = selectable ? "auto" : "none";
  1008. node.style.MozUserSelect = selectable ? "" : "none";
  1009. node.onselectstart = selectable ? null : function(){return false;};
  1010. if(has("ie")){
  1011. node.unselectable = selectable ? "" : "on";
  1012. var nodes = node.getElementsByTagName("*");
  1013. for(var i = 0; i < nodes.length; i++){
  1014. nodes[i].unselectable = selectable ? "" : "on";
  1015. }
  1016. }
  1017. };
  1018. // feature detection
  1019. if(has("webkit")){
  1020. var elem = win.doc.createElement("div");
  1021. elem.style.webkitTransform = "translate3d(0px,1px,0px)";
  1022. win.doc.documentElement.appendChild(elem);
  1023. var v = win.doc.defaultView.getComputedStyle(elem, '')["-webkit-transform"];
  1024. dm.hasTranslate3d = v && v.indexOf("matrix") === 0;
  1025. win.doc.documentElement.removeChild(elem);
  1026. }
  1027. };
  1028. dm.scrollable = scrollable; // for backward compatibility
  1029. return scrollable;
  1030. });