2016-02-20 13:31:29 +03:00
|
|
|
/**
|
2016-08-12 15:29:58 +03:00
|
|
|
* Very simple and fast tree grid/table, compatible with dynamic loading and stickyheaders.js
|
2016-03-09 23:52:32 +03:00
|
|
|
* License: MPL 2.0+, (c) Vitaliy Filippov 2016+
|
2016-08-15 16:22:13 +03:00
|
|
|
* Version: 2016-08-15
|
2016-02-20 13:31:29 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* USAGE:
|
|
|
|
*
|
2016-06-05 01:17:51 +03:00
|
|
|
* var TG = new TreeGrid({ items: items, header: header, renderer: renderer });
|
2016-02-20 13:31:29 +03:00
|
|
|
* document.body.appendChild(TG.table);
|
|
|
|
*
|
2016-06-05 01:17:51 +03:00
|
|
|
* // renderer is a function that generates cell HTML properties for a node
|
|
|
|
* renderer = function(node) { return [ { innerHTML, style, className, title }, ... ] }
|
|
|
|
*
|
2016-02-20 13:31:29 +03:00
|
|
|
* items: [ node={
|
2016-06-05 01:37:11 +03:00
|
|
|
* key: <unique key of this node across siblings>,
|
2016-02-20 13:31:29 +03:00
|
|
|
* children: [ node... ],
|
|
|
|
* leaf: true/false,
|
2016-02-24 17:08:00 +03:00
|
|
|
* collapsed: true/false,
|
|
|
|
* data: <user data>
|
2016-02-20 13:31:29 +03:00
|
|
|
* }, ... ]
|
|
|
|
*
|
|
|
|
* header: [ 'html' or { innerHTML, style, className, title }, ... ]
|
2016-03-10 15:04:09 +03:00
|
|
|
*
|
|
|
|
* Readonly properties:
|
|
|
|
*
|
|
|
|
* TG.table - grid <table> element
|
|
|
|
* TG.root - root node
|
|
|
|
*
|
|
|
|
* Methods:
|
|
|
|
*
|
|
|
|
* // change header and clear grid contents
|
|
|
|
* TG.setHeader(header);
|
|
|
|
*
|
|
|
|
* // change "leaf" status of a node and its children
|
|
|
|
* node.setChildren(isLeaf, newChildren);
|
|
|
|
*
|
|
|
|
* // expand/collapse a node
|
|
|
|
* node.toggle();
|
|
|
|
*
|
2016-06-05 01:17:51 +03:00
|
|
|
* // change node.data and re-render node
|
|
|
|
* node.render();
|
|
|
|
*
|
2016-03-10 15:04:09 +03:00
|
|
|
* // use simple cell editing
|
2016-03-10 15:59:57 +03:00
|
|
|
* TG.initCellEditing();
|
2016-03-10 15:04:09 +03:00
|
|
|
*
|
|
|
|
* // use simple mouse cell selection
|
2016-03-10 15:59:57 +03:00
|
|
|
* // HTMLElement restrictToNode: the element to listen for mousedown events
|
|
|
|
* TG.initCellSelection(restrictToNode);
|
|
|
|
*
|
|
|
|
* Callbacks:
|
|
|
|
*
|
|
|
|
* // called before expanding node. should return false if you want to prevent expanding the node
|
|
|
|
* TG.onExpand = function(node) {};
|
|
|
|
* // called before a cell is selected with mouse
|
|
|
|
* TG.onCellSelect = function(node, colIndex, td) { return <whether to allow selection (boolean)>; }
|
|
|
|
* // called when a cell is deselected with mouse
|
|
|
|
* TG.onCellDeselect = function(node, colIndex, td) {}
|
|
|
|
* // called before a cell is edited
|
|
|
|
* TG.onStartCellEdit = function(node, colIndex, td) { return {
|
|
|
|
* abort: <whether to prevent editing>,
|
|
|
|
* value: <override initial textbox value>
|
|
|
|
* } }
|
|
|
|
* // called before a cell is stopped being edited
|
|
|
|
* TG.onStopCellEdit = function (node, colIndex, value, td) { return { html: <new cell content> } }
|
|
|
|
*
|
2016-02-20 13:31:29 +03:00
|
|
|
*/
|
2016-06-05 01:17:51 +03:00
|
|
|
function TreeGrid(options)
|
2016-02-20 13:31:29 +03:00
|
|
|
{
|
2016-06-05 01:17:51 +03:00
|
|
|
this.renderer = options.renderer;
|
2016-02-20 13:31:29 +03:00
|
|
|
this.bindProps = { 'style': 1, 'className': 1, 'title': 1, 'innerHTML': 1 };
|
|
|
|
this.levelIndent = 20;
|
|
|
|
this.table = document.createElement('table');
|
|
|
|
this.table.className = 'grid';
|
|
|
|
this.thead = document.createElement('thead');
|
|
|
|
this.tbody = document.createElement('tbody');
|
|
|
|
this.table.appendChild(this.thead);
|
|
|
|
this.table.appendChild(this.tbody);
|
|
|
|
this.thead.appendChild(document.createElement('tr'));
|
2016-06-05 01:17:51 +03:00
|
|
|
this.setHeader(options.header);
|
|
|
|
new TreeGridNode({ children: options.items }, this);
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
|
|
|
|
2016-08-15 16:22:13 +03:00
|
|
|
TreeGrid.prototype.initStickyHeaders = function(options)
|
2016-08-12 15:29:58 +03:00
|
|
|
{
|
|
|
|
this.sticky = true;
|
2016-08-15 16:22:13 +03:00
|
|
|
this.noStickyResize = options.noWindowResize;
|
|
|
|
if (this.header && this.header.length)
|
|
|
|
this._makeStickyHeaders();
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeGrid.prototype._makeStickyHeaders = function()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
makeStickyHeaders(this.table);
|
|
|
|
this.stickyInit = true;
|
|
|
|
this.table.className += ' stickyheaders';
|
|
|
|
if (!this.noStickyResize)
|
2016-08-13 23:13:37 +03:00
|
|
|
{
|
2016-08-15 16:22:13 +03:00
|
|
|
addListener(window, 'resize', function()
|
|
|
|
{
|
|
|
|
if (self.stickyInit)
|
|
|
|
fixStickyColumnSizes(self.table);
|
|
|
|
});
|
2016-08-13 23:13:37 +03:00
|
|
|
}
|
2016-08-12 15:29:58 +03:00
|
|
|
}
|
|
|
|
|
2016-02-20 13:31:29 +03:00
|
|
|
TreeGrid.prototype.setHeader = function(newHeader)
|
|
|
|
{
|
|
|
|
var tr = this.thead.rows[0];
|
|
|
|
tr.innerHTML = '';
|
|
|
|
for (var i = 0; i < newHeader.length; i++)
|
|
|
|
{
|
|
|
|
var th = document.createElement('th');
|
|
|
|
this._setProps(th, newHeader[i]);
|
|
|
|
tr.appendChild(th);
|
|
|
|
}
|
|
|
|
this.header = newHeader;
|
2016-03-10 13:23:16 +03:00
|
|
|
this.editedCells = [];
|
2016-02-20 13:31:29 +03:00
|
|
|
// header change clears the whole grid by now
|
|
|
|
if (this.root)
|
2016-03-04 12:27:56 +03:00
|
|
|
this.root.setChildren(false, []);
|
2016-08-12 15:29:58 +03:00
|
|
|
if (this.sticky)
|
|
|
|
{
|
|
|
|
if (!this.stickyInit)
|
2016-08-15 16:22:13 +03:00
|
|
|
this._makeStickyHeaders();
|
2016-08-12 15:29:58 +03:00
|
|
|
else
|
|
|
|
fixStickyHeader(this.table);
|
|
|
|
}
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeGrid.prototype.onExpand = function(node)
|
|
|
|
{
|
|
|
|
// handle node expand/collapse here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeGrid.prototype._setProps = function(el, props)
|
|
|
|
{
|
2016-05-18 18:38:22 +03:00
|
|
|
if (props === undefined || props === null)
|
|
|
|
for (var j in this.bindProps)
|
|
|
|
el[j] = '';
|
|
|
|
else if (typeof props == 'string')
|
2016-02-20 13:31:29 +03:00
|
|
|
el.innerHTML = props;
|
|
|
|
else
|
|
|
|
for (var j in this.bindProps)
|
2016-06-06 15:34:14 +03:00
|
|
|
el[j] = props[j] !== undefined ? props[j] : '';
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:52:32 +03:00
|
|
|
// Simple cell editing
|
|
|
|
|
2016-06-05 01:17:51 +03:00
|
|
|
TreeGrid.prototype._getCellIndex = function(n)
|
2016-03-09 23:52:32 +03:00
|
|
|
{
|
2016-03-10 13:23:16 +03:00
|
|
|
var i = 0;
|
|
|
|
while ((n = n.previousSibling))
|
2016-03-09 23:52:32 +03:00
|
|
|
i += (n.nodeType != 3 ? 1 : 0);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2016-03-10 15:59:57 +03:00
|
|
|
TreeGrid.prototype.initCellEditing = function()
|
2016-03-09 23:52:32 +03:00
|
|
|
{
|
|
|
|
var self = this;
|
2016-03-10 13:23:16 +03:00
|
|
|
self.editedCells = []; // FIXME maybe remove and use just class selector
|
2016-03-09 23:52:32 +03:00
|
|
|
addListener(this.table, 'dblclick', function(evt)
|
|
|
|
{
|
2016-03-10 14:26:55 +03:00
|
|
|
evt = evt||window.event;
|
2016-03-09 23:52:32 +03:00
|
|
|
var td = evt.target||evt.srcElement;
|
|
|
|
while (td.nodeName != 'TABLE' && td.nodeName != 'TD')
|
|
|
|
td = td.parentNode;
|
|
|
|
if (td.nodeName == 'TD' && td.parentNode._node && td.previousSibling && td.className != 'celleditor')
|
|
|
|
{
|
2016-06-05 01:17:51 +03:00
|
|
|
var params = self.onStartCellEdit && self.onStartCellEdit(td.parentNode._node, self._getCellIndex(td), td) || {};
|
2016-03-09 23:52:32 +03:00
|
|
|
if (params.abort)
|
|
|
|
return;
|
|
|
|
self.editedCells.push(td);
|
|
|
|
td.className += ' celleditor';
|
|
|
|
if (params.value === undefined)
|
|
|
|
params.value = td.innerHTML;
|
2016-03-10 13:23:16 +03:00
|
|
|
td.innerHTML = '<input type="text" value="'+htmlspecialchars(params.value)+'" />';
|
2016-03-09 23:52:32 +03:00
|
|
|
addListener(td.firstChild, 'keydown', function(evt)
|
|
|
|
{
|
|
|
|
if (evt.keyCode == 13 || evt.keyCode == 10)
|
|
|
|
self.stopCellEditing(td);
|
|
|
|
});
|
|
|
|
td.firstChild.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addListener(document.body, 'click', function(evt)
|
|
|
|
{
|
|
|
|
if (!self.editedCells.length)
|
|
|
|
return;
|
|
|
|
var td = evt.target||evt.srcElement;
|
|
|
|
while (td && td.nodeName != 'TD')
|
|
|
|
td = td.parentNode;
|
|
|
|
if (td && /\bcelleditor\b/.exec(td.className))
|
|
|
|
return;
|
|
|
|
for (var i = self.editedCells.length-1; i >= 0; i--)
|
|
|
|
self.stopCellEditing(self.editedCells[i], 1);
|
|
|
|
self.editedCells = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeGrid.prototype.stopCellEditing = function(td, _int)
|
|
|
|
{
|
2016-06-05 01:17:51 +03:00
|
|
|
var i = this._getCellIndex(td);
|
|
|
|
var node = td.parentNode._node;
|
|
|
|
node._oldCells[i] = undefined;
|
2016-07-06 18:26:34 +03:00
|
|
|
var params = this.onStopCellEdit && this.onStopCellEdit(node, i, td.firstChild ? td.firstChild.value : null, td) || {};
|
2016-06-05 01:17:51 +03:00
|
|
|
node.render(i);
|
2016-03-09 23:52:32 +03:00
|
|
|
if (!_int)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.editedCells.length; i++)
|
|
|
|
{
|
|
|
|
if (this.editedCells[i] == td)
|
|
|
|
{
|
|
|
|
this.editedCells.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 14:46:50 +03:00
|
|
|
// Simple cell selection
|
|
|
|
|
2016-03-10 15:59:57 +03:00
|
|
|
TreeGrid.prototype.initCellSelection = function(restrictToNode)
|
2016-03-10 14:46:50 +03:00
|
|
|
{
|
|
|
|
var startDrag, dragDiv;
|
|
|
|
|
|
|
|
var self = this;
|
2016-06-05 01:17:51 +03:00
|
|
|
self.table.className += ' disable-text-select';
|
2016-03-10 15:59:57 +03:00
|
|
|
|
2016-04-13 15:51:34 +03:00
|
|
|
self.selectCell = function(cell)
|
|
|
|
{
|
|
|
|
if (!cell.parentNode._node || cell.className.indexOf(' selected') >= 0)
|
|
|
|
return;
|
2016-06-05 01:17:51 +03:00
|
|
|
var i = self._getCellIndex(cell);
|
2016-04-13 15:51:34 +03:00
|
|
|
if (!self.onCellSelect || self.onCellSelect(cell.parentNode._node, i, cell))
|
|
|
|
cell.className += ' selected';
|
|
|
|
};
|
|
|
|
|
|
|
|
self.deselectCell = function(cell)
|
|
|
|
{
|
2016-06-05 01:17:51 +03:00
|
|
|
self.onCellDeselect && self.onCellDeselect(cell.parentNode._node, self._getCellIndex(cell), cell);
|
2016-04-13 15:51:34 +03:00
|
|
|
cell.className = cell.className.replace(' selected', '');
|
|
|
|
};
|
|
|
|
|
2016-03-10 15:59:57 +03:00
|
|
|
addListener(restrictToNode || document.body, 'mousedown', function(evt)
|
2016-03-10 14:46:50 +03:00
|
|
|
{
|
2016-03-10 15:59:57 +03:00
|
|
|
evt = getEventCoord(evt);
|
|
|
|
if (!evt.shiftKey)
|
2016-03-10 14:46:50 +03:00
|
|
|
{
|
2016-03-10 15:59:57 +03:00
|
|
|
var els = self.table.querySelectorAll('td.selected');
|
|
|
|
for (var i = 0; i < els.length; i++)
|
2016-04-13 15:51:34 +03:00
|
|
|
self.deselectCell(els[i]);
|
2016-03-10 14:46:50 +03:00
|
|
|
}
|
|
|
|
if (evt.which != 1)
|
|
|
|
return;
|
|
|
|
startDrag = [ evt.pageX, evt.pageY ];
|
|
|
|
if (!dragDiv)
|
|
|
|
{
|
|
|
|
dragDiv = document.createElement('div');
|
|
|
|
dragDiv.className = 'selection-rect';
|
2016-04-04 14:14:48 +03:00
|
|
|
dragDiv.style.display = 'none';
|
2016-03-10 14:46:50 +03:00
|
|
|
document.body.appendChild(dragDiv);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addListener(document.body, 'mousemove', function(evt)
|
|
|
|
{
|
|
|
|
if (startDrag)
|
|
|
|
{
|
|
|
|
evt = getEventCoord(evt);
|
2016-04-04 14:14:48 +03:00
|
|
|
if (dragDiv.style.display == 'none')
|
|
|
|
{
|
|
|
|
if ((startDrag[0] < evt.pageX-10 || startDrag[0] > evt.pageX+10) &&
|
|
|
|
(startDrag[1] < evt.pageY-10 || startDrag[1] > evt.pageY+10))
|
|
|
|
dragDiv.style.display = 'block';
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2016-03-10 14:46:50 +03:00
|
|
|
if (startDrag[0] < evt.pageX)
|
|
|
|
{
|
|
|
|
dragDiv.style.left = startDrag[0]+'px';
|
|
|
|
dragDiv.style.width = (evt.pageX-startDrag[0])+'px';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dragDiv.style.left = evt.pageX+'px';
|
|
|
|
dragDiv.style.width = (startDrag[0]-evt.pageX)+'px';
|
|
|
|
}
|
|
|
|
if (startDrag[1] < evt.pageY)
|
|
|
|
{
|
|
|
|
dragDiv.style.top = startDrag[1]+'px';
|
|
|
|
dragDiv.style.height = (evt.pageY-startDrag[1])+'px';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dragDiv.style.top = evt.pageY+'px';
|
|
|
|
dragDiv.style.height = (startDrag[1]-evt.pageY)+'px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addListener(document.body, 'mouseup', function(evt)
|
|
|
|
{
|
|
|
|
if (startDrag)
|
|
|
|
{
|
|
|
|
evt = getEventCoord(evt);
|
|
|
|
dragDiv.style.display = 'none';
|
|
|
|
var x1 = startDrag[0], y1 = startDrag[1], x2 = evt.pageX, y2 = evt.pageY;
|
|
|
|
if (x2 < x1)
|
|
|
|
{
|
|
|
|
var t = x2;
|
|
|
|
x2 = x1;
|
|
|
|
x1 = t;
|
|
|
|
}
|
|
|
|
if (y2 < y1)
|
|
|
|
{
|
|
|
|
var t = y2;
|
|
|
|
y2 = y1;
|
|
|
|
y1 = t;
|
|
|
|
}
|
|
|
|
startDrag = null;
|
|
|
|
if (x2 > x1+10 && y2 > y1+10)
|
2016-06-05 01:17:51 +03:00
|
|
|
{
|
|
|
|
var bodyPos = getOffset(self.tbody);
|
|
|
|
if (x1 < bodyPos.left+self.tbody.offsetWidth &&
|
|
|
|
y1 < bodyPos.top+self.tbody.offsetHeight &&
|
|
|
|
x2 > bodyPos.left && y2 > bodyPos.top)
|
|
|
|
{
|
|
|
|
if (x1 <= bodyPos.left)
|
|
|
|
x1 = bodyPos.left+1;
|
|
|
|
if (y1 <= bodyPos.top)
|
|
|
|
y1 = bodyPos.top+1;
|
|
|
|
if (x2 >= bodyPos.left+self.tbody.offsetWidth)
|
|
|
|
x2 = bodyPos.left+self.tbody.offsetWidth-1;
|
|
|
|
if (y2 >= bodyPos.top+self.tbody.offsetHeight)
|
|
|
|
y2 = bodyPos.top+self.tbody.offsetHeight-1;
|
|
|
|
var e1 = document.elementFromPoint(x1, y1);
|
|
|
|
var e2 = document.elementFromPoint(x2, y2);
|
|
|
|
while (e1 && e1.nodeName != 'TD')
|
|
|
|
e1 = e1.parentNode;
|
|
|
|
while (e2 && e2.nodeName != 'TD')
|
|
|
|
e2 = e2.parentNode;
|
|
|
|
if (e1 && e2)
|
|
|
|
{
|
|
|
|
var col1 = self._getCellIndex(e1);
|
|
|
|
var col2 = self._getCellIndex(e2);
|
|
|
|
var row = e1.parentNode;
|
|
|
|
do
|
|
|
|
{
|
2016-08-12 17:08:01 +03:00
|
|
|
if (row.style.display != 'none' && row.style.visibility != 'hidden')
|
2016-06-05 01:17:51 +03:00
|
|
|
for (var i = col1; i <= col2; i++)
|
2016-08-12 17:08:01 +03:00
|
|
|
if (!self.stickyInit || i != 1)
|
|
|
|
self.selectCell(row.cells[i]);
|
2016-06-05 01:17:51 +03:00
|
|
|
if (row == e2.parentNode)
|
|
|
|
break;
|
|
|
|
row = row.nextSibling;
|
|
|
|
} while (row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 14:14:48 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var t = evt.target || evt.srcElement;
|
2016-04-13 15:51:34 +03:00
|
|
|
self.selectCell(t);
|
2016-04-04 14:14:48 +03:00
|
|
|
}
|
|
|
|
if (x2 > x1+10 && y2 > y1+10 || evt.shiftKey)
|
2016-03-10 14:46:50 +03:00
|
|
|
{
|
2016-03-10 15:59:57 +03:00
|
|
|
if (document.selection)
|
|
|
|
document.selection.empty();
|
|
|
|
else
|
|
|
|
window.getSelection().removeAllRanges();
|
2016-03-10 14:46:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:52:32 +03:00
|
|
|
// Tree grid node class
|
|
|
|
|
2016-02-20 13:31:29 +03:00
|
|
|
function TreeGridNode(node, grid, level, insertBefore, startHidden)
|
|
|
|
{
|
|
|
|
this.grid = grid;
|
|
|
|
this.level = level;
|
2016-06-05 01:37:11 +03:00
|
|
|
this.key = node.key;
|
2016-02-24 17:08:00 +03:00
|
|
|
this.data = node.data;
|
2016-02-20 13:31:29 +03:00
|
|
|
if (this.level === undefined)
|
|
|
|
this.level = -1;
|
|
|
|
if (!grid.root)
|
|
|
|
grid.root = this;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.leaf = node.leaf;
|
|
|
|
this.collapsed = node.collapsed || !node.leaf && (!node.children || !node.children.length);
|
|
|
|
this.tr = document.createElement('tr');
|
|
|
|
if (startHidden)
|
|
|
|
this.tr.style.display = 'none';
|
2016-02-24 18:48:09 +03:00
|
|
|
this.tr._node = this;
|
2016-06-05 01:17:51 +03:00
|
|
|
this._oldCells = [];
|
|
|
|
this.render();
|
2016-02-20 13:31:29 +03:00
|
|
|
insertBefore ? grid.tbody.insertBefore(this.tr, insertBefore) : grid.tbody.appendChild(this.tr);
|
|
|
|
}
|
|
|
|
this.children = [];
|
2016-06-05 01:37:11 +03:00
|
|
|
this.childrenByKey = {};
|
2016-02-20 13:31:29 +03:00
|
|
|
if (node.children)
|
2016-06-05 01:37:11 +03:00
|
|
|
{
|
2016-08-12 15:29:58 +03:00
|
|
|
var trs = [];
|
2016-02-20 13:31:29 +03:00
|
|
|
for (var i = 0; i < node.children.length; i++)
|
2016-06-05 01:37:11 +03:00
|
|
|
{
|
|
|
|
var child = new TreeGridNode(node.children[i], grid, this.level+1, insertBefore, this.collapsed);
|
|
|
|
child._index = i;
|
|
|
|
this.children.push(child);
|
2016-06-06 15:34:14 +03:00
|
|
|
if (child.key !== undefined)
|
2016-06-05 01:37:11 +03:00
|
|
|
this.childrenByKey[child.key] = child;
|
2016-08-12 15:29:58 +03:00
|
|
|
if (grid.stickyInit && !this.collapsed)
|
|
|
|
trs.push(child.tr);
|
2016-06-05 01:37:11 +03:00
|
|
|
}
|
2016-08-12 15:29:58 +03:00
|
|
|
if (trs.length)
|
|
|
|
fixStickyRows(grid.table, trs);
|
2016-06-05 01:37:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 14:02:11 +03:00
|
|
|
TreeGridNode.prototype.getKey = function()
|
|
|
|
{
|
|
|
|
return this.key;
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:37:11 +03:00
|
|
|
TreeGridNode.prototype.getChildByKey = function(key)
|
|
|
|
{
|
|
|
|
return this.childrenByKey[key];
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:17:51 +03:00
|
|
|
TreeGridNode.prototype._addCollapser = function()
|
2016-05-18 18:38:22 +03:00
|
|
|
{
|
|
|
|
var collapser = document.createElement('div');
|
|
|
|
if (this.leaf)
|
|
|
|
collapser.className = 'collapser collapser-inactive';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
collapser.className = this.collapsed ? 'collapser collapser-collapsed' : 'collapser collapser-expanded';
|
|
|
|
addListener(collapser, 'click', this._getToggleHandler());
|
|
|
|
}
|
|
|
|
var c0 = this.tr.cells[0];
|
|
|
|
c0.childNodes.length ? c0.insertBefore(collapser, c0.firstChild) : c0.appendChild(collapser);
|
|
|
|
if (this.grid._initialPadding === undefined)
|
|
|
|
{
|
|
|
|
var p0 = curStyle(c0).paddingLeft || '';
|
|
|
|
if (p0.substr(p0.length-2) == 'px')
|
|
|
|
p0 = parseInt(p0.substr(0, p0.length-2));
|
|
|
|
else
|
|
|
|
p0 = 0;
|
|
|
|
this.grid._initialPadding = p0;
|
|
|
|
}
|
|
|
|
c0.style.paddingLeft = (this.grid._initialPadding + this.grid.levelIndent * this.level + 20) + 'px';
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:17:51 +03:00
|
|
|
TreeGridNode.prototype._hashEqual = function(a, b)
|
|
|
|
{
|
|
|
|
for (var i in a)
|
|
|
|
if (b[i] !== a[i])
|
|
|
|
return false;
|
|
|
|
for (var i in b)
|
|
|
|
if (b[i] !== a[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeGridNode.prototype._renderCell = function(i, cell)
|
|
|
|
{
|
2016-08-12 15:29:58 +03:00
|
|
|
var ri = this.stickyInit ? (i > 0 ? i+1 : 0) : i;
|
|
|
|
if (!this.tr.cells[ri])
|
2016-06-05 01:17:51 +03:00
|
|
|
this.tr.appendChild(document.createElement('td'));
|
|
|
|
// virtualDOM-like approach: compare old HTML properties
|
|
|
|
if (!this._oldCells[i] || cell && !this._hashEqual(this._oldCells[i], cell))
|
|
|
|
{
|
2016-08-12 15:29:58 +03:00
|
|
|
this.grid._setProps(this.tr.cells[ri], cell);
|
2016-06-05 01:17:51 +03:00
|
|
|
if (i == 0)
|
|
|
|
this._addCollapser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeGridNode.prototype.render = function(colidx)
|
2016-05-18 18:38:22 +03:00
|
|
|
{
|
2016-06-05 01:17:51 +03:00
|
|
|
var cells = this.grid.renderer(this, colidx);
|
|
|
|
if (colidx !== null && colidx !== undefined && colidx >= 0 && colidx < this.grid.header.length)
|
|
|
|
{
|
|
|
|
this._renderCell(colidx, cells[colidx]);
|
|
|
|
this._oldCells[colidx] = cells[colidx];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (var i = 0; i < this.grid.header.length; i++)
|
|
|
|
this._renderCell(i, cells[i]);
|
|
|
|
this._oldCells = cells;
|
|
|
|
}
|
2016-05-18 18:38:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-15 11:16:50 +03:00
|
|
|
TreeGridNode.prototype._getToggleHandler = function()
|
|
|
|
{
|
|
|
|
var self = this;
|
|
|
|
if (!self._toggleHandlerClosure)
|
|
|
|
self._toggleHandlerClosure = function(e) { self._toggleHandler(); };
|
|
|
|
return self._toggleHandlerClosure;
|
|
|
|
}
|
|
|
|
|
2016-02-20 13:31:29 +03:00
|
|
|
TreeGridNode.prototype._toggleHandler = function()
|
|
|
|
{
|
|
|
|
if (!this.grid.onExpand(this))
|
|
|
|
return;
|
|
|
|
this.toggle();
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeGridNode.prototype.toggle = function()
|
|
|
|
{
|
|
|
|
if (this.leaf)
|
|
|
|
return;
|
|
|
|
this.collapsed = !this.collapsed;
|
|
|
|
this.tr.cells[0].firstChild.className = this.collapsed ? 'collapser collapser-collapsed' : 'collapser collapser-expanded';
|
|
|
|
if (this.collapsed)
|
|
|
|
{
|
|
|
|
// collapse all children
|
|
|
|
var c = this.tr.nextSibling;
|
2016-02-24 18:48:09 +03:00
|
|
|
while (c && c._node.level > this.level)
|
2016-02-20 13:31:29 +03:00
|
|
|
{
|
|
|
|
c.style.display = 'none';
|
|
|
|
c = c.nextSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// expand all children except children of collapsed subnodes
|
|
|
|
var st = this.children.concat([]);
|
2016-08-12 15:29:58 +03:00
|
|
|
var trs = [];
|
2016-02-20 13:31:29 +03:00
|
|
|
while (st.length)
|
|
|
|
{
|
|
|
|
var e = st.pop();
|
|
|
|
e.tr.style.display = '';
|
|
|
|
if (!e.collapsed)
|
|
|
|
st = st.concat(e.children);
|
2016-08-12 15:29:58 +03:00
|
|
|
if (this.grid.stickyInit)
|
|
|
|
trs.push(e.tr);
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
2016-08-12 15:29:58 +03:00
|
|
|
if (trs.length)
|
|
|
|
initStickyRows(this.grid.table, trs, true);
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
2016-08-12 15:29:58 +03:00
|
|
|
if (this.grid.stickyInit)
|
|
|
|
fixStickyColumnSizes(this.grid.table);
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeGridNode.prototype.setChildren = function(isLeaf, newChildren)
|
|
|
|
{
|
|
|
|
if (!this.tr)
|
|
|
|
this.grid.tbody.innerHTML = '';
|
|
|
|
else
|
2016-03-04 12:27:56 +03:00
|
|
|
{
|
2016-02-24 18:48:09 +03:00
|
|
|
while (this.tr.nextSibling && this.tr.nextSibling._node.level > this.level)
|
2016-02-20 13:31:29 +03:00
|
|
|
this.grid.tbody.removeChild(this.tr.nextSibling);
|
2016-05-15 11:16:50 +03:00
|
|
|
if (this.leaf != isLeaf)
|
|
|
|
{
|
|
|
|
if (isLeaf)
|
|
|
|
{
|
|
|
|
this.tr.cells[0].firstChild.className = 'collapser collapser-inactive';
|
|
|
|
removeListener(this.tr.cells[0].firstChild, 'click', this._getToggleHandler());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.tr.cells[0].firstChild.className = this.collapsed ? 'collapser collapser-collapsed' : 'collapser collapser-expanded';
|
|
|
|
addListener(this.tr.cells[0].firstChild, 'click', this._getToggleHandler());
|
|
|
|
}
|
|
|
|
}
|
2016-03-04 12:27:56 +03:00
|
|
|
}
|
2016-05-15 11:16:50 +03:00
|
|
|
this.leaf = isLeaf;
|
2016-02-20 13:31:29 +03:00
|
|
|
this.children = [];
|
2016-06-05 01:37:11 +03:00
|
|
|
this.childrenByKey = {};
|
2016-03-25 18:47:55 +03:00
|
|
|
this.addChildren(newChildren);
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:29:58 +03:00
|
|
|
/*// experimental & broken
|
2016-06-05 01:37:11 +03:00
|
|
|
TreeGridNode.prototype._syncChildren = function()
|
2016-03-25 18:47:55 +03:00
|
|
|
{
|
2016-05-15 19:06:43 +03:00
|
|
|
var i, j;
|
|
|
|
for (var i = 0; i < this.children.length; i++)
|
|
|
|
{
|
|
|
|
delete this.children[i]._rownum;
|
|
|
|
}
|
|
|
|
var rows = [];
|
|
|
|
var e = this.tr ? this.tr.nextSibling : this.grid.tbody.firstChild;
|
|
|
|
i = 0;
|
|
|
|
while (e && e._node.level > this.level)
|
|
|
|
{
|
|
|
|
if (e._node.level == this.level+1)
|
|
|
|
{
|
|
|
|
if (rows.length > 0)
|
|
|
|
rows[rows.length-1][2] = i;
|
|
|
|
e._node._rownum = rows.length;
|
|
|
|
rows.push([ e, i, i ]);
|
|
|
|
}
|
|
|
|
e = e.nextSibling;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (rows.length > 0)
|
|
|
|
rows[rows.length-1][2] = i;
|
|
|
|
e = this.tr ? this.tr.nextSibling : this.grid.tbody.firstChild;
|
|
|
|
for (var i = 0; i < this.children.length; i++)
|
|
|
|
{
|
|
|
|
if (this.children[i]._rownum !== undefined)
|
|
|
|
{
|
|
|
|
var k = rows[this.children[i]._rownum][0];
|
|
|
|
var n = rows[this.children[i]._rownum][2]-rows[this.children[i]._rownum][1];
|
|
|
|
delete this.children[i]._rownum;
|
|
|
|
var m;
|
|
|
|
while (n > 0)
|
|
|
|
{
|
|
|
|
// Ставим k перед e
|
|
|
|
m = k;
|
|
|
|
k = k.nextSibling;
|
|
|
|
if (!e)
|
|
|
|
this.grid.tbody.appendChild(k);
|
|
|
|
else if (e == k)
|
|
|
|
e = e.nextSibling;
|
|
|
|
else
|
|
|
|
e.parentNode.insertBefore(k, e);
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.children[i] = new TreeGridNode(this.children[i], this.grid, this.level+1, e, this.collapsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
while (e._node.level > this.level)
|
|
|
|
{
|
|
|
|
k = e;
|
|
|
|
e = e.nextSibling;
|
|
|
|
k.parentNode.removeChild(k);
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 15:29:58 +03:00
|
|
|
}*/
|
2016-05-15 19:06:43 +03:00
|
|
|
|
2016-06-05 01:37:11 +03:00
|
|
|
TreeGridNode.prototype.removeChild = function(nodeOrIndex)
|
2016-05-15 19:06:43 +03:00
|
|
|
{
|
2016-06-05 01:37:11 +03:00
|
|
|
if (nodeOrIndex instanceof TreeGridNode)
|
|
|
|
{
|
|
|
|
if (this.children[nodeOrIndex._index] == nodeOrIndex)
|
|
|
|
nodeOrIndex = nodeOrIndex._index;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!this.children[nodeOrIndex])
|
|
|
|
return false;
|
|
|
|
var e = this.children[nodeOrIndex].tr;
|
|
|
|
var l = this.children[nodeOrIndex].level;
|
2016-06-06 15:34:14 +03:00
|
|
|
if (this.children[nodeOrIndex].key !== undefined)
|
2016-06-05 01:37:11 +03:00
|
|
|
delete this.childrenByKey[this.children[nodeOrIndex].key];
|
|
|
|
this.children.splice(nodeOrIndex, 1);
|
|
|
|
for (var i = nodeOrIndex; i < this.children.length; i++)
|
|
|
|
this.children[i]._index--;
|
2016-05-15 19:06:43 +03:00
|
|
|
var k;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
k = e;
|
|
|
|
e = e.nextSibling;
|
|
|
|
k.parentNode.removeChild(k);
|
|
|
|
} while (e && e._node.level > l);
|
2016-06-05 01:37:11 +03:00
|
|
|
return true;
|
2016-05-15 19:06:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeGridNode.prototype.addChildren = function(nodes, insertBefore)
|
|
|
|
{
|
|
|
|
var e;
|
|
|
|
if (insertBefore < 0)
|
|
|
|
insertBefore = 0;
|
|
|
|
if (insertBefore === undefined || insertBefore === null || insertBefore === false ||
|
|
|
|
insertBefore >= this.children.length)
|
|
|
|
{
|
|
|
|
insertBefore = this.children.length;
|
|
|
|
e = this;
|
|
|
|
while (e.children.length)
|
|
|
|
e = e.children[e.children.length-1];
|
|
|
|
e = e.tr ? e.tr.nextSibling : null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
e = this.children[insertBefore].tr;
|
2016-08-12 15:29:58 +03:00
|
|
|
var trs = [];
|
2016-05-15 19:06:43 +03:00
|
|
|
for (var i = 0; i < nodes.length; i++)
|
2016-06-05 01:37:11 +03:00
|
|
|
{
|
|
|
|
var child = new TreeGridNode(nodes[i], this.grid, this.level+1, e, this.collapsed);
|
|
|
|
child._index = insertBefore+i;
|
|
|
|
this.children.splice(insertBefore+i, 0, child);
|
2016-06-06 15:34:14 +03:00
|
|
|
if (child.key !== undefined)
|
2016-06-05 01:37:11 +03:00
|
|
|
this.childrenByKey[child.key] = child;
|
2016-08-12 17:08:01 +03:00
|
|
|
// TODO: fix sticky only once per whole node batch, including children
|
2016-08-12 15:29:58 +03:00
|
|
|
if (this.grid.stickyInit && !this.collapsed)
|
|
|
|
trs.push(child.tr);
|
2016-06-05 01:37:11 +03:00
|
|
|
}
|
|
|
|
for (var i = insertBefore+nodes.length; i < this.children.length; i++)
|
|
|
|
this.children[i]._index += nodes.length;
|
2016-08-12 15:29:58 +03:00
|
|
|
if (trs.length)
|
|
|
|
fixStickyRows(this.grid.table, trs);
|
2016-05-18 18:38:22 +03:00
|
|
|
return this.children.slice(insertBefore, nodes.length);
|
2016-02-20 13:31:29 +03:00
|
|
|
}
|
2016-08-15 16:22:13 +03:00
|
|
|
|
|
|
|
;
|