浏览代码

Wire up content for tooltip widget. Add dispose.

Afshin Darian 8 年之前
父节点
当前提交
ffb7c27d25
共有 1 个文件被更改,包括 35 次插入0 次删除
  1. 35 0
      src/tooltip/widget.ts

+ 35 - 0
src/tooltip/widget.ts

@@ -5,6 +5,10 @@ import {
   Message
 } from 'phosphor/lib/core/messaging';
 
+import {
+  PanelLayout
+} from 'phosphor/lib/ui/panel';
+
 import {
   Widget
 } from 'phosphor/lib/ui/widget';
@@ -30,10 +34,24 @@ class TooltipWidget extends Widget {
    */
   constructor(options: TooltipWidget.IOptions) {
     super();
+    this.layout = new PanelLayout();
     this.model = options.model;
+    this.model.contentChanged.connect(this._onContentChanged, this);
     this.addClass(TOOLTIP_CLASS);
   }
 
+  /**
+   * Dispose of the resources held by the widget.
+   */
+  dispose(): void {
+    if (this._content === null) {
+      return;
+    }
+    this._content.dispose();
+    this._content = null;
+    super.dispose();
+  }
+
   /**
    * The tooltip widget's data model.
    */
@@ -58,8 +76,25 @@ class TooltipWidget extends Widget {
    * Handle `'update-request'` messages.
    */
   protected onUpdateRequest(msg: Message): void {
+    let layout = this.layout as PanelLayout;
+    if (this._content) {
+      layout.addWidget(this._content);
+    }
     super.onUpdateRequest(msg);
   }
+
+  /**
+   * Handle model content changes.
+   */
+  private _onContentChanged(): void {
+    if (this._content) {
+      this._content.dispose();
+    }
+    this._content = this.model.content;
+    this.update();
+  }
+
+  private _content: Widget = null;
 }
 
 /**