Sfoglia il codice sorgente

move jquery ui drag init to use triggers

Adam Shaw 7 anni fa
parent
commit
197799bd16
2 ha cambiato i file con 71 aggiunte e 45 eliminazioni
  1. 63 43
      src/Calendar.ts
  2. 8 2
      src/common/Mixin.ts

+ 63 - 43
src/Calendar.ts

@@ -37,6 +37,11 @@ export default class Calendar {
   static englishDefaults: any = englishDefaults
   static rtlDefaults: any = rtlDefaults
 
+  // global handler registry
+  static on: EmitterInterface['on']
+  static off: EmitterInterface['off']
+  static trigger: EmitterInterface['trigger']
+
   on: EmitterInterface['on']
   one: EmitterInterface['one']
   off: EmitterInterface['off']
@@ -61,7 +66,6 @@ export default class Calendar {
   defaultTimedEventDuration: moment.Duration
   localeData: object
 
-  $document: any // jQuery
   el: HTMLElement
   contentEl: HTMLElement
   suggestedViewHeight: number
@@ -407,51 +411,13 @@ export default class Calendar {
       )
     }
 
-    if (!this.$document && window['jQuery']) {
-      this.$document = window['jQuery'](document)
-    }
-    if (this.$document) { // we can only attach jQueryUI drag handlers to a jQuery element reference
-      this.$document.on('dragstart sortstart', this.handleJqUiDragStart)
-    }
-  }
-
-
-  handleJqUiDragStart = (ev, ui) => {
-
-    const handleJqUiDragMove = (ev, ui) => {
-      if (this.view) {
-        this.view.handleExternalDragMove(ev)
-      }
-    }
-    const handleJqUiDragStop = (ev, ui) => {
-      if (this.view) {
-        this.view.handleExternalDragStop(ev)
-      }
-      this.$document
-        .off('drag', handleJqUiDragMove)
-        .off('dragstop', handleJqUiDragStop)
-    }
-
-    this.$document
-      .on('drag', handleJqUiDragMove)
-      .on('dragstop', handleJqUiDragStop)
-
-    if (this.view) {
-      const el = ((ui && ui.item) ? ui.item[0] : null) || ev.target
-      this.view.handlExternalDragStart(
-        ev.originalEvent,
-        el,
-        ev.name === 'dragstart' // don't watch mouse/touch movements if doing jqui drag (not sort)
-      )
-    }
+    this.trigger('initialRender')
+    Calendar.trigger('initialRender', this)
   }
 
 
   destroy() {
-
-    if (this.$document) {
-      this.$document.off('dragstart sortstart', this.handleJqUiDragStart)
-    }
+    let wasRendered = Boolean(this.contentEl && this.contentEl.parentNode)
 
     if (this.view) {
       this.clearView()
@@ -477,7 +443,12 @@ export default class Calendar {
       this.windowResizeProxy = null
     }
 
-    GlobalEmitter.unneeded()
+    if (wasRendered) {
+      GlobalEmitter.unneeded()
+
+      this.trigger('destroy')
+      Calendar.trigger('destroy', this)
+    }
   }
 
 
@@ -1358,6 +1329,7 @@ export default class Calendar {
 
 }
 
+EmitterMixin.mixIntoObj(Calendar) // for global registry
 EmitterMixin.mixInto(Calendar)
 ListenerMixin.mixInto(Calendar)
 
@@ -1378,3 +1350,51 @@ function filterLegacyEventInstances(legacyEventInstances, legacyQuery) {
     })
   }
 }
+
+
+// jQuery UI dragging
+
+if (window['jQuery']) {
+  let $document = window['jQuery'](document)
+
+  Calendar.on('initialRender', function(calendar) {
+
+    const handleDragStart = function(ev, ui) {
+
+      const handleDragMove = (ev, ui) => {
+        if (calendar.view) {
+          calendar.view.handleExternalDragMove(ev)
+        }
+      }
+      const handleDragStop = (ev, ui) => {
+        if (calendar.view) {
+          calendar.view.handleExternalDragStop(ev)
+        }
+        $document
+          .off('drag', handleDragMove)
+          .off('dragstop', handleDragStop)
+      }
+
+      $document
+        .on('drag', handleDragMove)
+        .on('dragstop', handleDragStop)
+
+      if (calendar.view) {
+        const el = ((ui && ui.item) ? ui.item[0] : null) || ev.target
+        calendar.view.handlExternalDragStart(
+          ev.originalEvent,
+          el,
+          ev.name === 'dragstart' // don't watch mouse/touch movements if doing jqui drag (not sort)
+        )
+      }
+
+    }
+
+    $document.on('dragstart sortstart', handleDragStart)
+
+    calendar.on('destroy', function(calendar) {
+      $document.off('dragstart sortstart', handleDragStart)
+    })
+  })
+
+}

+ 8 - 2
src/common/Mixin.ts

@@ -1,10 +1,16 @@
 
 export default class Mixin {
 
+  // mix into a CLASS
   static mixInto(destClass) {
+    this.mixIntoObj(destClass.prototype)
+  }
+
+  // mix into ANY object
+  static mixIntoObj(destObj) {
     Object.getOwnPropertyNames(this.prototype).forEach((name) => { // copy methods
-      if (!destClass.prototype[name]) { // if destination class doesn't already define it
-        destClass.prototype[name] = this.prototype[name]
+      if (!destObj[name]) { // if destination doesn't already define it
+        destObj[name] = this.prototype[name]
       }
     })
   }