Added the possibility to use "option" field type outside a group. This is convenient to create just a single checkbox.

This commit is contained in:
Tomas Bures 2018-12-28 20:54:00 +01:00
parent 64af46b685
commit 2e847460f4
10 changed files with 117 additions and 26 deletions

View file

@ -17,7 +17,7 @@ async function ajaxListTx(tx, params, queryFun, columns, options) {
columnsNames.push(col.name);
if (col.raw) {
columnsSelect.push(tx.raw(col.raw));
columnsSelect.push(tx.raw(col.raw, col.data || []));
} else if (col.query) {
columnsSelect.push(function () { return col.query(this); });
}

View file

@ -215,7 +215,19 @@ fieldTypes.option = {
grouped: false,
enumerated: false,
cardinality: Cardinality.SINGLE,
parsePostValue: (field, value) => !(['false', 'no', '0', ''].indexOf((value || '').toString().trim().toLowerCase()) >= 0)
parsePostValue: (field, value) => {
if (Array.isArray(value)) {
// HTML checkbox does not provide any value when not checked. To detect the presence of the checkbox, we add a hidden field with the same name (see subscription-custom-fields.hbs:130).
// When the checkbox is selected, two values are returned and "value" is an array instead of a string. We assume the hidden field always comes after the actual value and thus take
// the first value in the array
value = value[0]
}
return !(['false', 'no', '0', ''].indexOf((value || '').toString().trim().toLowerCase()) >= 0)
},
getHbsType: field => 'typeOption',
forHbs: (field, value) => value ? 1 : 0,
render: (field, value) => value ? field.settings.checkedLabel : field.settings.uncheckedLabel
};
fieldTypes['date'] = {
@ -358,15 +370,15 @@ async function listDTAjax(context, listId, params) {
// All this is to show options always below their group parent
.innerJoin('custom_fields AS parent_fields', function() {
this.on(function() {
this.on('custom_fields.type', '=', knex.raw('?', ['option']))
this.onNotNull('custom_fields.group')
.on('custom_fields.group', '=', 'parent_fields.id');
}).orOn(function() {
this.on('custom_fields.type', '<>', knex.raw('?', ['option']))
this.orOnNull('custom_fields.group')
.on('custom_fields.id', '=', 'parent_fields.id');
});
})
.where('custom_fields.list', listId),
[ 'custom_fields.id', 'custom_fields.name', 'custom_fields.type', 'custom_fields.key', 'custom_fields.order_list' ],
[ 'custom_fields.id', 'custom_fields.name', 'custom_fields.type', 'custom_fields.key', 'custom_fields.order_list', 'custom_fields.group' ],
{
orderByBuilder: (builder, orderColumn, orderDir) => {
// We use here parent_fields to keep options always below their parent group
@ -374,13 +386,13 @@ async function listDTAjax(context, listId, params) {
builder
.orderBy(knex.raw('-parent_fields.order_list'), orderDir === 'asc' ? 'desc' : 'asc') // This is MySQL speciality. It sorts the rows in ascending order with NULL values coming last
.orderBy('parent_fields.name', orderDir)
.orderBy(knex.raw('custom_fields.type = "option"'), 'asc')
.orderBy(knex.raw('custom_fields.group is not null'), 'asc')
} else {
const parentColumn = orderColumn.replace(/^custom_fields/, 'parent_fields');
builder
.orderBy(parentColumn, orderDir)
.orderBy('parent_fields.name', orderDir)
.orderBy(knex.raw('custom_fields.type = "option"'), 'asc');
.orderBy(knex.raw('custom_fields.group is not null'), 'asc');
}
}
}
@ -444,8 +456,7 @@ async function serverValidate(context, listId, data) {
async function _validateAndPreprocess(tx, listId, entity, isCreate) {
enforce(entity.type === 'option' || !entity.group, 'Only option may have a group assigned');
enforce(entity.type !== 'option' || entity.group, 'Option must have a group assigned.');
enforce(entity.type !== 'option' || (entity.orderListBefore === 'none' && entity.orderSubscribeBefore === 'none' && entity.orderManageBefore === 'none'), 'Option cannot be made visible');
enforce(!entity.group || (entity.orderListBefore === 'none' && entity.orderSubscribeBefore === 'none' && entity.orderManageBefore === 'none'), 'Field with a group assigned cannot be made visible');
enforce(!entity.group || await tx('custom_fields').where({list: listId, id: entity.group}).first(), 'Group field does not exist');
enforce(entity.name, 'Name must be present');
@ -638,6 +649,7 @@ function forHbsWithFieldsGrouped(fieldsGrouped, subscription) { // assumes group
const entry = {
name: fld.name,
key: fld.key,
field: fld,
[type.getHbsType(fld)]: true,
order_subscribe: fld.order_subscribe,
order_manage: fld.order_manage,
@ -726,6 +738,7 @@ function _fromText(listId, data, flds, isGrouped, keyName, singleCardUsesKeyName
if (isGrouped) {
for (const fld of flds) {
const fldKey = fld[keyName];
if (fldKey && fldKey in data) {
const type = fieldTypes[fld.type];
const fldCol = getFieldColumn(fld);

View file

@ -76,6 +76,11 @@ fieldTypes.birthday = {
listRender: (groupedField, value) => formatBirthday(groupedField.settings.dateFormat, value)
};
fieldTypes.option = {
afterJSON: (groupedField, entity) => {},
listRender: (groupedField, value) => value ? groupedField.settings.checkedLabel : groupedField.settings.uncheckedLabel
};
function getSubscriptionTableName(listId) {
@ -97,7 +102,6 @@ function groupSubscription(groupedFieldsMap, entity) {
const fieldType = fields.getFieldType(fld.type);
if (fieldType.grouped) {
let value = null;
if (fieldType.cardinality === fields.Cardinality.SINGLE) {
@ -273,7 +277,8 @@ async function listDTAjax(context, listId, segmentId, params) {
} else {
columns.push({
name: listTable + '.' + fldCol,
raw: 0
raw: '?',
data: [0]
})
}

View file

@ -323,7 +323,7 @@ async function migrateCustomForms(knex) {
async function migrateCustomFields(knex) {
// -----------------------------------------------------------------------------------------------------
// Move form field order to custom fileds and make all fields configurable
// Move form field order to custom fields and make all fields configurable
// -----------------------------------------------------------------------------------------------------
await knex.schema.table('custom_fields', table => {
table.integer('order_subscribe');

View file

@ -49,6 +49,16 @@
</div>
{{/if}}
{{#if typeOption}}
<div class="form-group checkbox">
<label>{{name}}</label>
<label class="label-checkbox">
<input type="checkbox" name="{{key}}" value="1" {{#if value}} checked {{/if}}> {{field.settings.checkedLabel}}
</label>
<input type="hidden" value="0" name="{{key}}">
</div>
{{/if}}
{{#if typeGpg}}
<div class="form-group gpg {{key}}">
<label for="{{key}}">{{name}}</label>
@ -117,6 +127,7 @@
{{#if typeCheckboxGrouped}}
<div class="form-group checkbox">
<input type="hidden" value="<present>" name="{{key}}">
<label>{{name}}</label>
{{#each options}}
<label class="label-checkbox">