Skip to content

Commit

Permalink
Merge branch 'master' into monthonly-datepicker-hide-year-title
Browse files Browse the repository at this point in the history
  • Loading branch information
svenstm authored Dec 6, 2018
2 parents 8da7a05 + b05cee4 commit 722e2ce
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 58 deletions.
13 changes: 12 additions & 1 deletion js/legalform-calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function LegalFormCalc($) {

if (type === 'group' && field.multiple) {
value = typeof(value) !== 'undefined' ? [value] : [];
} else if (type === 'money' && value) {
value = parseFloat(value);
}

addGroupedData(data, step.group, field.name, value);
Expand Down Expand Up @@ -88,7 +90,7 @@ function LegalFormCalc($) {
data[name + '-validation'] = expandCondition(field.validation, step.group || '', true);
}

if (type === 'expression') {
if (type === 'expression' && !step.repeater) {
setComputedForExpression(name, step, field, data);
} else if (type === 'external_data' || field.external_source) {
setComputedForExternalUrls(name, step, field, data);
Expand Down Expand Up @@ -155,6 +157,15 @@ function LegalFormCalc($) {
meta.yearly = !!(typeof field.yearly !== 'undefined' && field.yearly);
}

if (type === 'expression' && step.repeater) {
var expression = {};
var name = (step.group ? step.group + '.' : '') + field.name;
var key = name + '-expression';

setComputedForExpression(name, step, field, expression);
meta.expressionTmpl = expression[key];
}

addGroupedData(data, step.group, field.name, meta);
});

Expand Down
18 changes: 10 additions & 8 deletions js/legalform-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ function LegalFormHtml($) {
};

this.model = null;
this.isTestEnv = false;
this.disableRequiredFields = false;

/**
* Build form html
* @param {array} definition Form definition
* @param {boolean} isTestEnv If we're building form for testing purposes
* @return {string} Form html
* @param {array} definition Form definition
* @param {object} builderOptions Additional options for buildong form html
* @return {string} Form html
*/
this.build = function(definition, isTestEnv) {
self.isTestEnv = !!isTestEnv;
this.build = function(definition, builderOptions) {
if (typeof builderOptions === 'undefined') builderOptions = {};

self.disableRequiredFields = !!builderOptions.disableRequiredFields;
self.model = (new FormModel(definition)).getModel();

var lines = [];
Expand Down Expand Up @@ -165,7 +167,7 @@ function LegalFormHtml($) {
var type = self.model.getFieldType(data);
var excl = mode === 'build' ?
'data-mask;' :
(self.isTestEnv ? 'required;' : '');
(self.disableRequiredFields ? 'required;' : '');

switch (type) {
case 'number':
Expand Down Expand Up @@ -335,7 +337,7 @@ function LegalFormHtml($) {
var more = value === null ? {checked: data.value} : {name: data.value, value: value};
attrs = $.extend(attrs, more);

if (self.isTestEnv) {
if (self.disableRequiredFields) {
excl += ';required;';
}
} else {
Expand Down
32 changes: 18 additions & 14 deletions js/legalform-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
* Validation for LegalForm
*/
(function($) {
function LegalFormValidation() {
function LegalFormValidation(builderOptions) {
if (typeof builderOptions === 'undefined') builderOptions = {};

this.ractive = null;
this.el = null;
this.elWizard = null;
this.disableRequiredFields = !!builderOptions.disableRequiredFields;

//Fields for custom validation
var textFields = 'input[type="text"], input[type="number"], input[type="email"], textarea';
Expand Down Expand Up @@ -228,25 +231,26 @@
}

// Implement validation for group checkboxes
if (meta.type === 'group') {
if (meta.type === 'group' && $input.attr('multiple')) {
const checkBoxId = $input.attr('data-id');
const allCheckboxes = $("[data-id='" + checkBoxId + "']");
const $allCheckboxes = $('[data-id="' + checkBoxId + '"]');
const isRequired = !$input.closest('.form-group').find('label > span').length ? false :
$input.closest('.form-group').find('label > span')[0].className === 'required' ? true : false;

let checked = 0;

for (var i = 0; i < allCheckboxes.length; i++) {
if (allCheckboxes[i].checked) {
checked++;
} else if (allCheckboxes[i].type !== 'radio') {
$(allCheckboxes[i]).prop('required', false);
if (isRequired && this.disableRequiredFields) {
$allCheckboxes.prop('required', false);
} else {
let checked = 0;
for (var i = 0; i < $allCheckboxes.length; i++) {
if ($allCheckboxes[i].checked) checked++;
}
}

if (isRequired && checked === 0) {
$input.get(0).setCustomValidity(error);
return;
if (isRequired) $allCheckboxes.prop('required', !checked);

if (isRequired && checked === 0) {
$input.get(0).setCustomValidity(error);
return;
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions js/legalform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ function LegalForm($) {

/**
* Build form html
* @param {array} definition Form definition
* @param {boolean} isTestEnv If we're building form for testing purposes
* @return {string} Form html
* @param {array} definition Form definition
* @param {object} builderOptions Additional options for buildong form html
* @return {string} Form html
*/
this.build = function(definition, isTestEnv) {
this.build = function(definition, builderOptions) {
var handler = new LegalFormHtml($);
return handler.build(definition, isTestEnv);
return handler.build(definition, builderOptions);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions js/lib/parse-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = parseNumber;
}

var numberRegexp = new RegExp('^(?:((?:\\d{1,3}(?:\\.\\d{3})+|\\d+)(?:,\\d{1,})?)|((?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d{1,})?))$');
var numberRegexp = new RegExp('^(?:((?:\\d{1,3}(?:\\.\\d{3})+|\\d+)(,\\d{1,})?)|((?:\\d{1,3}(?:,\\d{3})+|\\d+)(\\.\\d{1,})?))$');
var dotRegexp = /\./g;
var commaRegexp = /,/g;

Expand All @@ -23,7 +23,9 @@ function parseNumber(number) {
var match = number.match(numberRegexp);
if (!match) return null;

var isDecimalComma = typeof match[1] !== 'undefined';
var isDecimalComma =
typeof match[2] !== 'undefined' ||
(typeof match[3] !== 'undefined' && typeof match[4] === 'undefined');

number = isDecimalComma ?
number.replace(dotRegexp, '').replace(',', '.') :
Expand Down
71 changes: 71 additions & 0 deletions js/lib/ractive-dynamic-computed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Dynamic addition and removing of computed properties in current used version of Ractive (0.9.13) is not supported out of the box.
* So we use this object for that purpose. It uses code, extracted from ractive, and simplified to only cover our needs
* (that is only support computed properties, given as strings).
*/

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = RactiveDynamicComputed;
}

function RactiveDynamicComputed() {
var self = this;

this.dotRegExp = /\./g;
this.computedVarRegExp = /\$\{([^\}]+)\}/g;

/**
* Remove computed property from existing rative instance
* @param {object} ractive
* @param {string} key
*/
this.remove = function(ractive, key) {
var escapedKey = key.replace(dotRegExp, '\\.');

delete ractive.computed[key];
delete ractive.viewmodel.computations[escapedKey];
}

/**
* Add computed expression to existing ractive instance
* @param {object} ractive
* @param {string} key
* @param {string} value
*/
this.add = function(ractive, key, value) {
var signature = getComputationSignature(ractive, key, value);

ractive.computed[key] = value;
ractive.viewmodel.compute(key, signature);
}

function getComputationSignature(ractive, key, signature) {
if (typeof signature !== 'string') {
throw 'Unable to dynamically add computed property with value of type ' + (typeof signature);
}

var getter = createFunctionFromString(signature, ractive);
var getterString = signature;

return {
getter: getter,
setter: undefined,
getterString: getterString,
setterString: undefined,
getterUseStack: undefined
};
}

function createFunctionFromString(str, bindTo) {
var hasThis;

var functionBody = 'return (' + str.replace(self.computedVarRegExp, function (match, keypath) {
hasThis = true;
return ("__ractive.get(\"" + keypath + "\")");
}) + ');';

if (hasThis) { functionBody = "var __ractive = this; " + functionBody; }
var fn = new Function( functionBody );
return hasThis ? fn.bind( bindTo ) : fn;
}
}
22 changes: 22 additions & 0 deletions js/lib/repeated-step-expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = tmplToExpression;
}

var dotRegExp = /\./g;

/**
* Insert repeated step index into expression
* @param {object} step
* @param {string} expression
* @return {string}
*/
function tmplToExpression(expressionTmpl, group, idx) {
var prefix = group + '.';
prefix = prefix.replace(dotRegExp, '\\.');

var prefixRegExp = new RegExp('\\$\\{' + prefix, 'g');
var replacement = '${' + group + '[' + idx + '].';

return expressionTmpl.replace(prefixRegExp, replacement);
}
Loading

0 comments on commit 722e2ce

Please sign in to comment.