Fix forms issues with sections

pull/1/head
yflory 3 years ago
parent b964af4da2
commit 8f3a1bf93e

@ -2759,6 +2759,17 @@ define([
var getFormCreator = function (uid, inSection) { var getFormCreator = function (uid, inSection) {
if (!APP.isEditor) { return; } if (!APP.isEditor) { return; }
var full = !uid; var full = !uid;
var addControl = function (type) {
var btn = h('button.btn.btn-secondary', {
title: full ? '' : Messages['form_type_'+type],
'data-type': type
}, [
(TYPES[type] || STATIC_TYPES[type]).icon.cloneNode(),
full ? h('span', Messages['form_type_'+type]) : undefined
]);
$(btn).click(function () {
// Get the array in which we want to create the new block
// and the position in this array
var arr = content.order; var arr = content.order;
var idx = content.order.indexOf(uid); var idx = content.order.indexOf(uid);
if (!full) { if (!full) {
@ -2772,29 +2783,21 @@ define([
idx = obj.idx; idx = obj.idx;
} }
} }
var addControl = function (type) {
var btn = h('button.btn.btn-secondary', { var _uid = Util.uid();
title: full ? '' : Messages['form_type_'+type],
'data-type': type
}, [
(TYPES[type] || STATIC_TYPES[type]).icon.cloneNode(),
full ? h('span', Messages['form_type_'+type]) : undefined
]);
$(btn).click(function () {
var uid = Util.uid();
// Make sure we can't create a section inside another one // Make sure we can't create a section inside another one
if (type === 'section' && arr !== content.order) { return; } if (type === 'section' && arr !== content.order) { return; }
content.form[uid] = { content.form[_uid] = {
//q: Messages.form_default, //q: Messages.form_default,
//opts: opts //opts: opts
type: type, type: type,
}; };
if (full || inSection) { if (full || inSection) {
arr.push(uid); arr.push(_uid);
} else { } else {
arr.splice(idx, 0, uid); arr.splice(idx, 0, _uid);
} }
framework.localChange(); framework.localChange();
updateForm(framework, content, true); updateForm(framework, content, true);
@ -2843,7 +2846,7 @@ define([
var uid = $el.attr('data-id'); var uid = $el.attr('data-id');
$el.before(getFormCreator(uid)); $el.before(getFormCreator(uid));
}); });
// Add to section // Add to the end of a section
$container.find('.cp-form-section-sortable').each(function (i, el) { $container.find('.cp-form-section-sortable').each(function (i, el) {
var $el = $(el); var $el = $(el);
var uid = $el.closest('.cp-form-block').attr('data-id'); var uid = $el.closest('.cp-form-block').attr('data-id');
@ -2945,7 +2948,7 @@ define([
}); });
} }
if (APP.isEditor && blockIdx && type === "section") { if (APP.isEditor && type === "section") {
var getConditionsValues = function () { var getConditionsValues = function () {
order = getFullOrder(content); order = getFullOrder(content);
var blockIdx = order.indexOf(uid); var blockIdx = order.indexOf(uid);
@ -3509,22 +3512,6 @@ define([
}); });
updateAddInline(); updateAddInline();
// In view mode, hide sections when conditions aren't met
evOnChange.reg(function (reset, save, condition) {
if (!reset && !condition) { return; }
getSections(content).forEach(function (uid) {
var block = content.form[uid];
if (block.type !== 'section') { return; }
if (!block.opts || !Array.isArray(block.opts.questions) || !block.opts.when) {
return;
}
var show = checkCondition(block);
block.opts.questions.forEach(function (_uid) {
$container.find('.cp-form-block[data-id="'+_uid+'"]').toggle(show);
});
});
});
if (editable) { if (editable) {
if (APP.mainSortable) { APP.mainSortable.destroy(); } if (APP.mainSortable) { APP.mainSortable.destroy(); }
APP.mainSortable = Sortable.create($container[0], { APP.mainSortable = Sortable.create($container[0], {
@ -3549,6 +3536,22 @@ define([
return; return;
} }
// In view mode, hide sections when conditions aren't met
evOnChange.reg(function (reset, save, condition) {
if (!reset && !condition) { return; }
getSections(content).forEach(function (uid) {
var block = content.form[uid];
if (block.type !== 'section') { return; }
if (!block.opts || !Array.isArray(block.opts.questions) || !block.opts.when) {
return;
}
var show = checkCondition(block);
block.opts.questions.forEach(function (_uid) {
$container.find('.cp-form-block[data-id="'+_uid+'"]').toggle(show);
});
});
});
// If the form is already submitted, show an info message // If the form is already submitted, show an info message
if (APP.hasAnswered) { if (APP.hasAnswered) {
showAnsweredPage(framework, content, answers); showAnsweredPage(framework, content, answers);
@ -3568,7 +3571,6 @@ define([
$container.append(makeFormControls(framework, content, Boolean(answers), evOnChange)); $container.append(makeFormControls(framework, content, Boolean(answers), evOnChange));
// In view mode, tell the user if answers are forced to be anonymous or authenticated // In view mode, tell the user if answers are forced to be anonymous or authenticated
if (!APP.isEditor) {
var infoTxt; var infoTxt;
var loggedIn = framework._.sfCommon.isLoggedIn(); var loggedIn = framework._.sfCommon.isLoggedIn();
if (content.answers.makeAnonymous) { if (content.answers.makeAnonymous) {
@ -3586,7 +3588,6 @@ define([
b.setEditable(false); b.setEditable(false);
}); });
} }
}
// Embed mode is enforced so we add the title at the top and a CryptPad logo // Embed mode is enforced so we add the title at the top and a CryptPad logo
// at the bottom // at the bottom

Loading…
Cancel
Save