controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. define(function(require, exports, module) {
  2. /*
  3. wrapped scroll controller
  4. */
  5. "use strict";
  6. var Util = require('../util'),
  7. Base = require('../base');
  8. var Controller = function(cfg) {
  9. Controller.superclass.constructor.call(this, cfg);
  10. this.userConfig = Util.mix({}, cfg);
  11. this.init();
  12. }
  13. Util.extend(Controller, Base, {
  14. init: function() {
  15. var self = this;
  16. self.xscroll = self.userConfig.xscroll;
  17. },
  18. add: function(scroll, cfg) {
  19. var self = this;
  20. cfg = Util.extend({
  21. captureBounce: false,
  22. stopPropagation: true
  23. }, cfg)
  24. if (!self.__scrolls) {
  25. self.__scrolls = {};
  26. }
  27. if (scroll.guid && !self.__scrolls[scroll.guid]) {
  28. scroll.parentscroll = self.xscroll;
  29. self._bind(scroll);
  30. return self.__scrolls[scroll.guid] = scroll;
  31. }
  32. return;
  33. },
  34. remove: function(scroll) {
  35. var self = this;
  36. if (!scroll || !scroll.guid) return;
  37. var subscroll = self.__scrolls[scroll.guid];
  38. if (subscroll) {
  39. subscroll.parentscroll = null;
  40. self._unbind(scroll);
  41. subscroll = null;
  42. }
  43. },
  44. get: function(guid) {
  45. if (guid) {
  46. return this.__scrolls[guid];
  47. }
  48. return this.__scrolls;
  49. },
  50. _unbind: function(sub) {
  51. },
  52. _bind: function(sub) {
  53. var self = this,
  54. xscroll = self.xscroll;
  55. xscroll.renderTo.addEventListener("touchstart", function() {
  56. xscroll._resetLockConfig();
  57. });
  58. sub.renderTo.addEventListener("touchstart", function() {
  59. sub._resetLockConfig();
  60. });
  61. xscroll.on("panend", xscroll._resetLockConfig);
  62. sub.on("panend", sub._resetLockConfig);
  63. sub.on("panstart", function(e) {
  64. //vertical scroll enabled
  65. if (!sub.userConfig.lockY && !xscroll.userConfig.lockY) {
  66. //outside of boundry
  67. if (sub.isBoundryOut()) {
  68. xscroll.userConfig.lockY = true;
  69. return;
  70. }
  71. if (e.direction == 16 && sub.getBoundryOutTop() >= 0) {
  72. sub.userConfig.lockY = true;
  73. } else if (e.direction == 8 && sub.getBoundryOutTop() >= 0 && sub.getBoundryOutBottom() < 0) {
  74. xscroll.userConfig.lockY = true;
  75. }
  76. if (e.direction == 8 && sub.getBoundryOutBottom() >= 0) {
  77. sub.userConfig.lockY = true;
  78. } else if (e.direction == 16 && sub.getBoundryOutBottom() >= 0 && sub.getBoundryOutTop() < 0) {
  79. xscroll.userConfig.lockY = true;
  80. }
  81. if (sub.getBoundryOutTop() < 0 && sub.getBoundryOutBottom() < 0) {
  82. xscroll.userConfig.lockY = true;
  83. }
  84. }
  85. //horizontal scroll enabled
  86. if (!sub.userConfig.lockX && !xscroll.userConfig.lockX) {
  87. if (sub.isBoundryOut()) {
  88. xscroll.userConfig.lockX = true;
  89. return;
  90. }
  91. if (e.direction == 4 && sub.getBoundryOutLeft() >= 0) {
  92. sub.userConfig.lockX = true;
  93. } else if (e.direction == 2 && sub.getBoundryOutLeft() >= 0 && sub.getBoundryOutRight() < 0) {
  94. xscroll.userConfig.lockX = true;
  95. }
  96. if (e.direction == 2 && sub.getBoundryOutRight() >= 0) {
  97. sub.userConfig.lockX = true;
  98. } else if (e.direction == 4 && sub.getBoundryOutRight() >= 0 && sub.getBoundryOutLeft() < 0) {
  99. xscroll.userConfig.lockX = true;
  100. }
  101. if (sub.getBoundryOutLeft() < 0 && sub.getBoundryOutRight() < 0) {
  102. xscroll.userConfig.lockX = true;
  103. }
  104. }
  105. if (!sub.userConfig.lockX && xscroll.userConfig.lockX) {
  106. //pan x
  107. if (e.direction == 2 || e.direction == 4) {
  108. xscroll.userConfig.lockY = true;
  109. } else {
  110. sub.userConfig.lockX = true;
  111. }
  112. }
  113. if (!sub.userConfig.lockY && xscroll.userConfig.lockY) {
  114. //pan y
  115. if (e.direction == 8 || e.direction == 16) {
  116. xscroll.userConfig.lockX = true;
  117. } else {
  118. sub.userConfig.lockY = true;
  119. }
  120. }
  121. });
  122. }
  123. });
  124. if (typeof module == 'object' && module.exports) {
  125. module.exports = Controller;
  126. }
  127. /** ignored by jsdoc **/
  128. else {
  129. return Controller;
  130. }
  131. });