Explorar o código

more cleaning

Adam Shaw %!s(int64=7) %!d(string=hai) anos
pai
achega
0a370dcaed

+ 5 - 7
src/interactions-external/Draggable.ts

@@ -1,18 +1,16 @@
 import FeaturefulElementDragging from '../dnd/FeaturefulElementDragging'
 import ExternalElementDragging from './ExternalElementDragging'
+import { DragMetaInput } from '../structs/drag-meta'
 
-export interface ExternalDraggableEventSettings {
-  el: HTMLElement
-  event?: any
-}
+// TODO: somehow accept settings for FeaturefulElementDragging
 
 export default class ExternalDraggableEvent {
 
   dragging: FeaturefulElementDragging
 
-  constructor(settings: ExternalDraggableEventSettings) {
-    this.dragging = new FeaturefulElementDragging(settings.el)
-    new ExternalElementDragging(this.dragging, settings.event)
+  constructor(el: HTMLElement, dragMeta?: DragMetaInput) {
+    this.dragging = new FeaturefulElementDragging(el)
+    new ExternalElementDragging(this.dragging, dragMeta)
   }
 
   destroy() {

+ 17 - 9
src/interactions-external/DumbElementDragging.ts

@@ -1,7 +1,14 @@
 import PointerDragging, { PointerDragEvent } from '../dnd/PointerDragging'
 import ElementDragging from '../dnd/ElementDragging'
 
-/* needs to fire events:
+// TODO: rename file InferredElementDragging
+
+export interface DumbElementDraggingSettings {
+  itemSelector?: string
+  mirrorSelector?: string
+}
+
+/* emits:
 - pointerdown
 - dragstart
 - dragmove
@@ -10,21 +17,21 @@ import ElementDragging from '../dnd/ElementDragging'
 */
 export default class DumbElementDragging extends ElementDragging {
 
-  options: any
   pointer: PointerDragging
-  currentMirrorEl: HTMLElement
   shouldIgnoreMove: boolean = false
+  mirrorSelector: string
+  currentMirrorEl: HTMLElement | null = null
 
-  constructor(options) {
+  constructor(options: DumbElementDraggingSettings) {
     super()
 
-    this.options = options
-
     let pointer = this.pointer = new PointerDragging(document)
-    pointer.selector = options.itemSelector || '[data-event]' // TODO: better
+    pointer.selector = options.itemSelector || '[data-event]'
     pointer.emitter.on('pointerdown', this.handlePointerDown)
     pointer.emitter.on('pointermove', this.handlePointerMove)
     pointer.emitter.on('pointerup', this.handlePointerUp)
+
+    this.mirrorSelector = options.mirrorSelector || ''
   }
 
   destroy() {
@@ -66,8 +73,9 @@ export default class DumbElementDragging extends ElementDragging {
         this.currentMirrorEl = null
       }
     } else {
-      let selector = this.options.mirrorSelector
-      let mirrorEl = selector ? document.querySelector(selector) as HTMLElement : null
+      let mirrorEl = this.mirrorSelector ?
+        document.querySelector(this.mirrorSelector) as HTMLElement :
+        null
 
       if (mirrorEl) {
         this.currentMirrorEl = mirrorEl

+ 2 - 2
src/interactions-external/ExternalElementDragging.ts

@@ -8,7 +8,7 @@ import * as externalHooks from '../exports'
 import { DateSpan } from '../reducers/date-span'
 import Calendar from '../Calendar'
 import { EventInteractionState } from '../reducers/event-interaction'
-import { DragMeta, parseDragMeta } from '../structs/drag-meta'
+import { DragMetaInput, DragMeta, parseDragMeta } from '../structs/drag-meta'
 
 export interface EventRes { // TODO: relate this to EventRenderRange?
   def: EventDef
@@ -28,7 +28,7 @@ export default class ExternalElementDragging {
   explicitDragMeta: DragMeta | null = null
   dragMeta: DragMeta | null = null
 
-  constructor(dragging: ElementDragging, rawEventDragData?: DragMeta) {
+  constructor(dragging: ElementDragging, rawEventDragData?: DragMetaInput) {
 
     let hitDragging = this.hitDragging = new HitDragging(dragging, browserContext.componentHash)
     hitDragging.requireInitial = false // will start outside of a component

+ 21 - 12
src/interactions-external/GenericDragging.ts

@@ -1,24 +1,33 @@
 import ExternalElementDragging from './ExternalElementDragging'
-import DumbElementDragging from './DumbElementDragging'
+import DumbElementDragging, { DumbElementDraggingSettings } from './DumbElementDragging'
 
-let externalDragging
+// TODO: change file
 
-// TODO: protect against multiple enables/disables
+export class GenericDragging {
 
-export default {
+  dragging: DumbElementDragging | null = null
+  externalDragging: ExternalElementDragging | null = null
+  isEnabled: boolean = false
 
-  enable(options) {
-    let dragging = new DumbElementDragging(options || {})
-    externalDragging = new ExternalElementDragging(dragging)
-  },
+  enable(options?: DumbElementDraggingSettings) {
+    if (!this.isEnabled) {
+      this.isEnabled = true
+
+      new ExternalElementDragging(
+        this.dragging = new DumbElementDragging(options || {})
+      )
+    }
+  }
 
   disable() {
-    if (externalDragging) {
-      externalDragging.destroy()
-      externalDragging = null
+    if (this.isEnabled) {
+      this.isEnabled = false
+
+      this.dragging!.destroy()
+      this.dragging = null
     }
   }
 
 }
 
-
+export default new GenericDragging()

+ 11 - 2
src/structs/drag-meta.ts

@@ -1,6 +1,15 @@
-import { createDuration, Duration } from '../datelib/duration'
+import { createDuration, Duration, DurationInput } from '../datelib/duration'
 import { refineProps } from '../reducers/utils'
 
+export interface DragMetaInput {
+  time?: DurationInput
+  duration?: DurationInput
+  create?: boolean
+  stick?: boolean
+  [extendedPropName: string]: any
+  // TODO: somehow join with EventInput, but minus datetime props
+}
+
 export interface DragMeta {
   time: Duration | null
   duration: Duration | null
@@ -16,7 +25,7 @@ const DRAG_META_PROPS = {
   stick: Boolean
 }
 
-export function parseDragMeta(raw: object): DragMeta {
+export function parseDragMeta(raw: DragMetaInput): DragMeta {
   let leftoverProps = {}
   let refined = refineProps(raw, DRAG_META_PROPS, leftoverProps)