jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
115 lines (114 loc) • 3.7 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var phosphor_widget_1 = require('phosphor-widget');
var docregistry_1 = require('../docregistry');
var d3_dsv_1 = require('d3-dsv');
/**
* The class name added to a csv widget.
*/
var CSV_CLASS = 'jp-CSVWidget';
/**
* A widget for csv tables.
*/
var CSVWidget = (function (_super) {
__extends(CSVWidget, _super);
/**
* Construct a new csv table widget.
*/
function CSVWidget(context) {
var _this = this;
_super.call(this);
this._context = context;
this.node.tabIndex = -1;
this.addClass(CSV_CLASS);
if (context.model.toString()) {
this.update();
}
context.pathChanged.connect(function () {
_this.update();
});
context.model.contentChanged.connect(function () {
_this.update();
});
context.contentsModelChanged.connect(function () {
_this.update();
});
}
/**
* Dispose of the resources used by the widget.
*/
CSVWidget.prototype.dispose = function () {
if (this.isDisposed) {
return;
}
this._context = null;
_super.prototype.dispose.call(this);
};
/**
* Handle `update-request` messages for the widget.
*/
CSVWidget.prototype.onUpdateRequest = function (msg) {
this.title.text = this._context.path.split('/').pop();
var cm = this._context.contentsModel;
if (cm === null) {
return;
}
var content = this._context.model.toString();
this.renderTable(content);
};
/**
* Render an html table from a csv string.
*/
CSVWidget.prototype.renderTable = function (content) {
var parsed = d3_dsv_1.csvParse(content);
var table = document.createElement('table');
var header = document.createElement('tr');
for (var _i = 0, _a = parsed.columns; _i < _a.length; _i++) {
var name_1 = _a[_i];
var th = document.createElement('th');
th.textContent = name_1;
header.appendChild(th);
}
table.appendChild(header);
for (var _b = 0, parsed_1 = parsed; _b < parsed_1.length; _b++) {
var row = parsed_1[_b];
var tr = document.createElement('tr');
for (var _c = 0, _d = parsed.columns; _c < _d.length; _c++) {
var col = _d[_c];
var td = document.createElement('td');
td.textContent = row[col];
tr.appendChild(td);
}
table.appendChild(tr);
}
this.node.textContent = '';
this.node.appendChild(table);
};
return CSVWidget;
}(phosphor_widget_1.Widget));
exports.CSVWidget = CSVWidget;
/**
* A widget factory for csv tables.
*/
var CSVWidgetFactory = (function (_super) {
__extends(CSVWidgetFactory, _super);
function CSVWidgetFactory() {
_super.apply(this, arguments);
}
/**
* Create a new widget given a context.
*/
CSVWidgetFactory.prototype.createNew = function (context, kernel) {
var widget = new CSVWidget(context);
this.widgetCreated.emit(widget);
return widget;
};
return CSVWidgetFactory;
}(docregistry_1.ABCWidgetFactory));
exports.CSVWidgetFactory = CSVWidgetFactory;