Dashboard sipadu mbip
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

dd-scroll-debug.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('dd-scroll', function(Y) {
  9. /**
  10. * Base scroller class used to create the Plugin.DDNodeScroll and Plugin.DDWinScroll.
  11. * This class should not be called on it's own, it's designed to be a plugin.
  12. * @module dd
  13. * @submodule dd-scroll
  14. */
  15. /**
  16. * Base scroller class used to create the Plugin.DDNodeScroll and Plugin.DDWinScroll.
  17. * This class should not be called on it's own, it's designed to be a plugin.
  18. * @class Scroll
  19. * @extends Base
  20. * @namespace DD
  21. * @constructor
  22. */
  23. var S = function() {
  24. S.superclass.constructor.apply(this, arguments);
  25. },
  26. WS, NS,
  27. HOST = 'host',
  28. BUFFER = 'buffer',
  29. PARENT_SCROLL = 'parentScroll',
  30. WINDOW_SCROLL = 'windowScroll',
  31. SCROLL_TOP = 'scrollTop',
  32. SCROLL_LEFT = 'scrollLeft',
  33. OFFSET_WIDTH = 'offsetWidth',
  34. OFFSET_HEIGHT = 'offsetHeight';
  35. S.ATTRS = {
  36. /**
  37. * @attribute parentScroll
  38. * @description Internal config option to hold the node that we are scrolling. Should not be set by the developer.
  39. * @type Node
  40. */
  41. parentScroll: {
  42. value: false,
  43. setter: function(node) {
  44. if (node) {
  45. return node;
  46. }
  47. return false;
  48. }
  49. },
  50. /**
  51. * @attribute buffer
  52. * @description The number of pixels from the edge of the screen to turn on scrolling. Default: 30
  53. * @type Number
  54. */
  55. buffer: {
  56. value: 30,
  57. validator: Y.Lang.isNumber
  58. },
  59. /**
  60. * @attribute scrollDelay
  61. * @description The number of milliseconds delay to pass to the auto scroller. Default: 235
  62. * @type Number
  63. */
  64. scrollDelay: {
  65. value: 235,
  66. validator: Y.Lang.isNumber
  67. },
  68. /**
  69. * @attribute host
  70. * @description The host we are plugged into.
  71. * @type Object
  72. */
  73. host: {
  74. value: null
  75. },
  76. /**
  77. * @attribute windowScroll
  78. * @description Turn on window scroll support, default: false
  79. * @type Boolean
  80. */
  81. windowScroll: {
  82. value: false,
  83. validator: Y.Lang.isBoolean
  84. },
  85. /**
  86. * @attribute vertical
  87. * @description Allow vertical scrolling, default: true.
  88. * @type Boolean
  89. */
  90. vertical: {
  91. value: true,
  92. validator: Y.Lang.isBoolean
  93. },
  94. /**
  95. * @attribute horizontal
  96. * @description Allow horizontal scrolling, default: true.
  97. * @type Boolean
  98. */
  99. horizontal: {
  100. value: true,
  101. validator: Y.Lang.isBoolean
  102. }
  103. };
  104. Y.extend(S, Y.Base, {
  105. /**
  106. * @private
  107. * @property _scrolling
  108. * @description Tells if we are actively scrolling or not.
  109. * @type Boolean
  110. */
  111. _scrolling: null,
  112. /**
  113. * @private
  114. * @property _vpRegionCache
  115. * @description Cache of the Viewport dims.
  116. * @type Object
  117. */
  118. _vpRegionCache: null,
  119. /**
  120. * @private
  121. * @property _dimCache
  122. * @description Cache of the dragNode dims.
  123. * @type Object
  124. */
  125. _dimCache: null,
  126. /**
  127. * @private
  128. * @property _scrollTimer
  129. * @description Holder for the Timer object returned from Y.later.
  130. * @type {Y.later}
  131. */
  132. _scrollTimer: null,
  133. /**
  134. * @private
  135. * @method _getVPRegion
  136. * @description Sets the _vpRegionCache property with an Object containing the dims from the viewport.
  137. */
  138. _getVPRegion: function() {
  139. var r = {},
  140. n = this.get(PARENT_SCROLL),
  141. b = this.get(BUFFER),
  142. ws = this.get(WINDOW_SCROLL),
  143. xy = ((ws) ? [] : n.getXY()),
  144. w = ((ws) ? 'winWidth' : OFFSET_WIDTH),
  145. h = ((ws) ? 'winHeight' : OFFSET_HEIGHT),
  146. t = ((ws) ? n.get(SCROLL_TOP) : xy[1]),
  147. l = ((ws) ? n.get(SCROLL_LEFT) : xy[0]);
  148. r = {
  149. top: t + b,
  150. right: (n.get(w) + l) - b,
  151. bottom: (n.get(h) + t) - b,
  152. left: l + b
  153. };
  154. this._vpRegionCache = r;
  155. return r;
  156. },
  157. initializer: function() {
  158. var h = this.get(HOST);
  159. h.after('drag:start', Y.bind(this.start, this));
  160. h.after('drag:end', Y.bind(this.end, this));
  161. h.on('drag:align', Y.bind(this.align, this));
  162. //TODO - This doesn't work yet??
  163. Y.one('win').on('scroll', Y.bind(function() {
  164. this._vpRegionCache = null;
  165. }, this));
  166. },
  167. /**
  168. * @private
  169. * @method _checkWinScroll
  170. * @description Check to see if we need to fire the scroll timer. If scroll timer is running this will scroll the window.
  171. * @param {Boolean} move Should we move the window. From Y.later
  172. */
  173. _checkWinScroll: function(move) {
  174. var r = this._getVPRegion(),
  175. ho = this.get(HOST),
  176. ws = this.get(WINDOW_SCROLL),
  177. xy = ho.lastXY,
  178. scroll = false,
  179. b = this.get(BUFFER),
  180. win = this.get(PARENT_SCROLL),
  181. sTop = win.get(SCROLL_TOP),
  182. sLeft = win.get(SCROLL_LEFT),
  183. w = this._dimCache.w,
  184. h = this._dimCache.h,
  185. bottom = xy[1] + h,
  186. top = xy[1],
  187. right = xy[0] + w,
  188. left = xy[0],
  189. nt = top,
  190. nl = left,
  191. st = sTop,
  192. sl = sLeft;
  193. if (this.get('horizontal')) {
  194. if (left <= r.left) {
  195. scroll = true;
  196. nl = xy[0] - ((ws) ? b : 0);
  197. sl = sLeft - b;
  198. }
  199. if (right >= r.right) {
  200. scroll = true;
  201. nl = xy[0] + ((ws) ? b : 0);
  202. sl = sLeft + b;
  203. }
  204. }
  205. if (this.get('vertical')) {
  206. if (bottom >= r.bottom) {
  207. scroll = true;
  208. nt = xy[1] + ((ws) ? b : 0);
  209. st = sTop + b;
  210. }
  211. if (top <= r.top) {
  212. scroll = true;
  213. nt = xy[1] - ((ws) ? b : 0);
  214. st = sTop - b;
  215. }
  216. }
  217. if (st < 0) {
  218. st = 0;
  219. nt = xy[1];
  220. }
  221. if (sl < 0) {
  222. sl = 0;
  223. nl = xy[0];
  224. }
  225. if (nt < 0) {
  226. nt = xy[1];
  227. }
  228. if (nl < 0) {
  229. nl = xy[0];
  230. }
  231. if (move) {
  232. ho.actXY = [nl, nt];
  233. ho._moveNode({ node: win, top: st, left: sl});
  234. if (!st && !sl) {
  235. this._cancelScroll();
  236. }
  237. } else {
  238. if (scroll) {
  239. this._initScroll();
  240. } else {
  241. this._cancelScroll();
  242. }
  243. }
  244. },
  245. /**
  246. * @private
  247. * @method _initScroll
  248. * @description Cancel a previous scroll timer and init a new one.
  249. */
  250. _initScroll: function() {
  251. this._cancelScroll();
  252. this._scrollTimer = Y.Lang.later(this.get('scrollDelay'), this, this._checkWinScroll, [true], true);
  253. },
  254. /**
  255. * @private
  256. * @method _cancelScroll
  257. * @description Cancel a currently running scroll timer.
  258. */
  259. _cancelScroll: function() {
  260. this._scrolling = false;
  261. if (this._scrollTimer) {
  262. this._scrollTimer.cancel();
  263. delete this._scrollTimer;
  264. }
  265. },
  266. /**
  267. * @method align
  268. * @description Called from the drag:align event to determine if we need to scroll.
  269. */
  270. align: function(e) {
  271. if (this._scrolling) {
  272. this._cancelScroll();
  273. e.preventDefault();
  274. }
  275. if (!this._scrolling) {
  276. this._checkWinScroll();
  277. }
  278. },
  279. /**
  280. * @private
  281. * @method _setDimCache
  282. * @description Set the cache of the dragNode dims.
  283. */
  284. _setDimCache: function() {
  285. var node = this.get(HOST).get('dragNode');
  286. this._dimCache = {
  287. h: node.get(OFFSET_HEIGHT),
  288. w: node.get(OFFSET_WIDTH)
  289. };
  290. },
  291. /**
  292. * @method start
  293. * @description Called from the drag:start event
  294. */
  295. start: function() {
  296. this._setDimCache();
  297. },
  298. /**
  299. * @method end
  300. * @description Called from the drag:end event
  301. */
  302. end: function(xy) {
  303. this._dimCache = null;
  304. this._cancelScroll();
  305. },
  306. /**
  307. * @method toString
  308. * @description General toString method for logging
  309. * @return String name for the object
  310. */
  311. toString: function() {
  312. return S.NAME + ' #' + this.get('node').get('id');
  313. }
  314. });
  315. Y.namespace('Plugin');
  316. /**
  317. * Extends the Scroll class to make the window scroll while dragging.
  318. * @class DDWindowScroll
  319. * @extends Scroll
  320. * @namespace Plugin
  321. * @constructor
  322. */
  323. WS = function() {
  324. WS.superclass.constructor.apply(this, arguments);
  325. };
  326. WS.ATTRS = Y.merge(S.ATTRS, {
  327. /**
  328. * @attribute windowScroll
  329. * @description Turn on window scroll support, default: true
  330. * @type Boolean
  331. */
  332. windowScroll: {
  333. value: true,
  334. setter: function(scroll) {
  335. if (scroll) {
  336. this.set(PARENT_SCROLL, Y.one('win'));
  337. }
  338. return scroll;
  339. }
  340. }
  341. });
  342. Y.extend(WS, S, {
  343. //Shouldn't have To Do this..
  344. initializer: function() {
  345. this.set('windowScroll', this.get('windowScroll'));
  346. }
  347. });
  348. /**
  349. * @property NS
  350. * @default winscroll
  351. * @readonly
  352. * @protected
  353. * @static
  354. * @description The Scroll instance will be placed on the Drag instance under the winscroll namespace.
  355. * @type {String}
  356. */
  357. WS.NAME = WS.NS = 'winscroll';
  358. Y.Plugin.DDWinScroll = WS;
  359. /**
  360. * Extends the Scroll class to make a parent node scroll while dragging.
  361. * @class DDNodeScroll
  362. * @extends Scroll
  363. * @namespace Plugin
  364. * @constructor
  365. */
  366. NS = function() {
  367. NS.superclass.constructor.apply(this, arguments);
  368. };
  369. NS.ATTRS = Y.merge(S.ATTRS, {
  370. /**
  371. * @attribute node
  372. * @description The node we want to scroll. Used to set the internal parentScroll attribute.
  373. * @type Node
  374. */
  375. node: {
  376. value: false,
  377. setter: function(node) {
  378. var n = Y.one(node);
  379. if (!n) {
  380. if (node !== false) {
  381. Y.error('DDNodeScroll: Invalid Node Given: ' + node);
  382. }
  383. } else {
  384. this.set(PARENT_SCROLL, n);
  385. }
  386. return n;
  387. }
  388. }
  389. });
  390. Y.extend(NS, S, {
  391. //Shouldn't have To Do this..
  392. initializer: function() {
  393. this.set('node', this.get('node'));
  394. }
  395. });
  396. /**
  397. * @property NS
  398. * @default nodescroll
  399. * @readonly
  400. * @protected
  401. * @static
  402. * @description The NodeScroll instance will be placed on the Drag instance under the nodescroll namespace.
  403. * @type {String}
  404. */
  405. NS.NAME = NS.NS = 'nodescroll';
  406. Y.Plugin.DDNodeScroll = NS;
  407. Y.DD.Scroll = S;
  408. }, '3.4.0' ,{skinnable:false, optional:['dd-proxy'], requires:['dd-drag']});