Added support for JSON

This commit is contained in:
Andris Reinman 2016-08-29 15:51:20 +03:00
parent 35078defb6
commit 69ff628cda
7 changed files with 55 additions and 5 deletions

View file

@ -24,6 +24,7 @@ module.exports.types = {
'date-eur': 'Date (DD/MM/YYYY)',
'birthday-us': 'Birthday (MM/DD)',
'birthday-eur': 'Birthday (DD/MM)',
json: 'JSON value for custom rendering',
option: 'Option'
};
@ -34,6 +35,7 @@ module.exports.genericTypes = {
website: 'string',
longtext: 'textarea',
gpg: 'textarea',
json: 'textarea',
number: 'number',
'date-us': 'date',
'date-eur': 'date',
@ -106,7 +108,7 @@ module.exports.get = (id, callback) => {
return callback(err);
}
let field = rows && rows[0] && tools.convertKeys(rows[0]) || false;
field.isGroup = module.exports.grouped.includes(field.type);
field.isGroup = module.exports.grouped.includes(field.type) || field.type === 'json';
return callback(null, field);
});
});
@ -329,6 +331,7 @@ function addCustomField(listId, name, defaultValue, type, group, groupTemplate,
break;
case 'gpg':
case 'longtext':
case 'json':
query = 'ALTER TABLE `subscription__' + listId + '` ADD COLUMN `' + column + '` TEXT DEFAULT NULL';
break;
case 'number':
@ -412,6 +415,35 @@ module.exports.getRow = (fieldList, values, useDate, showAll, onlyExisting) => {
row.push(item);
break;
}
case 'json':
{
let value;
let json = (valueList[field.column] || '').toString().trim();
try {
let parsed = JSON.parse(json);
if (Array.isArray(parsed)) {
parsed = {
values: parsed
};
}
value = json ? render(field.groupTemplate, parsed) : '';
} catch (E) {
value = E.message;
}
let item = {
type: field.type,
name: field.name,
column: field.column,
value: (valueList[field.column] || '').toString().trim(),
visible: !!field.visible,
mergeTag: field.key,
mergeValue: value || field.defaultValue,
['type' + (field.type || '').toString().trim().replace(/(?:^|\-)([a-z])/g, (m, c) => c.toUpperCase())]: true
};
row.push(item);
break;
}
case 'number':
{
let item = {

View file

@ -28,6 +28,7 @@
<option value="website" {{#if selectedWebsite}} selected {{/if}}>Website</option>
<option value="gpg" {{#if selectedGpg}} selected {{/if}}>GPG Public Key</option>
<option value="longtext" {{#if selectedLongtext}} selected {{/if}}>Multi-line text</option>
<option value="json" {{#if selectedJson}} selected {{/if}}>JSON</option>
<optgroup label="Date">
<option value="date-us" {{#if selectedDateUs}} selected {{/if}}>Date (MM/DD/YYYY)</option>
<option value="date-eur" {{#if selectedDateEur}} selected {{/if}}>Date (DD/MM/YYYY)</option>
@ -67,10 +68,10 @@
</div>
<div class="form-group">
<label for="group-template" class="col-sm-2 control-label">Group template</label>
<label for="group-template" class="col-sm-2 control-label">Template</label>
<div class="col-sm-10">
<textarea class="form-control gpg-text" rows="3" name="group-template" id="group-template">{{field.groupTemplate}}</textarea>
<span class="help-block">For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from <code>\{{values}}</code> array, for example <code>\{{#each values}} \{{this}} \{{/each}}</code>. If template is not defined then multiple values are joined with commas.</span>
<span class="help-block">For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from <code>\{{values}}</code> array, for example <code>\{{#each values}} \{{this}} \{{/each}}</code>. If template is not defined then multiple values are joined with commas. You can also use this template to render JSON values (if the JSON is an array then the array is exposed as <code>values</code>, otherwise you can access the JSON keys directly).</span>
</div>
</div>

View file

@ -35,6 +35,7 @@
<option value="website" {{#if selectedWebsite}} selected {{/if}}>Website</option>
<option value="gpg" {{#if selectedGpg}} selected {{/if}}>GPG Public Key</option>
<option value="longtext" {{#if selectedLongtext}} selected {{/if}}>Multi-line text</option>
<option value="json" {{#if selectedJson}} selected {{/if}}>JSON</option>
<optgroup label="Date">
<option value="date-us" {{#if selectedDateUs}} selected {{/if}}>Date (MM/DD/YYYY)</option>
<option value="date-eur" {{#if selectedDateEur}} selected {{/if}}>Date (DD/MM/YYYY)</option>
@ -81,10 +82,10 @@
{{#if field.isGroup}}
<div class="form-group">
<label for="group-template" class="col-sm-2 control-label">Group template</label>
<label for="group-template" class="col-sm-2 control-label">Template</label>
<div class="col-sm-10">
<textarea class="form-control gpg-text" rows="3" name="group-template" id="description">{{field.groupTemplate}}</textarea>
<span class="help-block">For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from <code>\{{values}}</code> array, for example <code>\{{#each values}} \{{this}} \{{/each}}</code>. If template is not defined then multiple values are joined with commas.</span>
<span class="help-block">For group elements like checkboxes you can control the appearance of the merge tag with an optional template. The template uses handlebars syntax and you can find all values from <code>\{{values}}</code> array, for example <code>\{{#each values}} \{{this}} \{{/each}}</code>. If template is not defined then multiple values are joined with commas. You can also use this template to render JSON values (if the JSON is an array then the array is exposed as <code>values</code>, otherwise you can access the JSON keys directly).</span>
</div>
</div>

View file

@ -54,6 +54,10 @@
<textarea class="form-control" rows="3" name="{{column}}">{{value}}</textarea>
{{/if}}
{{#if typeJson}}
<textarea class="form-control gpg-text" rows="3" name="{{column}}" placeholder="{&quot;data&quot;:&quot;value&quot;}">{{value}}</textarea>
{{/if}}
{{#if typeGpg}}
<textarea class="form-control gpg-text" rows="3" name="{{column}}" placeholder="Begins with &#39;-----BEGIN PGP PUBLIC KEY BLOCK-----&#39;">{{value}}</textarea>
<span class="help-block">Insert a GPG public key that will be used to encrypt messages sent this subscriber</span>

View file

@ -67,6 +67,10 @@
<textarea class="form-control" rows="3" name="{{column}}">{{value}}</textarea>
{{/if}}
{{#if typeJson}}
<textarea class="form-control gpg-text" rows="3" name="{{column}}" placeholder="{&quot;data&quot;:&quot;value&quot;}">{{value}}</textarea>
{{/if}}
{{#if typeGpg}}
<textarea class="form-control gpg-text" rows="3" name="{{column}}" placeholder="Begins with &#39;-----BEGIN PGP PUBLIC KEY BLOCK-----&#39;">{{value}}</textarea>
<span class="help-block">Insert a GPG public key that will be used to encrypt messages sent this subscriber</span>

View file

@ -48,6 +48,10 @@
<textarea class="form-control" rows="3" name="{{column}}">{{value}}</textarea>
{{/if}}
{{#if typeJson}}
<textarea class="form-control gpg-text" rows="3" name="{{column}}" placeholder="{&quot;data&quot;:&quot;value&quot;}">{{value}}</textarea>
{{/if}}
{{#if typeGpg}}
{{#if ../hasPubkey}}
<div class="pull-right">

View file

@ -45,6 +45,10 @@
<textarea class="form-control" rows="3" name="{{column}}">{{value}}</textarea>
{{/if}}
{{#if typeJson}}
<textarea class="form-control gpg-text" rows="3" name="{{column}}" placeholder="{&quot;data&quot;:&quot;value&quot;}">{{value}}</textarea>
{{/if}}
{{#if typeGpg}}
{{#if ../hasPubkey}}
<div class="pull-right">