From f46a820ad83684894dfc32a89531d6d29552a227 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 9 Jul 2021 11:41:08 +0200 Subject: [PATCH 1/4] Don't sort 'sorted lists' in forms by default --- www/form/inner.js | 5 ++++- www/form/main.js | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/www/form/inner.js b/www/form/inner.js index 2a1501ecb..f9427024f 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -1470,6 +1470,7 @@ define([ if (!Array.isArray(opts.values)) { return; } var map = {}; var invMap = {}; + var sorted = false; var els = opts.values.map(function (data, i) { var uid = Util.uid(); map[uid] = data; @@ -1479,7 +1480,7 @@ define([ h('i.fa.fa-ellipsis-v'), h('i.fa.fa-ellipsis-v'), ]), - h('span.cp-form-sort-order', (i+1)), + h('span.cp-form-sort-order', '?'), h('span', data) ]); $(div).data('val', data); @@ -1494,6 +1495,7 @@ define([ $tag.find('.cp-form-type-sort').each(function (i, el) { $(el).find('.cp-form-sort-order').text(i+1); }); + sorted = true; }; var cursorGetter; var setCursorGetter = function (f) { cursorGetter = f; }; @@ -1516,6 +1518,7 @@ define([ return { tag: tag, getValue: function () { + if (!sorted) { return; } return sortable.toArray().map(function (id) { return map[id]; }); diff --git a/www/form/main.js b/www/form/main.js index 9ccac3d00..112b548db 100644 --- a/www/form/main.js +++ b/www/form/main.js @@ -265,6 +265,7 @@ define([ }, function (obj) { if (obj && obj.error) { return void cb(obj); } var messages = obj.messages; + if (!messages.length) { return void cb(); } var res = Utils.Crypto.Mailbox.openOwnSecretLetter(messages[0].msg, { validateKey: data.validateKey, ephemeral_private: Nacl.util.decodeBase64(answer.curvePrivate), From ed28b5ec7b76b0bd509c7c8ef3245f0594e8ee56 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 9 Jul 2021 11:45:48 +0200 Subject: [PATCH 2/4] Randomize 'ordered list' initial state in forms --- www/common/common-util.js | 9 +++++++++ www/form/inner.js | 1 + 2 files changed, 10 insertions(+) diff --git a/www/common/common-util.js b/www/common/common-util.js index 51540a660..1fd78cec1 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -9,6 +9,15 @@ return Array.prototype.slice.call(A, start, end); }; + Util.shuffleArray = function (a) { + for (var i = a.length - 1; i > 0; i--) { + var j = Math.floor(Math.random() * (i + 1)); + var tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } + }; + Util.bake = function (f, args) { if (typeof(args) === 'undefined') { args = []; } if (!Array.isArray(args)) { args = [args]; } diff --git a/www/form/inner.js b/www/form/inner.js index f9427024f..ce6379908 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -1471,6 +1471,7 @@ define([ var map = {}; var invMap = {}; var sorted = false; + Util.shuffleArray(opts.values); var els = opts.values.map(function (data, i) { var uid = Util.uid(); map[uid] = data; From 1b081c52df90a3feee824f3fe6ace9e29dcd99a2 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 9 Jul 2021 11:46:29 +0200 Subject: [PATCH 3/4] lint compliance --- www/form/inner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/form/inner.js b/www/form/inner.js index ce6379908..a1c7f4c93 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -1472,7 +1472,7 @@ define([ var invMap = {}; var sorted = false; Util.shuffleArray(opts.values); - var els = opts.values.map(function (data, i) { + var els = opts.values.map(function (data) { var uid = Util.uid(); map[uid] = data; invMap[data] = uid; From 1b0a0fe14fd13ec5815f2bae852bf9544ff406af Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 9 Jul 2021 11:56:53 +0200 Subject: [PATCH 4/4] Improve reset button in forms --- www/form/inner.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/www/form/inner.js b/www/form/inner.js index a1c7f4c93..c49910411 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -1492,11 +1492,11 @@ define([ els ]); var $tag = $(tag); - var reorder = function () { + var reorder = function (reset) { $tag.find('.cp-form-type-sort').each(function (i, el) { - $(el).find('.cp-form-sort-order').text(i+1); + $(el).find('.cp-form-sort-order').text(reset ? '?' : i+1); }); - sorted = true; + sorted = !reset; }; var cursorGetter; var setCursorGetter = function (f) { cursorGetter = f; }; @@ -1525,11 +1525,12 @@ define([ }); }, reset: function () { + Util.shuffleArray(opts.values); var toSort = (opts.values).map(function (val) { return invMap[val]; }); sortable.sort(toSort); - reorder(); + reorder(true); }, edit: function (cb, tmp) { var v = Util.clone(opts); @@ -1889,12 +1890,13 @@ define([ } var send = h('button.cp-open.btn.btn-primary', update ? Messages.form_update : Messages.form_submit); - var reset = h('button.cp-open.btn.btn-danger-alt', Messages.form_reset); + var reset = h('button.cp-open.cp-reset-button.btn.btn-danger-alt', Messages.form_reset); $(reset).click(function () { if (!Array.isArray(APP.formBlocks)) { return; } APP.formBlocks.forEach(function (data) { if (typeof(data.reset) === "function") { data.reset(); } }); + $(reset).attr('disabled', 'disabled'); }); var $send = $(send).click(function () { $send.attr('disabled', 'disabled'); @@ -2008,6 +2010,7 @@ define([ delete _answers._userdata; evOnChange.reg(function (noBeforeUnload, isSave) { if (noBeforeUnload) { return; } + $container.find('.cp-reset-button').removeAttr('disabled'); var results = getFormResults(); if (isSave) { answers = Util.clone(results || {}); @@ -2360,6 +2363,9 @@ define([ // In view mode, add "Submit" and "reset" buttons $container.append(makeFormControls(framework, content, Boolean(answers), evOnChange)); + if (!answers) { + $container.find('.cp-reset-button').attr('disabled', 'disabled'); + } }; var getTempFields = function () {