cryptpad/www/poll/inner.js

1326 lines
49 KiB
JavaScript
Raw Normal View History

2017-09-27 16:52:04 +00:00
define([
'jquery',
'/common/toolbar3.js',
'/common/common-util.js',
'/common/cryptget.js',
'/bower_components/nthen/index.js',
'/common/sframe-common.js',
'/common/common-realtime.js',
'/customize/application_config.js',
2017-11-24 12:40:21 +00:00
'/bower_components/chainpad-listmap/chainpad-listmap.js',
2017-09-27 16:52:04 +00:00
'/customize/pages.js',
'/poll/render.js',
2017-10-03 17:04:29 +00:00
'/common/diffMarked.js',
'/common/sframe-common-codemirror.js',
'/common/common-thumbnail.js',
'/common/common-interface.js',
2018-02-27 16:38:29 +00:00
'/common/hyperscript.js',
'/customize/messages.js',
2017-10-03 17:04:29 +00:00
'cm/lib/codemirror',
2018-01-11 14:45:12 +00:00
'/common/test.js',
'cm/addon/display/placeholder',
2017-10-03 17:04:29 +00:00
'cm/mode/markdown/markdown',
'css!cm/lib/codemirror.css',
2017-09-27 16:52:04 +00:00
'/bower_components/file-saver/FileSaver.min.js',
'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
'less!/bower_components/components-font-awesome/css/font-awesome.min.css',
'less!/customize/src/less2/main.less',
], function (
$,
Toolbar,
Util,
Cryptget,
nThen,
SFCommon,
CommonRealtime,
AppConfig,
Listmap,
Pages,
2017-10-03 17:04:29 +00:00
Renderer,
DiffMd,
SframeCM,
Thumb,
UI,
2018-02-27 16:38:29 +00:00
h,
Messages,
2018-01-11 14:45:12 +00:00
CMeditor,
Test)
2017-09-27 16:52:04 +00:00
{
2017-10-03 14:50:59 +00:00
var saveAs = window.saveAs;
2017-09-27 16:52:04 +00:00
var APP = window.APP = {
unlocked: {
row: [],
col: []
},
readOnly: false,
mobile: function () { return $('body').width() <= 600; } // Menu and content area are not inline-block anymore for mobiles
};
2017-11-13 15:32:40 +00:00
var Render = Renderer(APP);
2017-09-27 16:52:04 +00:00
var debug = $.noop; //console.log;
var metadataMgr;
var Title;
var common;
2017-10-02 17:02:31 +00:00
var copyObject = function (obj) {
return JSON.parse(JSON.stringify(obj));
};
2017-10-03 14:50:59 +00:00
var getCSV = APP.getCSV = function () {
if (!APP.proxy) { return; }
var data = copyObject(APP.proxy.content);
var res = '';
var escapeStr = function (str) {
return '"' + str.replace(/"/g, '""') + '"';
};
[null].concat(data.rowsOrder).forEach(function (rowId, i) {
[null].concat(data.colsOrder).forEach(function (colId, j) {
// thead
if (i === 0) {
if (j === 0) { res += ','; return; }
if (!colId) { throw new Error("Invalid data"); }
res += escapeStr(data.cols[colId] || Messages.anonymous) + ',';
return;
}
// tbody
if (!rowId) { throw new Error("Invalid data"); }
if (j === 0) {
res += escapeStr(data.rows[rowId] || Messages.poll_optionPlaceholder) + ',';
return;
}
if (!colId) { throw new Error("Invalid data"); }
res += (data.cells[colId + '_' + rowId] || 3) + ',';
});
// last column: total
// thead
if (i === 0) {
res += escapeStr(Messages.poll_total) + '\n';
return;
}
// tbody
if (!rowId) { throw new Error("Invalid data"); }
res += APP.count[rowId] || '?';
res += '\n';
});
return res;
};
var exportFile = function () {
var csv = getCSV();
var suggestion = Title.suggestTitle(Title.defaultTitle);
UI.prompt(Messages.exportPrompt,
2017-11-13 15:32:40 +00:00
Util.fixFileName(suggestion) + '.csv', function (filename) {
2017-10-03 14:50:59 +00:00
if (!(typeof(filename) === 'string' && filename)) { return; }
var blob = new Blob([csv], {type: "application/csv;charset=utf-8"});
saveAs(blob, filename);
});
};
2017-10-02 17:02:31 +00:00
/*
Make sure that the realtime data structure has all the required fields
*/
var prepareProxy = function (proxy, schema) {
if (proxy && proxy.version === 1) { return; }
debug("Configuring proxy schema...");
2017-09-27 16:52:04 +00:00
2017-10-02 17:02:31 +00:00
proxy.metadata = proxy.metadata || schema.metadata;
Object.keys(schema.metadata).forEach(function (k) {
if (!proxy.metadata[k]) { proxy.metadata[k] = schema.metadata[k]; }
});
2017-09-27 16:52:04 +00:00
2017-10-02 17:02:31 +00:00
proxy.content = proxy.content || schema.content;
Object.keys(schema.content).forEach(function (k) {
if (!proxy.content[k]) { proxy.content[k] = schema.content[k]; }
});
proxy.version = 1;
proxy.type = 'poll';
};
2017-09-27 16:52:04 +00:00
2017-10-03 12:11:11 +00:00
/*
Set the user id (user column) in the pad attributes
*/
2017-10-02 17:02:31 +00:00
var setUserId = function (id, cb) {
2017-10-03 17:04:29 +00:00
cb = cb || $.noop;
2017-10-02 17:02:31 +00:00
APP.userid = id;
common.setPadAttribute('userid', id, function (e) {
if (e) {
console.error(e);
return void cb(e);
}
cb();
});
};
2017-09-27 16:52:04 +00:00
var sortColumns = function (order, firstcol) {
var colsOrder = order.slice();
2017-10-02 17:02:31 +00:00
// Never put at the first position an uncommitted column
if (APP.proxy.content.colsOrder.indexOf(firstcol) === -1) { return colsOrder; }
2017-09-27 16:52:04 +00:00
colsOrder.sort(function (a, b) {
return (a === firstcol) ? -1 :
((b === firstcol) ? 1 : 0);
});
return colsOrder;
};
2017-10-02 17:02:31 +00:00
var isUncommitted = function (id) {
2017-10-03 12:11:11 +00:00
var idArr = id.split('_');
var idx = idArr[0];
var idy = idArr[1] || idArr[0]; // if id is y-{...} (no 'x'), use idArr[0] as 'y' coordinate
return APP.uncommitted.content.colsOrder.indexOf(idx) !== -1 ||
APP.uncommitted.content.rowsOrder.indexOf(idy) !== -1;
2017-09-27 16:52:04 +00:00
};
var mergeUncommitted = function (proxy, uncommitted, commit) {
var newObj;
if (commit) {
newObj = proxy;
} else {
newObj = copyObject(proxy);
2017-09-27 16:52:04 +00:00
}
// Merge uncommitted into the proxy
uncommitted.content.colsOrder = uncommitted.content.colsOrder || [];
uncommitted.content.colsOrder.forEach(function (x) {
if (newObj.content.colsOrder.indexOf(x) !== -1) { return; }
newObj.content.colsOrder.push(x);
});
for (var k in uncommitted.content.cols) {
if (!newObj.content.cols[k]) {
newObj.content.cols[k] = uncommitted.content.cols[k];
}
}
for (var l in uncommitted.content.cells) {
if (!newObj.content.cells[l]) {
newObj.content.cells[l] = uncommitted.content.cells[l];
}
}
// Uncommitted rows
uncommitted.content.rowsOrder = uncommitted.content.rowsOrder || [];
uncommitted.content.rowsOrder.forEach(function (x) {
if (newObj.content.rowsOrder.indexOf(x) !== -1) { return; }
newObj.content.rowsOrder.push(x);
});
2017-10-02 17:02:31 +00:00
for (var m in uncommitted.content.rows) {
if (!newObj.content.rows[m]) {
newObj.content.rows[m] = uncommitted.content.rows[m];
2017-09-27 16:52:04 +00:00
}
}
if (commit) {
APP.uncommited = {};
prepareProxy(APP.uncommitted, copyObject(Render.Example));
}
return newObj;
};
2017-10-10 16:19:49 +00:00
var enableColumn = APP.enableColumn = function (id, table) {
table = table || $('body');
var $input = $(table).find('input[disabled="disabled"][data-rt-id^="' + id + '"]')
.removeAttr('disabled');
$input.closest('td').addClass('cp-app-poll-table-editing');
2017-10-10 16:19:49 +00:00
$(table).find('.cp-app-poll-table-lock[data-rt-id="' + id + '"]').addClass('fa-unlock')
.removeClass('fa-lock').attr('title', Messages.poll_unlocked);
};
var disableColumn = function (id) {
var $input = $('input[data-rt-id^="' + id + '"]')
.attr('disabled', 'disabled');
$input.closest('td').removeClass('cp-app-poll-table-editing');
$('.cp-app-poll-table-lock[data-rt-id="' + id + '"]').addClass('fa-lock')
.removeClass('fa-unlock').attr('title', Messages.poll_locked);
};
2017-10-10 16:19:49 +00:00
var enableRow = APP.enableRow = function (id, table) {
table = table || $('body');
var $input = $(table).find('input[disabled="disabled"][data-rt-id="' + id + '"]')
.removeAttr('disabled');
$input.closest('td').addClass('cp-app-poll-table-editing');
2017-10-10 16:19:49 +00:00
$(table).find('span.cp-app-poll-table-edit[data-rt-id="' + id + '"]')
.css('visibility', 'hidden');
};
var disableRow = function (id) {
var $input = $('input[type="text"][data-rt-id="' + id + '"]')
.attr('disabled', 'disabled');
$input.closest('td').removeClass('cp-app-poll-table-editing');
$('span.cp-app-poll-table-edit[data-rt-id="' + id + '"]').css('visibility', 'visible');
};
2017-09-27 16:52:04 +00:00
var unlockElements = function () {
APP.unlocked.row.forEach(enableRow);
APP.unlocked.col.forEach(enableColumn);
2017-09-27 16:52:04 +00:00
};
2017-10-11 13:37:47 +00:00
2017-09-27 16:52:04 +00:00
var setTablePublished = function (bool) {
if (APP.locked) { bool = true; }
if (APP.markdownTb) { APP.markdownTb.setState(!bool); }
2017-09-27 16:52:04 +00:00
if (bool) {
if (APP.$publish) { APP.$publish.hide(); }
if (APP.$admin) { APP.$admin.show(); }
2017-10-02 17:02:31 +00:00
$('#cp-app-poll-form').addClass('cp-app-poll-published');
2017-09-27 16:52:04 +00:00
} else {
if (APP.$publish) { APP.$publish.show(); }
if (APP.$admin) { APP.$admin.hide(); }
2017-10-02 17:02:31 +00:00
$('#cp-app-poll-form').removeClass('cp-app-poll-published');
2017-09-27 16:52:04 +00:00
}
};
2017-10-10 16:19:49 +00:00
var addScrollClass = function () {
2017-10-02 17:02:31 +00:00
var $scroll = $('#cp-app-poll-table-scroll');
2017-10-11 14:01:26 +00:00
var hasScroll = $scroll.width() < $scroll[0].scrollWidth && $scroll.width() > 100;
2017-10-02 17:02:31 +00:00
if (hasScroll) {
2017-10-10 16:19:49 +00:00
$scroll.addClass('cp-app-poll-table-scrolled');
return;
2017-10-02 17:02:31 +00:00
}
2017-10-10 16:19:49 +00:00
$scroll.removeClass('cp-app-poll-table-scrolled');
2017-10-02 17:02:31 +00:00
};
2017-10-11 13:37:47 +00:00
var updateTableButtons = function () {
var uncomColId = APP.uncommitted.content.colsOrder[0];
var uncomRowId = APP.uncommitted.content.rowsOrder[0];
var $createOption = $('tbody input[data-rt-id="' + uncomRowId+'"]')
.closest('td').find('> div');
$createOption.find('#cp-app-poll-create-option').remove();
$createOption.append(APP.$createRow);
var $createUser = $('thead input[data-rt-id="' + uncomColId + '"]')
.closest('td');
$createUser.find('#cp-app-poll-create-user').remove();
$createUser.prepend(APP.$createCol);
};
2017-10-02 17:02:31 +00:00
2017-09-27 16:52:04 +00:00
var updateDisplayedTable = function () {
setTablePublished(APP.proxy.published);
2017-10-10 16:19:49 +00:00
addScrollClass();
2017-10-11 13:37:47 +00:00
updateTableButtons();
2017-09-27 16:52:04 +00:00
};
var unlockColumn = function (id, cb) {
if (APP.unlocked.col.indexOf(id) === -1) {
APP.unlocked.col.push(id);
}
enableColumn(id);
if (typeof(cb) === "function") { cb(); }
2017-09-27 16:52:04 +00:00
};
var unlockRow = function (id, cb) {
if (APP.unlocked.row.indexOf(id) === -1) {
APP.unlocked.row.push(id);
}
enableRow(id);
if (typeof(cb) === "function") { cb(); }
2017-09-27 16:52:04 +00:00
};
var lockColumn = function (id, cb) {
var idx = APP.unlocked.col.indexOf(id);
if (idx !== -1) {
APP.unlocked.col.splice(idx, 1);
}
disableColumn(id);
if (typeof(cb) === "function") { cb(); }
2017-09-27 16:52:04 +00:00
};
var lockRow = function (id, cb) {
var idx = APP.unlocked.row.indexOf(id);
if (idx !== -1) {
APP.unlocked.row.splice(idx, 1);
}
disableRow(id);
if (typeof(cb) === "function") { cb(); }
2017-09-27 16:52:04 +00:00
};
/* Any time the realtime object changes, call this function */
var change = function (o, n, path, throttle, cb) {
if (path && !Array.isArray(path)) {
2017-09-27 16:52:04 +00:00
return;
}
if (path && path.join) {
debug("Change from [%s] to [%s] at [%s]",
o, n, path.join(', '));
}
var table = APP.$table[0];
common.notify();
var getFocus = function () {
var active = document.activeElement;
if (!active) { return; }
return {
el: active,
2017-10-02 17:02:31 +00:00
id: $(active).attr('data-rt-id'),
2017-09-27 16:52:04 +00:00
start: active.selectionStart,
end: active.selectionEnd
};
};
var setFocus = function (obj) {
2017-10-02 17:02:31 +00:00
var el;
if (document.body.contains(obj.el)) { el = obj.el; }
else if($('input[data-rt-id="' + obj.id + '"]').length) {
el = $('input[data-rt-id="' + obj.id + '"]')[0];
}
2017-09-27 16:52:04 +00:00
else { return; }
2017-10-02 17:02:31 +00:00
el.focus();
if (obj.start) { el.selectionStart = obj.start; }
if (obj.end) { el.selectionEnd = obj.end; }
2017-09-27 16:52:04 +00:00
};
var updateTable = function () {
var displayedObj = mergeUncommitted(APP.proxy, APP.uncommitted);
var colsOrder = sortColumns(displayedObj.content.colsOrder, APP.userid);
var conf = {
cols: colsOrder,
readOnly: APP.locked
};
2017-09-27 16:52:04 +00:00
var f = getFocus();
2017-10-02 17:02:31 +00:00
APP.$createRow.detach();
APP.$createCol.detach();
Render.updateTable(table, displayedObj, conf);
2017-10-02 17:02:31 +00:00
// Fix autocomplete bug:
displayedObj.content.rowsOrder.forEach(function (rowId) {
if (f.id === rowId) { return; }
$('input[data-rt-id="' + rowId +'"]').val(displayedObj.content.rows[rowId] || '');
2017-10-02 17:02:31 +00:00
});
displayedObj.content.colsOrder.forEach(function (colId) {
if (f.id === colId) { return; }
$('input[data-rt-id="' + colId +'"]')
.val(displayedObj.content.cols[colId] || '');
2017-09-27 16:52:04 +00:00
});
updateDisplayedTable();
setFocus(f);
if (typeof(cb) === "function") {
cb();
}
};
if (throttle) {
if (APP.throttled) { window.clearTimeout(APP.throttled); }
APP.throttled = window.setTimeout(function () {
updateTable();
2017-09-27 16:52:04 +00:00
}, throttle);
return;
}
window.setTimeout(updateTable);
};
var getRealtimeId = function (input) {
return input.getAttribute && input.getAttribute('data-rt-id');
};
2017-10-02 17:02:31 +00:00
var handleBookmark = function (id) {
setUserId(id === APP.userid ? '' : id, change);
};
2017-09-27 16:52:04 +00:00
/* Called whenever an event is fired on an input element */
var handleInput = function (input) {
var type = input.type.toLowerCase();
var id = getRealtimeId(input);
debug(input);
var object = APP.proxy;
var x = Render.getCoordinates(id)[0];
2017-10-02 17:02:31 +00:00
if (isUncommitted(id)) { object = APP.uncommitted; }
2017-09-27 16:52:04 +00:00
switch (type) {
case 'text':
debug("text[rt-id='%s'] [%s]", id, input.value);
Render.setValue(object, id, input.value);
change(null, null, null, 1000);
2017-09-27 16:52:04 +00:00
break;
case 'number':
debug("checkbox[tr-id='%s'] %s", id, input.value);
if (APP.unlocked.col.indexOf(x) >= 0 || x === APP.userid) {
var value = parseInt(input.value);
if (isNaN(value)) {
console.error("Got NaN?!");
break;
}
Render.setValue(object, id, value);
change();
} else {
debug('checkbox locked');
}
break;
default:
debug("Input[type='%s']", type);
break;
}
};
var hideInputs = function (id) {
if (APP.locked) { return; }
2017-09-27 16:52:04 +00:00
if (id) {
var type = Render.typeofId(id);
if (type === 'col') { return void lockColumn(id); }
if (type === 'row') { return void lockRow(id); }
2017-09-27 16:52:04 +00:00
return;
}
var keepColUnlocked = [APP.userid, APP.uncommitted.content.colsOrder[0]];
var keepRowUnlocked = APP.uncommitted.content.rowsOrder.slice();
var toLock = [];
APP.unlocked.col.forEach(function (id) {
if (keepColUnlocked.indexOf(id) !== -1) { return; }
toLock.push(id);
});
toLock.forEach(lockColumn);
2017-10-05 16:13:28 +00:00
toLock = [];
APP.unlocked.row.forEach(function (id) {
if (keepRowUnlocked.indexOf(id) !== -1) { return; }
2017-10-05 16:13:28 +00:00
toLock.push(id);
});
2017-10-05 16:13:28 +00:00
toLock.forEach(lockRow);
2017-09-27 16:52:04 +00:00
};
/* Called whenever an event is fired on a span */
var handleSpan = function (span) {
2017-10-03 17:04:29 +00:00
if (!span) { return; }
2017-09-27 16:52:04 +00:00
var id = span.getAttribute('data-rt-id');
var type = Render.typeofId(id);
var isRemove = span.className && span.className.split(' ')
.indexOf('cp-app-poll-table-remove') !== -1;
var isEdit = span.className && span.className.split(' ')
.indexOf('cp-app-poll-table-edit') !== -1;
2017-10-02 17:02:31 +00:00
var isBookmark = span.className && span.className.split(' ')
.indexOf('cp-app-poll-table-bookmark') !== -1;
2017-09-27 16:52:04 +00:00
var isLock = span.className && span.className.split(' ')
.indexOf('cp-app-poll-table-lock') !== -1;
var isLocked = span.className && span.className.split(' ').indexOf('fa-lock') !== -1;
if (type === 'row') {
if (isRemove) {
UI.confirm(Messages.poll_removeOption, function (res) {
2017-09-27 16:52:04 +00:00
if (!res) { return; }
Render.removeRow(APP.proxy, id, function () {
change();
});
});
} else if (isEdit) {
unlockRow(id, function () {
$('input[data-rt-id="' + id + '"]').focus();
2017-09-27 16:52:04 +00:00
});
}
} else if (type === 'col') {
if (isRemove) {
UI.confirm(Messages.poll_removeUser, function (res) {
2017-09-27 16:52:04 +00:00
if (!res) { return; }
Render.removeColumn(APP.proxy, id, function () {
change();
2017-10-03 17:04:29 +00:00
if (id === APP.userid) { setUserId(''); }
2017-09-27 16:52:04 +00:00
});
});
2017-10-02 17:02:31 +00:00
} else if (isBookmark) {
handleBookmark(id);
2017-09-27 16:52:04 +00:00
} else if (isLock && isLocked) {
unlockColumn(id, function () {
$('input[data-rt-id="' + id + '"]').focus();
2017-09-27 16:52:04 +00:00
});
} else if (isLock) {
lockColumn(id);
2017-09-27 16:52:04 +00:00
}
} else if (type === 'cell') {
change();
} else {
debug("UNHANDLED");
}
};
2017-10-23 12:53:31 +00:00
var optionOrder = {
3: 1, // ? => ✔
1: 2, // ✔ => ~
2: 0, // ~ => x
0: 3, // x => ?
// undefined => 3
};
2017-09-27 16:52:04 +00:00
var handleClick = function (e, isKeyup) {
if (APP.locked) { return; }
2017-09-27 16:52:04 +00:00
e.stopPropagation();
if (!APP.ready) { return; }
if (!isKeyup && e.which !== 1) { return; } // only allow left clicks
var target = e && e.target;
if (!target) { return void debug("NO TARGET"); }
var nodeName = target && target.nodeName;
switch (nodeName) {
case 'INPUT':
if ($(target).is('[type="text"]') && !isKeyup) { return; }
2017-09-27 16:52:04 +00:00
if (isKeyup && (e.keyCode === 13 || e.keyCode === 27)) {
var id = target.getAttribute('data-rt-id');
2017-10-02 17:02:31 +00:00
if ($(target).parents('.cp-app-poll-table-uncommitted').length
&& e.keyCode === 13) {
var type = Render.typeofId(id);
if (type === "row") { APP.$createRow.click(); }
else if (type === "col") { APP.$createCol.click(); }
break;
}
2017-09-27 16:52:04 +00:00
hideInputs(id);
break;
}
2017-10-02 17:02:31 +00:00
if ($(target).is('input[type="number"]')) {
// Nothing to do...
//console.error("number input focused?");
break;
}
2017-09-27 16:52:04 +00:00
handleInput(target);
break;
case 'LABEL':
var input = $('input[type="number"][id=' + $(target).attr('for') + ']');
var value = parseInt(input.val());
2017-10-23 12:53:31 +00:00
input.val(optionOrder[value]);
2017-09-27 16:52:04 +00:00
handleInput(input[0]);
break;
case 'SPAN':
handleSpan(target);
break;
case undefined:
//console.error(new Error("C'est pas possible!"));
break;
default:
debug(target, nodeName);
break;
}
};
/*
*/
2017-10-03 12:11:11 +00:00
var updatePublishButton = function () {
if (!APP.ready || !APP.proxy || !APP.$publishButton) { return; }
var p = APP.proxy.published;
var msg = (p ? Messages.poll_admin_button : Messages.poll_publish_button);
APP.$publishButton.attr('title', msg);
if (p) {
APP.$publishButton.removeClass('fa-check').addClass('fa-pencil');
return;
}
APP.$publishButton.addClass('fa-check').removeClass('fa-pencil');
};
2017-09-27 16:52:04 +00:00
var publish = APP.publish = function (bool) {
2017-10-03 12:11:11 +00:00
if (!APP.readOnly) {
if (!APP.ready) { return; }
if (APP.proxy.published !== bool) {
APP.proxy.published = bool;
}
} else {
// If readOnly, always put the app in published mode
bool = true;
2017-09-27 16:52:04 +00:00
}
console.log(bool);
$(APP.$mediaTagButton).toggle(!bool);
2017-09-27 16:52:04 +00:00
setTablePublished(bool);
2017-10-03 17:04:29 +00:00
/*['textarea'].forEach(function (sel) {
2017-09-27 16:52:04 +00:00
$(sel).attr('disabled', bool);
2017-10-03 17:04:29 +00:00
});*/
2017-10-03 12:11:11 +00:00
updatePublishButton();
APP.editor.refresh();
2017-09-27 16:52:04 +00:00
};
var setEditable = function (editable) {
APP.locked = APP.readOnly || !editable;
2017-09-27 16:52:04 +00:00
if (editable === false) {
// disable all the things
2017-10-24 15:42:08 +00:00
$('.cp-app-poll-realtime input, .cp-app-poll-realtime button, .cp-app-poll-upper button, .cp-app-poll-realtime textarea').attr('disabled', true);
2017-09-27 16:52:04 +00:00
$('span.cp-app-poll-table-edit, span.cp-app-poll-table-remove').hide();
$('span.cp-app-poll-table-lock').addClass('fa-lock').removeClass('fa-unlock')
.attr('title', Messages.poll_locked)
.css({'cursor': 'default'});
} else {
// enable
$('span.cp-app-poll-table-edit, span.cp-app-poll-table-remove').show();
$('span.cp-app-poll-table-lock').css({'cursor': ''});
$('.cp-app-poll-realtime button, .cp-app-poll-upper button, .cp-app-poll-realtime textarea').attr('disabled', false);
unlockElements();
}
};
2017-10-03 17:04:29 +00:00
var updatePublishedDescription = function () {
var v = APP.editor.getValue();
DiffMd.apply(DiffMd.render(v || ''), APP.$descriptionPublished);
2017-10-03 17:04:29 +00:00
};
2017-10-02 17:02:31 +00:00
var updateDescription = function (old, n) {
var o = APP.editor.getValue();
SframeCM.setValueAndCursor(APP.editor, o, n);
2017-10-03 17:04:29 +00:00
updatePublishedDescription();
2017-09-27 16:52:04 +00:00
common.notify();
};
var updateLocalDescription = function (n) {
2017-10-02 17:02:31 +00:00
APP.proxy.description = n;
2017-10-03 17:04:29 +00:00
updatePublishedDescription();
2017-09-27 16:52:04 +00:00
};
2017-10-04 17:06:16 +00:00
var getCommentId = Render.Uid('c');
var removeComment = function (uid) {
2017-11-03 17:38:04 +00:00
var idx = APP.proxy.commentsOrder.indexOf(uid);
if (idx !== -1) { APP.proxy.commentsOrder.splice(idx, 1); }
delete APP.proxy.comments[uid];
2017-10-04 17:06:16 +00:00
APP.updateComments();
};
/*var editComment = function (id) {
// TODO
};*/
var avatars = {};
var updateComments = APP.updateComments = function () {
if (!APP.proxy.comments) { APP.proxy.comments = {}; }
2017-11-03 17:38:04 +00:00
if (!APP.proxy.commentsOrder) { APP.proxy.commentsOrder = []; }
2017-10-04 17:06:16 +00:00
var profile;
if (common.isLoggedIn()) {
profile = metadataMgr.getUserData().profile;
}
var $comments = APP.$comments.html('');
var comments = APP.proxy.comments;
2017-11-03 17:38:04 +00:00
APP.proxy.commentsOrder.forEach(function (k) {
var c = comments[k];
2017-11-03 17:38:04 +00:00
if (!c) { return; }
var name = c.name || Messages.anonymous;
2017-10-04 17:06:16 +00:00
var $c = $('<div>', {
'class': 'cp-app-poll-comments-list-el'
}).prependTo($comments);
// Metadata
var $data = $('<div>', { 'class': 'cp-app-poll-comments-list-data' }).appendTo($c);
var $avatar = $('<span>', {
'class': 'cp-app-poll-comments-list-data-avatar cp-avatar'
}).appendTo($data);
if (c.avatar && avatars[c.avatar]) {
$avatar.append(avatars[c.avatar]);
} else {
common.displayAvatar($avatar, c.avatar, name, function ($img) {
2017-10-04 17:06:16 +00:00
if (c.avatar && $img.length) { avatars[c.avatar] = $img[0].outerHTML; }
});
}
if (c.profile) {
$('<a>', {
'href': APP.origin + '/profile/#' + c.profile,
'target': '_blank',
'class': 'cp-app-poll-comments-list-data-name'
}).appendTo($data).text(name);
2017-10-04 17:06:16 +00:00
} else {
$('<span>', {
'class': 'cp-app-poll-comments-list-data-name'
}).appendTo($data).text(name);
2017-10-04 17:06:16 +00:00
}
$('<span>', {
'class': 'cp-app-poll-comments-list-data-time'
}).appendTo($data).text(new Date(c.time).toLocaleString());
// Message
var $msg = $('<div>', { 'class': 'cp-app-poll-comments-list-msg' }).appendTo($c);
$('<div>', {
'class': 'cp-app-poll-comments-list-msg-text'
}).appendTo($msg).text(c.msg);
var $actions = $('<div>', {
'class': 'cp-app-poll-comments-list-msg-actions'
}).appendTo($msg);
// Actions
if (!APP.readOnly && (!c.profile || c.profile === profile)) {
2017-10-04 17:06:16 +00:00
$('<button>', {
'class': 'btn btn-secondary fa fa-times',
'title': Messages.poll_comment_remove,
'data-rt-id': k
}).appendTo($actions).click(function () { removeComment(k); });
2017-10-04 17:06:16 +00:00
/*$('<button>', {
'class': 'fa fa-pencil',
'title': 'TODO: edit comment',
'data-rt-id': c.uid
}).appendTo($actions).click(editComment);*/
}
});
common.notify();
};
var resetComment = function () {
APP.$addComment.find('.cp-app-poll-comments-add-name')
.val(metadataMgr.getUserData().name || '');
2017-10-04 17:06:16 +00:00
APP.$addComment.find('.cp-app-poll-comments-add-msg').val('');
};
var addComment = function () {
if (!APP.proxy.comments) { APP.proxy.comments = {}; }
2017-11-03 17:38:04 +00:00
if (!APP.proxy.commentsOrder) { APP.proxy.commentsOrder = []; }
var name = APP.$addComment.find('.cp-app-poll-comments-add-name').val().trim();
var msg = APP.$addComment.find('.cp-app-poll-comments-add-msg').val().trim();
2017-10-04 17:06:16 +00:00
var time = +new Date();
if (!msg) { return; }
2017-10-04 17:06:16 +00:00
var profile, avatar;
if (common.isLoggedIn()) {
profile = metadataMgr.getUserData().profile;
avatar = metadataMgr.getUserData().avatar;
}
var uid = getCommentId();
2017-11-03 17:38:04 +00:00
APP.proxy.commentsOrder.push(uid);
APP.proxy.comments[uid] = {
2017-10-04 17:06:16 +00:00
msg: msg,
name: name,
time: time,
profile: profile,
avatar: avatar
};
2017-10-04 17:06:16 +00:00
resetComment();
updateComments();
};
var initThumbnails = function () {
var privateDat = metadataMgr.getPrivateData();
if (!privateDat.thumbnails) { return; } // Thumbnails are disabled
var hash = privateDat.availableHashes.editHash ||
privateDat.availableHashes.viewHash;
if (!hash) { return; }
var href = privateDat.pathname + '#' + hash;
var $el = $('.cp-app-poll-realtime');
//var $el = $('#cp-app-poll-table');
var scrollTop;
var options = {
getContainer: function () { return $el[0]; },
filter: function (el, before) {
if (before) {
$el.parents().css('overflow', 'visible');
scrollTop = $('#cp-app-poll-form').scrollTop();
$el.css('max-height', Math.max(600, $(el).width()) + 'px');
$el.find('tr td:first-child, tr td:last-child, tr td:nth-last-child(2)')
.css('position', 'static');
$el.find('#cp-app-poll-comments').css('display', 'none');
$el.find('#cp-app-poll-table-container').css('text-align', 'center');
$el.find('#cp-app-poll-table-scroll').css('margin', 'auto');
$el.find('#cp-app-poll-table-scroll').css('max-width', '100%');
return;
}
$el.parents().css('overflow', '');
$el.css('max-height', '');
$el.find('#cp-app-poll-comments').css('display', '');
$el.find('#cp-app-poll-table-container').css('text-align', '');
$el.find('#cp-app-poll-table-scroll').css('margin', '');
$el.find('#cp-app-poll-table-scroll').css('max-width', '');
$el.find('tr td:first-child, tr td:last-child, tr td:nth-last-child(2)')
.css('position', '');
$('#cp-app-poll-form').scrollTop(scrollTop);
},
href: href,
getContent: function () { return JSON.stringify(APP.proxy.content); }
};
2017-11-07 13:51:53 +00:00
Thumb.initPadThumbnails(common, options);
};
var checkDeletedCells = function () {
// faster than forEach?
var c;
2018-03-02 17:33:43 +00:00
if (!APP.proxy || !APP.proxy.content) { return; }
for (var k in APP.proxy.content.cells) {
c = Render.getCoordinates(k);
if (APP.proxy.content.colsOrder.indexOf(c[0]) === -1 ||
APP.proxy.content.rowsOrder.indexOf(c[1]) === -1) {
delete APP.proxy.content.cells[k];
}
}
};
2017-10-03 12:11:11 +00:00
var onReady = function (info, userid) {
2017-09-27 16:52:04 +00:00
var proxy = APP.proxy;
var isNew = false;
var userDoc = JSON.stringify(proxy);
if (userDoc === "" || userDoc === "{}") { isNew = true; }
if (!isNew) {
if (proxy.info) {
2017-11-03 17:38:04 +00:00
// Migration
2017-09-27 16:52:04 +00:00
proxy.metadata = proxy.info;
delete proxy.info;
}
if (proxy.table) {
2017-11-03 17:38:04 +00:00
// Migration
2017-09-27 16:52:04 +00:00
proxy.content = proxy.table;
delete proxy.table;
}
checkDeletedCells();
2017-11-03 17:38:04 +00:00
if (proxy.comments && !proxy.commentsOrder) { // Migration
proxy.commentsOrder = Object.keys(copyObject(proxy.comments)).sort(function (a, b) {
return proxy.comments[a].time > proxy.comments[b].time;
});
}
2017-09-27 16:52:04 +00:00
if (proxy && proxy.metadata) {
metadataMgr.updateMetadata(proxy.metadata);
}
if (typeof (proxy) !== 'object' || Array.isArray(proxy) ||
(proxy.metadata && typeof(proxy.metadata.type) !== 'undefined' &&
proxy.metadata.type !== 'poll')) {
var errorText = Messages.typeError;
UI.errorLoadingScreen(errorText);
2017-09-27 16:52:04 +00:00
throw new Error(errorText);
}
} else {
2017-10-02 17:02:31 +00:00
Title.updateTitle(Title.defaultTitle);
2017-09-27 16:52:04 +00:00
}
if (typeof(proxy.type) === 'undefined') {
proxy.type = 'poll';
}
// Add uncommitted and unlock uncommited & own column
var uncommitted = APP.uncommitted = {};
prepareProxy(proxy, copyObject(Render.Example));
prepareProxy(uncommitted, copyObject(Render.Example));
var coluid = Render.coluid();
if (userid) {
// If userid exists, it means the user already has a pinned column
// and we should unlock it
unlockColumn(userid);
2017-09-27 16:52:04 +00:00
}
uncommitted.content.colsOrder.push(coluid);
unlockColumn(coluid);
var rowuid = Render.rowuid();
uncommitted.content.rowsOrder.push(rowuid);
unlockRow(rowuid);
2017-09-27 16:52:04 +00:00
/*
Extract uncommitted data (row or column) and create a new uncommitted row or column
*/
2017-09-27 16:52:04 +00:00
var getUncommitted = function (type) {
2017-10-02 17:02:31 +00:00
var ret = {}, toRemove;
2017-09-27 16:52:04 +00:00
var uncommitted = APP.uncommitted.content;
if (type === 'col') {
ret.colsOrder = uncommitted.colsOrder.slice();
ret.cols = copyObject(uncommitted.cols);
// get only the cells corresponding to the committed rows
2017-10-02 17:02:31 +00:00
toRemove = Object.keys(uncommitted.cells).filter(function (coor) {
2017-09-27 16:52:04 +00:00
var c = Render.getCoordinates(coor);
return APP.proxy.content.rowsOrder.indexOf(c[1]) !== -1;
});
ret.cells = {};
toRemove.forEach(function (k) {
ret.cells[k] = uncommitted.cells[k];
delete uncommitted.cells[k];
});
uncommitted.colsOrder = [Render.coluid()];
uncommitted.cols = {};
return ret;
}
// Row
ret.rowsOrder = uncommitted.rowsOrder.slice();
ret.rows = copyObject(uncommitted.rows);
// get only the cells corresponding to the committed rows
2017-10-02 17:02:31 +00:00
toRemove = Object.keys(uncommitted.cells).filter(function (coor) {
2017-09-27 16:52:04 +00:00
var c = Render.getCoordinates(coor);
return APP.proxy.content.colsOrder.indexOf(c[1]) !== -1;
});
ret.cells = {};
toRemove.forEach(function (k) {
ret.cells[k] = uncommitted.cells[k];
delete uncommitted.cells[k];
});
uncommitted.rowsOrder = [Render.rowuid()];
uncommitted.rows = {};
return ret;
};
APP.$createCol = $('#cp-app-poll-create-user').click(function () {
var uncommittedCopy = { content: getUncommitted('col') };
var id = uncommittedCopy.content.colsOrder[0];
2017-10-03 17:04:29 +00:00
if (!APP.userid) { setUserId(id); }
2017-09-27 16:52:04 +00:00
mergeUncommitted(proxy, uncommittedCopy, true);
change(null, null, null, null, function() {
unlockColumn(id);
unlockColumn(APP.uncommitted.content.colsOrder[0]);
2017-09-27 16:52:04 +00:00
});
});
APP.$createRow = $('#cp-app-poll-create-option').click(function () {
var uncommittedCopy = { content: getUncommitted('row') };
mergeUncommitted(proxy, uncommittedCopy, true);
change(null, null, null, null, function() {
2017-10-02 17:02:31 +00:00
var newId = APP.uncommitted.content.rowsOrder[0];
$('input[data-rt-id="' + newId + '"]').focus();
2017-09-27 16:52:04 +00:00
});
});
2017-10-10 16:19:49 +00:00
var displayedObj = mergeUncommitted(proxy, uncommitted, false);
var colsOrder = sortColumns(displayedObj.content.colsOrder, userid);
Render.updateTable($('#cp-app-poll-table-scroll').find('table')[0], displayedObj, {
cols: colsOrder,
readOnly: APP.readOnly
});
2017-09-27 16:52:04 +00:00
// Description
2017-10-03 17:04:29 +00:00
APP.editor.on('change', function () {
var val = APP.editor.getValue();
2017-09-27 16:52:04 +00:00
updateLocalDescription(val);
});
2017-10-04 17:06:16 +00:00
APP.$addComment.find('.cp-app-poll-comments-add-submit').click(addComment);
APP.$addComment.find('.cp-app-poll-comments-add-cancel').click(resetComment);
2017-09-27 16:52:04 +00:00
2017-10-10 16:19:49 +00:00
var $table = APP.$table = $('#cp-app-poll-table-scroll').find('table');
2017-09-27 16:52:04 +00:00
updateDisplayedTable();
updateDescription(null, APP.proxy.description || '');
initThumbnails();
2017-09-27 16:52:04 +00:00
var markdownTb = APP.markdownTb = common.createMarkdownToolbar(APP.editor);
$('.CodeMirror').parent().prepend(markdownTb.toolbar);
APP.toolbar.$rightside.append(markdownTb.button);
2017-10-04 17:06:16 +00:00
// Initialize author name for comments.
// Disable name modification for logged in users
var $cName = APP.$addComment.find('.cp-app-poll-comments-add-name')
.val(metadataMgr.getUserData().name || '');
if (common.isLoggedIn()) { $cName.attr('disabled', 'disabled'); }
updateComments();
2017-09-27 16:52:04 +00:00
$table
.click(handleClick)
.on('keyup', function (e) { handleClick(e, true); });
$(window).click(function(e) {
if (e.which !== 1) { return; }
hideInputs();
});
proxy
2017-10-02 17:02:31 +00:00
.on('change', ['metadata'], function () {
2017-09-27 16:52:04 +00:00
metadataMgr.updateMetadata(proxy.metadata);
})
.on('change', ['content'], function () {
change(null, null, null, 100);
})
2017-10-02 17:02:31 +00:00
.on('change', ['description'], updateDescription)
2017-10-04 17:06:16 +00:00
.on('change', ['comments'], updateComments)
.on('change', ['published'], function () {
publish(proxy.published);
})
.on('remove', [], function () {
change(null, null, null, 100);
});
2017-09-27 16:52:04 +00:00
// If the user's column is not committed, add his username
var $userInput = $('.cp-app-poll-table-uncommitted > input');
if (!APP.userid) {
2017-10-02 17:02:31 +00:00
var uname = metadataMgr.getUserData().name;
APP.uncommitted.content.cols[APP.uncommitted.content.colsOrder[0]] = uname;
2017-10-02 17:02:31 +00:00
$userInput.val(uname);
2017-09-27 16:52:04 +00:00
}
APP.ready = true;
if (!proxy.published) {
publish(false);
} else {
publish(true);
}
2018-01-11 14:45:12 +00:00
var passIfOk = function (t) {
t.assert($('#cp-app-poll-description-published').text().indexOf(
"Content for the description") === 0);
t.assert($('.cp-app-poll-comments-list-data-name').text().indexOf(
"Mr.Me") === 0);
t.assert($('.cp-app-poll-comments-list-msg-text').text().indexOf(
"Example comment yay") === 0);
t.assert($('input[value="Candy"]').length === 1);
t.assert($('input[value="IceCream"]').length === 1);
t.assert($('input[value="Soda"]').length === 1);
t.assert($('input[value="Meeee"]').length === 1);
t.pass();
};
if (!APP.readOnly) {
console.log("Here is the test");
Test(function (t) {
if ($('input[value="Candy"]').length) {
t.fail("Test has already been performed");
return;
}
nThen(function (waitFor) {
console.log("Here is the test1");
APP.editor.setValue("Content for the description");
$('.cp-app-poll-table-editing .cp-app-poll-table-text-cell input').val(
'Candy').keyup();
$('#cp-app-poll-create-option').click();
// TODO(cjd): Need to click outside to lock the first option we create.. bug?
$(window).trigger({ type: "click", which: 1 });
setTimeout(waitFor());
}).nThen(function (waitFor) {
$('.cp-app-poll-table-editing .cp-app-poll-table-text-cell input').val(
'IceCream').keyup();
$('#cp-app-poll-create-option').click();
setTimeout(waitFor());
}).nThen(function (waitFor) {
$('.cp-app-poll-table-editing .cp-app-poll-table-text-cell input').val(
'Soda').keyup();
$('#cp-app-poll-create-option').click();
setTimeout(waitFor());
}).nThen(function (waitFor) {
// Switch to non-admin mode
$('.cp-toolbar-icon-publish').click();
2018-01-11 14:45:12 +00:00
setTimeout(waitFor());
}).nThen(function (waitFor) {
$('.cp-app-poll-comments-add-name').val("Mr.Me").keyup();
$('.cp-app-poll-comments-add-msg').val("Example comment yay").keyup();
setTimeout(waitFor());
}).nThen(function (waitFor) {
$('.cp-app-poll-comments-add-submit').click();
setTimeout(waitFor());
}).nThen(function (waitFor) {
$('#cp-app-poll-create-user').parent().find('input').val('Meeee').keyup();
[1,3,2].forEach(function (num, i) {
var x = $($('.cp-app-poll-table-checkbox-contain label')[i]);
for (var ii = 0; ii < num; ii++) {
x.trigger({ type: 'click', which: 1 });
}
});
setTimeout(waitFor());
}).nThen(function (waitFor) {
$('#cp-app-poll-create-user').click();
setTimeout(waitFor());
}).nThen(function (waitFor) {
APP.rt.realtime.onSettle(waitFor());
}).nThen(function (/*waitFor*/) {
2018-01-11 14:45:12 +00:00
passIfOk(t);
});
});
} else {
Test(passIfOk);
}
UI.removeLoadingScreen();
var privateDat = metadataMgr.getPrivateData();
var skipTemp = Util.find(privateDat,
['settings', 'general', 'creation', 'noTemplate']);
var skipCreation = Util.find(privateDat, ['settings', 'general', 'creation', 'skip']);
if (isNew && (!AppConfig.displayCreationScreen || (!skipTemp && skipCreation))) {
2017-10-03 12:11:11 +00:00
common.openTemplatePicker();
}
2017-09-27 16:52:04 +00:00
};
// Manage disconnections because of network or error
var onDisconnect = function (info) {
if (APP.unrecoverable) { return; }
if (info && info.type) {
// Server error
return void common.onServerError(info, APP.toolbar, function () {
APP.unrecoverable = true;
setEditable(false);
});
}
setEditable(false);
//UI.alert(Messages.common_connectionLost, undefined, true);
2017-09-27 16:52:04 +00:00
};
2017-10-03 12:11:11 +00:00
var onReconnect = function () {
if (APP.unrecoverable) { return; }
2017-09-27 16:52:04 +00:00
setEditable(true);
//UI.findOKButton().click();
2017-09-27 16:52:04 +00:00
};
var getHeadingText = function () {
if (!APP.editor) { return; }
return SframeCM.getHeadingText(APP.editor);
};
2017-09-27 16:52:04 +00:00
var onCreate = function (info) {
APP.myID = info.myID;
if (APP.realtime !== info.realtime) {
APP.realtime = info.realtime;
}
metadataMgr = common.getMetadataMgr();
var titleCfg = { getHeadingText: getHeadingText };
Title = common.createTitle(titleCfg);
2017-09-27 16:52:04 +00:00
var configTb = {
displayed: [
'userlist',
'title',
'useradmin',
'spinner',
'newpad',
'share',
'limit',
'unpinnedWarning'
],
2017-09-27 16:52:04 +00:00
title: Title.getTitleConfig(),
metadataMgr: metadataMgr,
2017-10-03 12:11:11 +00:00
readOnly: APP.readOnly,
2017-09-27 16:52:04 +00:00
realtime: info.realtime,
sfCommon: common,
$container: APP.$bar,
$contentContainer: APP.$content
};
2017-10-03 12:11:11 +00:00
APP.toolbar = Toolbar.create(configTb);
2017-09-27 16:52:04 +00:00
Title.setToolbar(APP.toolbar);
var $rightside = APP.toolbar.$rightside;
var $drawer = APP.toolbar.$drawer;
2017-09-27 16:52:04 +00:00
metadataMgr.onChange(function () {
var md = copyObject(metadataMgr.getMetadata());
APP.proxy.metadata = md;
});
/* add a forget button */
var forgetCb = function (err) {
if (err) { return; }
setEditable(false);
};
2017-10-03 12:11:11 +00:00
var $forgetPad = common.createButton('forget', true, {}, forgetCb);
2017-09-27 16:52:04 +00:00
$rightside.append($forgetPad);
var $properties = common.createButton('properties', true);
$drawer.append($properties);
2017-09-27 16:52:04 +00:00
/* save as template */
2017-10-03 12:11:11 +00:00
if (!metadataMgr.getPrivateData().isTemplate) {
2017-09-27 16:52:04 +00:00
var templateObj = {
rt: info.realtime,
2017-10-03 12:11:11 +00:00
getTitle: function () { return metadataMgr.getMetadata().title; }
2017-09-27 16:52:04 +00:00
};
2017-10-03 12:11:11 +00:00
var $templateButton = common.createButton('template', true, templateObj);
2017-09-27 16:52:04 +00:00
$rightside.append($templateButton);
}
2017-10-03 12:11:11 +00:00
2017-10-03 14:50:59 +00:00
/* add an export button */
var $export = common.createButton('export', true, {}, exportFile);
$drawer.append($export);
2017-10-03 14:50:59 +00:00
var helpMenu = common.createHelpMenu(['poll']);
2018-02-27 16:38:29 +00:00
$('#cp-app-poll-form').prepend(helpMenu.menu);
$drawer.append(helpMenu.button);
2017-10-03 12:11:11 +00:00
if (APP.readOnly) { publish(true); return; }
var $publish = common.createButton('', true, {
name: 'publish',
icon: 'fa-check',
hiddenReadOnly: true
}).click(function () { publish(!APP.proxy.published); }).appendTo($rightside);
2017-10-03 12:11:11 +00:00
APP.$publishButton = $publish;
updatePublishButton();
if (common.isLoggedIn()) {
var fileDialogCfg = {
onSelect: function (data) {
if (data.type === 'file' && APP.editor) {
var mt = '<media-tag src="' + data.src + '" data-crypto-key="cryptpad:' + data.key + '"></media-tag>';
APP.editor.replaceSelection(mt);
return;
}
}
};
common.initFilePicker(fileDialogCfg);
APP.$mediaTagButton = common.createButton('mediatag', true).click(function () {
var pickerCfg = {
types: ['file'],
where: ['root']
};
common.openFilePicker(pickerCfg);
}).appendTo($rightside);
var $tags = common.createButton('hashtag', true);
$rightside.append($tags);
}
2017-09-27 16:52:04 +00:00
};
var main = function () {
nThen(function (waitFor) {
$(waitFor(function () {
UI.addLoadingScreen();
2017-09-27 16:52:04 +00:00
var $div = $('<div>').append(Pages['/poll/']());
$('body').append($div.html());
}));
SFCommon.create(waitFor(function (c) { APP.common = common = c; }));
}).nThen(function (waitFor) {
common.getSframeChannel().onReady(waitFor());
}).nThen(function (waitFor) {
common.handleNewFile(waitFor);
2017-09-27 16:52:04 +00:00
}).nThen(function (/* waitFor */) {
console.log('here');
Test.registerInner(common.getSframeChannel());
var metadataMgr = common.getMetadataMgr();
APP.locked = APP.readOnly = metadataMgr.getPrivateData().readOnly;
2017-09-27 16:52:04 +00:00
APP.loggedIn = common.isLoggedIn();
APP.SFCommon = common;
if (APP.readOnly) {
$('#cp-app-poll-form').addClass('cp-app-poll-readonly');
}
2017-10-04 17:06:16 +00:00
APP.origin = common.getMetadataMgr().getPrivateData().origin;
2017-09-27 16:52:04 +00:00
APP.$body = $('body');
APP.$bar = $('#cp-toolbar');
APP.$content = $('#cp-app-poll-content');
2017-10-03 17:04:29 +00:00
APP.$descriptionPublished = $('#cp-app-poll-description-published');
APP.$description = $('#cp-app-poll-description');
2017-10-04 17:06:16 +00:00
APP.$comments = $('#cp-app-poll-comments-list');
APP.$addComment = $('#cp-app-poll-comments-add');
2017-09-27 16:52:04 +00:00
2017-10-03 17:04:29 +00:00
APP.editor = CMeditor.fromTextArea(APP.$description[0], {
lineNumbers: true,
lineWrapping: true,
styleActiveLine : true,
mode: "markdown",
});
2017-11-03 17:38:04 +00:00
APP.$descriptionPublished.click(function (e) {
if (!e.target) { return; }
var $t = $(e.target);
if ($t.is('a') || $t.parents('a').length) {
e.preventDefault();
var $a = $t.is('a') ? $t : $t.parents('a').first();
var href = $a.attr('href');
if (!href) { return; }
common.openUnsafeURL(href);
2017-11-03 17:38:04 +00:00
}
});
2017-10-03 17:04:29 +00:00
2017-09-27 16:52:04 +00:00
var listmapConfig = {
data: {},
common: common,
logLevel: 1
};
2017-10-03 12:11:11 +00:00
if (APP.readOnly) {
2017-10-04 17:06:16 +00:00
$('#cp-app-poll-create-user, #cp-app-poll-create-option, #cp-app-poll-comments-add')
.remove();
$('#cp-app-poll-comments-add-title').remove();
2017-09-27 16:52:04 +00:00
}
var rt = APP.rt = Listmap.create(listmapConfig);
APP.proxy = rt.proxy;
var firstConnection = true;
2017-09-27 16:52:04 +00:00
rt.proxy.on('create', onCreate)
.on('ready', function (info) {
if (!firstConnection) { return; } // TODO fix this issue in listmap
firstConnection = false;
2017-09-27 16:52:04 +00:00
common.getPadAttribute('userid', function (e, userid) {
if (e) { console.error(e); }
APP.userid = userid;
2017-10-03 12:11:11 +00:00
onReady(info, userid);
2017-09-27 16:52:04 +00:00
});
})
.on('disconnect', onDisconnect)
.on('reconnect', onReconnect);
});
};
main();
});