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.
807 lines
26 KiB
JavaScript
807 lines
26 KiB
JavaScript
9 years ago
|
define([
|
||
8 years ago
|
'jquery',
|
||
9 years ago
|
'/bower_components/textpatcher/TextPatcher.js',
|
||
|
'/bower_components/chainpad-listmap/chainpad-listmap.js',
|
||
|
'/bower_components/chainpad-crypto/crypto.js',
|
||
|
'/common/cryptpad-common.js',
|
||
8 years ago
|
'/common/cryptget.js',
|
||
8 years ago
|
'/bower_components/hyperjson/hyperjson.js',
|
||
|
'render.js',
|
||
8 years ago
|
'/common/toolbar2.js',
|
||
8 years ago
|
'/bower_components/file-saver/FileSaver.min.js',
|
||
|
|
||
7 years ago
|
'less!/bower_components/components-font-awesome/css/font-awesome.min.css',
|
||
7 years ago
|
'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
|
||
8 years ago
|
'less!/customize/src/less/toolbar.less',
|
||
8 years ago
|
'less!/customize/src/less/cryptpad.less',
|
||
8 years ago
|
'less!/poll/poll.less',
|
||
8 years ago
|
], function ($, TextPatcher, Listmap, Crypto, Cryptpad, Cryptget, Hyperjson, Renderer, Toolbar) {
|
||
9 years ago
|
|
||
8 years ago
|
var Messages = Cryptpad.Messages;
|
||
|
|
||
8 years ago
|
$(function () {
|
||
|
|
||
7 years ago
|
var HIDE_INTRODUCTION_TEXT = "hide-text";
|
||
8 years ago
|
var defaultName;
|
||
9 years ago
|
|
||
|
var secret = Cryptpad.getSecrets();
|
||
8 years ago
|
var readOnly = secret.keys && !secret.keys.editKeyStr;
|
||
8 years ago
|
// DEPRECATE_F
|
||
8 years ago
|
if (!secret.keys) {
|
||
|
secret.keys = secret.key;
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
var DEBUG = false;
|
||
|
var debug = console.log;
|
||
|
if (!DEBUG) {
|
||
|
debug = function() {};
|
||
|
}
|
||
|
|
||
8 years ago
|
Cryptpad.addLoadingScreen();
|
||
8 years ago
|
var onConnectError = function () {
|
||
8 years ago
|
Cryptpad.errorLoadingScreen(Messages.websocketError);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var Render = Renderer(Cryptpad);
|
||
|
|
||
8 years ago
|
var APP = window.APP = {
|
||
|
Toolbar: Toolbar,
|
||
|
Hyperjson: Hyperjson,
|
||
|
Render: Render,
|
||
|
$bar: $('#toolbar'),
|
||
|
editable: {
|
||
|
row: [],
|
||
|
col: []
|
||
8 years ago
|
},
|
||
|
locked: false
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var sortColumns = function (order, firstcol) {
|
||
|
var colsOrder = order.slice();
|
||
|
colsOrder.sort(function (a, b) {
|
||
|
return (a === firstcol) ? -1 :
|
||
|
((b === firstcol) ? 1 : 0);
|
||
8 years ago
|
});
|
||
8 years ago
|
return colsOrder;
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var isOwnColumnCommitted = function () {
|
||
|
return APP.proxy && APP.proxy.table.colsOrder.indexOf(APP.userid) !== -1;
|
||
|
};
|
||
9 years ago
|
|
||
8 years ago
|
var mergeUncommitted = function (proxy, uncommitted, commit) {
|
||
|
var newObj;
|
||
|
if (commit) {
|
||
|
newObj = proxy;
|
||
|
} else {
|
||
|
newObj = $.extend(true, {}, proxy);
|
||
|
}
|
||
|
// We have uncommitted data only if the user's column is not in the proxy
|
||
|
// If it is already is the proxy, nothing to merge
|
||
|
if (isOwnColumnCommitted()) {
|
||
|
return newObj;
|
||
|
}
|
||
|
// Merge uncommitted into the proxy
|
||
|
uncommitted.table.colsOrder.forEach(function (x) {
|
||
|
if (newObj.table.colsOrder.indexOf(x) !== -1) { return; }
|
||
|
newObj.table.colsOrder.push(x);
|
||
|
});
|
||
|
for (var k in uncommitted.table.cols) {
|
||
|
if (!newObj.table.cols[k]) {
|
||
|
newObj.table.cols[k] = uncommitted.table.cols[k];
|
||
|
}
|
||
|
}
|
||
|
for (var l in uncommitted.table.cells) {
|
||
|
if (!newObj.table.cells[l]) {
|
||
|
newObj.table.cells[l] = uncommitted.table.cells[l];
|
||
|
}
|
||
|
}
|
||
|
return newObj;
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
var styleUncommittedColumn = function () {
|
||
|
var id = APP.userid;
|
||
9 years ago
|
|
||
8 years ago
|
// Enable the checkboxes for the user's column (committed or not)
|
||
|
$('input[disabled="disabled"][data-rt-id^="' + id + '"]').removeAttr('disabled');
|
||
8 years ago
|
$('input[type="number"][data-rt-id^="' + id + '"]').addClass('enabled');
|
||
8 years ago
|
$('.lock[data-rt-id="' + id + '"]').addClass('fa-unlock').removeClass('fa-lock').attr('title', Messages.poll_unlocked);
|
||
9 years ago
|
|
||
8 years ago
|
if (isOwnColumnCommitted()) { return; }
|
||
|
$('[data-rt-id^="' + id + '"]').closest('td').addClass("uncommitted");
|
||
|
$('td.uncommitted .cover').addClass("uncommitted");
|
||
|
$('.uncommitted input[type="text"]').attr("placeholder", Messages.poll_userPlaceholder);
|
||
|
};
|
||
9 years ago
|
|
||
8 years ago
|
var unlockElements = function () {
|
||
|
APP.editable.row.forEach(function (id) {
|
||
8 years ago
|
var $input = $('input[type="text"][disabled="disabled"][data-rt-id="' + id + '"]').removeAttr('disabled');
|
||
8 years ago
|
$input.parent().parent().addClass('editing');
|
||
8 years ago
|
$('span.edit[data-rt-id="' + id + '"]').css('visibility', 'hidden');
|
||
9 years ago
|
});
|
||
8 years ago
|
APP.editable.col.forEach(function (id) {
|
||
8 years ago
|
var $input = $('input[disabled="disabled"][data-rt-id^="' + id + '"]').removeAttr('disabled');
|
||
8 years ago
|
$input.parent().addClass('editing');
|
||
8 years ago
|
$('input[type="number"][data-rt-id^="' + id + '"]').addClass('enabled');
|
||
8 years ago
|
$('.lock[data-rt-id="' + id + '"]').addClass('fa-unlock').removeClass('fa-lock').attr('title', Messages.poll_unlocked);
|
||
9 years ago
|
});
|
||
8 years ago
|
};
|
||
9 years ago
|
|
||
8 years ago
|
var updateTableButtons = function () {
|
||
|
if (!isOwnColumnCommitted()) {
|
||
|
$('#commit').show();
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
var $createOption = APP.$table.find('tfoot tr td:first-child');
|
||
|
var $commitCell = APP.$table.find('tfoot tr td:nth-child(2)');
|
||
|
$createOption.append(APP.$createRow);
|
||
|
$commitCell.append(APP.$commit);
|
||
7 years ago
|
$('#create-user, #create-option').css('display', 'inline-flex');
|
||
8 years ago
|
if (!APP.proxy || !APP.proxy.table.rowsOrder || APP.proxy.table.rowsOrder.length === 0) { $('#create-user').hide(); }
|
||
|
var width = $('#table').outerWidth();
|
||
|
if (width) {
|
||
|
//$('#create-user').css('left', width + 30 + 'px');
|
||
|
}
|
||
9 years ago
|
};
|
||
8 years ago
|
|
||
8 years ago
|
var setTablePublished = function (bool) {
|
||
|
if (bool) {
|
||
|
if (APP.$publish) { APP.$publish.hide(); }
|
||
8 years ago
|
if (APP.$admin) { APP.$admin.show(); }
|
||
8 years ago
|
$('#create-option').hide();
|
||
|
$('.remove[data-rt-id^="y"], .edit[data-rt-id^="y"]').hide();
|
||
|
} else {
|
||
|
if (APP.$publish) { APP.$publish.show(); }
|
||
8 years ago
|
if (APP.$admin) { APP.$admin.hide(); }
|
||
8 years ago
|
$('#create-option').show();
|
||
|
$('.remove[data-rt-id^="y"], .edit[data-rt-id^="y"]').show();
|
||
8 years ago
|
}
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
var updateDisplayedTable = function () {
|
||
|
styleUncommittedColumn();
|
||
|
unlockElements();
|
||
|
updateTableButtons();
|
||
|
setTablePublished(APP.proxy.published);
|
||
8 years ago
|
|
||
|
/*
|
||
|
APP.proxy.table.rowsOrder.forEach(function (rowId) {
|
||
|
$('[data-rt-id="' + rowId +'"]').val(APP.proxy.table.rows[rowId] || '');
|
||
|
});*/
|
||
8 years ago
|
};
|
||
9 years ago
|
|
||
8 years ago
|
var unlockColumn = function (id, cb) {
|
||
|
if (APP.editable.col.indexOf(id) === -1) {
|
||
|
APP.editable.col.push(id);
|
||
|
}
|
||
|
if (typeof(cb) === "function") {
|
||
|
cb();
|
||
|
}
|
||
|
};
|
||
|
var unlockRow = function (id, cb) {
|
||
|
if (APP.editable.row.indexOf(id) === -1) {
|
||
|
APP.editable.row.push(id);
|
||
|
}
|
||
|
if (typeof(cb) === "function") {
|
||
|
cb();
|
||
|
}
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
/* Any time the realtime object changes, call this function */
|
||
8 years ago
|
var change = function (o, n, path, throttle, cb) {
|
||
8 years ago
|
if (path && !Cryptpad.isArray(path)) {
|
||
8 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
if (path && path.join) {
|
||
8 years ago
|
debug("Change from [%s] to [%s] at [%s]",
|
||
8 years ago
|
o, n, path.join(', '));
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
var table = APP.$table[0];
|
||
9 years ago
|
|
||
8 years ago
|
var displayedObj = mergeUncommitted(APP.proxy, APP.uncommitted);
|
||
9 years ago
|
|
||
8 years ago
|
var colsOrder = sortColumns(displayedObj.table.colsOrder, APP.userid);
|
||
|
var conf = {
|
||
|
cols: colsOrder,
|
||
|
readOnly: readOnly
|
||
|
};
|
||
9 years ago
|
|
||
8 years ago
|
//Render.updateTable(table, displayedObj, conf);
|
||
9 years ago
|
|
||
8 years ago
|
/* FIXME browser autocomplete fills in new fields sometimes
|
||
|
calling updateTable twice removes the autofilled in values
|
||
|
setting autocomplete="off" is not reliable
|
||
9 years ago
|
|
||
8 years ago
|
https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
|
||
|
*/
|
||
8 years ago
|
Cryptpad.notify();
|
||
8 years ago
|
|
||
8 years ago
|
var getFocus = function () {
|
||
|
var active = document.activeElement;
|
||
|
if (!active) { return; }
|
||
|
return {
|
||
|
el: active,
|
||
|
start: active.selectionStart,
|
||
|
end: active.selectionEnd
|
||
|
};
|
||
|
};
|
||
|
var setFocus = function (obj) {
|
||
|
if (obj.el) { obj.el.focus(); }
|
||
|
else { return; }
|
||
|
if (obj.start) { obj.el.selectionStart = obj.start; }
|
||
|
if (obj.end) { obj.el.selectionEnd = obj.end; }
|
||
|
};
|
||
|
|
||
|
var updateTable = function () {
|
||
8 years ago
|
var displayedObj2 = mergeUncommitted(APP.proxy, APP.uncommitted);
|
||
8 years ago
|
var f = getFocus();
|
||
8 years ago
|
Render.updateTable(table, displayedObj2, conf);
|
||
8 years ago
|
APP.proxy.table.rowsOrder.forEach(function (rowId) {
|
||
|
$('input[data-rt-id="' + rowId +'"]').val(APP.proxy.table.rows[rowId] || '');
|
||
|
});
|
||
8 years ago
|
updateDisplayedTable();
|
||
8 years ago
|
setFocus(f);
|
||
8 years ago
|
if (typeof(cb) === "function") {
|
||
8 years ago
|
cb();
|
||
|
}
|
||
8 years ago
|
};
|
||
|
|
||
|
if (throttle) {
|
||
|
if (APP.throttled) { window.clearTimeout(APP.throttled); }
|
||
|
updateTable();
|
||
8 years ago
|
APP.throttled = window.setTimeout(function () {
|
||
|
updateDisplayedTable();
|
||
|
}, throttle);
|
||
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
window.setTimeout(updateTable);
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
var getRealtimeId = function (input) {
|
||
|
return input.getAttribute && input.getAttribute('data-rt-id');
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
/* Called whenever an event is fired on an input element */
|
||
8 years ago
|
var handleInput = function (input) {
|
||
8 years ago
|
var type = input.type.toLowerCase();
|
||
|
var id = getRealtimeId(input);
|
||
8 years ago
|
|
||
8 years ago
|
debug(input);
|
||
8 years ago
|
|
||
8 years ago
|
var object = APP.proxy;
|
||
8 years ago
|
|
||
8 years ago
|
var x = Render.getCoordinates(id)[0];
|
||
|
if (type !== "row" && x === APP.userid && APP.proxy.table.colsOrder.indexOf(x) === -1) {
|
||
|
object = APP.uncommitted;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
switch (type) {
|
||
|
case 'text':
|
||
8 years ago
|
debug("text[rt-id='%s'] [%s]", id, input.value);
|
||
8 years ago
|
Render.setValue(object, id, input.value);
|
||
8 years ago
|
change(null, null, null, 50);
|
||
8 years ago
|
break;
|
||
8 years ago
|
case 'number':
|
||
|
debug("checkbox[tr-id='%s'] %s", id, input.value);
|
||
8 years ago
|
if (APP.editable.col.indexOf(x) >= 0 || x === APP.userid) {
|
||
8 years ago
|
var value = parseInt(input.value);
|
||
|
|
||
|
if (isNaN(value)) {
|
||
|
console.error("Got NaN?!");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
Render.setValue(object, id, value);
|
||
8 years ago
|
change();
|
||
|
} else {
|
||
8 years ago
|
debug('checkbox locked');
|
||
8 years ago
|
}
|
||
|
break;
|
||
|
default:
|
||
8 years ago
|
debug("Input[type='%s']", type);
|
||
8 years ago
|
break;
|
||
|
}
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var hideInputs = function (target, isKeyup) {
|
||
8 years ago
|
if (APP.locked) { return; }
|
||
8 years ago
|
if (!isKeyup && $(target).is('[type="text"]')) {
|
||
|
return;
|
||
|
}
|
||
|
$('.lock[data-rt-id!="' + APP.userid + '"]').addClass('fa-lock').removeClass('fa-unlock').attr('title', Messages.poll_locked);
|
||
|
var $cells = APP.$table.find('thead td:not(.uncommitted), tbody td');
|
||
|
$cells.find('[type="text"][data-rt-id!="' + APP.userid + '"]').attr('disabled', true);
|
||
8 years ago
|
$cells.removeClass('editing');
|
||
8 years ago
|
$('.edit[data-rt-id!="' + APP.userid + '"]').css('visibility', 'visible');
|
||
|
APP.editable.col = [APP.userid];
|
||
|
APP.editable.row = [];
|
||
|
};
|
||
|
|
||
8 years ago
|
/* Called whenever an event is fired on a span */
|
||
|
var handleSpan = function (span) {
|
||
|
var id = span.getAttribute('data-rt-id');
|
||
|
var type = Render.typeofId(id);
|
||
|
var isRemove = span.className && span.className.split(' ').indexOf('remove') !== -1;
|
||
|
var isEdit = span.className && span.className.split(' ').indexOf('edit') !== -1;
|
||
8 years ago
|
var isLock = span.className && span.className.split(' ').indexOf('lock') !== -1;
|
||
|
var isLocked = span.className && span.className.split(' ').indexOf('fa-lock') !== -1;
|
||
8 years ago
|
if (type === 'row') {
|
||
|
if (isRemove) {
|
||
|
Cryptpad.confirm(Messages.poll_removeOption, function (res) {
|
||
|
if (!res) { return; }
|
||
|
Render.removeRow(APP.proxy, id, function () {
|
||
|
change();
|
||
8 years ago
|
});
|
||
8 years ago
|
});
|
||
|
} else if (isEdit) {
|
||
8 years ago
|
hideInputs(span);
|
||
8 years ago
|
unlockRow(id, function () {
|
||
8 years ago
|
change(null, null, null, null, function() {
|
||
|
$('input[data-rt-id="' + id + '"]').focus();
|
||
8 years ago
|
});
|
||
8 years ago
|
});
|
||
|
}
|
||
|
} else if (type === 'col') {
|
||
|
if (isRemove) {
|
||
|
Cryptpad.confirm(Messages.poll_removeUser, function (res) {
|
||
|
if (!res) { return; }
|
||
|
Render.removeColumn(APP.proxy, id, function () {
|
||
|
change();
|
||
8 years ago
|
});
|
||
8 years ago
|
});
|
||
8 years ago
|
} else if (isLock && isLocked) {
|
||
|
hideInputs(span);
|
||
8 years ago
|
unlockColumn(id, function () {
|
||
8 years ago
|
change(null, null, null, null, function() {
|
||
|
$('input[data-rt-id="' + id + '"]').focus();
|
||
8 years ago
|
});
|
||
8 years ago
|
});
|
||
8 years ago
|
}
|
||
8 years ago
|
} else if (type === 'cell') {
|
||
|
change();
|
||
|
} else {
|
||
8 years ago
|
debug("UNHANDLED");
|
||
8 years ago
|
}
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
var handleClick = function (e, isKeyup) {
|
||
8 years ago
|
if (APP.locked) { return; }
|
||
|
|
||
8 years ago
|
e.stopPropagation();
|
||
8 years ago
|
|
||
8 years ago
|
if (!APP.ready) { return; }
|
||
8 years ago
|
if (!isKeyup && e.which !== 1) { return; } // only allow left clicks
|
||
8 years ago
|
|
||
8 years ago
|
var target = e && e.target;
|
||
8 years ago
|
|
||
8 years ago
|
if (!target) { return void debug("NO TARGET"); }
|
||
9 years ago
|
|
||
8 years ago
|
var nodeName = target && target.nodeName;
|
||
8 years ago
|
var shouldLock = $(target).hasClass('fa-unlock');
|
||
8 years ago
|
|
||
8 years ago
|
if ((!$(target).parents('#table tbody').length && $(target).hasClass('lock'))) {
|
||
8 years ago
|
hideInputs(e);
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
switch (nodeName) {
|
||
|
case 'INPUT':
|
||
8 years ago
|
if (isKeyup && (e.keyCode === 13 || e.keyCode === 27)) {
|
||
8 years ago
|
hideInputs(target, isKeyup);
|
||
|
break;
|
||
8 years ago
|
}
|
||
8 years ago
|
if ($(target).is('input[type="number"]')) { console.error("number input focused?"); break; }
|
||
8 years ago
|
|
||
8 years ago
|
handleInput(target);
|
||
|
break;
|
||
8 years ago
|
case 'LABEL':
|
||
|
var input = $('input[type="number"][id=' + $(target).attr('for') + ']');
|
||
|
var value = parseInt(input.val());
|
||
8 years ago
|
|
||
8 years ago
|
input.val((value + 1) % 4);
|
||
|
|
||
|
handleInput(input[0]);
|
||
|
break;
|
||
8 years ago
|
case 'SPAN':
|
||
8 years ago
|
if (shouldLock) {
|
||
|
break;
|
||
|
}
|
||
8 years ago
|
handleSpan(target);
|
||
|
break;
|
||
|
case undefined:
|
||
|
//console.error(new Error("C'est pas possible!"));
|
||
|
break;
|
||
|
default:
|
||
8 years ago
|
debug(target, nodeName);
|
||
8 years ago
|
break;
|
||
|
}
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
/*
|
||
|
Make sure that the realtime data structure has all the required fields
|
||
|
*/
|
||
|
var prepareProxy = function (proxy, schema) {
|
||
|
if (proxy && proxy.version === 1) { return; }
|
||
8 years ago
|
debug("Configuring proxy schema...");
|
||
8 years ago
|
|
||
8 years ago
|
proxy.info = proxy.info || schema.info;
|
||
|
Object.keys(schema.info).forEach(function (k) {
|
||
|
if (!proxy.info[k]) { proxy.info[k] = schema.info[k]; }
|
||
|
});
|
||
|
|
||
|
proxy.table = proxy.table || schema.table;
|
||
|
Object.keys(schema.table).forEach(function (k) {
|
||
|
if (!proxy.table[k]) { proxy.table[k] = schema.table[k]; }
|
||
|
});
|
||
|
|
||
8 years ago
|
proxy.version = 1;
|
||
8 years ago
|
proxy.type = 'poll';
|
||
8 years ago
|
};
|
||
9 years ago
|
|
||
8 years ago
|
/*
|
||
9 years ago
|
|
||
8 years ago
|
*/
|
||
|
var publish = APP.publish = function (bool) {
|
||
|
if (!APP.ready) { return; }
|
||
|
if (APP.proxy.published !== bool) {
|
||
|
APP.proxy.published = bool;
|
||
|
}
|
||
|
setTablePublished(bool);
|
||
|
['textarea'].forEach(function (sel) {
|
||
|
$(sel).attr('disabled', bool);
|
||
9 years ago
|
});
|
||
9 years ago
|
};
|
||
|
|
||
8 years ago
|
var showHelp = function(help) {
|
||
|
if (typeof help === 'undefined') { help = !$('#howItWorks').is(':visible'); }
|
||
|
|
||
|
var msg = (help ? Messages.poll_hide_help_button : Messages.poll_show_help_button);
|
||
|
|
||
|
$('#howItWorks').toggle(help);
|
||
7 years ago
|
$('#help').text(msg);
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var Title;
|
||
8 years ago
|
var UserList;
|
||
9 years ago
|
|
||
8 years ago
|
var copyObject = function (obj) {
|
||
|
return JSON.parse(JSON.stringify(obj));
|
||
|
};
|
||
9 years ago
|
|
||
8 years ago
|
// special UI elements
|
||
|
var $description = $('#description').attr('placeholder', Messages.poll_descriptionHint || 'description');
|
||
9 years ago
|
|
||
8 years ago
|
var ready = function (info, userid, readOnly) {
|
||
|
debug("READY");
|
||
|
debug('userid: %s', userid);
|
||
8 years ago
|
|
||
8 years ago
|
var proxy = APP.proxy;
|
||
8 years ago
|
|
||
8 years ago
|
var isNew = false;
|
||
|
var userDoc = JSON.stringify(proxy);
|
||
|
if (userDoc === "" || userDoc === "{}") { isNew = true; }
|
||
8 years ago
|
|
||
8 years ago
|
if (!isNew && typeof(proxy.type) !== 'undefined' && proxy.type !== 'poll') {
|
||
|
var errorText = Messages.typeError;
|
||
|
Cryptpad.errorLoadingScreen(errorText);
|
||
|
throw new Error(errorText);
|
||
|
}
|
||
|
|
||
|
if (typeof(proxy.type) === 'undefined') {
|
||
|
proxy.type = 'poll';
|
||
|
}
|
||
|
|
||
8 years ago
|
var uncommitted = APP.uncommitted = {};
|
||
|
prepareProxy(proxy, copyObject(Render.Example));
|
||
|
prepareProxy(uncommitted, copyObject(Render.Example));
|
||
|
if (!readOnly && proxy.table.colsOrder.indexOf(userid) === -1 &&
|
||
|
uncommitted.table.colsOrder.indexOf(userid) === -1) {
|
||
|
uncommitted.table.colsOrder.unshift(userid);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
var displayedObj = mergeUncommitted(proxy, uncommitted, false);
|
||
8 years ago
|
|
||
8 years ago
|
var colsOrder = sortColumns(displayedObj.table.colsOrder, userid);
|
||
8 years ago
|
|
||
8 years ago
|
var $table = APP.$table = $(Render.asHTML(displayedObj, null, colsOrder, readOnly));
|
||
|
APP.$createRow = $('#create-option').click(function () {
|
||
|
Render.createRow(proxy, function (empty, id) {
|
||
|
change(null, null, null, null, function() {
|
||
8 years ago
|
handleSpan($('.edit[data-rt-id="' + id + '"]')[0]);
|
||
8 years ago
|
});
|
||
9 years ago
|
});
|
||
8 years ago
|
});
|
||
9 years ago
|
|
||
8 years ago
|
APP.$createCol = $('#create-user').click(function () {
|
||
|
Render.createColumn(proxy, function (empty, id) {
|
||
|
change(null, null, null, null, function() {
|
||
8 years ago
|
handleSpan($('.lock[data-rt-id="' + id + '"]')[0]);
|
||
8 years ago
|
});
|
||
9 years ago
|
});
|
||
8 years ago
|
});
|
||
9 years ago
|
|
||
8 years ago
|
// Commit button
|
||
|
APP.$commit = $('#commit').click(function () {
|
||
|
var uncommittedCopy = JSON.parse(JSON.stringify(APP.uncommitted));
|
||
|
APP.uncommitted = {};
|
||
|
prepareProxy(APP.uncommitted, copyObject(Render.Example));
|
||
|
mergeUncommitted(proxy, uncommittedCopy, true);
|
||
|
APP.$commit.hide();
|
||
|
change();
|
||
|
});
|
||
9 years ago
|
|
||
8 years ago
|
// #publish button is removed in readonly
|
||
|
APP.$publish = $('#publish')
|
||
|
.click(function () {
|
||
|
publish(true);
|
||
9 years ago
|
});
|
||
|
|
||
8 years ago
|
APP.$admin = $('#admin')
|
||
|
.click(function () {
|
||
|
publish(false);
|
||
|
});
|
||
|
|
||
8 years ago
|
APP.$help = $('#help')
|
||
8 years ago
|
.click(function () {
|
||
8 years ago
|
showHelp();
|
||
8 years ago
|
});
|
||
9 years ago
|
|
||
8 years ago
|
// Title
|
||
|
if (APP.proxy.info.defaultTitle) {
|
||
8 years ago
|
Title.updateDefaultTitle(APP.proxy.info.defaultTitle);
|
||
8 years ago
|
} else {
|
||
8 years ago
|
APP.proxy.info.defaultTitle = Title.defaultTitle;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
var andThen = function () {
|
||
|
if (readOnly) { return; }
|
||
|
Cryptpad.setPadAttribute('userid', userid, function (e) {
|
||
|
if (e) { console.error(e); }
|
||
|
});
|
||
|
};
|
||
|
|
||
8 years ago
|
if (Cryptpad.initialName && !APP.proxy.info.title) {
|
||
|
APP.proxy.info.title = Cryptpad.initialName;
|
||
8 years ago
|
Title.updateTitle(Cryptpad.initialName, null, andThen);
|
||
8 years ago
|
} else {
|
||
8 years ago
|
Title.updateTitle(APP.proxy.info.title || Title.defaultTitle, null, andThen);
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
// Description
|
||
|
var resize = function () {
|
||
|
var lineCount = $description.val().split('\n').length;
|
||
|
$description.css('height', lineCount + 'rem');
|
||
|
};
|
||
|
$description.on('change keyup', function () {
|
||
|
var val = $description.val();
|
||
|
proxy.info.description = val;
|
||
8 years ago
|
resize();
|
||
8 years ago
|
});
|
||
|
resize();
|
||
|
if (typeof(proxy.info.description) !== 'undefined') {
|
||
|
$description.val(proxy.info.description);
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
$('#tableScroll').html('').prepend($table);
|
||
|
updateDisplayedTable();
|
||
|
|
||
|
$table
|
||
|
.click(handleClick)
|
||
|
.on('keyup', function (e) { handleClick(e, true); });
|
||
|
|
||
8 years ago
|
$(window).click(function(e) {
|
||
|
if (e.which !== 1) { return; }
|
||
|
hideInputs();
|
||
|
});
|
||
8 years ago
|
|
||
8 years ago
|
proxy
|
||
|
.on('change', ['info'], function (o, n, p) {
|
||
|
if (p[1] === 'title') {
|
||
8 years ago
|
Title.updateTitle(n);
|
||
|
Cryptpad.notify();
|
||
8 years ago
|
} else if (p[1] === "userData") {
|
||
|
UserList.addToUserData(APP.proxy.info.userData);
|
||
|
} else if (p[1] === 'description') {
|
||
|
var op = TextPatcher.diff(o, n);
|
||
|
var el = $description[0];
|
||
|
|
||
|
var selects = ['selectionStart', 'selectionEnd'].map(function (attr) {
|
||
|
return TextPatcher.transformCursor(el[attr], op);
|
||
|
});
|
||
|
$description.val(n);
|
||
|
if (op) {
|
||
|
el.selectionStart = selects[0];
|
||
|
el.selectionEnd = selects[1];
|
||
9 years ago
|
}
|
||
8 years ago
|
Cryptpad.notify();
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
debug("change: (%s, %s, [%s])", o, n, p.join(', '));
|
||
|
})
|
||
|
.on('change', ['table'], change)
|
||
|
.on('remove', [], change);
|
||
8 years ago
|
|
||
7 years ago
|
var userInput = $('.uncommitted > input');
|
||
|
if (userInput.val() === '')
|
||
|
{
|
||
|
userInput.val(Cryptpad.getProxy()[Cryptpad.displayNameKey]);
|
||
|
}
|
||
|
|
||
8 years ago
|
UserList.addToUserData(APP.proxy.info.userData);
|
||
8 years ago
|
|
||
8 years ago
|
APP.ready = true;
|
||
|
if (!proxy.published) {
|
||
|
publish(false);
|
||
|
} else {
|
||
|
publish(true);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
Cryptpad.removeLoadingScreen();
|
||
|
|
||
|
if (readOnly) { return; }
|
||
|
UserList.getLastName(APP.toolbar.$userNameButton, isNew);
|
||
|
};
|
||
|
|
||
8 years ago
|
var setEditable = function (editable) {
|
||
|
APP.locked = !editable;
|
||
|
|
||
|
if (editable === false) {
|
||
|
// disable all the things
|
||
8 years ago
|
$('.realtime input, .realtime button, .upper button, .realtime textarea').attr('disabled', APP.locked);
|
||
8 years ago
|
$('span.edit, span.remove').hide();
|
||
|
$('span.lock').addClass('fa-lock').removeClass('fa-unlock')
|
||
|
.attr('title', Messages.poll_locked)
|
||
|
.css({'cursor': 'default'});
|
||
|
} else {
|
||
|
// enable
|
||
|
$('span.edit, span.remove').show();
|
||
|
$('span.lock').css({'cursor': ''});
|
||
8 years ago
|
$('.realtime button, .upper button, .realtime textarea').attr('disabled', APP.locked);
|
||
8 years ago
|
unlockElements();
|
||
|
}
|
||
|
};
|
||
|
|
||
8 years ago
|
var disconnect = function () {
|
||
8 years ago
|
setEditable(false);
|
||
8 years ago
|
APP.toolbar.failed();
|
||
|
Cryptpad.alert(Messages.common_connectionLost, undefined, true);
|
||
|
};
|
||
|
|
||
|
var reconnect = function (info) {
|
||
8 years ago
|
setEditable(true);
|
||
8 years ago
|
APP.toolbar.reconnecting(info.myId);
|
||
|
Cryptpad.findOKButton().click();
|
||
|
};
|
||
|
|
||
|
var create = function (info) {
|
||
|
APP.myID = info.myID;
|
||
|
|
||
|
var editHash;
|
||
|
if (!readOnly) {
|
||
|
editHash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if (APP.realtime !== info.realtime) {
|
||
|
APP.realtime = info.realtime;
|
||
|
APP.patchText = TextPatcher.create({
|
||
|
realtime: info.realtime,
|
||
|
logging: true,
|
||
9 years ago
|
});
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
var onLocal = function () {
|
||
|
APP.proxy.info.userData = UserList.userData;
|
||
8 years ago
|
};
|
||
8 years ago
|
UserList = Cryptpad.createUserList(info, onLocal, Cryptget, Cryptpad);
|
||
8 years ago
|
|
||
8 years ago
|
var onLocalTitle = function () {
|
||
|
APP.proxy.info.title = Title.isDefaultTitle() ? "" : Title.title;
|
||
8 years ago
|
};
|
||
8 years ago
|
Title = Cryptpad.createTitle({}, onLocalTitle, Cryptpad);
|
||
8 years ago
|
|
||
8 years ago
|
var configTb = {
|
||
8 years ago
|
displayed: ['title', 'useradmin', 'spinner', 'lag', 'state', 'share', 'userlist', 'newpad', 'limit', 'upgrade'],
|
||
8 years ago
|
userList: UserList.getToolbarConfig(),
|
||
|
share: {
|
||
|
secret: secret,
|
||
|
channel: info.channel
|
||
|
},
|
||
8 years ago
|
title: Title.getTitleConfig(),
|
||
8 years ago
|
common: Cryptpad,
|
||
|
readOnly: readOnly,
|
||
|
ifrw: window,
|
||
|
realtime: info.realtime,
|
||
|
network: info.network,
|
||
8 years ago
|
$container: APP.$bar,
|
||
|
$contentContainer: $('#content')
|
||
8 years ago
|
};
|
||
|
APP.toolbar = Toolbar.create(configTb);
|
||
9 years ago
|
|
||
8 years ago
|
Title.setToolbar(APP.toolbar);
|
||
9 years ago
|
|
||
8 years ago
|
var $rightside = APP.toolbar.$rightside;
|
||
9 years ago
|
|
||
8 years ago
|
/* add a forget button */
|
||
|
var forgetCb = function (err) {
|
||
|
if (err) { return; }
|
||
8 years ago
|
setEditable(false);
|
||
8 years ago
|
};
|
||
8 years ago
|
var $forgetPad = Cryptpad.createButton('forget', true, {}, forgetCb);
|
||
|
$rightside.append($forgetPad);
|
||
8 years ago
|
|
||
8 years ago
|
// set the hash
|
||
|
if (!readOnly) { Cryptpad.replaceHash(editHash); }
|
||
8 years ago
|
|
||
8 years ago
|
/* save as template */
|
||
|
if (!Cryptpad.isTemplate(window.location.href)) {
|
||
|
var templateObj = {
|
||
|
rt: info.realtime,
|
||
|
Crypt: Cryptget,
|
||
|
getTitle: function () { return document.title; }
|
||
9 years ago
|
};
|
||
8 years ago
|
var $templateButton = Cryptpad.createButton('template', true, templateObj);
|
||
|
$rightside.append($templateButton);
|
||
|
}
|
||
|
};
|
||
8 years ago
|
|
||
9 years ago
|
// don't initialize until the store is ready.
|
||
|
Cryptpad.ready(function () {
|
||
8 years ago
|
Cryptpad.reportAppUsage();
|
||
8 years ago
|
var config = {
|
||
|
websocketURL: Cryptpad.getWebsocketURL(),
|
||
|
channel: secret.channel,
|
||
|
readOnly: readOnly,
|
||
|
data: {},
|
||
|
// our public key
|
||
|
validateKey: secret.keys.validateKey || undefined,
|
||
|
//readOnly: readOnly,
|
||
|
crypto: Crypto.createEncryptor(secret.keys),
|
||
|
userName: 'poll',
|
||
|
network: Cryptpad.getNetwork()
|
||
|
};
|
||
|
|
||
8 years ago
|
if (readOnly) {
|
||
8 years ago
|
$('#commit, #create-user, #create-option, #publish, #admin').remove();
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
var parsedHash = Cryptpad.parsePadUrl(window.location.href);
|
||
|
defaultName = Cryptpad.getDefaultName(parsedHash);
|
||
|
var rt = window.rt = APP.rt = Listmap.create(config);
|
||
|
APP.proxy = rt.proxy;
|
||
|
rt.proxy
|
||
|
.on('create', create)
|
||
|
.on('ready', function (info) {
|
||
|
Cryptpad.getPadAttribute('userid', function (e, userid) {
|
||
|
if (e) { console.error(e); }
|
||
|
if (!userid) { userid = Render.coluid(); }
|
||
|
APP.userid = userid;
|
||
8 years ago
|
ready(info, userid, readOnly);
|
||
9 years ago
|
});
|
||
8 years ago
|
})
|
||
8 years ago
|
.on('disconnect', disconnect)
|
||
|
.on('reconnect', reconnect);
|
||
8 years ago
|
|
||
7 years ago
|
Cryptpad.getAttribute(['poll', HIDE_INTRODUCTION_TEXT], function (e, value) {
|
||
8 years ago
|
if (e) { console.error(e); }
|
||
8 years ago
|
if (!value) {
|
||
7 years ago
|
Cryptpad.setAttribute(['poll', HIDE_INTRODUCTION_TEXT], "1", function (e) {
|
||
8 years ago
|
if (e) { console.error(e); }
|
||
8 years ago
|
});
|
||
8 years ago
|
showHelp(true);
|
||
|
} else {
|
||
8 years ago
|
showHelp(false);
|
||
8 years ago
|
}
|
||
9 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
Cryptpad.onLogout(function () { setEditable(false); });
|
||
9 years ago
|
});
|
||
8 years ago
|
Cryptpad.onError(function (info) {
|
||
|
if (info) {
|
||
|
onConnectError();
|
||
|
}
|
||
|
});
|
||
8 years ago
|
|
||
|
});
|
||
9 years ago
|
});
|