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.
454 lines
14 KiB
JavaScript
454 lines
14 KiB
JavaScript
8 years ago
|
define([
|
||
8 years ago
|
//'/common/cryptpad-common.js',
|
||
8 years ago
|
'/bower_components/hyperjson/hyperjson.js',
|
||
8 years ago
|
'/bower_components/textpatcher/TextPatcher.js',
|
||
8 years ago
|
'/bower_components/diff-dom/diffDOM.js',
|
||
8 years ago
|
], function (Hyperjson, TextPatcher) {
|
||
8 years ago
|
var DiffDOM = window.diffDOM;
|
||
|
|
||
|
var Example = {
|
||
|
info: {
|
||
8 years ago
|
title: '',
|
||
|
description: '',
|
||
8 years ago
|
userData: {}
|
||
8 years ago
|
},
|
||
|
table: {
|
||
|
/* TODO
|
||
|
|
||
|
deprecate the practice of storing cells, cols, and rows separately.
|
||
|
|
||
|
Instead, keep everything in one map, and iterate over columns and rows
|
||
|
by maintaining indexes in rowsOrder and colsOrder
|
||
|
|
||
|
*/
|
||
|
cells: {},
|
||
|
cols: {},
|
||
|
colsOrder: [],
|
||
|
rows: {},
|
||
|
rowsOrder: []
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
var Renderer = function (Cryptpad) {
|
||
|
|
||
8 years ago
|
var Render = {
|
||
|
Example: Example
|
||
|
};
|
||
|
|
||
|
var Uid = Render.Uid = function (prefix, f) {
|
||
|
f = f || function () {
|
||
|
return Number(Math.random() * Number.MAX_SAFE_INTEGER)
|
||
|
.toString(32).replace(/\./g, '');
|
||
|
};
|
||
|
return function () { return prefix + '-' + f(); };
|
||
|
};
|
||
|
|
||
|
var coluid = Render.coluid = Uid('x');
|
||
|
var rowuid = Render.rowuid = Uid('y');
|
||
|
|
||
|
var isRow = Render.isRow = function (id) { return /^y\-[^_]*$/.test(id); };
|
||
|
var isColumn = Render.isColumn = function (id) { return /^x\-[^_]*$/.test(id); };
|
||
|
var isCell = Render.isCell = function (id) { return /^x\-[^_]*_y\-.*$/.test(id); };
|
||
|
|
||
|
var typeofId = Render.typeofId = function (id) {
|
||
|
if (isRow(id)) { return 'row'; }
|
||
|
if (isColumn(id)) { return 'col'; }
|
||
|
if (isCell(id)) { return 'cell'; }
|
||
|
return null;
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.getCoordinates = function (id) {
|
||
8 years ago
|
return id.split('_');
|
||
|
};
|
||
|
|
||
8 years ago
|
var getColumnValue = Render.getColumnValue = function (obj, colId) {
|
||
|
return Cryptpad.find(obj, ['table', 'cols'].concat([colId]));
|
||
|
};
|
||
|
|
||
|
var getRowValue = Render.getRowValue = function (obj, rowId) {
|
||
|
return Cryptpad.find(obj, ['table', 'rows'].concat([rowId]));
|
||
|
};
|
||
|
|
||
|
var getCellValue = Render.getCellValue = function (obj, cellId) {
|
||
8 years ago
|
var value = Cryptpad.find(obj, ['table', 'cells'].concat([cellId]));
|
||
|
if (typeof value === 'boolean') {
|
||
8 years ago
|
return (value === true ? 1 : 0);
|
||
8 years ago
|
} else {
|
||
|
return value;
|
||
|
}
|
||
8 years ago
|
};
|
||
|
|
||
|
var setRowValue = Render.setRowValue = function (obj, rowId, value) {
|
||
|
var parent = Cryptpad.find(obj, ['table', 'rows']);
|
||
|
if (typeof(parent) === 'object') { return (parent[rowId] = value); }
|
||
|
return null;
|
||
|
};
|
||
|
|
||
|
var setColumnValue = Render.setColumnValue = function (obj, colId, value) {
|
||
|
var parent = Cryptpad.find(obj, ['table', 'cols']);
|
||
|
if (typeof(parent) === 'object') { return (parent[colId] = value); }
|
||
|
return null;
|
||
|
};
|
||
|
|
||
|
var setCellValue = Render.setCellValue = function (obj, cellId, value) {
|
||
|
var parent = Cryptpad.find(obj, ['table', 'cells']);
|
||
|
if (typeof(parent) === 'object') { return (parent[cellId] = value); }
|
||
|
return null;
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.createColumn = function (obj, cb, id, value) {
|
||
8 years ago
|
var order = Cryptpad.find(obj, ['table', 'colsOrder']);
|
||
|
if (!order) { throw new Error("Uninitialized realtime object!"); }
|
||
|
id = id || coluid();
|
||
|
value = value || "";
|
||
|
setColumnValue(obj, id, value);
|
||
|
order.push(id);
|
||
|
if (typeof(cb) === 'function') { cb(void 0, id); }
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.removeColumn = function (obj, id, cb) {
|
||
8 years ago
|
var order = Cryptpad.find(obj, ['table', 'colsOrder']);
|
||
|
var parent = Cryptpad.find(obj, ['table', 'cols']);
|
||
|
|
||
|
if (!(order && parent)) { throw new Error("Uninitialized realtime object!"); }
|
||
|
|
||
|
var idx = order.indexOf(id);
|
||
|
if (idx === -1) {
|
||
|
return void console
|
||
|
.error(new Error("Attempted to remove id which does not exist"));
|
||
|
}
|
||
|
|
||
8 years ago
|
Object.keys(obj.table.cells).forEach(function (key) {
|
||
|
if (key.indexOf(id) === 0) {
|
||
|
delete obj.table.cells[key];
|
||
|
}
|
||
|
});
|
||
|
|
||
8 years ago
|
order.splice(idx, 1);
|
||
|
if (parent[id]) { delete parent[id]; }
|
||
|
if (typeof(cb) === 'function') {
|
||
|
cb();
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.createRow = function (obj, cb, id, value) {
|
||
8 years ago
|
var order = Cryptpad.find(obj, ['table', 'rowsOrder']);
|
||
|
if (!order) { throw new Error("Uninitialized realtime object!"); }
|
||
|
id = id || rowuid();
|
||
|
value = value || "";
|
||
|
setRowValue(obj, id, value);
|
||
|
order.push(id);
|
||
|
if (typeof(cb) === 'function') { cb(void 0, id); }
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.removeRow = function (obj, id, cb) {
|
||
8 years ago
|
var order = Cryptpad.find(obj, ['table', 'rowsOrder']);
|
||
|
var parent = Cryptpad.find(obj, ['table', 'rows']);
|
||
|
|
||
|
if (!(order && parent)) { throw new Error("Uninitialized realtime object!"); }
|
||
|
|
||
|
var idx = order.indexOf(id);
|
||
|
if (idx === -1) {
|
||
|
return void console
|
||
|
.error(new Error("Attempted to remove id which does not exist"));
|
||
|
}
|
||
|
|
||
|
order.splice(idx, 1);
|
||
|
if (parent[id]) { delete parent[id]; }
|
||
|
if (typeof(cb) === 'function') { cb(); }
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.setValue = function (obj, id, value) {
|
||
8 years ago
|
var type = typeofId(id);
|
||
8 years ago
|
|
||
8 years ago
|
switch (type) {
|
||
|
case 'row': return setRowValue(obj, id, value);
|
||
|
case 'col': return setColumnValue(obj, id, value);
|
||
|
case 'cell': return setCellValue(obj, id, value);
|
||
|
case null: break;
|
||
|
default:
|
||
|
console.log("[%s] has type [%s]", id, type);
|
||
|
throw new Error("Unexpected type!");
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.getValue = function (obj, id) {
|
||
8 years ago
|
switch (typeofId(id)) {
|
||
|
case 'row': return getRowValue(obj, id);
|
||
|
case 'col': return getColumnValue(obj, id);
|
||
|
case 'cell': return getCellValue(obj, id);
|
||
|
case null: break;
|
||
|
default: throw new Error("Unexpected type!");
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var getRowIds = Render.getRowIds = function (obj) {
|
||
|
return Cryptpad.find(obj, ['table', 'rowsOrder']);
|
||
|
};
|
||
|
|
||
|
var getColIds = Render.getColIds = function (obj) {
|
||
|
return Cryptpad.find(obj, ['table', 'colsOrder']);
|
||
|
};
|
||
|
|
||
|
var getCells = Render.getCells = function (obj) {
|
||
|
return Cryptpad.find(obj, ['table', 'cells']);
|
||
|
};
|
||
|
|
||
|
/* cellMatrix takes a proxy object, and optionally an alternate ordering
|
||
|
of row/column keys (as an array).
|
||
|
|
||
|
it returns an array of arrays containing the relevant data for each
|
||
|
cell in table we wish to construct.
|
||
|
*/
|
||
8 years ago
|
var cellMatrix = Render.cellMatrix = function (obj, rows, cols, readOnly) {
|
||
8 years ago
|
if (typeof(obj) !== 'object') {
|
||
|
throw new Error('expected realtime-proxy object');
|
||
|
}
|
||
|
|
||
|
var cells = getCells(obj);
|
||
|
rows = rows || getRowIds(obj);
|
||
8 years ago
|
rows.push('');
|
||
8 years ago
|
cols = cols || getColIds(obj);
|
||
|
|
||
|
return [null].concat(rows).map(function (row, i) {
|
||
|
if (i === 0) {
|
||
|
return [null].concat(cols.map(function (col) {
|
||
8 years ago
|
var result = {
|
||
8 years ago
|
'data-rt-id': col,
|
||
|
type: 'text',
|
||
8 years ago
|
value: getColumnValue(obj, col) || "",
|
||
8 years ago
|
placeholder: Cryptpad.Messages.poll_userPlaceholder,
|
||
8 years ago
|
disabled: 'disabled'
|
||
8 years ago
|
};
|
||
8 years ago
|
return result;
|
||
8 years ago
|
}));
|
||
|
}
|
||
8 years ago
|
if (i === rows.length) {
|
||
8 years ago
|
return [null].concat(cols.map(function () {
|
||
8 years ago
|
return {
|
||
|
'class': 'lastRow',
|
||
|
};
|
||
|
}));
|
||
|
}
|
||
8 years ago
|
|
||
|
return [{
|
||
|
'data-rt-id': row,
|
||
|
value: getRowValue(obj, row),
|
||
|
type: 'text',
|
||
8 years ago
|
placeholder: Cryptpad.Messages.poll_optionPlaceholder,
|
||
8 years ago
|
disabled: 'disabled'
|
||
8 years ago
|
}].concat(cols.map(function (col) {
|
||
|
var id = [col, rows[i-1]].join('_');
|
||
8 years ago
|
var val = cells[id];
|
||
8 years ago
|
var result = {
|
||
|
'data-rt-id': id,
|
||
8 years ago
|
type: 'number',
|
||
8 years ago
|
autocomplete: 'nope',
|
||
8 years ago
|
value: '3',
|
||
8 years ago
|
};
|
||
8 years ago
|
if (readOnly) {
|
||
|
result.disabled = "disabled";
|
||
|
}
|
||
8 years ago
|
if (typeof val !== 'undefined') {
|
||
|
if (typeof val === 'boolean') { val = (val ? '1' : '0'); }
|
||
|
result.value = val;
|
||
|
}
|
||
8 years ago
|
return result;
|
||
|
}));
|
||
|
});
|
||
|
};
|
||
|
|
||
|
var makeRemoveElement = Render.makeRemoveElement = function (id) {
|
||
8 years ago
|
return ['SPAN', {
|
||
|
'data-rt-id': id,
|
||
8 years ago
|
'title': Cryptpad.Messages.poll_remove,
|
||
8 years ago
|
class: 'remove',
|
||
|
}, ['✖']];
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var makeEditElement = Render.makeEditElement = function (id) {
|
||
|
return ['SPAN', {
|
||
|
'data-rt-id': id,
|
||
8 years ago
|
'title': Cryptpad.Messages.poll_edit,
|
||
8 years ago
|
class: 'edit',
|
||
8 years ago
|
}, ['✐']];
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var makeLockElement = Render.makeLockElement = function (id) {
|
||
|
return ['SPAN', {
|
||
|
'data-rt-id': id,
|
||
8 years ago
|
'title': Cryptpad.Messages.poll_locked,
|
||
8 years ago
|
class: 'lock fa fa-lock',
|
||
|
}, []];
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var makeHeadingCell = Render.makeHeadingCell = function (cell, readOnly) {
|
||
8 years ago
|
if (!cell) { return ['TD', {}, []]; }
|
||
|
if (cell.type === 'text') {
|
||
8 years ago
|
var elements = [['INPUT', cell, []]];
|
||
|
if (!readOnly) {
|
||
8 years ago
|
elements.unshift(makeRemoveElement(cell['data-rt-id']));
|
||
|
elements.unshift(makeLockElement(cell['data-rt-id']));
|
||
8 years ago
|
}
|
||
|
return ['TD', {}, elements];
|
||
8 years ago
|
}
|
||
|
return ['TD', cell, []];
|
||
|
};
|
||
|
|
||
8 years ago
|
var clone = function (o) {
|
||
|
return JSON.parse(JSON.stringify(o));
|
||
|
};
|
||
|
|
||
|
var makeCheckbox = Render.makeCheckbox = function (cell) {
|
||
|
var attrs = clone(cell);
|
||
|
|
||
|
// FIXME
|
||
|
attrs.id = cell['data-rt-id'];
|
||
|
|
||
|
var labelClass = 'cover';
|
||
|
|
||
8 years ago
|
// TODO implement Yes/No/Maybe/Undecided
|
||
8 years ago
|
return ['TD', {class:"checkbox-cell"}, [
|
||
|
['DIV', {class: 'checkbox-contain'}, [
|
||
|
['INPUT', attrs, []],
|
||
|
['SPAN', {class: labelClass}, []],
|
||
|
['LABEL', {
|
||
|
for: attrs.id,
|
||
|
'data-rt-id': attrs.id,
|
||
|
}, []]
|
||
|
]]
|
||
|
]];
|
||
|
};
|
||
|
|
||
8 years ago
|
var makeBodyCell = Render.makeBodyCell = function (cell, readOnly) {
|
||
8 years ago
|
if (cell && cell.type === 'text') {
|
||
8 years ago
|
var elements = [['INPUT', cell, []]];
|
||
|
if (!readOnly) {
|
||
8 years ago
|
elements.push(makeRemoveElement(cell['data-rt-id']));
|
||
|
elements.push(makeEditElement(cell['data-rt-id']));
|
||
8 years ago
|
}
|
||
8 years ago
|
return ['TD', {}, [
|
||
8 years ago
|
['DIV', {class: 'text-cell'}, elements]
|
||
8 years ago
|
]];
|
||
|
}
|
||
|
|
||
8 years ago
|
if (cell && cell.type === 'number') {
|
||
8 years ago
|
return makeCheckbox(cell);
|
||
8 years ago
|
}
|
||
|
return ['TD', cell, []];
|
||
|
};
|
||
|
|
||
8 years ago
|
var makeBodyRow = Render.makeBodyRow = function (row, readOnly) {
|
||
|
return ['TR', {}, row.map(function (cell) {
|
||
|
return makeBodyCell(cell, readOnly);
|
||
|
})];
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var toHyperjson = Render.toHyperjson = function (matrix, readOnly) {
|
||
8 years ago
|
if (!matrix || !matrix.length) { return; }
|
||
8 years ago
|
var head = ['THEAD', {}, [ ['TR', {}, matrix[0].map(function (cell) {
|
||
|
return makeHeadingCell(cell, readOnly);
|
||
|
})] ]];
|
||
8 years ago
|
var foot = ['TFOOT', {}, matrix.slice(-1).map(function (row) {
|
||
|
return makeBodyRow(row, readOnly);
|
||
|
})];
|
||
|
var body = ['TBODY', {}, matrix.slice(1, -1).map(function (row) {
|
||
8 years ago
|
return makeBodyRow(row, readOnly);
|
||
|
})];
|
||
8 years ago
|
return ['TABLE', {id:'table'}, [head, foot, body]];
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
Render.asHTML = function (obj, rows, cols, readOnly) {
|
||
8 years ago
|
return Hyperjson.toDOM(toHyperjson(cellMatrix(obj, rows, cols, readOnly), readOnly));
|
||
8 years ago
|
};
|
||
|
|
||
|
var diffIsInput = Render.diffIsInput = function (info) {
|
||
|
var nodeName = Cryptpad.find(info, ['node', 'nodeName']);
|
||
|
if (nodeName !== 'INPUT') { return; }
|
||
|
return true;
|
||
|
};
|
||
|
|
||
|
var getInputType = Render.getInputType = function (info) {
|
||
|
return Cryptpad.find(info, ['node', 'type']);
|
||
|
};
|
||
|
|
||
8 years ago
|
var preserveCursor = Render.preserveCursor = function (info) {
|
||
|
if (['modifyValue', 'modifyAttribute'].indexOf(info.diff.action) !== -1) {
|
||
|
var element = info.node;
|
||
8 years ago
|
|
||
|
if (typeof(element.selectionStart) !== 'number') { return; }
|
||
|
|
||
8 years ago
|
var o = info.oldValue || '';
|
||
|
var n = info.newValue || '';
|
||
|
var op = TextPatcher.diff(o, n);
|
||
|
|
||
|
info.selection = ['selectionStart', 'selectionEnd'].map(function (attr) {
|
||
8 years ago
|
return TextPatcher.transformCursor(element[attr], op);
|
||
8 years ago
|
});
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var recoverCursor = Render.recoverCursor = function (info) {
|
||
8 years ago
|
try {
|
||
|
if (info.selection && info.node) {
|
||
|
info.node.selectionStart = info.selection[0];
|
||
|
info.node.selectionEnd = info.selection[1];
|
||
|
}
|
||
|
} catch (err) {
|
||
8 years ago
|
// FIXME LOL empty try-catch?
|
||
8 years ago
|
//console.log(info.node);
|
||
|
//console.error(err);
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
var diffOptions = {
|
||
|
preDiffApply: function (info) {
|
||
|
if (!diffIsInput(info)) { return; }
|
||
|
switch (getInputType(info)) {
|
||
8 years ago
|
case 'number':
|
||
8 years ago
|
//console.log('checkbox');
|
||
|
//console.log("[preDiffApply]", info);
|
||
|
break;
|
||
|
case 'text':
|
||
8 years ago
|
preserveCursor(info);
|
||
8 years ago
|
break;
|
||
|
default: break;
|
||
|
}
|
||
|
},
|
||
|
postDiffApply: function (info) {
|
||
8 years ago
|
if (info.selection) { recoverCursor(info); }
|
||
8 years ago
|
/*
|
||
|
if (!diffIsInput(info)) { return; }
|
||
|
switch (getInputType(info)) {
|
||
|
case 'checkbox':
|
||
|
console.log("[postDiffApply]", info);
|
||
|
break;
|
||
|
case 'text': break;
|
||
|
default: break;
|
||
|
}*/
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
Render.updateTable = function (table, obj, conf) {
|
||
8 years ago
|
var DD = new DiffDOM(diffOptions);
|
||
|
|
||
8 years ago
|
var rows = conf ? conf.rows : null;
|
||
|
var cols = conf ? conf.cols : null;
|
||
8 years ago
|
var readOnly = conf ? conf.readOnly : false;
|
||
|
var matrix = cellMatrix(obj, rows, cols, readOnly);
|
||
8 years ago
|
|
||
8 years ago
|
var hj = toHyperjson(matrix, readOnly);
|
||
8 years ago
|
|
||
|
if (!hj) { throw new Error("Expected Hyperjson!"); }
|
||
|
|
||
|
var table2 = Hyperjson.toDOM(hj);
|
||
|
var patch = DD.diff(table, table2);
|
||
|
DD.apply(table, patch);
|
||
|
};
|
||
|
|
||
|
return Render;
|
||
8 years ago
|
};
|
||
|
|
||
|
return Renderer;
|
||
8 years ago
|
});
|