Bureaucracy.cells = {};

Bureaucracy.cells.update = function(label, self) {
	var oldValue = self.innerText;
	var newValue = prompt(label, oldValue) || oldValue;
	self.innerHTML = newValue;
	return newValue;
}

Bureaucracy.cells.set = function(rowId, colId, self) {
	var row = Bureaucracy.rows.find(rowId);
	var col = Bureaucracy.cols.find(colId);
	var value = Bureaucracy.cells.update(col.name, self);
	row[col._id] = value;
}

Bureaucracy.cells.edit = function(placeholder) {
	return function(value, id) {
		if (value === null) value = placeholder;
		var link = document.createElement('a');
		link.href = '#';
		link.innerText = value;
		link.setAttribute('onClick', 'Bureaucracy.cells.set(\''+id+'\', \''+this._id+'\', this)');
		return link.outerHTML;
	}
}

Bureaucracy.cells = {};

Bureaucracy.cells.set = function(rowId, colId, self) {
	var row = Bureaucracy.rows.find(rowId);
	var col = Bureaucracy.cols.find(colId);
	row[col._id] = self.value;
}

Bureaucracy.cells.edit = function(row, col) {
	if (row[col._id] === null) row[col._id] = "";
	// TODO
	if (col.type == Object) {
		var a = document.createElement('a');
		a.href = '#';
		a.innerHTML = row[col._id];
		a.setAttribute('onClick', 'alert("TODO")')
		return a.outerHTML;
	}
	else {
		var input = document.createElement('input');
		input.id = row._id + '-' + col._id;
		input.type = 'text' // TODO
		input.setAttribute('value', row[col._id]);
		input.setAttribute('onBlur', 'Bureaucracy.cells.set(\''+row._id+'\', \''+col._id+'\', this)');
		return input.outerHTML;
	}
}

Switch editable cells from prompt to input element