cryptpad/www/poll/render.js

579 lines
20 KiB
JavaScript
Raw Normal View History

2017-09-27 16:52:04 +00:00
define([
2017-10-12 10:20:27 +00:00
'jquery',
2017-09-27 16:52:04 +00:00
'/bower_components/hyperjson/hyperjson.js',
'/bower_components/textpatcher/TextPatcher.js',
2017-11-13 15:32:40 +00:00
'/common/common-util.js',
'/customize/messages.js',
2017-09-27 16:52:04 +00:00
'/bower_components/diff-dom/diffDOM.js',
2017-11-13 15:32:40 +00:00
], function ($, Hyperjson, TextPatcher, Util, Messages) {
2017-09-27 16:52:04 +00:00
var DiffDOM = window.diffDOM;
var Example = {
metadata: {
title: '',
userData: {}
},
2017-10-04 17:06:16 +00:00
description: '',
comments: {},
2017-09-27 16:52:04 +00:00
content: {
/* 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: []
}
};
2017-11-13 15:32:40 +00:00
var Renderer = function (APP) {
2017-09-27 16:52:04 +00:00
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;
};
Render.getCoordinates = function (id) {
return id.split('_');
};
var getColumnValue = Render.getColumnValue = function (obj, colId) {
2017-11-13 15:32:40 +00:00
return Util.find(obj, ['content', 'cols'].concat([colId]));
2017-09-27 16:52:04 +00:00
};
var getRowValue = Render.getRowValue = function (obj, rowId) {
2017-11-13 15:32:40 +00:00
return Util.find(obj, ['content', 'rows'].concat([rowId]));
2017-09-27 16:52:04 +00:00
};
var getCellValue = Render.getCellValue = function (obj, cellId) {
2017-11-13 15:32:40 +00:00
var value = Util.find(obj, ['content', 'cells'].concat([cellId]));
2017-09-27 16:52:04 +00:00
if (typeof value === 'boolean') {
return (value === true ? 1 : 0);
} else {
return value;
}
};
var setRowValue = Render.setRowValue = function (obj, rowId, value) {
2017-11-13 15:32:40 +00:00
var parent = Util.find(obj, ['content', 'rows']);
2017-09-27 16:52:04 +00:00
if (typeof(parent) === 'object') { return (parent[rowId] = value); }
return null;
};
var setColumnValue = Render.setColumnValue = function (obj, colId, value) {
2017-11-13 15:32:40 +00:00
var parent = Util.find(obj, ['content', 'cols']);
2017-09-27 16:52:04 +00:00
if (typeof(parent) === 'object') { return (parent[colId] = value); }
return null;
};
var setCellValue = Render.setCellValue = function (obj, cellId, value) {
2017-11-13 15:32:40 +00:00
var parent = Util.find(obj, ['content', 'cells']);
2017-09-27 16:52:04 +00:00
if (typeof(parent) === 'object') { return (parent[cellId] = value); }
return null;
};
Render.createColumn = function (obj, cb, id, value) {
2017-11-13 15:32:40 +00:00
var order = Util.find(obj, ['content', 'colsOrder']);
2017-09-27 16:52:04 +00:00
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); }
};
Render.removeColumn = function (obj, id, cb) {
2017-11-13 15:32:40 +00:00
var order = Util.find(obj, ['content', 'colsOrder']);
var parent = Util.find(obj, ['content', 'cols']);
2017-09-27 16:52:04 +00:00
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"));
}
Object.keys(obj.content.cells).forEach(function (key) {
if (key.indexOf(id) === 0) {
delete obj.content.cells[key];
}
});
order.splice(idx, 1);
if (parent[id]) { delete parent[id]; }
if (typeof(cb) === 'function') {
cb();
}
};
Render.createRow = function (obj, cb, id, value) {
2017-11-13 15:32:40 +00:00
var order = Util.find(obj, ['content', 'rowsOrder']);
2017-09-27 16:52:04 +00:00
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); }
};
Render.removeRow = function (obj, id, cb) {
2017-11-13 15:32:40 +00:00
var order = Util.find(obj, ['content', 'rowsOrder']);
var parent = Util.find(obj, ['content', 'rows']);
2017-09-27 16:52:04 +00:00
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(); }
};
Render.setValue = function (obj, id, value) {
var type = typeofId(id);
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!");
}
};
Render.getValue = function (obj, id) {
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) {
2017-11-13 15:32:40 +00:00
return Util.find(obj, ['content', 'rowsOrder']);
2017-09-27 16:52:04 +00:00
};
var getColIds = Render.getColIds = function (obj) {
2017-11-13 15:32:40 +00:00
return Util.find(obj, ['content', 'colsOrder']);
2017-09-27 16:52:04 +00:00
};
var getCells = Render.getCells = function (obj) {
2017-11-13 15:32:40 +00:00
return Util.find(obj, ['content', 'cells']);
2017-09-27 16:52:04 +00:00
};
/* 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.
*/
var cellMatrix = Render.cellMatrix = function (obj, rows, cols, readOnly) {
if (typeof(obj) !== 'object') {
throw new Error('expected realtime-proxy object');
}
var cells = getCells(obj);
rows = rows || getRowIds(obj);
rows.push('');
cols = cols || getColIds(obj);
return [null].concat(rows).map(function (row, i) {
if (i === 0) {
return [null].concat(cols.map(function (col) {
var result = {
'data-rt-id': col,
type: 'text',
value: getColumnValue(obj, col) || "",
2017-11-13 15:32:40 +00:00
title: getColumnValue(obj, col) || Messages.anonymous,
placeholder: Messages.anonymous,
2017-09-27 16:52:04 +00:00
disabled: 'disabled'
};
return result;
2017-10-10 16:19:49 +00:00
})).concat([{
2017-11-13 15:32:40 +00:00
content: Messages.poll_total
2017-10-10 16:19:49 +00:00
}]);
2017-09-27 16:52:04 +00:00
}
if (i === rows.length) {
return [null].concat(cols.map(function () {
return {
'class': 'cp-app-poll-table-lastrow',
};
}));
}
return [{
'data-rt-id': row,
value: getRowValue(obj, row) || '',
2017-11-13 15:32:40 +00:00
title: getRowValue(obj, row) || Messages.poll_optionPlaceholder,
2017-09-27 16:52:04 +00:00
type: 'text',
2017-11-13 15:32:40 +00:00
placeholder: Messages.poll_optionPlaceholder,
2017-09-27 16:52:04 +00:00
disabled: 'disabled',
}].concat(cols.map(function (col) {
var id = [col, rows[i-1]].join('_');
var val = cells[id];
var result = {
'data-rt-id': id,
type: 'number',
autocomplete: 'nope',
value: '3',
};
if (readOnly) {
result.disabled = "disabled";
}
if (typeof val !== 'undefined') {
if (typeof val === 'boolean') { val = (val ? '1' : '0'); }
result.value = val;
}
return result;
2017-10-02 17:02:31 +00:00
})).concat([{
'data-rt-count-id': row
}]);
2017-09-27 16:52:04 +00:00
});
};
var makeRemoveElement = Render.makeRemoveElement = function (id) {
return ['SPAN', {
'data-rt-id': id,
2017-11-13 15:32:40 +00:00
'title': Messages.poll_remove,
2017-09-27 16:52:04 +00:00
class: 'cp-app-poll-table-remove',
}, ['✖']];
};
var makeEditElement = Render.makeEditElement = function (id) {
return ['SPAN', {
'data-rt-id': id,
2017-11-13 15:32:40 +00:00
'title': Messages.poll_edit,
2017-09-27 16:52:04 +00:00
class: 'cp-app-poll-table-edit',
}, ['✐']];
};
var makeLockElement = Render.makeLockElement = function (id) {
return ['SPAN', {
'data-rt-id': id,
2017-11-13 15:32:40 +00:00
'title': Messages.poll_locked,
2017-09-27 16:52:04 +00:00
class: 'cp-app-poll-table-lock fa fa-lock',
}, []];
};
2017-10-02 17:02:31 +00:00
var makeBookmarkElement = Render.makeBookmarkElement = function (id) {
return ['SPAN', {
'data-rt-id': id,
2017-11-13 15:32:40 +00:00
'title': Messages.poll_bookmark_col,
2017-10-02 17:02:31 +00:00
'style': 'visibility: hidden;',
2017-10-03 14:50:59 +00:00
class: 'cp-app-poll-table-bookmark fa fa-thumb-tack',
2017-10-02 17:02:31 +00:00
}, []];
};
2017-09-27 16:52:04 +00:00
var makeHeadingCell = Render.makeHeadingCell = function (cell, readOnly) {
if (!cell) { return ['TD', {}, []]; }
if (cell.type === 'text') {
var elements = [['INPUT', cell, []]];
if (!readOnly) {
2017-10-02 17:02:31 +00:00
var buttons = [];
buttons.unshift(makeRemoveElement(cell['data-rt-id']));
buttons.unshift(makeLockElement(cell['data-rt-id']));
buttons.unshift(makeBookmarkElement(cell['data-rt-id']));
elements.unshift(['DIV', {'class': 'cp-app-poll-table-buttons'}, buttons]);
2017-09-27 16:52:04 +00:00
}
return ['TD', {}, elements];
}
2017-10-10 16:19:49 +00:00
return ['TD', cell, [cell.content]];
2017-09-27 16:52:04 +00:00
};
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 = 'cp-app-poll-table-cover';
// TODO implement Yes/No/Maybe/Undecided
return ['TD', {class:"cp-app-poll-table-checkbox-cell"}, [
['DIV', {class: 'cp-app-poll-table-checkbox-contain'}, [
['INPUT', attrs, []],
['SPAN', {class: labelClass}, []],
['LABEL', {
for: attrs.id,
'data-rt-id': attrs.id,
}, []]
]]
]];
};
var makeBodyCell = Render.makeBodyCell = function (cell, readOnly) {
if (cell && cell.type === 'text') {
var elements = [['INPUT', cell, []]];
if (!readOnly) {
elements.push(makeRemoveElement(cell['data-rt-id']));
elements.push(makeEditElement(cell['data-rt-id']));
}
return ['TD', {}, [
['DIV', {class: 'cp-app-poll-table-text-cell'}, elements]
]];
}
if (cell && cell.type === 'number') {
return makeCheckbox(cell);
}
return ['TD', cell, []];
};
var makeBodyRow = Render.makeBodyRow = function (row, readOnly) {
return ['TR', {}, row.map(function (cell) {
return makeBodyCell(cell, readOnly);
})];
};
var toHyperjson = Render.toHyperjson = function (matrix, readOnly) {
if (!matrix || !matrix.length) { return; }
var head = ['THEAD', {}, [ ['TR', {}, matrix[0].map(function (cell) {
return makeHeadingCell(cell, readOnly);
})] ]];
var foot = ['TFOOT', {}, matrix.slice(-1).map(function (row) {
return makeBodyRow(row, readOnly);
})];
var body = ['TBODY', {}, matrix.slice(1, -1).map(function (row) {
return makeBodyRow(row, readOnly);
})];
return ['TABLE', {id:'cp-app-poll-table'}, [head, foot, body]];
};
Render.asHTML = function (obj, rows, cols, readOnly) {
return Hyperjson.toDOM(toHyperjson(cellMatrix(obj, rows, cols, readOnly), readOnly));
};
var diffIsInput = Render.diffIsInput = function (info) {
2017-11-13 15:32:40 +00:00
var nodeName = Util.find(info, ['node', 'nodeName']);
2017-09-27 16:52:04 +00:00
if (nodeName !== 'INPUT') { return; }
return true;
};
var getInputType = Render.getInputType = function (info) {
2017-11-13 15:32:40 +00:00
return Util.find(info, ['node', 'type']);
2017-09-27 16:52:04 +00:00
};
var preserveCursor = Render.preserveCursor = function (info) {
if (['modifyValue', 'modifyAttribute'].indexOf(info.diff.action) !== -1) {
var element = info.node;
if (typeof(element.selectionStart) !== 'number') { return; }
var o = info.oldValue || '';
var n = info.newValue || '';
var op = TextPatcher.diff(o, n);
info.selection = ['selectionStart', 'selectionEnd'].map(function (attr) {
return TextPatcher.transformCursor(element[attr], op);
});
}
};
var recoverCursor = Render.recoverCursor = function (info) {
try {
if (info.selection && info.node) {
info.node.selectionStart = info.selection[0];
info.node.selectionEnd = info.selection[1];
}
} catch (err) {
// FIXME LOL empty try-catch?
//console.log(info.node);
//console.error(err);
}
};
var diffOptions = {
preDiffApply: function (info) {
if (!diffIsInput(info)) { return; }
switch (getInputType(info)) {
case 'number':
//console.log('checkbox');
//console.log("[preDiffApply]", info);
break;
case 'text':
preserveCursor(info);
break;
default: break;
}
},
postDiffApply: function (info) {
if (info.selection) { recoverCursor(info); }
/*
if (!diffIsInput(info)) { return; }
switch (getInputType(info)) {
case 'checkbox':
console.log("[postDiffApply]", info);
break;
case 'text': break;
default: break;
}*/
}
};
2017-10-10 16:19:49 +00:00
var styleUserColumn = function (table) {
var userid = APP.userid;
if (!userid) { return; }
// Enable input for the userid column
APP.enableColumn(userid, table);
$(table).find('input[disabled="disabled"][data-rt-id^="' + userid + '"]')
2017-11-13 15:32:40 +00:00
.attr('placeholder', Messages.poll_userPlaceholder);
2017-10-10 16:19:49 +00:00
$(table).find('.cp-app-poll-table-lock[data-rt-id="' + userid + '"]').remove();
$(table).find('[data-rt-id^="' + userid + '"]').closest('td')
.addClass("cp-app-poll-table-own");
$(table).find('.cp-app-poll-table-bookmark[data-rt-id="' + userid + '"]')
.css('visibility', '')
.addClass('cp-app-poll-table-bookmark-full')
2017-11-13 15:32:40 +00:00
.attr('title', Messages.poll_bookmarked_col);
2017-10-10 16:19:49 +00:00
};
var styleUncommittedColumn = function (table) {
APP.uncommitted.content.colsOrder.forEach(function(id) {
// Enable the checkboxes for the uncommitted column
APP.enableColumn(id, table);
$(table).find('.cp-app-poll-table-lock[data-rt-id="' + id + '"]').remove();
$(table).find('.cp-app-poll-table-remove[data-rt-id="' + id + '"]').remove();
$(table).find('.cp-app-poll-table-bookmark[data-rt-id="' + id + '"]').remove();
$(table).find('td.cp-app-poll-table-uncommitted .cover')
.addClass("cp-app-poll-table-uncommitted");
var $uncommittedCol = $(table).find('[data-rt-id^="' + id + '"]').closest('td');
$uncommittedCol.addClass("cp-app-poll-table-uncommitted");
});
APP.uncommitted.content.rowsOrder.forEach(function(id) {
// Enable the checkboxes for the uncommitted column
APP.enableRow(id, table);
$(table).find('.cp-app-poll-table-edit[data-rt-id="' + id + '"]').remove();
$(table).find('.cp-app-poll-table-remove[data-rt-id="' + id + '"]').remove();
$(table).find('[data-rt-id="' + id + '"]').closest('tr')
.addClass("cp-app-poll-table-uncommitted");
});
};
var unlockElements = function (table) {
APP.unlocked.row.forEach(function (id) { APP.enableRow(id, table); });
APP.unlocked.col.forEach(function (id) { APP.enableColumn(id, table); });
};
var updateTableButtons = function (table) {
var uncomColId = APP.uncommitted.content.colsOrder[0];
var uncomRowId = APP.uncommitted.content.rowsOrder[0];
var $createOption = $(table).find('tbody input[data-rt-id="' + uncomRowId+'"]')
.closest('td').find('> div');
$createOption.append(APP.$createRow);
var $createUser = $(table).find('thead input[data-rt-id="' + uncomColId + '"]')
.closest('td');
$createUser.prepend(APP.$createCol);
if (APP.proxy.content.colsOrder.indexOf(APP.userid) === -1) {
$(table).find('.cp-app-poll-table-bookmark').css('visibility', '');
}
};
var addCount = function (table) {
var $tr = $(table).find('tbody tr').first();
var winner = {
v: 0,
ids: []
};
APP.count = {};
APP.proxy.content.rowsOrder.forEach(function (rId) {
var count = Object.keys(APP.proxy.content.cells)
.filter(function (k) {
return k.indexOf(rId) !== -1 && APP.proxy.content.cells[k] === 1;
}).length;
if (count > winner.v) {
winner.v = count;
winner.ids = [rId];
} else if (count && count === winner.v) {
winner.ids.push(rId);
}
APP.count[rId] = count;
var h = $tr.height() || 28;
$(table).find('[data-rt-count-id="' + rId + '"]')
.text(count)
.css({
'height': h+'px',
'line-height': h+'px'
});
});
winner.ids.forEach(function (rId) {
$(table).find('[data-rt-id="' + rId + '"]').closest('td')
.addClass('cp-app-poll-table-winner');
$(table).find('[data-rt-count-id="' + rId + '"]')
.addClass('cp-app-poll-table-winner');
});
};
var styleTable = function (table) {
styleUserColumn(table);
styleUncommittedColumn(table);
unlockElements(table);
updateTableButtons(table);
addCount(table);
};
2017-09-27 16:52:04 +00:00
Render.updateTable = function (table, obj, conf) {
var DD = new DiffDOM(diffOptions);
var rows = conf ? conf.rows : null;
var cols = conf ? conf.cols : null;
var readOnly = conf ? conf.readOnly : false;
var matrix = cellMatrix(obj, rows, cols, readOnly);
var hj = toHyperjson(matrix, readOnly);
if (!hj) { throw new Error("Expected Hyperjson!"); }
var table2 = Hyperjson.toDOM(hj);
2017-10-10 16:19:49 +00:00
styleTable(table2);
2017-09-27 16:52:04 +00:00
var patch = DD.diff(table, table2);
DD.apply(table, patch);
};
return Render;
};
return Renderer;
});