Преглед изворни кода

Remove hard coded tooltip handling of escape.

Afshin Darian пре 7 година
родитељ
комит
32915496fb
1 измењених фајлова са 12 додато и 18 уклоњено
  1. 12 18
      packages/tooltip/src/widget.ts

+ 12 - 18
packages/tooltip/src/widget.ts

@@ -36,9 +36,9 @@ import {
 const TOOLTIP_CLASS = 'jp-Tooltip';
 
 /**
- * The class added to widgets that have spawned a tooltip and anchor it.
+ * The class added to the body when a tooltip exists on the page.
  */
-const ANCHOR_CLASS = 'jp-Tooltip-anchor';
+const BODY_CLASS = 'jp-mod-tooltip';
 
 /**
  * The minimum height of a tooltip widget.
@@ -66,21 +66,20 @@ class Tooltip extends Widget {
   constructor(options: Tooltip.IOptions) {
     super();
 
-    let layout = this.layout = new PanelLayout();
-    this.anchor = options.anchor;
+    const layout = this.layout = new PanelLayout();
+    const model = new MimeModel({ data: options.bundle });
 
+    this.anchor = options.anchor;
     this.addClass(TOOLTIP_CLASS);
-    this.anchor.addClass(ANCHOR_CLASS);
-
     this._editor = options.editor;
     this._rendermime = options.rendermime;
-    let model = new MimeModel({
-      data: options.bundle
-    });
-    let mimeType = this._rendermime.preferredMimeType(options.bundle, false);
+
+    const mimeType = this._rendermime.preferredMimeType(options.bundle, false);
+
     if (!mimeType) {
       return;
     }
+
     this._content = this._rendermime.createRenderer(mimeType);
     this._content.renderModel(model);
     layout.addWidget(this._content);
@@ -95,9 +94,6 @@ class Tooltip extends Widget {
    * Dispose of the resources held by the widget.
    */
   dispose(): void {
-    if (this.anchor && !this.anchor.isDisposed) {
-      this.anchor.removeClass(ANCHOR_CLASS);
-    }
     if (this._content) {
       this._content.dispose();
       this._content = null;
@@ -125,13 +121,9 @@ class Tooltip extends Widget {
 
     switch (event.type) {
     case 'keydown':
-      // If the keydown event is inside the tooltip, allow it to continue unless
-      // it is the escape key.
-      if (node.contains(target) && (event as KeyboardEvent).keyCode !== 27) {
+      if (node.contains(target)) {
         return;
       }
-
-      // Any other keydown event should dispose of the tooltip.
       this.dispose();
       break;
     case 'mousedown':
@@ -161,6 +153,7 @@ class Tooltip extends Widget {
    * Handle `'after-attach'` messages.
    */
   protected onAfterAttach(msg: Message): void {
+    document.body.classList.add(BODY_CLASS);
     document.addEventListener('keydown', this, USE_CAPTURE);
     document.addEventListener('mousedown', this, USE_CAPTURE);
     this.anchor.node.addEventListener('scroll', this, USE_CAPTURE);
@@ -171,6 +164,7 @@ class Tooltip extends Widget {
    * Handle `before-detach` messages for the widget.
    */
   protected onBeforeDetach(msg: Message): void {
+    document.body.classList.remove(BODY_CLASS);
     document.removeEventListener('keydown', this, USE_CAPTURE);
     document.removeEventListener('mousedown', this, USE_CAPTURE);
     this.anchor.node.removeEventListener('scroll', this, USE_CAPTURE);