constructor()

in src/widgets/revision/widget.ts [58:143]


  constructor(options: Revision.IOptions) {
    super();
    this.addClass(REVISION_CLASS);
    let model = (this.model = options.model);
    let outputRenderer = new OutputRenderer();

    let layout = (this.layout = new PanelLayout());

    let notebookWidget = new Widget({ node: document.createElement('div') });
    notebookWidget.addClass(REVISION_NOTEBOOK_CLASS);
    let nbLayout = (notebookWidget.layout = new PanelLayout());
    layout.addWidget(notebookWidget);

    // Add header
    let header: HTMLElement = document.createElement('h1');
    header.textContent = this.model.isLatest
      ? 'Current version'
      : getRelativeTime(options.now, this.model.timeCreated);
    let headerWidget: Widget = new Widget({ node: header });
    headerWidget.addClass(REVISION_HEADER_CLASS);
    nbLayout.addWidget(headerWidget);

    // Add buttons for gathering
    let buttons = new Widget({ node: document.createElement('div') });
    buttons.addClass(REVISION_BUTTONS_CLASS);
    const panelLayout = (buttons.layout = new PanelLayout());
    panelLayout.addWidget(
      this.createButton(
        'Open in notebook',
        'jp-Toolbar-gatherbutton-BookIcon',
        GatherState.GATHER_TO_NOTEBOOK
      )
    );
    panelLayout.addWidget(
      this.createButton(
        'Copy to clipboard',
        'jp-Toolbar-gatherbutton-CellsIcon',
        GatherState.GATHER_TO_CLIPBOARD
      )
    );
    nbLayout.addWidget(buttons);

    // Add the revision's code
    let cellsWidget = new Widget({ node: document.createElement('div') });
    cellsWidget.addClass(REVISION_CELLS_CLASS);
    let cellsLayout = (cellsWidget.layout = new PanelLayout());
    nbLayout.addWidget(cellsWidget);

    cellsLayout.addWidget(
      new CodeVersion({
        model: model.source,
      })
    );

    if (model.output && model.output.length >= 1) {
      let outputElement = outputRenderer.render(model.output);
      if (outputElement) {
        cellsLayout.addWidget(
          new Widget({
            node: outputElement,
          })
        );
      }
    }

    // Scroll to the bottom. Create as an observer as the cells will be initialized dynamically.
    if (MutationObserver) {
      let observer = new MutationObserver(mutations => {
        for (let mutation of mutations) {
          let target = mutation.target as HTMLElement;
          if (
            target.classList &&
            target.classList.contains('CodeMirror-measure')
          ) {
            cellsWidget.node.scrollTop = cellsWidget.node.scrollHeight;
          }
          break;
        }
      });
      observer.observe(cellsWidget.node, {
        attributes: false,
        childList: true,
        subtree: true,
      });
    }
  }