2022-06-06 08:53:59 +00:00
|
|
|
require('./style.scss');
|
2022-06-06 12:49:18 +00:00
|
|
|
var Component = require('vn/component');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
module.exports = new Class({
|
2022-06-06 12:49:18 +00:00
|
|
|
Extends: Component
|
2015-01-23 13:09:30 +00:00
|
|
|
,Tag: 'htk-grid'
|
|
|
|
,Child: 'model'
|
|
|
|
,Properties:
|
|
|
|
{
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* The source data model.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
model: {
|
2015-01-23 13:09:30 +00:00
|
|
|
type: Db.Model
|
2022-05-21 21:31:56 +00:00
|
|
|
,set: function(x) {
|
|
|
|
this.link({_model: x},
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
|
|
|
'status-changed': this.onModelChange
|
|
|
|
,'row-deleted': this.onRowDelete
|
|
|
|
,'row-updated': this.onRowUpdate
|
|
|
|
,'row-inserted': this.onRowInsert
|
|
|
|
,'updatable-changed': this.onUpdatableChange
|
|
|
|
});
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var set = new Db.SimpleIterator({model: x});
|
|
|
|
this.link({_set: set});
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.onUpdatableChange();
|
|
|
|
this.onModelChange();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
,get: function() {
|
2015-09-28 09:46:24 +00:00
|
|
|
return this._model;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
},
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* Message that should be displayed when source model is not ready.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
emptyMessage: {
|
2015-01-23 13:09:30 +00:00
|
|
|
type: String
|
2016-10-20 08:37:08 +00:00
|
|
|
,value: null
|
2015-07-03 05:49:45 +00:00
|
|
|
},
|
2015-11-19 13:57:23 +00:00
|
|
|
/**
|
|
|
|
* Wether to display the header with column titles.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
showHeader: {
|
2015-07-03 05:49:45 +00:00
|
|
|
type: Boolean
|
2022-05-21 21:31:56 +00:00
|
|
|
,set: function(x) {
|
2015-07-03 05:49:45 +00:00
|
|
|
this._showHeader = x;
|
|
|
|
this.thead.style.display = x ? 'table-header-group' : 'none';
|
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
,get: function() {
|
2015-07-03 05:49:45 +00:00
|
|
|
return this._showHeader;
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
,_model: null
|
2016-05-05 15:47:45 +00:00
|
|
|
,_set: null
|
2022-05-21 21:31:56 +00:00
|
|
|
,columns: new Array()
|
2015-01-23 13:09:30 +00:00
|
|
|
,internalColumn: null
|
|
|
|
,internalColumns: 0
|
2015-04-01 08:24:15 +00:00
|
|
|
,sortColumn: -1
|
|
|
|
,sortWay: null
|
2015-07-03 05:49:45 +00:00
|
|
|
,_showHeader: true
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,render: function() {
|
|
|
|
var table = this.createRoot('table');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var thead = this.createElement('thead');
|
|
|
|
table.appendChild(thead);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.thead = this.createElement('tr')
|
|
|
|
thead.appendChild(this.thead);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.tbody = this.createElement('tbody');
|
|
|
|
table.appendChild(this.tbody);
|
2015-11-09 08:14:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,appendChild: function(child) {
|
|
|
|
this.appendColumn(child);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,removeClicked: function(column, value, row) {
|
|
|
|
if (confirm(_('ReallyDelete')))
|
|
|
|
this._model.deleteRow(row);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,onRowDelete: function(model, row) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var tableRows = this.tbody.childNodes;
|
2022-05-21 21:31:56 +00:00
|
|
|
this.tbody.removeChild(tableRows[row]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
for (var i = row; i < tableRows.length; i++)
|
|
|
|
tableRows[i].className = (i % 2) ? 'pair-row' : '';
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.showNoRecordsFound();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,onRowInsert: function() {
|
|
|
|
this.buildRow(1);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,renderCell: function(row, column, tr) {
|
2022-05-28 15:49:46 +00:00
|
|
|
if (column.column)
|
|
|
|
column.value = this._model.data[row][column.column];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
if (column.renderer) {
|
2016-05-05 15:47:45 +00:00
|
|
|
this._set.row = row;
|
2022-05-21 21:31:56 +00:00
|
|
|
column.renderer(column, this._set);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
return column.render(tr);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,refreshRow: function(row, columns) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var x = this.columns;
|
|
|
|
var tr = this.tbody.childNodes[row];
|
|
|
|
|
|
|
|
for (var i = 0; i < x.length; i++)
|
2022-05-21 21:31:56 +00:00
|
|
|
if (x[i].renderer || columns.indexOf(x[i].columnIndex) != -1) {
|
|
|
|
var cell = this.renderCell(row, x[i], tr);
|
|
|
|
tr.replaceChild(cell, tr.childNodes[i]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,onRowUpdate: function(model, row, columns) {
|
|
|
|
this.refreshRow(row, columns);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,buildRow: function(count) {
|
|
|
|
for (var i = 0; i < count; i++) {
|
|
|
|
var tr = this.createElement('tr');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (i % 2)
|
|
|
|
tr.className = 'pair-row';
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
for (var j = 0; j < this.columns.length; j++) {
|
|
|
|
var cell = this.renderCell(i, this.columns[j], tr);
|
|
|
|
tr.appendChild(cell);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.tbody.appendChild(tr);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,onUpdatableChange: function() {
|
|
|
|
if (this._model && this._model.updatable) {
|
|
|
|
if (!this.internalColumn) {
|
|
|
|
this.internalColumn = new Htk.ColumnButton({
|
2016-09-26 09:28:47 +00:00
|
|
|
icon: 'delete'
|
2015-01-23 13:09:30 +00:00
|
|
|
,tip: _('Remove')
|
|
|
|
});
|
2022-05-21 21:31:56 +00:00
|
|
|
this.internalColumn.on('clicked', this.removeClicked, this);
|
|
|
|
this.insertInternalColumn(0, this.internalColumn);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2022-05-21 21:31:56 +00:00
|
|
|
} else if (this.internalColumn) {
|
2015-01-23 13:09:30 +00:00
|
|
|
this.internalColumn = null;
|
2022-05-21 21:31:56 +00:00
|
|
|
this.removeInternalColumn(0);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,onModelChange: function() {
|
2016-10-20 08:37:08 +00:00
|
|
|
var emptyMessage = this.emptyMessage ?
|
|
|
|
this.emptyMessage : _('NoData');
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
if (!this._model) {
|
|
|
|
this.showMessage(emptyMessage, 'refresh');
|
2015-01-23 13:09:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._node.removeChild(this.tbody);
|
|
|
|
this.tbody = this.createElement('tbody');
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
switch (this._model.status) {
|
2015-01-23 13:09:30 +00:00
|
|
|
case Db.Model.Status.READY:
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.columns.length; i++)
|
2022-05-28 15:49:46 +00:00
|
|
|
this.columns[i].updateColumnName(this._model);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.buildRow(this._model.numRows);
|
|
|
|
this.showNoRecordsFound();
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Db.Model.Status.LOADING:
|
2022-05-21 21:31:56 +00:00
|
|
|
this.showMessage(_('Loading'), null);
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
case Db.Model.Status.CLEAN:
|
2022-05-21 21:31:56 +00:00
|
|
|
this.showMessage(emptyMessage, 'refresh');
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
case Db.Model.Status.ERROR:
|
2022-05-21 21:31:56 +00:00
|
|
|
this.showMessage(_('ErrorLoadingData'), 'error');
|
2015-01-23 13:09:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this._node.appendChild(this.tbody);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,showNoRecordsFound: function() {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (this._model.numRows == 0)
|
2022-05-21 21:31:56 +00:00
|
|
|
this.showMessage(_('EmptyList'), 'block');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,showMessage: function(message, src) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (this.columns.length == 0)
|
|
|
|
return;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var tr = this.createElement('tr');
|
|
|
|
this.tbody.appendChild(tr);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var td = this.createElement('td');
|
2015-07-03 05:49:45 +00:00
|
|
|
td.className = 'message';
|
2015-01-23 13:09:30 +00:00
|
|
|
td.colSpan = this.columns.length;
|
2022-05-21 21:31:56 +00:00
|
|
|
tr.appendChild(td);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
if (src) {
|
|
|
|
const icon = new Htk.Icon({
|
|
|
|
name: src
|
|
|
|
});
|
|
|
|
td.appendChild(icon.node);
|
|
|
|
} else {
|
|
|
|
var spinner = new Htk.Spinner();
|
|
|
|
spinner.start();
|
|
|
|
td.appendChild(spinner.node);
|
2015-11-19 13:57:23 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
var message = this.createTextNode(message);
|
|
|
|
td.appendChild(message);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,scrollToRow: function(row) {
|
|
|
|
if (row >= 0) {
|
|
|
|
var height = parseInt(this.tr.style.height);
|
2015-01-23 13:09:30 +00:00
|
|
|
this.node.scrollTop = (row - 2) * height;
|
|
|
|
|
|
|
|
if (this.selectedRow)
|
|
|
|
this.selectedRow.style.backgroundColor = null;
|
|
|
|
|
|
|
|
this.selectedRow = this.tbody.childNodes[row]
|
|
|
|
this.selectedRow.style.backgroundColor = '#AAD';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,sortModel: function(column) {
|
2022-05-28 15:49:46 +00:00
|
|
|
var columnName = column.column;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
if (this._model && columnName != -1) {
|
|
|
|
if (this.sortColumn === columnName
|
2015-04-01 08:24:15 +00:00
|
|
|
&& this.sortWay === Db.Model.SortWay.ASC)
|
|
|
|
this.sortWay = Db.Model.SortWay.DESC;
|
|
|
|
else
|
|
|
|
this.sortWay = Db.Model.SortWay.ASC;
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
this.sortColumn = columnName;
|
2015-04-01 08:24:15 +00:00
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
this._model.sortByName(columnName, this.sortWay);
|
2015-04-01 08:24:15 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,columnChanged: function(column, row, newValue) {
|
2022-05-28 15:49:46 +00:00
|
|
|
var columnName = column.column;
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
if (columnName != -1)
|
|
|
|
this._model.set(row, columnName, newValue);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,addColumn: function(pos, column) {
|
|
|
|
var header = column.renderHeader();
|
|
|
|
|
|
|
|
if (pos == -1 || pos >= this.columns.length) {
|
|
|
|
pos = this.columns.push(column) - 1;
|
|
|
|
this.thead.appendChild(header);
|
|
|
|
} else {
|
|
|
|
this.columns.splice(pos, 0, column);
|
|
|
|
this.thead.insertBefore(header, this.thead.childNodes[pos]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
header.addEventListener('click',
|
|
|
|
this.sortModel.bind(this, column));
|
2015-01-23 13:09:30 +00:00
|
|
|
header.title = _('Sort');
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
column.on('changed', this.columnChanged, this);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
var rows = this.tbody.childNodes;
|
|
|
|
|
|
|
|
if (this._model && this._model.numRows > 0)
|
2022-05-21 21:31:56 +00:00
|
|
|
for (var i = 0; i < rows.length; i++) {
|
|
|
|
var cell = this.renderCell(i, column, rows[i]);
|
|
|
|
rows[i].insertBefore(cell, rows[i].childNodes[pos+1]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,insertInternalColumn: function(pos, column) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (pos < 0 || pos > this.internalColumns)
|
|
|
|
pos = this.internalColumns;
|
|
|
|
|
|
|
|
this.internalColumns++;
|
2022-05-21 21:31:56 +00:00
|
|
|
return this.addColumn(pos, column);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,insertColumn: function(pos, column) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (pos > 0)
|
|
|
|
pos += this.internalColumns;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
return this.addColumn(pos, column) - this.internalColumns;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,appendColumn: function(column) {
|
|
|
|
return this.insertColumn(-1, column);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,deleteColumn: function(pos) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (pos < 0 || pos >= this.columns.length)
|
|
|
|
return;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.columns.splice(pos, 1);
|
|
|
|
this.thead.removeChild(this.thead.childNodes[pos]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
var rows = this.tbody.childNodes;
|
|
|
|
|
|
|
|
if (this._model && this._model.numRows > 0)
|
|
|
|
for (var i = 0; i < rows.length; i++)
|
2022-05-21 21:31:56 +00:00
|
|
|
rows[i].removeChild(rows[i].childNodes[pos]);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,removeInternalColumn: function(pos) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (this.internalColumns == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pos < 0 || pos > this.internalColumns)
|
|
|
|
pos = this.internalColumns;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.deleteColumn(pos);
|
2015-01-23 13:09:30 +00:00
|
|
|
this.internalColumns--;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,removeColumn: function(pos) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (pos > 0)
|
|
|
|
pos += this.internalColumns;
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
this.deleteColumn(pos);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 21:31:56 +00:00
|
|
|
,reloadModel: function() {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (this._model != null)
|
2022-05-21 21:31:56 +00:00
|
|
|
this.onModelChange();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
2015-11-17 10:34:33 +00:00
|
|
|
|