FlippableView.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mobile.FlippableView"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.FlippableView"] = true;
  8. dojo.provide("dojox.mobile.FlippableView");
  9. dojo.require("dijit._WidgetBase");
  10. dojo.require("dojox.mobile");
  11. dojo.require("dojox.mobile._ScrollableMixin");
  12. // summary:
  13. // A container that can be flipped horizontally.
  14. // description:
  15. // FlippableView allows the user to swipe the screen left or right to
  16. // flip between the views.
  17. // When FlippableView is flipped, it finds an adjacent FlippableView,
  18. // and opens it.
  19. dojo.declare(
  20. "dojox.mobile.FlippableView",
  21. [dojox.mobile.View, dojox.mobile._ScrollableMixin],
  22. {
  23. scrollDir: "f",
  24. weight: 1.2,
  25. buildRendering: function(){
  26. this.inherited(arguments);
  27. dojo.addClass(this.domNode, "mblFlippableView");
  28. this.containerNode = this.domNode;
  29. this.containerNode.style.position = "absolute";
  30. },
  31. onTouchStart: function(e){
  32. var nextView = this._nextView(this.domNode);
  33. if(nextView){
  34. nextView.stopAnimation();
  35. }
  36. var prevView = this._previousView(this.domNode);
  37. if(prevView){
  38. prevView.stopAnimation();
  39. }
  40. this.inherited(arguments);
  41. },
  42. _nextView: function(node){
  43. for(var n = node.nextSibling; n; n = n.nextSibling){
  44. if(n.nodeType == 1){ return dijit.byNode(n); }
  45. }
  46. return null;
  47. },
  48. _previousView: function(node){
  49. for(var n = node.previousSibling; n; n = n.previousSibling){
  50. if(n.nodeType == 1){ return dijit.byNode(n); }
  51. }
  52. return null;
  53. },
  54. scrollTo: function(/*Object*/to){
  55. if(!this._beingFlipped){
  56. var newView, x;
  57. if(to.x < 0){
  58. newView = this._nextView(this.domNode);
  59. x = to.x + this.domNode.offsetWidth;
  60. }else{
  61. newView = this._previousView(this.domNode);
  62. x = to.x - this.domNode.offsetWidth;
  63. }
  64. if(newView){
  65. newView.domNode.style.display = "";
  66. newView._beingFlipped = true;
  67. newView.scrollTo({x:x});
  68. newView._beingFlipped = false;
  69. }
  70. }
  71. this.inherited(arguments);
  72. },
  73. slideTo: function(/*Object*/to, /*Number*/duration, /*String*/easing){
  74. if(!this._beingFlipped){
  75. var w = this.domNode.offsetWidth;
  76. var pos = this.getPos();
  77. var newView, newX;
  78. if(pos.x < 0){ // moving to left
  79. newView = this._nextView(this.domNode);
  80. if(pos.x < -w/4){ // slide to next
  81. if(newView){
  82. to.x = -w;
  83. newX = 0;
  84. }
  85. }else{ // go back
  86. if(newView){
  87. newX = w;
  88. }
  89. }
  90. }else{ // moving to right
  91. newView = this._previousView(this.domNode);
  92. if(pos.x > w/4){ // slide to previous
  93. if(newView){
  94. to.x = w;
  95. newX = 0;
  96. }
  97. }else{ // go back
  98. if(newView){
  99. newX = -w;
  100. }
  101. }
  102. }
  103. if(newView){
  104. newView._beingFlipped = true;
  105. newView.slideTo({x:newX}, duration, easing);
  106. newView._beingFlipped = false;
  107. if(newX === 0){ // moving to another view
  108. dojox.mobile.currentView = newView;
  109. }
  110. }
  111. }
  112. this.inherited(arguments);
  113. },
  114. onFlickAnimationEnd: function(e){
  115. // Hide all the views other than the currently showing one.
  116. // Otherwise, when the orientation is changed, other views
  117. // may appear unexpectedly.
  118. var children = this.domNode.parentNode.childNodes;
  119. for(var i = 0; i < children.length; i++){
  120. var c = children[i];
  121. if(c.nodeType == 1 && c != dojox.mobile.currentView.domNode){
  122. c.style.display = "none";
  123. }
  124. }
  125. this.inherited(arguments);
  126. }
  127. });
  128. }