Merge branch 'form' into staging

pull/1/head
ansuz 4 years ago
commit 85db842d8f

@ -64,7 +64,8 @@
@cryptpad_color_light_green: #c5ffa8; @cryptpad_color_light_green: #c5ffa8;
@cryptpad_color_light_green_fade: fade(@cryptpad_color_light_green, 20%); @cryptpad_color_light_green_fade: fade(@cryptpad_color_light_green, 20%);
@cryptpad_color_light_yellow: #FFE69C; @cryptpad_color_light_yellow: #FFE69C;
@cryptpad_color_yellow_fade: fade(#FFE69C, 15%); @cryptpad_color_yellow_fade: fade(@cryptpad_color_light_yellow, 50%);
@cryptpad_color_yellow_fader: fade(#FFE69C, 15%); // not in light theme
@cryptpad_color_lighter_blue: #d2e1f2; @cryptpad_color_lighter_blue: #d2e1f2;
@cryptpad_color_link:@cryptpad_color_brand_300; @cryptpad_color_link:@cryptpad_color_brand_300;
@ -115,7 +116,7 @@
@cp_forms-disabled: @cryptpad_color_grey_500; @cp_forms-disabled: @cryptpad_color_grey_500;
// Bootstrap alerts // Bootstrap alerts
@cp_alerts-warning-bg: @cryptpad_color_yellow_fade; @cp_alerts-warning-bg: @cryptpad_color_yellow_fader;
@cp_alerts-warning-fg: @cryptpad_color_light_yellow; @cp_alerts-warning-fg: @cryptpad_color_light_yellow;
@cp_alerts-warning-text: @cryptpad_color_light_yellow; @cp_alerts-warning-text: @cryptpad_color_light_yellow;
@cp_alerts-danger-bg: @cryptpad_color_red_fader; @cp_alerts-danger-bg: @cryptpad_color_red_fader;
@ -432,8 +433,8 @@
@cp_form-bg2: @cryptpad_color_grey_900; @cp_form-bg2: @cryptpad_color_grey_900;
@cp_form-border: @cryptpad_color_grey_800; @cp_form-border: @cryptpad_color_grey_800;
@cp_form-poll-color: @cryptpad_color_grey_800; @cp_form-poll-color: @cryptpad_color_grey_800;
@cp_form-poll-no: @cryptpad_color_light_red; @cp_form-poll-no: fade(@cryptpad_color_red, 25%);
@cp_form-poll-yes: @cryptpad_color_light_green; @cp_form-poll-yes: fade(@cryptpad_color_green, 25%);
@cp_form-poll-maybe: @cryptpad_color_light_yellow; @cp_form-poll-maybe: @cryptpad_color_grey_700;
@cp_form-poll-yes-color: @cryptpad_color_green; @cp_form-poll-yes-color: @cryptpad_color_green;
@cp_form-invalid: @cryptpad_color_red; @cp_form-invalid: @cryptpad_color_red;

@ -432,8 +432,8 @@
@cp_form-bg2: @cryptpad_color_grey_100; @cp_form-bg2: @cryptpad_color_grey_100;
@cp_form-border: @cryptpad_color_grey_200; @cp_form-border: @cryptpad_color_grey_200;
@cp_form-poll-color: @cryptpad_color_grey_800; @cp_form-poll-color: @cryptpad_color_grey_800;
@cp_form-poll-no: @cryptpad_color_light_red; @cp_form-poll-no: fade(@cryptpad_color_light_red, 75%);
@cp_form-poll-yes: @cryptpad_color_light_green; @cp_form-poll-yes: fade(@cryptpad_color_light_green, 75%);
@cp_form-poll-maybe: @cryptpad_color_light_yellow; @cp_form-poll-maybe: @cryptpad_color_grey_300;
@cp_form-poll-yes-color: @cryptpad_color_green; @cp_form-poll-yes-color: @cryptpad_color_green;
@cp_form-invalid: @cryptpad_color_red; @cp_form-invalid: @cryptpad_color_red;

@ -1069,6 +1069,7 @@ define([
editor.replaceSelections(newTexts, 'around'); editor.replaceSelections(newTexts, 'around');
editor.focus(); editor.focus();
}; };
Messages.mdToolbar_embed = "Embed file"; // XXX
for (var k in actions) { for (var k in actions) {
$('<button>', { $('<button>', {
'data-type': k, 'data-type': k,

@ -572,7 +572,9 @@ define([
if (!readOnly) { onLocal(); } if (!readOnly) { onLocal(); }
evOnReady.fire(newPad); evOnReady.fire(newPad);
common.openPadChat(onLocal); // In forms, only editors can see the chat
if (!readOnly || type !== 'form') { common.openPadChat(onLocal); }
if (!readOnly && cursorGetter) { if (!readOnly && cursorGetter) {
common.openCursorChannel(onLocal); common.openCursorChannel(onLocal);
cursor = common.createCursor(onLocal); cursor = common.createCursor(onLocal);

@ -166,6 +166,7 @@ MessengerUI, Messages, Pages) {
var showColors = false; var showColors = false;
var updateUserList = function (toolbar, config, forceOffline) { var updateUserList = function (toolbar, config, forceOffline) {
if (!config.displayed || config.displayed.indexOf('userlist') === -1) { return; } if (!config.displayed || config.displayed.indexOf('userlist') === -1) { return; }
if (toolbar.isAlone) { return; }
// Make sure the elements are displayed // Make sure the elements are displayed
var $userButtons = toolbar.userlist; var $userButtons = toolbar.userlist;
var $userlistContent = toolbar.userlistContent; var $userlistContent = toolbar.userlistContent;
@ -1216,6 +1217,7 @@ MessengerUI, Messages, Pages) {
if (!config.metadataMgr) { return; } if (!config.metadataMgr) { return; }
var metadataMgr = config.metadataMgr; var metadataMgr = config.metadataMgr;
var notify = function(type, name, oldname) { var notify = function(type, name, oldname) {
if (toolbar.isAlone) { return; }
// type : 1 (+1 user), 0 (rename existing user), -1 (-1 user) // type : 1 (+1 user), 0 (rename existing user), -1 (-1 user)
if (typeof name === "undefined") { return; } if (typeof name === "undefined") { return; }
name = name || Messages.anonymous; name = name || Messages.anonymous;
@ -1515,6 +1517,15 @@ MessengerUI, Messages, Pages) {
} }
}; };
// disable notification, userlist and chat
toolbar.alone = function () {
toolbar.userlist.hide();
toolbar.chat.hide();
$('.cp-toolbar-userlist-drawer').remove();
$('.cp-toolbar-chat-drawer').remove();
toolbar.isAlone = true;
};
// On log out, remove permanently the realtime elements of the toolbar // On log out, remove permanently the realtime elements of the toolbar
Common.onLogout(function () { Common.onLogout(function () {
failed(); failed();

@ -32,6 +32,9 @@
div.cp-form-creator-results, .cp-app-form-button-creator { div.cp-form-creator-results, .cp-app-form-button-creator {
display: none !important; display: none !important;
} }
.cp-form-block {
.tools_unselectable();
}
} }
#cp-app-form-container { #cp-app-form-container {
@ -67,6 +70,9 @@
.cp-form-creator-settings { .cp-form-creator-settings {
padding: 30px; padding: 30px;
.cp-form-actions {
margin-top: 5px;
}
& > div:not(:last-child) { & > div:not(:last-child) {
margin-bottom: 20px; margin-bottom: 20px;
} }
@ -90,6 +96,7 @@
div.cp-form-creator-content, div.cp-form-creator-results { div.cp-form-creator-content, div.cp-form-creator-results {
max-width: 1000px; max-width: 1000px;
min-width: 300px; min-width: 300px;
margin-top: 10px;
padding: 10px; padding: 10px;
display: flex; display: flex;
flex-flow: column; flex-flow: column;
@ -193,6 +200,24 @@
.cp-form-page + .cp-form-send-container { .cp-form-page + .cp-form-send-container {
margin-top: 10px; margin-top: 10px;
} }
.cp-form-send-container {
text-align: center;
margin: 50px auto 100px auto;
button {
&:not(:last-child) {
margin-right: 10px;
}
}
.cp-form-invalid-warning {
color: @cp_form-invalid;
ul {
list-style-type: disclosure-closed;
}
li {
text-align: left;
}
}
}
.cp-form-page-container { .cp-form-page-container {
display: flex; display: flex;
@ -206,16 +231,16 @@
justify-content: center; justify-content: center;
} }
button { button {
&.cp-next { display: flex;
.fa { height: 38px;
margin-right: 0; align-items: center;
margin-left: 5px; i {
} margin-right: 0;
font-size: 25px;
} }
} }
} }
.cp-form-block { .cp-form-block {
.tools_unselectable();
background: @cp_form-bg1; background: @cp_form-bg1;
padding: 10px; padding: 10px;
&:not(:last-child) { &:not(:last-child) {
@ -240,12 +265,22 @@
&.sortable-ghost { visibility: hidden; } &.sortable-ghost { visibility: hidden; }
&.sortable-drag { opacity: 0.9 !important; } &.sortable-drag { opacity: 0.9 !important; }
.flatpickr-calendar.inline {
box-shadow: unset;
border: 1px solid @cryptpad_text_col;
}
.cp-form-block-question { .cp-form-block-question {
margin-bottom: 5px; margin-bottom: 5px;
.cp-form-block-question-number {
font-weight: bold;
margin-right: 10px;
}
} }
.cp-form-block-content { .cp-form-block-content {
overflow-x: auto; overflow-x: auto;
.cp-form-page-break-edit { .cp-form-page-break-edit {
font-size: 20px;
text-align: center; text-align: center;
padding: 10px; padding: 10px;
i { i {
@ -258,7 +293,13 @@
justify-content: space-between; justify-content: space-between;
} }
input:invalid { input:invalid {
border: 1px solid @cp_form-invalid; border: 2px solid @cp_form-invalid;
color: @cp_form-invalid;
}
media-tag {
& > * {
max-width: 100%;
}
} }
} }
.cp-form-input-block { .cp-form-input-block {
@ -312,6 +353,12 @@
margin-left: 10px; margin-left: 10px;
} }
} }
.cp-form-multiple-picker {
margin: 10px 0px 0px 0px;
button {
margin: 10px 10px 0px 0px;
}
}
} }
} }
.cp-form-edit-max-options { .cp-form-edit-max-options {
@ -357,7 +404,7 @@
} }
} }
.cp-form-edit-block-input { .cp-form-edit-block-input {
margin-bottom: 5px; // XXX DB margin bug margin-bottom: 5px;
&.sortable-ghost { visibility: hidden; } &.sortable-ghost { visibility: hidden; }
&.sortable-drag { opacity: 0.9 !important; } &.sortable-drag { opacity: 0.9 !important; }
display: flex; display: flex;
@ -409,9 +456,9 @@
} }
.cp-form-results-type-textarea-data { .cp-form-results-type-textarea-data {
white-space: pre-wrap; white-space: pre-wrap;
font-size: 14px; padding: 5px 10px;
border: 1px solid @cp_profile-hint; background: @cp_form-bg2;
padding: 0 5px; &:not(:last-child) { margin-bottom: 1px; }
} }
.cp-form-results-type-radio { .cp-form-results-type-radio {
display: table; display: table;
@ -475,48 +522,125 @@
} }
} }
} }
.cp-form-sort-hint {
margin-bottom: 10px;
}
.cp-form-type-sort { .cp-form-type-sort {
cursor: grab; cursor: grab;
padding: 2px; padding: 5px;
.cp-form-handle { .cp-form-handle {
margin-right: 5px; margin-right: 5px;
} }
.cp-form-sort-order { .cp-form-sort-order {
border: 1px solid @cp_profile-hint; border: 1px solid @cryptpad_text_col;
padding: 0 5px; padding: 0 5px;
margin-right: 5px; margin-right: 5px;
} }
&:hover {
background-color: @cp_app-bg;
color: @cryptpad_color_link;
.cp-form-sort-order {
border-color: @cryptpad_color_link;
}
}
} }
.cp-form-type-poll-container {
overflow: auto;
}
.cp-form-type-poll { .cp-form-type-poll {
display: inline-flex; display: inline-flex;
flex-flow: column; flex-flow: column;
& > div { & > div {
display: flex; display: flex;
} }
.cp-form-poll-body {
flex-flow: column;
max-height: 225px;
overflow: auto;
& > div {
display: flex;
}
}
.cp-poll-cell { .cp-poll-cell {
width: 100px; width: 100px;
height: 40px; height: 35px;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
margin-top:5px;
margin-left:5px;
&:first-child { &:first-child {
width: 200px; width: 200px;
} }
button { button {
width: 100%; width: 100%;
border-top: 0px;
border-left: 0px;
background-color: @cp_form-bg1;
.fa{
margin-left:10px;
transform: rotate(-45deg);
}
}
.cp-form-total-yes {
margin-right: 5px;
}
&.cp-poll-best {
font-weight: bold;
}
}
.cp-poll-answer-name {
padding: 0 5px;
display: flex;
:last-child {
flex: 1;
min-width: 0;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.cp-avatar {
.avatar_main(30px);
margin-right: 10px;
} }
} }
.cp-poll-time-day { .cp-poll-time-day {
flex-basis: 100px; flex-basis: 100px;
border-right: 1px solid @cryptpad_text_col; border-bottom: 1px solid @cryptpad_text_col;
border-left: 1px solid @cryptpad_text_col; }
border-top: 1px solid @cryptpad_text_col; &:not(.cp-form-poll-switch) {
& > div {
&:last-child {
margin-bottom: 5px;
}
}
.cp-form-poll-body {
&::-webkit-scrollbar {
display: none;
}
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
} }
&.cp-form-poll-switch { &.cp-form-poll-switch {
display: flex;
flex-flow: row; flex-flow: row;
.cp-avatar {
.avatar_main(20px);
}
& > div { & > div {
flex-flow: column; flex-flow: column;
&.cp-form-poll-body {
flex-flow: row;
max-width: 550px;
max-height: unset;
scroll-snap-type: x mandatory;
& > div {
flex-flow: column;
}
}
} }
.cp-poll-cell:not(.cp-poll-switch) { .cp-poll-cell:not(.cp-poll-switch) {
&:first-child { &:first-child {
@ -529,9 +653,8 @@
.cp-poll-time-day { .cp-poll-time-day {
flex-basis: 40px; flex-basis: 40px;
border-right: none; border-right: none;
border-bottom: 1px solid @cryptpad_text_col; border-right: 1px solid @cryptpad_text_col;
border-left: 1px solid @cryptpad_text_col; border-bottom: 0px;
border-top: 1px solid @cryptpad_text_col;
} }
} }
.cp-form-poll-choice, .cp-form-poll-answer { .cp-form-poll-choice, .cp-form-poll-answer {
@ -539,6 +662,7 @@
display: none; display: none;
} }
color: @cp_form-poll-color; color: @cp_form-poll-color;
font-size: 25px;
&[data-value="0"] { &[data-value="0"] {
background: @cp_form-poll-no; background: @cp_form-poll-no;
.cp-no { display: inline; } .cp-no { display: inline; }
@ -555,7 +679,9 @@
div.cp-form-poll-choice { div.cp-form-poll-choice {
cursor: pointer; cursor: pointer;
padding: 5px; padding: 5px;
border: 5px double @cp_form-bg1; //margin: 10px 0px 0px 10px;
//border: 1px solid @cryptpad_text_col;
color: @cryptpad_text_col;
} }
div.cp-form-poll-answer { div.cp-form-poll-answer {
color: @cp_form-poll-yes-color; color: @cp_form-poll-yes-color;

@ -27,7 +27,7 @@ define([
'/bower_components/sortablejs/Sortable.min.js', '/bower_components/sortablejs/Sortable.min.js',
'cm/addon/display/placeholder', 'cm/addon/display/placeholder',
'cm/mode/markdown/markdown', 'cm/mode/gfm/gfm',
'css!cm/lib/codemirror.css', 'css!cm/lib/codemirror.css',
'css!/bower_components/codemirror/lib/codemirror.css', 'css!/bower_components/codemirror/lib/codemirror.css',
@ -86,13 +86,18 @@ define([
Messages.form_poll_text = "Text"; Messages.form_poll_text = "Text";
Messages.form_poll_day = "Day"; Messages.form_poll_day = "Day";
Messages.form_poll_time = "Time"; Messages.form_poll_time = "Time";
Messages.form_poll_switch = "Switch axes" // XXX DB
Messages.form_pollTotal = "Total";
Messages.form_pollYourAnswers = "Your answers";
Messages.form_textType = "Text type"; Messages.form_textType = "Text type";
Messages.form_text_text = "Text"; Messages.form_text_text = "Text";
Messages.form_text_url = "URL"; Messages.form_text_url = "URL";
Messages.form_text_email = "Email"; Messages.form_text_email = "Email";
Messages.form_text_number = "Number"; Messages.form_text_number = "Number";
Messages.form_input_ph_url = "Enter a URL here";
Messages.form_input_ph_email = "Email address";
Messages.form_default = "Your question here?"; Messages.form_default = "Your question here?";
Messages.form_type_input = "Text"; // XXX Messages.form_type_input = "Text"; // XXX
@ -103,6 +108,7 @@ define([
Messages.form_type_multicheck = "Multiline Checkbox"; // XXX Messages.form_type_multicheck = "Multiline Checkbox"; // XXX
Messages.form_type_poll = "Poll"; // XXX Messages.form_type_poll = "Poll"; // XXX
Messages.form_type_sort = "Ordered list"; // XXX Messages.form_type_sort = "Ordered list"; // XXX
Messages.form_sort_hint = "Please drag these items from most (1) to least ({0}) preferred.";
Messages.form_type_md = "Description"; // XXX Messages.form_type_md = "Description"; // XXX
Messages.form_type_page = "Page break"; // XXX Messages.form_type_page = "Page break"; // XXX
@ -111,17 +117,23 @@ define([
Messages.form_duplicates = "Duplicate entries have been removed"; Messages.form_duplicates = "Duplicate entries have been removed";
Messages.form_maxOptions = "{0} answer(s) max"; Messages.form_maxOptions = "{0} answer(s) max";
Messages.form_maxLength = "Characters limit: {0}/{1}";
Messages.form_submit = "Submit"; Messages.form_submit = "Submit";
Messages.form_update = "Update"; Messages.form_update = "Update";
Messages.form_submitWarning = "Submit anyway";
Messages.form_updateWarning = "Update anyway";
Messages.form_reset = "Reset"; Messages.form_reset = "Reset";
Messages.form_sent = "Sent"; Messages.form_sent = "Sent";
Messages.form_delete = "Delete"; Messages.form_delete = "Delete";
Messages.form_invalidWarning = "There are errors in some answers:";
Messages.form_invalidQuestion = "Question {0}";
Messages.form_cantFindAnswers = "Unable to retrieve your existing answers for this form."; Messages.form_cantFindAnswers = "Unable to retrieve your existing answers for this form.";
Messages.form_answered = "You already answered this form"; Messages.form_answered = "You already answered this form";
Messages.form_results = "Responses"; Messages.form_results = "Responses";
Messages.form_results_empty = "There are no responses";
Messages.form_editor = "Editor"; Messages.form_editor = "Editor";
Messages.form_form = "Form"; Messages.form_form = "Form";
Messages.form_viewResults = "Go to responses"; Messages.form_viewResults = "Go to responses";
@ -160,11 +172,10 @@ define([
Messages.form_add_option = "Add option"; Messages.form_add_option = "Add option";
Messages.form_add_item = "Add item"; Messages.form_add_item = "Add item";
Messages.form_addMultiple = "Add all"; Messages.form_addMultiple = "Add all";
Messages.form_addMultipleHint = "Add multiple dates and times"
Messages.form_clear = "Clear"; Messages.form_clear = "Clear";
Messages.form_page_prev = "Previous";
Messages.form_page = "Page {0}/{1}"; Messages.form_page = "Page {0}/{1}";
Messages.form_page_next = "Next";
Messages.form_anonymousBox = "Answer anonymously"; Messages.form_anonymousBox = "Answer anonymously";
@ -440,13 +451,13 @@ define([
// Calendar time // Calendar time
if (v.type) { if (v.type) {
var multipleInput = h('input'); var multipleInput = h('input', {placeholder: Messages.form_addMultipleHint});
var multipleClearButton = h('button.btn', Messages.form_clear); var multipleClearButton = h('button.btn', Messages.form_clear);
var addMultipleButton = h('button.btn', [ var addMultipleButton = h('button.btn', [
h('i.fa.fa-plus'), h('i.fa.fa-plus'),
h('span', Messages.form_addMultiple) h('span', Messages.form_addMultiple)
]); ]);
addMultiple = h('div', { style: "display: none;" }, [ addMultiple = h('div.cp-form-multiple-picker', { style: "display: none;" }, [
multipleInput, multipleInput,
addMultipleButton, addMultipleButton,
multipleClearButton multipleClearButton
@ -487,6 +498,7 @@ define([
$(addMultiple).hide(); $(addMultiple).hide();
} }
} else { } else {
$(addMultiple).hide();
$calendar.show(); $calendar.show();
$container.hide(); $container.hide();
} }
@ -500,6 +512,7 @@ define([
refreshView(); refreshView();
if (val !== "text") { if (val !== "text") {
$container.find('.cp-form-edit-block-input').remove(); $container.find('.cp-form-edit-block-input').remove();
$(add).click();
return; return;
} }
$container.find('input').each(function (i, input) { $container.find('input').each(function (i, input) {
@ -598,12 +611,13 @@ define([
val = +f.selectedDates[0]; val = +f.selectedDates[0];
} }
} }
if (values.indexOf(val) === -1) { values.push(val); } if (val && values.indexOf(val) === -1) { values.push(val); }
else { duplicates = true; } else { duplicates = true; }
}); });
} }
values = values.filter(Boolean); // Block empty or undeinfed options
if (!values.length) { if (!values.length) {
return void UI.warn(Messages.error); // XXX error message: no values return void UI.warn(Messages.error);
} }
var res = { values: values }; var res = { values: values };
@ -672,10 +686,13 @@ define([
var _dateT = new Date(data); var _dateT = new Date(data);
data = Flatpickr.formatDate(_dateT, timeFormat); data = Flatpickr.formatDate(_dateT, timeFormat);
} }
return h('div.cp-poll-cell.cp-form-poll-option', data); return h('div.cp-poll-cell.cp-form-poll-option', {
title: Util.fixHTML(data)
}, data);
}); });
// Insert axis switch button // Insert axis switch button
var switchAxis = h('button.btn.btn-default', [ var switchAxis = h('button.btn.btn-default', [
Messages.form_poll_switch,
h('i.fa.fa-exchange'), h('i.fa.fa-exchange'),
]); ]);
els.unshift(h('div.cp-poll-cell.cp-poll-switch', switchAxis)); els.unshift(h('div.cp-poll-cell.cp-poll-switch', switchAxis));
@ -700,10 +717,14 @@ define([
} }
// Add answers // Add answers
var bodyEls = [];
if (Array.isArray(answers)) { if (Array.isArray(answers)) {
answers.forEach(function (answer) { answers.forEach(function (answerObj) {
if (!answer.name || !answer.values) { return; } var answer = answerObj.results;
var _name = answer.name; if (!answer || !answer.values) { return; }
var name = Util.find(answerObj, ['user', 'name']) || answer.name || Messages.anonymous;
var avatar = h('span.cp-avatar');
APP.common.displayAvatar($(avatar), Util.find(answerObj, ['user', 'avatar']), name);
var values = answer.values || {}; var values = answer.values || {};
var els = opts.values.map(function (data) { var els = opts.values.map(function (data) {
var res = values[data] || 0; var res = values[data] || 0;
@ -713,10 +734,17 @@ define([
}, v); }, v);
return cell; return cell;
}); });
els.unshift(h('div.cp-poll-cell.cp-poll-answer-name', _name)); els.unshift(h('div.cp-poll-cell.cp-poll-answer-name', {
lines.push(h('div', els)); title: Util.fixHTML(name)
}, [
avatar,
h('span', name)
]));
bodyEls.push(h('div', els));
}); });
} }
var body = h('div.cp-form-poll-body', bodyEls);
lines.push(body);
var $s = $(switchAxis).click(function () { var $s = $(switchAxis).click(function () {
$s.closest('.cp-form-type-poll').toggleClass('cp-form-poll-switch'); $s.closest('.cp-form-type-poll').toggleClass('cp-form-poll-switch');
@ -724,6 +752,111 @@ define([
return lines; return lines;
}; };
var makePollTotal = function (answers, opts, myLine, evOnChange) {
if (!Array.isArray(answers)) { return; }
var totals = {};
var myTotals = {};
var updateMyTotals = function () {
if (!myLine) { return; }
opts.values.forEach(function (data) {
myLine.some(function (el) {
if ($(el).data('option') !== data) { return; }
var res = Number($(el).attr('data-value')) || 0;
if (res === 1) {
myTotals[data] = {
y: 1,
m: 0
};
}
else if (res === 2) {
myTotals[data] = {
y: 0,
m: 1
};
} else {
delete myTotals[data];
}
return true;
});
});
};
var totalEls = opts.values.map(function (data) {
var y = 0; // Yes
var m = 0; // Maybe
answers.forEach(function (answerObj) {
var answer = answerObj.results;
if (!answer || !answer.values) { return; }
var values = answer.values || {};
var res = Number(values[data]) || 0;
if (res === 1) { y++; }
else if (res === 2) { m++; }
});
totals[data] = {
y: y,
m: m
};
return h('div.cp-poll-cell', {
'data-id': data
}, [
h('span.cp-form-total-yes', y),
h('span.cp-form-total-maybe', '('+m+')'),
]);
});
totalEls.unshift(h('div.cp-poll-cell', Messages.form_pollTotal));
var total = h('div.cp-poll-total', totalEls);
var $total = $(total);
var refreshBest = function () {
var totalMax = {
value: 0,
data: []
};
Object.keys(totals).forEach(function (k) {
var obj = Util.clone(totals[k]);
if (myTotals[k]) {
obj.y += myTotals[k].y || 0;
obj.m += myTotals[k].m || 0;
}
if (obj.y === totalMax.value) {
totalMax.data.push(k);
} else if (obj.y > totalMax.value) {
totalMax.value = obj.y;
totalMax.data = [k];
}
});
if (totalMax.value) {
$total.find('[data-id]').removeClass('cp-poll-best');
totalMax.data.forEach(function (k) {
$total.find('[data-id="'+k+'"]').addClass('cp-poll-best');
});
}
};
refreshBest();
if (myLine && evOnChange) {
var updateValues = function () {
totalEls.forEach(function (cell) {
var $c = $(cell);
var data = $c.attr('data-id');
if (!data) { return; }
var y = totals[data].y + ((myTotals[data] || {}).y || 0);
var m = totals[data].m + ((myTotals[data] || {}).m || 0);
$c.find('.cp-form-total-yes').text(y);
$c.find('.cp-form-total-maybe').text('('+m+')');
});
};
evOnChange.reg(function () {
updateMyTotals();
updateValues();
refreshBest();
});
}
return total;
};
var getEmpty = function (empty) { var getEmpty = function (empty) {
if (empty) { if (empty) {
@ -744,10 +877,14 @@ define([
}; };
var getBlockAnswers = function (answers, uid, filterCurve) { var getBlockAnswers = function (answers, uid, filterCurve) {
if (!answers) { return; }
return Object.keys(answers || {}).map(function (user) { return Object.keys(answers || {}).map(function (user) {
if (filterCurve && user === filterCurve) { return; } if (filterCurve && user === filterCurve) { return; }
try { try {
return answers[user].msg[uid]; return {
user: answers[user].msg._userdata,
results: answers[user].msg[uid]
};
} catch (e) { console.error(e); } } catch (e) { console.error(e); }
}).filter(Boolean); }).filter(Boolean);
}; };
@ -877,7 +1014,8 @@ define([
get: function (opts, a, n, evOnChange) { get: function (opts, a, n, evOnChange) {
if (!opts) { opts = TYPES.input.defaultOpts; } if (!opts) { opts = TYPES.input.defaultOpts; }
var tag = h('input', { var tag = h('input', {
type: opts.type type: opts.type,
placeholder: Messages['form_input_ph_'+opts.type] || ''
}); });
var $tag = $(tag); var $tag = $(tag);
$tag.on('change keypress', Util.throttle(function () { $tag.on('change keypress', Util.throttle(function () {
@ -922,23 +1060,49 @@ define([
}, },
get: function (opts, a, n, evOnChange) { get: function (opts, a, n, evOnChange) {
if (!opts) { opts = TYPES.textarea.defaultOpts; } if (!opts) { opts = TYPES.textarea.defaultOpts; }
var tag = h('textarea', {maxlength: opts.maxLength}); var text = h('textarea', {maxlength: opts.maxLength});
var $tag = $(tag); var $text = $(text);
$tag.on('change keypress', Util.throttle(function () { var charCount = h('div.cp-form-type-textarea-charcount');
var updateChar = function () {
var l = $text.val().length;
if (l > opts.maxLength) {
$text.val($text.val().slice(0, opts.maxLength));
l = $text.val().length;
}
$(charCount).text(Messages._getKey('form_maxLength', [
$text.val().length,
opts.maxLength
]));
};
updateChar();
var tag = h('div.cp-form-type-textarea', [
text,
charCount
]);
var evChange = Util.throttle(function () {
evOnChange.fire(); evOnChange.fire();
}, 500)); }, 500);
$text.on('change keypress keyup keydown', function () {
setTimeout(updateChar);
evChange();
});
var cursorGetter; var cursorGetter;
var setCursorGetter = function (f) { cursorGetter = f; }; var setCursorGetter = function (f) { cursorGetter = f; };
return { return {
tag: tag, tag: tag,
getValue: function () { return $tag.val(); }, getValue: function () { return $text.val().slice(0, opts.maxLength); },
setValue: function (val) { $tag.val(val); }, setValue: function (val) {
$text.val(val);
updateChar();
},
edit: function (cb, tmp) { edit: function (cb, tmp) {
var v = Util.clone(opts); var v = Util.clone(opts);
return editTextOptions(v, setCursorGetter, cb, tmp); return editTextOptions(v, setCursorGetter, cb, tmp);
}, },
getCursor: function () { return cursorGetter(); }, getCursor: function () { return cursorGetter(); },
reset: function () { $tag.val(''); } reset: function () { $text.val(''); }
}; };
}, },
printResults: function (answers, uid) { printResults: function (answers, uid) {
@ -1108,6 +1272,7 @@ define([
printResults: function (answers, uid, form) { printResults: function (answers, uid, form) {
var structure = form[uid]; var structure = form[uid];
if (!structure) { return; } if (!structure) { return; }
var opts = structure.opts || TYPES.multiradio.defaultOpts;
var results = []; var results = [];
var empty = 0; var empty = 0;
var count = {}; var count = {};
@ -1125,7 +1290,6 @@ define([
}); });
}); });
Object.keys(count).forEach(function (q_uid) { Object.keys(count).forEach(function (q_uid) {
var opts = structure.opts || TYPES.multiradio.defaultOpts;
var q = findItem(opts.items, q_uid); var q = findItem(opts.items, q_uid);
var c = count[q_uid]; var c = count[q_uid];
var values = Object.keys(c).map(function (res) { var values = Object.keys(c).map(function (res) {
@ -1321,6 +1485,7 @@ define([
printResults: function (answers, uid, form) { printResults: function (answers, uid, form) {
var structure = form[uid]; var structure = form[uid];
if (!structure) { return; } if (!structure) { return; }
var opts = structure.opts || TYPES.multicheck.defaultOpts;
var results = []; var results = [];
var empty = 0; var empty = 0;
var count = {}; var count = {};
@ -1339,7 +1504,7 @@ define([
}); });
}); });
Object.keys(count).forEach(function (q_uid) { Object.keys(count).forEach(function (q_uid) {
var q = findItem(structure.opts.items, q_uid); var q = findItem(opts.items, q_uid);
var c = count[q_uid]; var c = count[q_uid];
var values = Object.keys(c).map(function (res) { var values = Object.keys(c).map(function (res) {
return h('div.cp-form-results-type-radio-data', [ return h('div.cp-form-results-type-radio-data', [
@ -1358,94 +1523,6 @@ define([
}, },
icon: h('i.cptools.cptools-form-grid-check') icon: h('i.cptools.cptools-form-grid-check')
}, },
poll: {
defaultOpts: {
type: 'text', // Text or Days or Time
values: [1, 2, 3].map(function (i) {
return Messages._getKey('form_defaultOption', [i]);
})
},
get: function (opts, answers, username, evOnChange) {
if (!opts) { opts = TYPES.poll.defaultOpts; }
if (!Array.isArray(opts.values)) { return; }
var lines = makePollTable(answers, opts);
// Add form
var addLine = opts.values.map(function (data) {
var cell = h('div.cp-poll-cell.cp-form-poll-choice', [
h('i.fa.fa-times.cp-no'),
h('i.fa.fa-check.cp-yes'),
h('i.fa.fa-question.cp-maybe'),
]);
var $c = $(cell);
$c.data('option', data);
var val = 0;
$c.attr('data-value', val);
$c.click(function () {
val = (val+1)%3;
$c.attr('data-value', val);
evOnChange.fire();
});
cell._setValue = function (v) {
val = v;
$c.attr('data-value', val);
};
return cell;
});
// Name input
var nameInput = h('input', { value: username || Messages.anonymous });
addLine.unshift(h('div.cp-poll-cell', nameInput));
lines.push(h('div', addLine));
var tag = h('div.cp-form-type-poll', lines);
var $tag = $(tag);
var cursorGetter;
var setCursorGetter = function (f) { cursorGetter = f; };
return {
tag: tag,
getValue: function () {
var res = {};
var name = $(nameInput).val().trim() || Messages.anonymous;
$tag.find('.cp-form-poll-choice').each(function (i, el) {
var $el = $(el);
res[$el.data('option')] = $el.attr('data-value');
});
return {
name: name,
values: res
};
},
reset: function () {
$tag.find('.cp-form-poll-choice').attr('data-value', 0);
},
edit: function (cb, tmp) {
var v = Util.clone(opts);
return editOptions(v, setCursorGetter, cb, tmp);
},
getCursor: function () { return cursorGetter(); },
setValue: function (res) {
this.reset();
if (!res || !res.values || !res.name) { return; }
var val = res.values;
$(nameInput).val(res.name);
$tag.find('.cp-form-poll-choice').each(function (i, el) {
if (!el._setValue) { return; }
var $el = $(el);
el._setValue(val[$el.data('option')] || 0);
});
}
};
},
printResults: function (answers, uid, form) {
var _answers = getBlockAnswers(answers, uid);
var lines = makePollTable(_answers, form[uid].opts);
return h('div.cp-form-type-poll', lines);
},
icon: h('i.cptools.cptools-form-poll')
},
sort: { sort: {
defaultOpts: { defaultOpts: {
values: [1,2].map(function (i) { values: [1,2].map(function (i) {
@ -1453,7 +1530,7 @@ define([
}) })
}, },
get: function (opts, a, n, evOnChange) { get: function (opts, a, n, evOnChange) {
if (!opts) { opts = TYPES.radio.defaultOpts; } if (!opts) { opts = TYPES.sort.defaultOpts; }
if (!Array.isArray(opts.values)) { return; } if (!Array.isArray(opts.values)) { return; }
var map = {}; var map = {};
var invMap = {}; var invMap = {};
@ -1472,7 +1549,10 @@ define([
$(div).data('val', data); $(div).data('val', data);
return div; return div;
}); });
var tag = h('div.cp-form-type-sort-container', els); var tag = h('div.cp-form-type-sort-container', [
h('div.cp-form-sort-hint', Messages._getKey('form_sort_hint', [els.length])),
els
]);
var $tag = $(tag); var $tag = $(tag);
var reorder = function () { var reorder = function () {
$tag.find('.cp-form-type-sort').each(function (i, el) { $tag.find('.cp-form-type-sort').each(function (i, el) {
@ -1527,7 +1607,7 @@ define([
}, },
printResults: function (answers, uid, form) { printResults: function (answers, uid, form) {
var opts = form[uid].opts || TYPES.radio.defaultOpts; var opts = form[uid].opts || TYPES.sort.defaultOpts;
var l = (opts.values || []).length; var l = (opts.values || []).length;
var results = []; var results = [];
var empty = 0; var empty = 0;
@ -1556,15 +1636,116 @@ define([
}, },
icon: h('i.cptools.cptools-form-list-ordered') icon: h('i.cptools.cptools-form-list-ordered')
}, },
poll: {
defaultOpts: {
type: 'text', // Text or Days or Time
values: [1, 2, 3].map(function (i) {
return Messages._getKey('form_defaultOption', [i]);
})
},
get: function (opts, answers, username, evOnChange) {
if (!opts) { opts = TYPES.poll.defaultOpts; }
if (!Array.isArray(opts.values)) { return; }
var lines = makePollTable(answers, opts);
// Add form
var addLine = opts.values.map(function (data) {
var cell = h('div.cp-poll-cell.cp-form-poll-choice', [
h('i.fa.fa-times.cp-no'),
h('i.fa.fa-check.cp-yes'),
h('i.fa.fa-question.cp-maybe'),
]);
var $c = $(cell);
$c.data('option', data);
var val = 0;
$c.attr('data-value', val);
$c.click(function () {
val = (val+1)%3;
$c.attr('data-value', val);
evOnChange.fire();
});
cell._setValue = function (v) {
val = v;
$c.attr('data-value', val);
};
return cell;
});
// Name input
//var nameInput = h('input', { value: username || Messages.anonymous });
var nameInput = h('span.cp-poll-your-answers', Messages.form_pollYourAnswers)
addLine.unshift(h('div.cp-poll-cell', nameInput));
lines.push(h('div', addLine));
var total = makePollTotal(answers, opts, addLine, evOnChange);
if (total) { lines.push(h('div', total)); }
var tag = h('div.cp-form-type-poll-container', h('div.cp-form-type-poll', lines));
var $tag = $(tag);
var cursorGetter;
var setCursorGetter = function (f) { cursorGetter = f; };
return {
tag: tag,
getValue: function () {
var res = {};
$tag.find('.cp-form-poll-choice').each(function (i, el) {
var $el = $(el);
res[$el.data('option')] = $el.attr('data-value');
});
return {
values: res
};
},
reset: function () {
$tag.find('.cp-form-poll-choice').attr('data-value', 0);
},
edit: function (cb, tmp) {
var v = Util.clone(opts);
return editOptions(v, setCursorGetter, cb, tmp);
},
getCursor: function () { return cursorGetter(); },
setValue: function (res) {
this.reset();
if (!res || !res.values) { return; }
var val = res.values;
$tag.find('.cp-form-poll-choice').each(function (i, el) {
if (!el._setValue) { return; }
var $el = $(el);
el._setValue(val[$el.data('option')] || 0);
});
}
};
},
printResults: function (answers, uid, form) {
var opts = form[uid].opts || TYPES.poll.defaultOpts;
var _answers = getBlockAnswers(answers, uid);
var lines = makePollTable(_answers, opts);
var total = makePollTotal(_answers, opts);
if (total) { lines.push(h('div', total)); }
return h('div.cp-form-type-poll', lines);
},
icon: h('i.cptools.cptools-form-poll')
},
}; };
var renderResults = function (content, answers) { var renderResults = function (content, answers) {
var $container = $('div.cp-form-creator-results').empty(); var $container = $('div.cp-form-creator-results').empty();
if (!Object.keys(answers || {}).length) {
$container.append(h('div.alert.alert-info', Messages.form_results_empty));
return;
}
var controls = h('div.cp-form-creator-results-controls'); var controls = h('div.cp-form-creator-results-controls');
var $controls = $(controls).appendTo($container); var $controls = $(controls).appendTo($container);
var results = h('div.cp-form-creator-results-content'); var results = h('div.cp-form-creator-results-content');
var $results = $(results).appendTo($container); var $results = $(results).appendTo($container);
var summary = true; var summary = true;
var form = content.form; var form = content.form;
@ -1715,7 +1896,7 @@ define([
}); });
return results; return results;
}; };
var makeFormControls = function (framework, content, update) { var makeFormControls = function (framework, content, update, evOnChange) {
var loggedIn = framework._.sfCommon.isLoggedIn(); var loggedIn = framework._.sfCommon.isLoggedIn();
var metadataMgr = framework._.cpNfInner.metadataMgr; var metadataMgr = framework._.cpNfInner.metadataMgr;
@ -1768,6 +1949,7 @@ define([
console.error(err || data.error); console.error(err || data.error);
return void UI.warn(Messages.error); return void UI.warn(Messages.error);
} }
window.onbeforeunload = undefined;
if (!update) { if (!update) {
// Add results button // Add results button
addResultsButton(framework, content); addResultsButton(framework, content);
@ -1783,7 +1965,53 @@ define([
reset = undefined; reset = undefined;
} }
var invalid = h('div.cp-form-invalid-warning');
var $invalid = $(invalid);
if (evOnChange) {
var origin, priv;
if (APP.common) {
var metadataMgr = APP.common.getMetadataMgr();
priv = metadataMgr.getPrivateData();
origin = priv.origin;
}
evOnChange.reg(function () {
var $container = $('div.cp-form-creator-content');
var $inputs = $container.find('input:invalid');
if (!$inputs.length) {
$send.text(update ? Messages.form_update : Messages.form_submit);
return void $invalid.empty();
}
$send.text(update ? Messages.form_updateWarning : Messages.form_submitWarning);
var lis = [];
$inputs.each(function (i, el) {
var $el = $(el).closest('.cp-form-block');
var number = $el.find('.cp-form-block-question-number').text();
var a = h('a', {
href: origin + '#' + Messages._getKey('form_invalidQuestion', [number])
}, Messages._getKey('form_invalidQuestion', [number]));
$(a).click(function (e) {
e.preventDefault();
if (!$el.is(':visible')) {
var pages = $el.closest('.cp-form-page').index();
if (APP.refreshPage) { APP.refreshPage(pages + 1); }
}
$el[0].scrollIntoView();
});
var li = h('li', a);
lis.push(li);
});
var list = h('ul', lis);
var content = [
h('span', Messages.form_invalidWarning),
list
];
$invalid.empty().append(content);
});
evOnChange.fire(true);
}
return h('div.cp-form-send-container', [ return h('div.cp-form-send-container', [
invalid,
cbox ? h('div', cbox) : undefined, cbox ? h('div', cbox) : undefined,
send, reset send, reset
]); ]);
@ -1801,7 +2029,8 @@ define([
var _answers = Util.clone(answers || {}); var _answers = Util.clone(answers || {});
delete _answers._proof; delete _answers._proof;
delete _answers._userdata; delete _answers._userdata;
evOnChange.reg(function () { evOnChange.reg(function (noBeforeUnload) {
if (noBeforeUnload) { return; }
var results = getFormResults(); var results = getFormResults();
if (!answers || Sortify(_answers) !== Sortify(results)) { if (!answers || Sortify(_answers) !== Sortify(results)) {
window.onbeforeunload = function () { window.onbeforeunload = function () {
@ -1884,6 +2113,7 @@ define([
var elements = []; var elements = [];
var n = 1; // Question number
content.order.forEach(function (uid) { content.order.forEach(function (uid) {
var block = form[uid]; var block = form[uid];
var type = block.type; var type = block.type;
@ -1913,7 +2143,10 @@ define([
var dragHandle; var dragHandle;
var q = h('div.cp-form-block-question', block.q || Messages.form_default); var q = h('div.cp-form-block-question', [
h('span.cp-form-block-question-number', (n++)+'.'),
h('span', block.q || Messages.form_default)
]);
var editButtons, editContainer; var editButtons, editContainer;
APP.formBlocks.push(data); APP.formBlocks.push(data);
@ -2013,7 +2246,7 @@ define([
framework._.cpNfInner.chainpad.onSettle(function () { framework._.cpNfInner.chainpad.onSettle(function () {
$(editButtons).show(); $(editButtons).show();
UI.log(Messages.saved); UI.log(Messages.saved);
var _answers = getBlockAnswers(APP.answers, uid); _answers = getBlockAnswers(APP.answers, uid);
data = model.get(newOpts, _answers, null, evOnChange); data = model.get(newOpts, _answers, null, evOnChange);
if (!data) { data = {}; } if (!data) { data = {}; }
$oldTag.before(data.tag).remove(); $oldTag.before(data.tag).remove();
@ -2085,19 +2318,19 @@ define([
var pageContainer = h('div.cp-form-page-container'); var pageContainer = h('div.cp-form-page-container');
var $page = $(pageContainer); var $page = $(pageContainer);
_content.push(pageContainer); _content.push(pageContainer);
var refreshPage = function (current) { var refreshPage = APP.refreshPage = function (current) {
$page.empty(); $page.empty();
if (!current || current < 1) { current = 1; } if (!current || current < 1) { current = 1; }
if (current > pages) { current = pages; } if (current > pages) { current = pages; }
var left = h('button.btn.btn-secondary.small.cp-prev', [ var left = h('button.btn.btn-secondary.cp-prev', [
h('i.fa.fa-chevron-left'), h('i.fa.fa-arrow-left'),
h('span', Messages.form_page_prev)
]); ]);
var state = h('span', Messages._getKey('form_page', [current, pages])); var state = h('span', Messages._getKey('form_page', [current, pages]));
var right = h('button.btn.btn-secondary.small.cp-next', [ var right = h('button.btn.btn-secondary.cp-next', [
h('span', Messages.form_page_next), h('i.fa.fa-arrow-right'),
h('i.fa.fa-chevron-right'),
]); ]);
if (current === pages) { $(right).css('visibility', 'hidden'); }
if (current === 1) { $(left).css('visibility', 'hidden'); }
$(left).click(function () { refreshPage(current - 1); }); $(left).click(function () { refreshPage(current - 1); });
$(right).click(function () { refreshPage(current + 1); }); $(right).click(function () { refreshPage(current + 1); });
$page.append([left, state, right]); $page.append([left, state, right]);
@ -2139,7 +2372,7 @@ define([
} }
// In view mode, add "Submit" and "reset" buttons // In view mode, add "Submit" and "reset" buttons
$container.append(makeFormControls(framework, content, Boolean(answers))); $container.append(makeFormControls(framework, content, Boolean(answers), evOnChange));
}; };
var getTempFields = function () { var getTempFields = function () {
@ -2172,6 +2405,9 @@ define([
var helpMenu = framework._.sfCommon.createHelpMenu(['text', 'pad']); var helpMenu = framework._.sfCommon.createHelpMenu(['text', 'pad']);
$toolbarContainer.after(helpMenu.menu); $toolbarContainer.after(helpMenu.menu);
if (!APP.isEditor) {
framework._.toolbar.alone();
}
var makeFormSettings = function () { var makeFormSettings = function () {
// Private / public status // Private / public status
@ -2180,7 +2416,7 @@ define([
var refreshPublic = function () { var refreshPublic = function () {
$results.empty(); $results.empty();
var makePublic = h('button.btn.btn-secondary', Messages.form_makePublic); var makePublic = h('button.btn.btn-secondary', Messages.form_makePublic);
var makePublicDiv = h('div', makePublic); var makePublicDiv = h('div.cp-form-actions', makePublic);
if (content.answers.privateKey) { makePublicDiv = undefined; } if (content.answers.privateKey) { makePublicDiv = undefined; }
var publicText = content.answers.privateKey ? Messages.form_isPublic : Messages.form_isPrivate; var publicText = content.answers.privateKey ? Messages.form_isPublic : Messages.form_isPrivate;
$results.append(h('span.cp-form-results-type', publicText)); $results.append(h('span.cp-form-results-type', publicText));

@ -230,7 +230,7 @@ define([
results[senderCurve] = { results[senderCurve] = {
msg: parsed, msg: parsed,
hash: hash, hash: hash,
time: cfg.time time: cfg && cfg.time
}; };
}; };
CPNetflux.start(config); CPNetflux.start(config);

Loading…
Cancel
Save