Преглед на файлове

move around scroller stuff

Adam Shaw преди 7 години
родител
ревизия
e51822333d
променени са 4 файла, в които са добавени 130 реда и са изтрити 139 реда
  1. 2 5
      src/common/OffsetCoordCache.ts
  2. 116 0
      src/common/scroll-controller.ts
  3. 9 119
      src/common/scroll-geom-cache.ts
  4. 3 15
      src/dnd/AutoScroller.ts

+ 2 - 5
src/common/OffsetCoordCache.ts

@@ -1,6 +1,6 @@
 import { getClippingParents, computeRect } from '../util/dom-geom'
 import { pointInsideRect } from '../util/geom'
-import { ElScrollControllerCache, ElScrollController } from '../dnd/scroll'
+import { ElScrollControllerCache } from '../common/scroll-geom-cache'
 
 export default class OffsetCoordCache { // TODO: rename to OffsetTracker?
 
@@ -14,10 +14,7 @@ export default class OffsetCoordCache { // TODO: rename to OffsetTracker?
     this.originOffsetTop = rect.top
 
     this.scrollCaches = getClippingParents(el).map(function(el) {
-      return new ElScrollControllerCache(
-        new ElScrollController(el),
-        true // listens to element for scrolling
-      )
+      return new ElScrollControllerCache(el, true) // listen=true
     })
   }
 

+ 116 - 0
src/common/scroll-controller.ts

@@ -0,0 +1,116 @@
+
+export abstract class ScrollController {
+
+  abstract getScrollTop(): number
+  abstract getScrollLeft(): number
+  abstract setScrollTop(number): void
+  abstract setScrollLeft(number): void
+  abstract getClientWidth(): number
+  abstract getClientHeight(): number
+  abstract getScrollWidth(): number
+  abstract getScrollHeight(): number
+
+  getMaxScrollTop() {
+    return this.getScrollHeight() - this.getClientHeight()
+  }
+
+  getMaxScrollLeft() {
+    return this.getScrollWidth() - this.getClientWidth()
+  }
+
+  canScrollUp() {
+    return this.getScrollTop() > 0
+  }
+
+  canScrollDown() {
+    return this.getScrollTop() < this.getMaxScrollTop()
+  }
+
+  canScrollLeft() {
+    return this.getScrollLeft() > 0
+  }
+
+  canScrollRight() {
+    return this.getScrollLeft() < this.getMaxScrollLeft()
+  }
+
+}
+
+export class ElScrollController extends ScrollController {
+
+  el: HTMLElement
+
+  constructor(el: HTMLElement) {
+    super()
+    this.el = el
+  }
+
+  getScrollTop() {
+    return this.el.scrollTop
+  }
+
+  getScrollLeft() {
+    return this.el.scrollLeft
+  }
+
+  setScrollTop(n: number) {
+    this.el.scrollTop = n
+  }
+
+  setScrollLeft(n: number) {
+    this.el.scrollLeft = n
+  }
+
+  getScrollWidth() {
+    return this.el.scrollWidth
+  }
+
+  getScrollHeight() {
+    return this.el.scrollHeight
+  }
+
+  getClientHeight() {
+    return this.el.clientHeight
+  }
+
+  getClientWidth() {
+    return this.el.clientWidth
+  }
+
+}
+
+export class WindowScrollController extends ScrollController {
+
+  getScrollTop() {
+    return window.scrollY
+  }
+
+  getScrollLeft() {
+    return window.scrollX
+  }
+
+  setScrollTop(n: number) {
+    window.scroll(window.scrollX, n)
+  }
+
+  setScrollLeft(n: number) {
+    window.scroll(n, window.scrollY)
+  }
+
+  getScrollWidth() {
+    return document.documentElement.scrollWidth
+  }
+
+  getScrollHeight() {
+    return document.documentElement.scrollHeight
+  }
+
+  getClientHeight() {
+    return document.documentElement.clientHeight
+  }
+
+  getClientWidth() {
+    return document.documentElement.clientWidth
+  }
+
+}

+ 9 - 119
src/dnd/scroll.ts → src/common/scroll-geom-cache.ts

@@ -1,125 +1,7 @@
 import { Rect } from '../util/geom'
 import { computeRect } from '../util/dom-geom'
+import { ScrollController, ElScrollController, WindowScrollController } from './scroll-controller'
 
-// TODO: join with RTL scroller normalization utils
-
-export abstract class ScrollController {
-
-  abstract getScrollTop(): number
-  abstract getScrollLeft(): number
-  abstract setScrollTop(number): void
-  abstract setScrollLeft(number): void
-  abstract getClientWidth(): number
-  abstract getClientHeight(): number
-  abstract getScrollWidth(): number
-  abstract getScrollHeight(): number
-
-  getMaxScrollTop() {
-    return this.getScrollHeight() - this.getClientHeight()
-  }
-
-  getMaxScrollLeft() {
-    return this.getScrollWidth() - this.getClientWidth()
-  }
-
-  canScrollUp() {
-    return this.getScrollTop() > 0
-  }
-
-  canScrollDown() {
-    return this.getScrollTop() < this.getMaxScrollTop()
-  }
-
-  canScrollLeft() {
-    return this.getScrollLeft() > 0
-  }
-
-  canScrollRight() {
-    return this.getScrollLeft() < this.getMaxScrollLeft()
-  }
-
-}
-
-export class ElScrollController extends ScrollController {
-
-  el: HTMLElement
-
-  constructor(el: HTMLElement) {
-    super()
-    this.el = el
-  }
-
-  getScrollTop() {
-    return this.el.scrollTop
-  }
-
-  getScrollLeft() {
-    return this.el.scrollLeft
-  }
-
-  setScrollTop(n: number) {
-    this.el.scrollTop = n
-  }
-
-  setScrollLeft(n: number) {
-    this.el.scrollLeft = n
-  }
-
-  getScrollWidth() {
-    return this.el.scrollWidth
-  }
-
-  getScrollHeight() {
-    return this.el.scrollHeight
-  }
-
-  getClientHeight() {
-    return this.el.clientHeight
-  }
-
-  getClientWidth() {
-    return this.el.clientWidth
-  }
-
-}
-
-export class WindowScrollController extends ScrollController {
-
-  getScrollTop() {
-    return window.scrollY
-  }
-
-  getScrollLeft() {
-    return window.scrollX
-  }
-
-  setScrollTop(n: number) {
-    window.scroll(window.scrollX, n)
-  }
-
-  setScrollLeft(n: number) {
-    window.scroll(n, window.scrollY)
-  }
-
-  getScrollWidth() {
-    return document.documentElement.scrollWidth
-  }
-
-  getScrollHeight() {
-    return document.documentElement.scrollHeight
-  }
-
-  getClientHeight() {
-    return document.documentElement.clientHeight
-  }
-
-  getClientWidth() {
-    return document.documentElement.clientWidth
-  }
-
-}
-
-// TODO: more of a "dimensions" cache
 export abstract class ScrollControllerCache extends ScrollController {
 
   rect: Rect
@@ -220,6 +102,10 @@ export class ElScrollControllerCache extends ScrollControllerCache {
 
   scrollController: ElScrollController
 
+  constructor(el: HTMLElement, doesListening) {
+    super(new ElScrollController(el), doesListening)
+  }
+
   getEventTarget(): EventTarget {
     return this.scrollController.el
   }
@@ -234,6 +120,10 @@ export class WindowScrollControllerCache extends ScrollControllerCache {
 
   scrollController: WindowScrollController
 
+  constructor(doesListening) {
+    super(new WindowScrollController(), doesListening)
+  }
+
   getEventTarget(): EventTarget {
     return window
   }

+ 3 - 15
src/dnd/AutoScroller.ts

@@ -1,10 +1,4 @@
-import {
-  ScrollControllerCache,
-  ElScrollControllerCache,
-  ElScrollController,
-  WindowScrollControllerCache,
-  WindowScrollController
-} from './scroll'
+import { ScrollControllerCache, ElScrollControllerCache, WindowScrollControllerCache } from '../common/scroll-geom-cache'
 
 interface Side { // rename to Edge?
   controller: ScrollControllerCache
@@ -171,15 +165,9 @@ export default class AutoScroller {
   private buildControllers() {
     return this.queryScrollerEls().map((el) => {
       if (el === window) {
-        return new WindowScrollControllerCache(
-          new WindowScrollController(),
-          false // don't listen to user-generated scrolls
-        )
+        return new WindowScrollControllerCache(false) // don't listen to user-generated scrolls
       } else {
-        return new ElScrollControllerCache(
-          new ElScrollController(el as HTMLElement),
-          false // don't listen to user-generated scrolls
-        )
+        return new ElScrollControllerCache(el, false) // don't listen to user-generated scrolls
       }
     })
   }