Debian Codegraph @

dojox/mobile/scrollable.js

raw symbols SCIP
1 define([
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 "dojo/dom-geometry",
11 "dojo/touch",
12 "dijit/registry",
13 "dijit/form/_TextBoxMixin",
14 "./sniff",
15 "./_css3",
16 "./_maskUtils",
17 "./common",
18 "dojo/_base/declare",
19 "dojo/has!dojo-bidi?dojox/mobile/bidi/Scrollable"
20 ], function(dojo, connect, event, lang, win, domClass, domConstruct, domStyle,
21 domGeom, touch, registry, TextBoxMixin, has, css3, maskUtils, common, declare, BidiScrollable){
22
23 // module:
24 // dojox/mobile/scrollable
25
26 // TODO: rename to Scrollable.js (capital S) for 2.0
27
28 // TODO: shouldn't be referencing this dojox/mobile variable, would be better to require the mobile.js module
29 var dm = lang.getObject("dojox.mobile", true);
30
31 // feature detection
32 has.add("translate3d", function(){
33 if(has("css3-animations")){
34 var elem = win.doc.createElement("div");
35 elem.style[css3.name("transform")] = "translate3d(0px,1px,0px)";
36 win.doc.documentElement.appendChild(elem);
37 var v = win.doc.defaultView.getComputedStyle(elem, '')[css3.name("transform", true)];
38 var hasTranslate3d = v && v.indexOf("matrix") === 0;
39 win.doc.documentElement.removeChild(elem);
40 return hasTranslate3d;
41 }
42 });
43
44 var Scrollable = function(){
45 // summary:
46 // Mixin for enabling touch scrolling capability.
47 // description:
48 // Mixin for enabling touch scrolling capability.
49 // Mobile WebKit browsers do not allow scrolling inner DIVs. (For instance,
50 // on iOS you need the two-finger operation to scroll them.)
51 // That means you cannot have fixed-positioned header/footer bars.
52 // To solve this issue, this module disables the browsers default scrolling
53 // behavior, and rebuilds its own scrolling machinery by handling touch
54 // events. In this module, this.domNode has height "100%" and is fixed to
55 // the window, and this.containerNode scrolls. If you place a bar outside
56 // of this.containerNode, then it will be fixed-positioned while
57 // this.containerNode is scrollable.
58 //
59 // This module has the following features:
60 //
61 // - Scrolls inner DIVs vertically, horizontally, or both.
62 // - Vertical and horizontal scroll bars.
63 // - Flashes the scroll bars when a view is shown.
64 // - Simulates the flick operation using animation.
65 // - Respects header/footer bars if any.
66 };
67
68 lang.extend(Scrollable, {
69 // fixedHeaderHeight: Number
70 // height of a fixed header
71 fixedHeaderHeight: 0,
72
73 // fixedFooterHeight: Number
74 // height of a fixed footer
75 fixedFooterHeight: 0,
76
77 // isLocalFooter: Boolean
78 // footer is view-local (as opposed to application-wide)
79 isLocalFooter: false,
80
81 // scrollBar: Boolean
82 // show scroll bar or not
83 scrollBar: true,
84
85 // scrollDir: String
86 // v: vertical, h: horizontal, vh: both, f: flip
87 scrollDir: "v",
88
89 // weight: Number
90 // frictional drag
91 weight: 0.6,
92
93 // fadeScrollBar: Boolean
94 fadeScrollBar: true,
95
96 // disableFlashScrollBar: Boolean
97 disableFlashScrollBar: false,
98
99 // threshold: Number
100 // drag threshold value in pixels
101 threshold: 4,
102
103 // constraint: Boolean
104 // bounce back to the content area
105 constraint: true,
106
107 // touchNode: DOMNode
108 // a node that will have touch event handlers
109 touchNode: null,
110
111 // propagatable: Boolean
112 // let touchstart event propagate up
113 propagatable: true,
114
115 // dirLock: Boolean
116 // disable the move handler if scroll starts in the unexpected direction
117 dirLock: false,
118
119 // height: String
120 // explicitly specified height of this widget (ex. "300px")
121 height: "",
122
123 // scrollType: Number
124 // - 1: use (-webkit-)transform:translate3d(x,y,z) style, use (-webkit-)animation for slide animation
125 // - 2: use top/left style,
126 // - 3: use (-webkit-)transform:translate3d(x,y,z) style, use (-webkit-)transition for slide animation
127 // - 0: use default value (3 for Android, iOS6+, and BlackBerry; otherwise 1)
128 scrollType: 0,
129
130 // _parentPadBorderExtentsBottom: [private] Number
131 // For Tooltip.js.
132 _parentPadBorderExtentsBottom: 0,
133
134 // _moved: [private] Boolean
135 // Flag that signals if the user have moved in (one of) the scroll
136 // direction(s) since touch start (a move under the threshold is ignored).
137 _moved: false,
138
139 init: function(/*Object?*/params){
140 // summary:
141 // Initialize according to the given params.
142 // description:
143 // Mixes in the given params into this instance. At least domNode
144 // and containerNode have to be given.
145 // Starts listening to the touchstart events.
146 // Calls resize(), if this widget is a top level widget.
147 if(params){
148 for(var p in params){
149 if(params.hasOwnProperty(p)){
150 this[p] = ((p == "domNode" || p == "containerNode") && typeof params[p] == "string") ?
151 win.doc.getElementById(params[p]) : params[p]; // mix-in params
152 }
153 }
154 }
155 // prevent browser scrolling on IE10 (evt.preventDefault() is not enough)
156 common._setTouchAction(this.domNode, "none");
157 this.touchNode = this.touchNode || this.containerNode;
158 this._v = (this.scrollDir.indexOf("v") != -1); // vertical scrolling
159 this._h = (this.scrollDir.indexOf("h") != -1); // horizontal scrolling
160 this._f = (this.scrollDir == "f"); // flipping views
161
162 this._ch = []; // connect handlers
163 this._ch.push(connect.connect(this.touchNode, touch.press, this, "onTouchStart"));
164 if(has("css3-animations")){
165 // flag for whether to use -webkit-transform:translate3d(x,y,z) or top/left style.
166 // top/left style works fine as a workaround for input fields auto-scrolling issue,
167 // so use top/left in case of Android by default.
168 this._useTopLeft = this.scrollType ? this.scrollType === 2 : false;
169 // Flag for using webkit transition on transform, instead of animation + keyframes.
170 // (keyframes create a slight delay before the slide animation...)
171 if(!this._useTopLeft){
172 this._useTransformTransition =
173 this.scrollType ? this.scrollType === 3 : has("ios") >= 6 || has("android") || has("bb");
174 }
175 if(!this._useTopLeft){
176 if(this._useTransformTransition){
177 this._ch.push(connect.connect(this.containerNode, css3.name("transitionEnd"), this, "onFlickAnimationEnd"));
178 // Note that there is no transitionstart event (#17822).
179 }else{
180 this._ch.push(connect.connect(this.containerNode, css3.name("animationEnd"), this, "onFlickAnimationEnd"));
181 this._ch.push(connect.connect(this.containerNode, css3.name("animationStart"), this, "onFlickAnimationStart"));
182
183 // Creation of keyframes takes a little time. If they are created
184 // in a lazy manner, a slight delay is noticeable when you start
185 // scrolling for the first time. This is to create keyframes up front.
186 for(var i = 0; i < 3; i++){
187 this.setKeyframes(null, null, i);
188 }
189 }
190 if(has("translate3d")){ // workaround for flicker issue on iPhone and Android 3.x/4.0
191 domStyle.set(this.containerNode, css3.name("transform"), "translate3d(0,0,0)");
192 }
193 }else{
194 this._ch.push(connect.connect(this.containerNode, css3.name("transitionEnd"), this, "onFlickAnimationEnd"));
195 // Note that there is no transitionstart event (#17822).
196 }
197 }
198
199 this._speed = {x:0, y:0};
200 this._appFooterHeight = 0;
201 if(this.isTopLevel() && !this.noResize){
202 this.resize();
203 }
204 var _this = this;
205 setTimeout(function(){
206 // Why not using widget.defer() instead of setTimeout()? Because this module
207 // is not always mixed into a widget (ex. dojox/mobile/_ComboBoxMenu), and adding
208 // a check to call either defer or setTimeout has been considered overkill.
209 _this.flashScrollBar();
210 }, 600);
211
212 // #16363: while navigating among input field using TAB (desktop keyboard) or
213 // NEXT (mobile soft keyboard), domNode.scrollTop gets modified (this holds even
214 // if the text widget has selectOnFocus at false, that is even if dijit's _FormWidgetMixin._onFocus
215 // does not trigger a global scrollIntoView). This messes up ScrollableView's own
216 // scrolling machinery. To avoid this misbehavior:
217 if(win.global.addEventListener){ // all supported browsers but IE8
218 // (for IE8, using attachEvent is not a solution, because it only works in bubbling phase)
219 this._onScroll = function(e){
220 if(!_this.domNode || _this.domNode.style.display === 'none'){ return; }
221 var scrollTop = _this.domNode.scrollTop;
222 var scrollLeft = _this.domNode.scrollLeft;
223 var pos;
224 if(scrollTop > 0 || scrollLeft > 0){
225 pos = _this.getPos();
226 // Reset to zero while compensating using our own scroll:
227 _this.domNode.scrollLeft = 0;
228 _this.domNode.scrollTop = 0;
229 _this.scrollTo({x: pos.x - scrollLeft, y: pos.y - scrollTop}); // no animation
230 }
231 };
232 win.global.addEventListener("scroll", this._onScroll, true);
233 }
234 // #17062: Ensure auto-scroll when navigating focusable fields
235 if(!this.disableTouchScroll && this.domNode.addEventListener){
236 this._onFocusScroll = function(e){
237 if(!_this.domNode || _this.domNode.style.display === 'none'){ return; }
238 var node = win.doc.activeElement;
239 var nodeRect, scrollableRect;
240 if(node){
241 nodeRect = node.getBoundingClientRect();
242 scrollableRect = _this.domNode.getBoundingClientRect();
243 if(nodeRect.height < _this.getDim().d.h){
244 // do not call scrollIntoView for elements with a height
245 // larger than the height of scrollable's content display
246 // area (it would be ergonomically harmful).
247
248 if(nodeRect.top < (scrollableRect.top + _this.fixedHeaderHeight)){
249 // scrolling towards top (to bring into the visible area an element
250 // located above it).
251 _this.scrollIntoView(node, true);
252 }else if((nodeRect.top + nodeRect.height) >
253 (scrollableRect.top + scrollableRect.height - _this.fixedFooterHeight)){
254 // scrolling towards bottom (to bring into the visible area an element
255 // located below it).
256 _this.scrollIntoView(node, false);
257 } // else do nothing (the focused node is already visible)
258 }
259 }
260 };
261 this.domNode.addEventListener("focus", this._onFocusScroll, true);
262 }
263 },
264
265 isTopLevel: function(){
266 // summary:
267 // Returns true if this is a top-level widget.
268 // description:
269 // Subclass may want to override.
270 return true;
271 },
272
273 cleanup: function(){
274 // summary:
275 // Uninitialize the module.
276 if(this._ch){
277 for(var i = 0; i < this._ch.length; i++){
278 connect.disconnect(this._ch[i]);
279 }
280 this._ch = null;
281 }
282 if(this._onScroll && win.global.removeEventListener){ // all supported browsers but IE8
283 win.global.removeEventListener("scroll", this._onScroll, true);
284 this._onScroll = null;
285 }
286
287 if(this._onFocusScroll && this.domNode.removeEventListener){
288 this.domNode.removeEventListener("focus", this._onFocusScroll, true);
289 this._onFocusScroll = null;
290 }
291 },
292
293 findDisp: function(/*DomNode*/node){
294 // summary:
295 // Finds the currently displayed view node from my sibling nodes.
296 if(!node.parentNode){ return null; }
297
298 // the given node is the first candidate
299 if(node.nodeType === 1 && domClass.contains(node, "mblSwapView") && node.style.display !== "none"){
300 return node;
301 }
302
303 var nodes = node.parentNode.childNodes;
304 for(var i = 0; i < nodes.length; i++){
305 var n = nodes[i];
306 if(n.nodeType === 1 && domClass.contains(n, "mblView") && n.style.display !== "none"){
307 return n;
308 }
309 }
310 return node;
311 },
312
313 getScreenSize: function(){
314 // summary:
315 // Returns the dimensions of the browser window.
316 return {
317 h: win.global.innerHeight||win.doc.documentElement.clientHeight||win.doc.documentElement.offsetHeight,
318 w: win.global.innerWidth||win.doc.documentElement.clientWidth||win.doc.documentElement.offsetWidth
319 };
320 },
321
322 resize: function(e){
323 // summary:
324 // Adjusts the height of the widget.
325 // description:
326 // If the height property is 'inherit', the height is inherited
327 // from its offset parent. If 'auto', the content height, which
328 // could be smaller than the entire screen height, is used. If an
329 // explicit height value (ex. "300px"), it is used as the new
330 // height. If nothing is specified as the height property, from the
331 // current top position of the widget to the bottom of the screen
332 // will be the new height.
333
334 // moved from init() to support dynamically added fixed bars
335 this._appFooterHeight = (this._fixedAppFooter) ? this._fixedAppFooter.offsetHeight : 0;
336 if(this.isLocalHeader){
337 this.containerNode.style.marginTop = this.fixedHeaderHeight + "px";
338 }
339
340 // Get the top position. Same as dojo.position(node, true).y
341 var top = 0;
342 for(var n = this.domNode; n && n.tagName != "BODY"; n = n.offsetParent){
343 n = this.findDisp(n); // find the first displayed view node
344 if(!n){ break; }
345 top += n.offsetTop + domGeom.getBorderExtents(n).h;
346 }
347
348 // adjust the height of this view
349 var h,
350 screenHeight = this.getScreenSize().h,
351 dh = screenHeight - top - this._appFooterHeight; // default height
352 if(this.height === "inherit"){
353 if(this.domNode.offsetParent){
354 h = domGeom.getContentBox(this.domNode.offsetParent).h - domGeom.getBorderExtents(this.domNode).h + "px";
355 }
356 }else if(this.height === "auto"){
357 var parent = this.domNode.offsetParent;
358 if(parent){
359 this.domNode.style.height = "0px";
360 var parentRect = parent.getBoundingClientRect(),
361 scrollableRect = this.domNode.getBoundingClientRect(),
362 contentBottom = parentRect.bottom - this._appFooterHeight - this._parentPadBorderExtentsBottom;
363 if(scrollableRect.bottom >= contentBottom){ // use entire screen
364 dh = screenHeight - (scrollableRect.top - parentRect.top) - this._appFooterHeight - this._parentPadBorderExtentsBottom;
365 }else{ // stretch to fill predefined area
366 dh = contentBottom - scrollableRect.bottom;
367 }
368 }
369 // content could be smaller than entire screen height
370 var contentHeight = Math.max(this.domNode.scrollHeight, this.containerNode.scrollHeight);
371 h = (contentHeight ? Math.min(contentHeight, dh) : dh) + "px";
372 }else if(this.height){
373 h = this.height;
374 }
375 if(!h){
376 h = dh + "px";
377 }
378 if(h.charAt(0) !== "-" && // to ensure that h is not negative (e.g. "-10px")
379 h !== "default"){
380 this.domNode.style.height = h;
381 }
382
383 if(!this._conn){
384 // to ensure that the view is within a scrolling area when resized.
385 this.onTouchEnd();
386 }
387 },
388
389 onFlickAnimationStart: function(e){
390 if(e){
391 event.stop(e);
392 }
393 },
394
395 onFlickAnimationEnd: function(e){
396 if(has("ios")){
397 this._keepInputCaretInActiveElement();
398 }
399 if(e){
400 var an = e.animationName;
401 if(an && an.indexOf("scrollableViewScroll2") === -1){
402 if(an.indexOf("scrollableViewScroll0") !== -1){ // scrollBarV
403 if(this._scrollBarNodeV){ domClass.remove(this._scrollBarNodeV, "mblScrollableScrollTo0"); }
404 }else if(an.indexOf("scrollableViewScroll1") !== -1){ // scrollBarH
405 if(this._scrollBarNodeH){ domClass.remove(this._scrollBarNodeH, "mblScrollableScrollTo1"); }
406 }else{ // fade or others
407 if(this._scrollBarNodeV){ this._scrollBarNodeV.className = ""; }
408 if(this._scrollBarNodeH){ this._scrollBarNodeH.className = ""; }
409 }
410 return;
411 }
412 if(this._useTransformTransition || this._useTopLeft){
413 var n = e.target;
414 if(n === this._scrollBarV || n === this._scrollBarH){
415 var cls = "mblScrollableScrollTo" + (n === this._scrollBarV ? "0" : "1");
416 if(domClass.contains(n, cls)){
417 domClass.remove(n, cls);
418 }else{
419 n.className = "";
420 }
421 return;
422 }
423 }
424 if(e.srcElement){
425 event.stop(e);
426 }
427 }
428 this.stopAnimation();
429 if(this._bounce){
430 var _this = this;
431 var bounce = _this._bounce;
432 setTimeout(function(){
433 _this.slideTo(bounce, 0.3, "ease-out");
434 }, 0);
435 _this._bounce = undefined;
436 }else{
437 this.hideScrollBar();
438 this.removeCover();
439 }
440 },
441
442 isFormElement: function(/*DOMNode*/node){
443 // summary:
444 // Returns true if the given node is a form control.
445 if(node && node.nodeType !== 1){ node = node.parentNode; }
446 if(!node || node.nodeType !== 1){ return false; }
447 var t = node.tagName;
448 return (t === "SELECT" || t === "INPUT" || t === "TEXTAREA" || t === "BUTTON");
449 },
450
451 onTouchStart: function(e){
452 // summary:
453 // User-defined function to handle touchStart events.
454 if(this.disableTouchScroll){ return; }
455 if(this._conn && (new Date()).getTime() - this.startTime < 500){
456 return; // ignore successive onTouchStart calls
457 }
458 if(!this._conn){
459 this._conn = [];
460 this._conn.push(connect.connect(win.doc, touch.move, this, "onTouchMove"));
461 this._conn.push(connect.connect(win.doc, touch.release, this, "onTouchEnd"));
462 }
463
464 this._aborted = false;
465 if(domClass.contains(this.containerNode, "mblScrollableScrollTo2")){
466 this.abort();
467 }else{ // reset scrollbar class especially for reseting fade-out animation
468 if(this._scrollBarNodeV){ this._scrollBarNodeV.className = ""; }
469 if(this._scrollBarNodeH){ this._scrollBarNodeH.className = ""; }
470 }
471 this.touchStartX = e.touches ? e.touches[0].pageX : e.clientX;
472 this.touchStartY = e.touches ? e.touches[0].pageY : e.clientY;
473 this.startTime = (new Date()).getTime();
474 this.startPos = this.getPos();
475 this._dim = this.getDim();
476 this._time = [0];
477 this._posX = [this.touchStartX];
478 this._posY = [this.touchStartY];
479 this._locked = false;
480 this._moved = false;
481
482 this._preventDefaultInNextTouchMove = true; // #17502
483 if(!this.isFormElement(e.target)){
484 // Call preventDefault to avoid browser scroll, except for form elements
485 // for which doing it in touch.press would forbid the virtual keyboard
486 // from showing up (for form elements which are text widgets, preventDefault
487 // is called in touch.move).
488 this.propagatable ? e.preventDefault() : event.stop(e);
489 this._preventDefaultInNextTouchMove = false;
490 }
491 },
492
493 onTouchMove: function(e){
494 // summary:
495 // User-defined function to handle touchMove events.
496 if(this._locked){ return; }
497
498 if(this._preventDefaultInNextTouchMove){ // #17502
499 this._preventDefaultInNextTouchMove = false; // only in the first touch.move
500 var enclosingWidget = registry.getEnclosingWidget(
501 // On touch-enabled devices, e.target can be different in touch.move than in
502 // touch.start. Hence:
503 ((e.targetTouches && e.targetTouches.length === 1) ? e.targetTouches[0] : e).target);
504 if(enclosingWidget && enclosingWidget.isInstanceOf(TextBoxMixin)){
505 // For touches on text widgets for which e.preventDefault() has not been
506 // called in onTouchStart, call it in onTouchMove() to avoid browser scroll.
507 // Not done on other elements such that for instance a native slider
508 // can still handle touchmove events.
509 this.propagatable ? e.preventDefault() : event.stop(e);
510 }
511 }
512
513 var x = e.touches ? e.touches[0].pageX : e.clientX;
514 var y = e.touches ? e.touches[0].pageY : e.clientY;
515 var dx = x - this.touchStartX;
516 var dy = y - this.touchStartY;
517 var to = {x:this.startPos.x + dx, y:this.startPos.y + dy};
518 var dim = this._dim;
519
520 dx = Math.abs(dx);
521 dy = Math.abs(dy);
522 if(this._time.length == 1){ // the first TouchMove after TouchStart
523 if(this.dirLock){
524 if(this._v && !this._h && dx >= this.threshold && dx >= dy ||
525 (this._h || this._f) && !this._v && dy >= this.threshold && dy >= dx){
526 this._locked = true;
527 return;
528 }
529 }
530 if(this._v && this._h){ // scrollDir="hv"
531 if(dy < this.threshold &&
532 dx < this.threshold){
533 return;
534 }
535 }else{
536 if(this._v && dy < this.threshold ||
537 (this._h || this._f) && dx < this.threshold){
538 return;
539 }
540 }
541 this._moved = true;
542 this.addCover();
543 this.showScrollBar();
544 }
545
546 var weight = this.weight;
547 if(this._v && this.constraint){
548 if(to.y > 0){ // content is below the screen area
549 to.y = Math.round(to.y * weight);
550 }else if(to.y < -dim.o.h){ // content is above the screen area
551 if(dim.c.h < dim.d.h){ // content is shorter than display
552 to.y = Math.round(to.y * weight);
553 }else{
554 to.y = -dim.o.h - Math.round((-dim.o.h - to.y) * weight);
555 }
556 }
557 }
558 if((this._h || this._f) && this.constraint){
559 if(to.x > 0){
560 to.x = Math.round(to.x * weight);
561 }else if(to.x < -dim.o.w){
562 if(dim.c.w < dim.d.w){
563 to.x = Math.round(to.x * weight);
564 }else{
565 to.x = -dim.o.w - Math.round((-dim.o.w - to.x) * weight);
566 }
567 }
568 }
569 this.scrollTo(to);
570
571 var max = 10;
572 var n = this._time.length; // # of samples
573 if(n >= 2){
574 this._moved = true;
575 // Check the direction of the finger move.
576 // If the direction has been changed, discard the old data.
577 var d0, d1;
578 if(this._v && !this._h){
579 d0 = this._posY[n - 1] - this._posY[n - 2];
580 d1 = y - this._posY[n - 1];
581 }else if(!this._v && this._h){
582 d0 = this._posX[n - 1] - this._posX[n - 2];
583 d1 = x - this._posX[n - 1];
584 }
585 if(d0 * d1 < 0){ // direction changed
586 // leave only the latest data
587 this._time = [this._time[n - 1]];
588 this._posX = [this._posX[n - 1]];
589 this._posY = [this._posY[n - 1]];
590 n = 1;
591 }
592 }
593 if(n == max){
594 this._time.shift();
595 this._posX.shift();
596 this._posY.shift();
597 }
598 this._time.push((new Date()).getTime() - this.startTime);
599 this._posX.push(x);
600 this._posY.push(y);
601 },
602
603 _keepInputCaretInActiveElement: function(){
604 var activeElement = win.doc.activeElement;
605 var initialValue;
606 if(activeElement && (activeElement.tagName == "INPUT" || activeElement.tagName == "TEXTAREA")){
607 initialValue = activeElement.value;
608 if(activeElement.type == "number" || activeElement.type == "week"){
609 if(initialValue){
610 activeElement.value = activeElement.value + 1;
611 }else{
612 activeElement.value = (activeElement.type == "week") ? "2013-W10" : 1;
613 }
614 activeElement.value = initialValue;
615 }else{
616 activeElement.value = activeElement.value + " ";
617 activeElement.value = initialValue;
618 }
619 }
620 },
621
622 onTouchEnd: function(/*Event*/e){
623 // summary:
624 // User-defined function to handle touchEnd events.
625 if(this._locked){ return; }
626 var speed = this._speed = {x:0, y:0};
627 var dim = this._dim;
628 var pos = this.getPos();
629 var to = {}; // destination
630 if(e){
631 if(!this._conn){ return; } // if we get onTouchEnd without onTouchStart, ignore it.
632 for(var i = 0; i < this._conn.length; i++){
633 connect.disconnect(this._conn[i]);
634 }
635 this._conn = null;
636
637 var clicked = false;
638 if(!this._aborted && !this._moved){
639 clicked = true;
640 }
641 if(clicked){ // clicked, not dragged or flicked
642 this.hideScrollBar();
643 this.removeCover();
644 // need to send a synthetic click?
645 if(has("touch") && has("clicks-prevented") && !this.isFormElement(e.target)){
646 var elem = e.target;
647 if(elem.nodeType != 1){
648 elem = elem.parentNode;
649 }
650 setTimeout(function(){
651 dm._sendClick(elem, e);
652 });
653 }
654 return;
655 }
656 speed = this._speed = this.getSpeed();
657 }else{
658 if(pos.x == 0 && pos.y == 0){ return; } // initializing
659 dim = this.getDim();
660 }
661
662 if(this._v){
663 to.y = pos.y + speed.y;
664 }
665 if(this._h || this._f){
666 to.x = pos.x + speed.x;
667 }
668
669 if(this.adjustDestination(to, pos, dim) === false){ return; }
670 if(this.constraint){
671 if(this.scrollDir == "v" && dim.c.h < dim.d.h){ // content is shorter than display
672 this.slideTo({y:0}, 0.3, "ease-out"); // go back to the top
673 return;
674 }else if(this.scrollDir == "h" && dim.c.w < dim.d.w){ // content is narrower than display
675 this.slideTo({x:0}, 0.3, "ease-out"); // go back to the left
676 return;
677 }else if(this._v && this._h && dim.c.h < dim.d.h && dim.c.w < dim.d.w){
678 this.slideTo({x:0, y:0}, 0.3, "ease-out"); // go back to the top-left
679 return;
680 }
681 }
682
683 var duration, easing = "ease-out";
684 var bounce = {};
685 if(this._v && this.constraint){
686 if(to.y > 0){ // going down. bounce back to the top.
687 if(pos.y > 0){ // started from below the screen area. return quickly.
688 duration = 0.3;
689 to.y = 0;
690 }else{
691 to.y = Math.min(to.y, 20);
692 easing = "linear";
693 bounce.y = 0;
694 }
695 }else if(-speed.y > dim.o.h - (-pos.y)){ // going up. bounce back to the bottom.
696 if(pos.y < -dim.o.h){ // started from above the screen top. return quickly.
697 duration = 0.3;
698 to.y = dim.c.h <= dim.d.h ? 0 : -dim.o.h; // if shorter, move to 0
699 }else{
700 to.y = Math.max(to.y, -dim.o.h - 20);
701 easing = "linear";
702 bounce.y = -dim.o.h;
703 }
704 }
705 }
706 if((this._h || this._f) && this.constraint){
707 if(to.x > 0){ // going right. bounce back to the left.
708 if(pos.x > 0){ // started from right of the screen area. return quickly.
709 duration = 0.3;
710 to.x = 0;
711 }else{
712 to.x = Math.min(to.x, 20);
713 easing = "linear";
714 bounce.x = 0;
715 }
716 }else if(-speed.x > dim.o.w - (-pos.x)){ // going left. bounce back to the right.
717 if(pos.x < -dim.o.w){ // started from left of the screen top. return quickly.
718 duration = 0.3;
719 to.x = dim.c.w <= dim.d.w ? 0 : -dim.o.w; // if narrower, move to 0
720 }else{
721 to.x = Math.max(to.x, -dim.o.w - 20);
722 easing = "linear";
723 bounce.x = -dim.o.w;
724 }
725 }
726 }
727 this._bounce = (bounce.x !== undefined || bounce.y !== undefined) ? bounce : undefined;
728
729 if(duration === undefined){
730 var distance, velocity;
731 if(this._v && this._h){
732 velocity = Math.sqrt(speed.x*speed.x + speed.y*speed.y);
733 distance = Math.sqrt(Math.pow(to.y - pos.y, 2) + Math.pow(to.x - pos.x, 2));
734 }else if(this._v){
735 velocity = speed.y;
736 distance = to.y - pos.y;
737 }else if(this._h){
738 velocity = speed.x;
739 distance = to.x - pos.x;
740 }
741 if(distance === 0 && !e){ return; } // #13154
742 duration = velocity !== 0 ? Math.abs(distance / velocity) : 0.01; // time = distance / velocity
743 }
744 this.slideTo(to, duration, easing);
745 },
746
747 adjustDestination: function(/*Object*/to, /*Object*/pos, /*Object*/dim){
748 // summary:
749 // A stub function to be overridden by subclasses.
750 // description:
751 // This function is called from onTouchEnd(). The purpose is to give its
752 // subclasses a chance to adjust the destination position. If this
753 // function returns false, onTouchEnd() returns immediately without
754 // performing scroll.
755 // to:
756 // The destination position. An object with x and y.
757 // pos:
758 // The current position. An object with x and y.
759 // dim:
760 // Dimension information returned by getDim().
761
762 // subclass may want to implement
763 return true; // Boolean
764 },
765
766 abort: function(){
767 // summary:
768 // Aborts scrolling.
769 // description:
770 // This function stops the scrolling animation that is currently
771 // running. It is called when the user touches the screen while
772 // scrolling.
773 this._aborted = true;
774 this.scrollTo(this.getPos());
775 this.stopAnimation();
776 },
777
778 stopAnimation: function(){
779 // summary:
780 // Stops the currently running animation.
781 domClass.remove(this.containerNode, "mblScrollableScrollTo2");
782 this.containerNode.className = this.containerNode.className
783 .replace(/mblScrollableScrollTo2-[^ ]+/, ""); // TODO: remove supports regexp?
784 // TODO: there must be a better way than hardcoding this class name in
785 // 20 different places and 2 files
786 if(this._scrollBarV){
787 this._scrollBarV.className = "";
788 }
789 if(this._scrollBarH){
790 this._scrollBarH.className = "";
791 }
792 if(this._useTransformTransition || this._useTopLeft){
793 this.containerNode.style[css3.name("transition")] = "";
794 if(this._scrollBarV) { this._scrollBarV.style[css3.name("transition")] = ""; }
795 if(this._scrollBarH) { this._scrollBarH.style[css3.name("transition")] = ""; }
796 }
797 },
798
799 scrollIntoView: function(/*DOMNode*/node, /*Boolean?*/alignWithTop, /*Number?*/duration){
800 // summary:
801 // Scrolls the pane until the searching node is in the view.
802 // node:
803 // A DOM node to be searched for view.
804 // alignWithTop:
805 // If true, aligns the node at the top of the pane.
806 // If false, aligns the node at the bottom of the pane.
807 // duration:
808 // Duration of scrolling in seconds. (ex. 0.3)
809 // If not specified, scrolls without animation.
810 // description:
811 // Just like the scrollIntoView method of DOM elements, this
812 // function causes the given node to scroll into view, aligning it
813 // either at the top or bottom of the pane.
814
815 if(!this._v){ return; } // cannot scroll vertically
816
817 var c = this.containerNode,
818 h = this.getDim().d.h, // the height of ScrollableView's content display area
819 top = 0;
820
821 // Get the top position of node relative to containerNode
822 for(var n = node; n !== c; n = n.offsetParent){
823 if(!n || n.tagName === "BODY"){ return; } // exit if node is not a child of scrollableView
824 top += n.offsetTop;
825 }
826 // Calculate scroll destination position
827 var y = alignWithTop ? Math.max(h - c.offsetHeight, -top) : Math.min(0, h - top - node.offsetHeight);
828
829 // Scroll to destination position
830 (duration && typeof duration === "number") ?
831 this.slideTo({y: y}, duration, "ease-out") : this.scrollTo({y: y});
832 },
833
834 getSpeed: function(){
835 // summary:
836 // Returns an object that indicates the scrolling speed.
837 // description:
838 // From the position and elapsed time information, calculates the
839 // scrolling speed, and returns an object with x and y.
840 var x = 0, y = 0, n = this._time.length;
841 // if the user holds the mouse or finger more than 0.5 sec, do not move.
842 if(n >= 2 && (new Date()).getTime() - this.startTime - this._time[n - 1] < 500){
843 var dy = this._posY[n - (n > 3 ? 2 : 1)] - this._posY[(n - 6) >= 0 ? n - 6 : 0];
844 var dx = this._posX[n - (n > 3 ? 2 : 1)] - this._posX[(n - 6) >= 0 ? n - 6 : 0];
845 var dt = this._time[n - (n > 3 ? 2 : 1)] - this._time[(n - 6) >= 0 ? n - 6 : 0];
846 y = this.calcSpeed(dy, dt);
847 x = this.calcSpeed(dx, dt);
848 }
849 return {x:x, y:y};
850 },
851
852 calcSpeed: function(/*Number*/distance, /*Number*/time){
853 // summary:
854 // Calculate the speed given the distance and time.
855 return Math.round(distance / time * 100) * 4;
856 },
857
858 scrollTo: function(/*Object*/to, /*Boolean?*/doNotMoveScrollBar, /*DomNode?*/node){
859 // summary:
860 // Scrolls to the given position immediately without animation.
861 // to:
862 // The destination position. An object with x and y.
863 // ex. {x:0, y:-5}
864 // doNotMoveScrollBar:
865 // If true, the scroll bar will not be updated. If not specified,
866 // it will be updated.
867 // node:
868 // A DOM node to scroll. If not specified, defaults to
869 // this.containerNode.
870
871 // scroll events
872 var scrollEvent, beforeTopHeight, afterBottomHeight;
873 var doScroll = true;
874 if(!this._aborted && this._conn){ // No scroll event if the call to scrollTo comes from abort or onTouchEnd
875 if(!this._dim){
876 this._dim = this.getDim();
877 }
878 beforeTopHeight = (to.y > 0)?to.y:0;
879 afterBottomHeight = (this._dim.o.h + to.y < 0)?-1 * (this._dim.o.h + to.y):0;
880 scrollEvent = {bubbles: false,
881 cancelable: false,
882 x: to.x,
883 y: to.y,
884 beforeTop: beforeTopHeight > 0,
885 beforeTopHeight: beforeTopHeight,
886 afterBottom: afterBottomHeight > 0,
887 afterBottomHeight: afterBottomHeight};
888 // before scroll event
889 doScroll = this.onBeforeScroll(scrollEvent);
890 }
891
892 if(doScroll){
893 var s = (node || this.containerNode).style;
894 if(has("css3-animations")){
895 if(!this._useTopLeft){
896 if(this._useTransformTransition){
897 s[css3.name("transition")] = "";
898 }
899 s[css3.name("transform")] = this.makeTranslateStr(to);
900 }else{
901 s[css3.name("transition")] = "";
902 if(this._v){
903 s.top = to.y + "px";
904 }
905 if(this._h || this._f){
906 s.left = to.x + "px";
907 }
908 }
909 }else{
910 if(this._v){
911 s.top = to.y + "px";
912 }
913 if(this._h || this._f){
914 s.left = to.x + "px";
915 }
916 }
917 if(has("ios")){
918 this._keepInputCaretInActiveElement();
919 }
920 if(!doNotMoveScrollBar){
921 this.scrollScrollBarTo(this.calcScrollBarPos(to));
922 }
923 if(scrollEvent){
924 // After scroll event
925 this.onAfterScroll(scrollEvent);
926 }
927 }
928 },
929
930 onBeforeScroll: function(/*Event*/e){
931 // e: Event
932 // the scroll event, that contains the following attributes:
933 // x (x coordinate of the scroll destination),
934 // y (y coordinate of the scroll destination),
935 // beforeTop (a boolean that is true if the scroll detination is before the top of the scrollable),
936 // beforeTopHeight (the number of pixels before the top of the scrollable for the scroll destination),
937 // afterBottom (a boolean that is true if the scroll destination is after the bottom of the scrollable),
938 // afterBottomHeight (the number of pixels after the bottom of the scrollable for the scroll destination)
939 // summary:
940 // called before a scroll is initiated. If this method returns false,
941 // the scroll is canceled.
942 // tags:
943 // callback
944 return true;
945 },
946
947 onAfterScroll: function(/*Event*/e){
948 // e: Event
949 // the scroll event, that contains the following attributes:
950 // x (x coordinate of the scroll destination),
951 // y (y coordinate of the scroll destination),
952 // beforeTop (a boolean that is true if the scroll detination is before the top of the scrollable),
953 // beforeTopHeight (the number of pixels before the top of the scrollable for the scroll destination),
954 // afterBottom (a boolean that is true if the scroll destination is after the bottom of the scrollable),
955 // afterBottomHeight (the number of pixels after the bottom of the scrollable for the scroll destination)
956 // summary:
957 // called after a scroll has been performed.
958 // tags:
959 // callback
960 },
961
962 slideTo: function(/*Object*/to, /*Number*/duration, /*String*/easing){
963 // summary:
964 // Scrolls to the given position with the slide animation.
965 // to:
966 // The scroll destination position. An object with x and/or y.
967 // ex. {x:0, y:-5}, {y:-29}, etc.
968 // duration:
969 // Duration of scrolling in seconds. (ex. 0.3)
970 // easing:
971 // The name of easing effect which webkit supports.
972 // "ease", "linear", "ease-in", "ease-out", etc.
973
974 this._runSlideAnimation(this.getPos(), to, duration, easing, this.containerNode, 2);
975 this.slideScrollBarTo(to, duration, easing);
976 },
977
978 makeTranslateStr: function(/*Object*/to){
979 // summary:
980 // Constructs a string value that is passed to the -webkit-transform property.
981 // to:
982 // The destination position. An object with x and/or y.
983 // description:
984 // Return value example: "translate3d(0px,-8px,0px)"
985
986 var y = this._v && typeof to.y == "number" ? to.y+"px" : "0px";
987 var x = (this._h||this._f) && typeof to.x == "number" ? to.x+"px" : "0px";
988 return has("translate3d") ?
989 "translate3d("+x+","+y+",0px)" : "translate("+x+","+y+")";
990 },
991
992 getPos: function(){
993 // summary:
994 // Gets the top position in the midst of animation.
995 if(has("css3-animations")){
996 var s = win.doc.defaultView.getComputedStyle(this.containerNode, '');
997 if(!this._useTopLeft){
998 var m = s[css3.name("transform")];
999 if(m && m.indexOf("matrix") === 0){
1000 var arr = m.split(/[,\s\)]+/);
1001 // IE10 returns a matrix3d
1002 var i = m.indexOf("matrix3d") === 0 ? 12 : 4;
1003 return {y:arr[i+1] - 0, x:arr[i] - 0};
1004 }
1005 return {x:0, y:0};
1006 }else{
1007 return {x:parseInt(s.left) || 0, y:parseInt(s.top) || 0};
1008 }
1009 }else{
1010 // this.containerNode.offsetTop does not work here,
1011 // because it adds the height of the top margin.
1012 var y = parseInt(this.containerNode.style.top) || 0;
1013 return {y:y, x:this.containerNode.offsetLeft};
1014 }
1015 },
1016
1017 getDim: function(){
1018 // summary:
1019 // Returns various internal dimensional information needed for calculation.
1020
1021 var d = {};
1022 // content width/height
1023 d.c = {h:this.containerNode.offsetHeight, w:this.containerNode.offsetWidth};
1024
1025 // view width/height
1026 d.v = {h:this.domNode.offsetHeight + this._appFooterHeight, w:this.domNode.offsetWidth};
1027
1028 // display width/height
1029 d.d = {h:d.v.h - this.fixedHeaderHeight - this.fixedFooterHeight - this._appFooterHeight, w:d.v.w};
1030
1031 // overflowed width/height
1032 d.o = {h:d.c.h - d.v.h + this.fixedHeaderHeight + this.fixedFooterHeight + this._appFooterHeight, w:d.c.w - d.v.w};
1033 return d;
1034 },
1035
1036 showScrollBar: function(){
1037 // summary:
1038 // Shows the scroll bar.
1039 // description:
1040 // This function creates the scroll bar instance if it does not
1041 // exist yet, and calls resetScrollBar() to reset its length and
1042 // position.
1043
1044 if(!this.scrollBar){ return; }
1045
1046 var dim = this._dim;
1047 if(this.scrollDir == "v" && dim.c.h <= dim.d.h){ return; }
1048 if(this.scrollDir == "h" && dim.c.w <= dim.d.w){ return; }
1049 if(this._v && this._h && dim.c.h <= dim.d.h && dim.c.w <= dim.d.w){ return; }
1050
1051 var createBar = function(self, dir){
1052 var bar = self["_scrollBarNode" + dir];
1053 if(!bar){
1054 var wrapper = domConstruct.create("div", null, self.domNode);
1055 var props = { position: "absolute", overflow: "hidden" };
1056 if(dir == "V"){
1057 props.right = "2px";
1058 props.width = "5px";
1059 }else{
1060 props.bottom = (self.isLocalFooter ? self.fixedFooterHeight : 0) + 2 + "px";
1061 props.height = "5px";
1062 }
1063 domStyle.set(wrapper, props);
1064 wrapper.className = "mblScrollBarWrapper";
1065 self["_scrollBarWrapper"+dir] = wrapper;
1066
1067 bar = domConstruct.create("div", null, wrapper);
1068 domStyle.set(bar, css3.add({
1069 opacity: 0.6,
1070 position: "absolute",
1071 backgroundColor: "#606060",
1072 fontSize: "1px",
1073 MozBorderRadius: "2px",
1074 zIndex: 2147483647 // max of signed 32-bit integer
1075 }, {
1076 borderRadius: "2px",
1077 transformOrigin: "0 0"
1078 }));
1079 domStyle.set(bar, dir == "V" ? {width: "5px"} : {height: "5px"});
1080 self["_scrollBarNode" + dir] = bar;
1081 }
1082 return bar;
1083 };
1084 if(this._v && !this._scrollBarV){
1085 this._scrollBarV = createBar(this, "V");
1086 }
1087 if(this._h && !this._scrollBarH){
1088 this._scrollBarH = createBar(this, "H");
1089 }
1090 this.resetScrollBar();
1091 },
1092
1093 hideScrollBar: function(){
1094 // summary:
1095 // Hides the scroll bar.
1096 // description:
1097 // If the fadeScrollBar property is true, hides the scroll bar with
1098 // the fade animation.
1099
1100 if(this.fadeScrollBar && has("css3-animations")){
1101 if(!dm._fadeRule){
1102 var node = domConstruct.create("style", null, win.doc.getElementsByTagName("head")[0]);
1103 node.textContent =
1104 ".mblScrollableFadeScrollBar{"+
1105 " " + css3.name("animation-duration", true) + ": 1s;"+
1106 " " + css3.name("animation-name", true) + ": scrollableViewFadeScrollBar;}"+
1107 "@" + css3.name("keyframes", true) + " scrollableViewFadeScrollBar{"+
1108 " from { opacity: 0.6; }"+
1109 " to { opacity: 0; }}";
1110 dm._fadeRule = node.sheet.cssRules[1];
1111 }
1112 }
1113 if(!this.scrollBar){ return; }
1114 var f = function(bar, self){
1115 domStyle.set(bar, css3.add({
1116 opacity: 0
1117 }, {
1118 animationDuration: ""
1119 }));
1120 // do not use fade animation in case of using top/left on Android
1121 // since it causes screen flicker during adress bar's fading out
1122 if(!(self._useTopLeft && has('android'))){
1123 bar.className = "mblScrollableFadeScrollBar";
1124 }
1125 };
1126 if(this._scrollBarV){
1127 f(this._scrollBarV, this);
1128 this._scrollBarV = null;
1129 }
1130 if(this._scrollBarH){
1131 f(this._scrollBarH, this);
1132 this._scrollBarH = null;
1133 }
1134 },
1135
1136 calcScrollBarPos: function(/*Object*/to){
1137 // summary:
1138 // Calculates the scroll bar position.
1139 // description:
1140 // Given the scroll destination position, calculates the top and/or
1141 // the left of the scroll bar(s). Returns an object with x and y.
1142 // to:
1143 // The scroll destination position. An object with x and y.
1144 // ex. {x:0, y:-5}
1145
1146 var pos = {};
1147 var dim = this._dim;
1148 var f = function(wrapperH, barH, t, d, c){
1149 var y = Math.round((d - barH - 8) / (d - c) * t);
1150 if(y < -barH + 5){
1151 y = -barH + 5;
1152 }
1153 if(y > wrapperH - 5){
1154 y = wrapperH - 5;
1155 }
1156 return y;
1157 };
1158 if(typeof to.y == "number" && this._scrollBarV){
1159 pos.y = f(this._scrollBarWrapperV.offsetHeight, this._scrollBarV.offsetHeight, to.y, dim.d.h, dim.c.h);
1160 }
1161 if(typeof to.x == "number" && this._scrollBarH){
1162 pos.x = f(this._scrollBarWrapperH.offsetWidth, this._scrollBarH.offsetWidth, to.x, dim.d.w, dim.c.w);
1163 }
1164 return pos;
1165 },
1166
1167 scrollScrollBarTo: function(/*Object*/to){
1168 // summary:
1169 // Moves the scroll bar(s) to the given position without animation.
1170 // to:
1171 // The destination position. An object with x and/or y.
1172 // ex. {x:2, y:5}, {y:20}, etc.
1173
1174 if(!this.scrollBar){ return; }
1175 if(this._v && this._scrollBarV && typeof to.y == "number"){
1176 if(has("css3-animations")){
1177 if(!this._useTopLeft){
1178 if(this._useTransformTransition){
1179 this._scrollBarV.style[css3.name("transition")] = "";
1180 }
1181 this._scrollBarV.style[css3.name("transform")] = this.makeTranslateStr({y:to.y});
1182 }else{
1183 domStyle.set(this._scrollBarV, css3.add({
1184 top: to.y + "px"
1185 }, {
1186 transition: ""
1187 }));
1188 }
1189 }else{
1190 this._scrollBarV.style.top = to.y + "px";
1191 }
1192 }
1193 if(this._h && this._scrollBarH && typeof to.x == "number"){
1194 if(has("css3-animations")){
1195 if(!this._useTopLeft){
1196 if(this._useTransformTransition){
1197 this._scrollBarH.style[css3.name("transition")] = "";
1198 }
1199 this._scrollBarH.style[css3.name("transform")] = this.makeTranslateStr({x:to.x});
1200 }else{
1201 domStyle.set(this._scrollBarH, css3.add({
1202 left: to.x + "px"
1203 }, {
1204 transition: ""
1205 }));
1206 }
1207 }else{
1208 this._scrollBarH.style.left = to.x + "px";
1209 }
1210 }
1211 },
1212
1213 slideScrollBarTo: function(/*Object*/to, /*Number*/duration, /*String*/easing){
1214 // summary:
1215 // Moves the scroll bar(s) to the given position with the slide animation.
1216 // to:
1217 // The destination position. An object with x and y.
1218 // ex. {x:0, y:-5}
1219 // duration:
1220 // Duration of the animation in seconds. (ex. 0.3)
1221 // easing:
1222 // The name of easing effect which webkit supports.
1223 // "ease", "linear", "ease-in", "ease-out", etc.
1224
1225 if(!this.scrollBar){ return; }
1226 var fromPos = this.calcScrollBarPos(this.getPos());
1227 var toPos = this.calcScrollBarPos(to);
1228 if(this._v && this._scrollBarV){
1229 this._runSlideAnimation({y:fromPos.y}, {y:toPos.y}, duration, easing, this._scrollBarV, 0);
1230 }
1231 if(this._h && this._scrollBarH){
1232 this._runSlideAnimation({x:fromPos.x}, {x:toPos.x}, duration, easing, this._scrollBarH, 1);
1233 }
1234 },
1235
1236 _runSlideAnimation: function(/*Object*/from, /*Object*/to, /*Number*/duration, /*String*/easing, /*DomNode*/node, /*Number*/idx){
1237 // tags:
1238 // private
1239
1240 // idx: 0:scrollbarV, 1:scrollbarH, 2:content
1241 if(has("css3-animations")){
1242 if(!this._useTopLeft){
1243 if(this._useTransformTransition){
1244 // for iOS6 (maybe others?): use -webkit-transform + -webkit-transition
1245 if(to.x === undefined){ to.x = from.x; }
1246 if(to.y === undefined){ to.y = from.y; }
1247 // make sure we actually change the transform, otherwise no webkitTransitionEnd is fired.
1248 if(to.x !== from.x || to.y !== from.y){
1249 this.onFlickAnimationStart(); // needed because there is no transitionstart event.
1250 domStyle.set(node, css3.add({}, {
1251 transitionProperty: css3.name("transform"),
1252 transitionDuration: duration + "s",
1253 transitionTimingFunction: easing
1254 }));
1255 var t = this.makeTranslateStr(to);
1256 setTimeout(function(){ // setTimeout is needed to prevent webkitTransitionEnd not fired
1257 domStyle.set(node, css3.add({}, {
1258 transform: t
1259 }));
1260 }, 0);
1261 domClass.add(node, "mblScrollableScrollTo"+idx);
1262 } else {
1263 // transform not changed, just hide the scrollbar
1264 this.hideScrollBar();
1265 this.removeCover();
1266 }
1267 }else{
1268 // use -webkit-transform + -webkit-animation
1269 var entering = this.findDisp(this.domNode) === this.domNode;
1270 var className = this.setKeyframes(from, to, idx, entering);
1271 domStyle.set(node, css3.add({}, {
1272 animationDuration: duration + "s",
1273 animationTimingFunction: easing
1274 }));
1275 domClass.add(node, className);
1276 domClass.add(node, "mblScrollableScrollTo"+idx);
1277 if(idx == 2){
1278 this.scrollTo(to, true, node);
1279 }else{
1280 this.scrollScrollBarTo(to);
1281 }
1282 }
1283 }else if(to.x !== undefined || to.y !== undefined){
1284 this.onFlickAnimationStart(); // #17822: needed because there is no transitionstart event.
1285 domStyle.set(node, css3.add({}, {
1286 // #17822 when scrolling on one direction, avoid unnecessarily animating
1287 // both top and left, because this leads to two transitionend events fired
1288 // instead of one in some browsers (Safari/iOS7 at least).
1289 transitionProperty: (to.x !== undefined && to.y !== undefined) ?
1290 "top, left" :
1291 to.y !== undefined ? "top" : "left",
1292 transitionDuration: duration + "s",
1293 transitionTimingFunction: easing
1294 }));
1295 setTimeout(function(){ // setTimeout is needed to prevent webkitTransitionEnd not fired
1296 var style = {};
1297 if(to.x !== undefined){
1298 style.left = to.x + "px";
1299 }
1300 if(to.y !== undefined){
1301 style.top = to.y + "px";
1302 }
1303 domStyle.set(node, style);
1304 }, 0);
1305 domClass.add(node, "mblScrollableScrollTo"+idx);
1306 }
1307 }else if(dojo.fx && dojo.fx.easing && duration){
1308 // If you want to support non-webkit browsers,
1309 // your application needs to load necessary modules as follows:
1310 //
1311 // | dojo.require("dojo.fx");
1312 // | dojo.require("dojo.fx.easing");
1313 //
1314 // This module itself does not make dependency on them.
1315 // TODO: for 2.0 the dojo global is going away. Use require("dojo/fx") and require("dojo/fx/easing") instead.
1316
1317 var self = this;
1318 var s = dojo.fx.slideTo({
1319 node: node,
1320 duration: duration*1000,
1321 left: to.x,
1322 top: to.y,
1323 easing: (easing == "ease-out") ? dojo.fx.easing.quadOut : dojo.fx.easing.linear,
1324 onBegin: function(){ // #17822
1325 if(idx == 2){
1326 self.onFlickAnimationStart();
1327 }
1328 },
1329 onEnd: function(){
1330 if(idx == 2){
1331 self.onFlickAnimationEnd();
1332 }
1333 }
1334 }).play();
1335 }else{
1336 // directly jump to the destination without animation
1337 if(idx == 2){
1338 this.onFlickAnimationStart(); // #17822
1339 this.scrollTo(to, false, node);
1340 this.onFlickAnimationEnd();
1341 }else{
1342 this.scrollScrollBarTo(to);
1343 }
1344 }
1345 },
1346
1347 resetScrollBar: function(){
1348 // summary:
1349 // Resets the scroll bar length, position, etc.
1350 var f = function(wrapper, bar, d, c, hd, v){
1351 if(!bar){ return; }
1352 var props = {};
1353 props[v ? "top" : "left"] = hd + 4 + "px"; // +4 is for top or left margin
1354 var t = (d - 8) <= 0 ? 1 : d - 8;
1355 props[v ? "height" : "width"] = t + "px";
1356 domStyle.set(wrapper, props);
1357 var l = Math.round(d * d / c); // scroll bar length
1358 l = Math.min(Math.max(l - 8, 5), t); // -8 is for margin for both ends
1359 bar.style[v ? "height" : "width"] = l + "px";
1360 domStyle.set(bar, {"opacity": 0.6});
1361 };
1362 var dim = this.getDim();
1363 f(this._scrollBarWrapperV, this._scrollBarV, dim.d.h, dim.c.h, this.fixedHeaderHeight, true);
1364 f(this._scrollBarWrapperH, this._scrollBarH, dim.d.w, dim.c.w, 0);
1365 this.createMask();
1366 },
1367
1368 createMask: function(){
1369 // summary:
1370 // Creates a mask for a scroll bar edge.
1371 // description:
1372 // This function creates a mask that hides corners of one scroll
1373 // bar edge to make it round edge. The other side of the edge is
1374 // always visible and round shaped with the border-radius style.
1375 if(!(has("mask-image"))){ return; }
1376 if(this._scrollBarWrapperV){
1377 var h = this._scrollBarWrapperV.offsetHeight;
1378 maskUtils.createRoundMask(this._scrollBarWrapperV, 0, 0, 0, 0, 5, h, 2, 2, 0.5);
1379 }
1380 if(this._scrollBarWrapperH){
1381 var w = this._scrollBarWrapperH.offsetWidth;
1382 maskUtils.createRoundMask(this._scrollBarWrapperH, 0, 0, 0, 0, w, 5, 2, 2, 0.5);
1383 }
1384 },
1385
1386 flashScrollBar: function(){
1387 // summary:
1388 // Shows the scroll bar instantly.
1389 // description:
1390 // This function shows the scroll bar, and then hides it 300ms
1391 // later. This is used to show the scroll bar to the user for a
1392 // short period of time when a hidden view is revealed.
1393 if(this.disableFlashScrollBar || !this.domNode){ return; }
1394 this._dim = this.getDim();
1395 if(this._dim.d.h <= 0){ return; } // dom is not ready
1396 this.showScrollBar();
1397 var _this = this;
1398 setTimeout(function(){
1399 _this.hideScrollBar();
1400 }, 300);
1401 },
1402
1403 addCover: function(){
1404 // summary:
1405 // Adds the transparent DIV cover.
1406 // description:
1407 // The cover is to prevent DOM events from affecting the child
1408 // widgets such as a list widget. Without the cover, for example,
1409 // child widgets may receive a click event and respond to it
1410 // unexpectedly when the user flicks the screen to scroll.
1411 // Note that only the desktop browsers need the cover.
1412
1413 if(!has("touch") && !(has("pointer-events") || has("MSPointer")) && !this.noCover){
1414 if(!dm._cover){
1415 dm._cover = domConstruct.create("div", null, win.doc.body);
1416 dm._cover.className = "mblScrollableCover";
1417 domStyle.set(dm._cover, {
1418 backgroundColor: "#ffff00",
1419 opacity: 0,
1420 position: "absolute",
1421 top: "0px",
1422 left: "0px",
1423 width: "100%",
1424 height: "100%",
1425 zIndex: 2147483647 // max of signed 32-bit integer
1426 });
1427 this._ch.push(connect.connect(dm._cover, touch.press, this, "onTouchEnd"));
1428 }else{
1429 dm._cover.style.display = "";
1430 }
1431 this.setSelectable(dm._cover, false);
1432 this.setSelectable(this.domNode, false);
1433 }
1434 },
1435
1436 removeCover: function(){
1437 // summary:
1438 // Removes the transparent DIV cover.
1439
1440 if(!has("touch") && dm._cover){
1441 dm._cover.style.display = "none";
1442 this.setSelectable(dm._cover, true);
1443 this.setSelectable(this.domNode, true);
1444 }
1445 },
1446
1447 setKeyframes: function(/*Object*/from, /*Object*/to, /*Number*/idx, entering){
1448 // summary:
1449 // Programmatically sets key frames for the scroll animation.
1450
1451 if(!dm._rule){
1452 dm._rule = [];
1453 }
1454
1455 var keyframeId = idx + (entering ? "-in" : "-out");
1456
1457 // idx: 0:scrollbarV, 1:scrollbarH, 2:content
1458 if(!(dm._rule[keyframeId])){
1459 var node = domConstruct.create("style", null, win.doc.getElementsByTagName("head")[0]);
1460 node.textContent =
1461 ".mblScrollableScrollTo"+keyframeId+"{" + css3.name("animation-name", true) + ": scrollableViewScroll"+keyframeId+";}"+
1462 "@" + css3.name("keyframes", true) + " scrollableViewScroll"+keyframeId+"{}";
1463 dm._rule[keyframeId] = node.sheet.cssRules[1];
1464 }
1465 var rule = dm._rule[keyframeId];
1466 if(rule){
1467 if(from){
1468 rule.deleteRule(has("webkit")?"from":0);
1469 (rule.insertRule||rule.appendRule).call(rule, "from { " + css3.name("transform", true) + ": "+this.makeTranslateStr(from)+"; }");
1470 }
1471 if(to){
1472 if(to.x === undefined){ to.x = from.x; }
1473 if(to.y === undefined){ to.y = from.y; }
1474 rule.deleteRule(has("webkit")?"to":1);
1475 (rule.insertRule||rule.appendRule).call(rule, "to { " + css3.name("transform", true) + ": "+this.makeTranslateStr(to)+"; }");
1476 }
1477 }
1478 return "mblScrollableScrollTo"+keyframeId;
1479 },
1480
1481 setSelectable: function(/*DomNode*/node, /*Boolean*/selectable){
1482 // summary:
1483 // Sets the given node as selectable or unselectable.
1484
1485 // dojo.setSelectable has dependency on dojo.query. Redefine our own.
1486 node.style.KhtmlUserSelect = selectable ? "auto" : "none";
1487 node.style.MozUserSelect = selectable ? "" : "none";
1488 node.onselectstart = selectable ? null : function(){return false;};
1489 if(has("ie")){
1490 node.unselectable = selectable ? "" : "on";
1491 var nodes = node.getElementsByTagName("*");
1492 for(var i = 0; i < nodes.length; i++){
1493 nodes[i].unselectable = selectable ? "" : "on";
1494 }
1495 }
1496 }
1497 });
1498 Scrollable = has("dojo-bidi") ? declare("dojox.mobile.Scrollable", [Scrollable, BidiScrollable]) : Scrollable;
1499 lang.setObject("dojox.mobile.scrollable", Scrollable);
1500
1501 return Scrollable;
1502 });