You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.3 KiB
JavaScript
121 lines
3.3 KiB
JavaScript
9 years ago
|
define([
|
||
|
'/bower_components/jquery/dist/jquery.min.js',
|
||
|
],function () {
|
||
|
var $ = window.jQuery;
|
||
|
|
||
|
var Table = function ($t, xy) {
|
||
|
var $head = $t.find('thead');
|
||
|
var $body = $t.find('tbody');
|
||
|
var $foot = $t.find('tfoot');
|
||
|
|
||
9 years ago
|
var rows = [];
|
||
|
var cols = [];
|
||
|
|
||
9 years ago
|
var addRow = function (first, Rest, uid) {
|
||
|
var $row = $('<tr>', {
|
||
|
'data-rt-uid': uid,
|
||
|
}).append($('<td>').append(first));
|
||
|
|
||
|
$head.find('th').each(function (i) {
|
||
|
var colId = $(this).data('rt-uid');
|
||
9 years ago
|
$row.append($('<td>', {
|
||
|
'class': 'checkbox-cell',
|
||
|
}).append(Rest(xy(colId, uid))));
|
||
9 years ago
|
});
|
||
|
|
||
9 years ago
|
rows.push(uid);
|
||
|
|
||
9 years ago
|
$body.append($row);
|
||
|
return $row;
|
||
|
};
|
||
|
|
||
|
var addColumn = function (first, Rest, uid) {
|
||
|
$head.find('tr').append($('<th>', {
|
||
|
'data-rt-uid': uid,
|
||
|
}).append(first));
|
||
|
|
||
9 years ago
|
var $width = $body.find('tr').each(function (i) {
|
||
9 years ago
|
// each checkbox needs a uid corresponding to its role
|
||
|
var rowId = $(this).data('rt-uid');
|
||
9 years ago
|
$(this).append($('<td>', {
|
||
|
'class': 'checkbox-cell',
|
||
|
}).append(Rest(xy(uid, rowId))));
|
||
9 years ago
|
});
|
||
|
|
||
9 years ago
|
cols.push(uid);
|
||
|
|
||
9 years ago
|
$foot.find('tr').append($('<td>', { }));
|
||
9 years ago
|
return $width.length;
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
var remove = function ($sel) {
|
||
|
$sel.fadeOut(750, function () {
|
||
|
$sel.remove();
|
||
|
});
|
||
|
};
|
||
|
|
||
9 years ago
|
var removeFromArray = function (A, e) {
|
||
|
var i = A.indexOf(e);
|
||
|
if (i === -1) { return; }
|
||
|
A.splice(i, 1);
|
||
|
};
|
||
|
|
||
9 years ago
|
var removeColumn = function (uid) {
|
||
|
//var I/
|
||
9 years ago
|
var $col = $head.find('th[data-rt-uid="' + uid + '"]');
|
||
9 years ago
|
if (!$col.length) { return; }
|
||
|
|
||
|
/* removing a column is difficult because the elements
|
||
|
all have different parents. */
|
||
|
|
||
|
// use the th
|
||
|
var th = $col[0];
|
||
|
|
||
|
// find its index in the tr
|
||
|
var index = Array.prototype.slice
|
||
|
.call(th.parentElement.children).indexOf(th);
|
||
|
|
||
|
// remove it
|
||
|
remove($col);
|
||
|
|
||
9 years ago
|
removeFromArray(cols, uid);
|
||
|
|
||
9 years ago
|
// remove all elements in the body which have the same index
|
||
|
$body.find('tr').each(function () {
|
||
|
var $this = $(this);
|
||
|
$this.find('td').eq(index).each(function () {
|
||
|
remove($(this));
|
||
|
});
|
||
|
});
|
||
|
};
|
||
|
|
||
|
var removeRow = function (uid) {
|
||
|
var $row = $body.find('tr[data-rt-uid="' + uid + '"]');
|
||
|
if (!$row.length) { return; }
|
||
|
remove($row);
|
||
9 years ago
|
removeFromArray(rows, uid);
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
var colExists = function (uid) {
|
||
|
return cols.indexOf(uid) !== -1;
|
||
|
};
|
||
|
|
||
|
var rowExists = function (uid) {
|
||
|
return rows.indexOf(uid) !== -1;
|
||
|
};
|
||
|
|
||
9 years ago
|
return {
|
||
|
$: $t,
|
||
|
addRow: addRow,
|
||
|
addColumn: addColumn,
|
||
9 years ago
|
removeRow: removeRow,
|
||
|
removeColumn: removeColumn,
|
||
9 years ago
|
rows: rows,
|
||
9 years ago
|
rowExists: rowExists,
|
||
9 years ago
|
cols: cols,
|
||
9 years ago
|
colExists: colExists,
|
||
9 years ago
|
};
|
||
|
};
|
||
|
return Table;
|
||
|
});
|