Files
2012-05-30 23:00:06 -04:00

109 lines
2.3 KiB
JavaScript

// Generated by CoffeeScript 1.3.1
var app, get, ready, render, sortableTable, _ref;
_ref = app = require('./index'), get = _ref.get, ready = _ref.ready;
render = require('./shared').render;
sortableTable = require('./sortableTable');
get('/table', function(page, model) {
return model.subscribe('table', function(err, table) {
table.setNull({
rows: [
{
name: 1,
cells: [{}, {}, {}]
}, {
name: 2,
cells: [{}, {}, {}]
}
],
lastRow: 1,
cols: [
{
name: 'A'
}, {
name: 'B'
}, {
name: 'C'
}
],
lastCol: 2
});
return render(page, 'tableEditor');
});
});
ready(function(model) {
var alpha, cols, rows;
rows = model.at('table.rows');
cols = model.at('table.cols');
app.tableEditor = {
deleteRow: function(e, el) {
return model.at(el).remove();
},
deleteCol: function(e, el) {
var i, row;
i = model.at(el).leaf();
row = rows.get('length');
while (row--) {
rows.remove("" + row + ".cells", i);
}
return cols.remove(i);
},
addRow: function() {
var cells, col, name;
name = model.incr('table.lastRow') + 1;
cells = [];
col = cols.get('length');
while (col--) {
cells.push({});
}
return rows.push({
name: name,
cells: cells
});
},
addCol: function() {
var name, row;
row = rows.get('length');
while (row--) {
rows.push("" + row + ".cells", {});
}
name = alpha(model.incr('table.lastCol'));
return cols.push({
name: name
});
}
};
alpha = function(num, out) {
var mod;
if (out == null) {
out = '';
}
mod = num % 26;
out = String.fromCharCode(65 + mod) + out;
if (num = Math.floor(num / 26)) {
return alpha(num - 1, out);
} else {
return out;
}
};
return sortableTable.init(app, app.tableEditor, {
onRowMove: function(from, to) {
return rows.move(from, to);
},
onColMove: function(from, to) {
var row, _results;
cols.move(from, to);
row = rows.get('length');
_results = [];
while (row--) {
_results.push(rows.move("" + row + ".cells", from, to));
}
return _results;
}
});
});