Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mol 30753 #152

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 113 additions & 3 deletions dist/milo_ui.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ function MLFoldTree$toggleItem(id, opened) {

},{}],8:[function(require,module,exports){
'use strict';

const async = require('async');
const FORMLIST_CHANGE_MESSAGE = 'mlformlistchange';

const MLFormList = module.exports = milo.createComponentClass({
Expand Down Expand Up @@ -744,7 +744,9 @@ const MLFormList = module.exports = milo.createComponentClass({
init: MLFormList$init,
moveItem: MLFormList$moveItem,
setItemSchema: MLFormList$setItemSchema,
destroy: MLFormList$destroy
destroy: MLFormList$destroy,
validateModel: MLFormList$validateModel,
clearSubSchemaValidation: MLFormList$clearSubSchemaValidation
}
});

Expand All @@ -769,6 +771,7 @@ function handleClick (type, event) {
function MLFormList$init () {
MLFormList.super.init.apply(this, arguments);
this.once('childrenbound', onChildrenBound);
this._invalidFormControls = {};
}

function MLFormList$setItemSchema (schema) {
Expand Down Expand Up @@ -844,7 +847,95 @@ function _triggerExternalPropagation () {
showHidePrepend.call(this);
}

},{}],9:[function(require,module,exports){
function MLFormList$clearSubSchemaValidation () {
this._invalidFormControls = {};
}

function MLFormList$validateModel (callback, invalidControls) {
const validations = [];
const self = this;
this._dataValidations = { fromModel: {} };
(this.model.m().get() || []).forEach((data, index) => {
this._subFormSchema.items.forEach((item) => {
if (item.validate && item.validate.fromModel && item.validate.fromModel[0] === 'required') {
this._dataValidations.fromModel[`${index}${item.modelPath}`] = [validateRequired];
}
});
});

_.eachKey(this._dataValidations.fromModel, function (validators, modelPath) {
const [index, path] = modelPath.split('.');
const data = (this.model.m().get() || [])[index][path];
validators = Array.isArray(validators) ? validators : [validators];

if (validators && validators.length) {
validations.push({
modelPath: modelPath,
data: data,
validators: validators
});
}
}, this);


let allValid = true;
async.each(validations,
function (validation, nextValidation) {
let lastResponse;
async.every(validation.validators,
function (validator, next) {
validator(validation.data, function (err, response) {
lastResponse = response || {};
next(err, lastResponse.valid);
});
},
function (err, valid) {
lastResponse.path = validation.modelPath;
lastResponse.valid = valid;
handleValidatedComponents.call(self, lastResponse, invalidControls);
if (!valid) allValid = false;
nextValidation(null);
}
);
},
function (err) {
invalidControls = Object.assign({}, invalidControls, self._invalidFormControls);
callback && callback({allValid, invalidControls});
}
);
}

function validateRequired(data, callback) {
const valid = typeof data != 'undefined'
&& (typeof data != 'string' || data.trim() != '');
const response = MLForm$$validatorResponse(valid, 'please enter a value', 'REQUIRED');
callback(null, response);
}

function MLForm$$validatorResponse(valid, reason, reasonCode) {
return valid
? { valid: true }
: { valid: false, reason: reason, reasonCode: reasonCode };
}

function handleValidatedComponents(response) {
if (response.valid) {
delete this._invalidFormControls[response.path];
} else {
const [index, modelPath] = response.path.split('.');
let reason = {
label: `List Item ${Number(index)+1}. ${modelPath}`,
reason: response.reason,
reasonCode: response.reasonCode
};
this._invalidFormControls[response.path] = {
reason: reason
};
}
}


},{"async":34}],9:[function(require,module,exports){
'use strict';

const componentsRegistry = milo.registry.components;
Expand Down Expand Up @@ -3507,6 +3598,7 @@ _.extendProto(MLForm, {
viewPathSchema: MLForm$viewPathSchema,
getModelPath: MLForm$getModelPath,
getViewPath: MLForm$getViewPath,
getSubSchemas: MLForm$getSubSchemas,
destroy: MLForm$destroy,
});

Expand Down Expand Up @@ -3911,6 +4003,22 @@ function MLForm$viewPathComponent(viewPath) {
return viewPathObj && viewPathObj.component;
}

/**
* Returns subSchemas of type formList
*
* @return {Schemas}
*/
function MLForm$getSubSchemas() {
let subSchemas = [];
for(const value in this._formViewPaths) {
if(Object.hasOwn(this._formViewPaths, value) && this._formViewPaths[value].schema && this._formViewPaths[value].schema.type === "formlist") {
subSchemas.push(this._formViewPaths[value]);
}
}
console.log("subSchemas", subSchemas)
return subSchemas;
}


/**
* Returns form schema for a given view path item (path as defined in Data facet)
Expand Down Expand Up @@ -4539,6 +4647,7 @@ formRegistry.setDefaults({


function registry_get(name) {
// debugger
var formItem = name && formTypes[name];

if (!formItem)
Expand All @@ -4548,6 +4657,7 @@ function registry_get(name) {
}

function registry_add(name, newFormItem) {
// debugger
check(name, String);
check(newFormItem, {
compClass: Match.Optional(String),
Expand Down
2 changes: 1 addition & 1 deletion dist/milo_ui.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/milo_ui.min.js.map

Large diffs are not rendered by default.

95 changes: 93 additions & 2 deletions lib/components/FormList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const async = require('async');
const FORMLIST_CHANGE_MESSAGE = 'mlformlistchange';

const MLFormList = module.exports = milo.createComponentClass({
Expand Down Expand Up @@ -27,7 +27,9 @@ const MLFormList = module.exports = milo.createComponentClass({
init: MLFormList$init,
moveItem: MLFormList$moveItem,
setItemSchema: MLFormList$setItemSchema,
destroy: MLFormList$destroy
destroy: MLFormList$destroy,
validateModel: MLFormList$validateModel,
clearSubSchemaValidation: MLFormList$clearSubSchemaValidation
}
});

Expand All @@ -52,6 +54,7 @@ function handleClick (type, event) {
function MLFormList$init () {
MLFormList.super.init.apply(this, arguments);
this.once('childrenbound', onChildrenBound);
this._invalidFormControls = {};
}

function MLFormList$setItemSchema (schema) {
Expand Down Expand Up @@ -126,3 +129,91 @@ function _triggerExternalPropagation () {
this.data.dispatchSourceMessage(FORMLIST_CHANGE_MESSAGE);
showHidePrepend.call(this);
}

function MLFormList$clearSubSchemaValidation () {
this._invalidFormControls = {};
}

function MLFormList$validateModel (callback, invalidControls) {
const validations = [];
const self = this;
this._dataValidations = { fromModel: {} };
(this.model.m().get() || []).forEach((data, index) => {
this._subFormSchema.items.forEach((item) => {
if (item.validate && item.validate.fromModel && item.validate.fromModel[0] === 'required') {
this._dataValidations.fromModel[`${index}${item.modelPath}`] = [validateRequired];
}
});
});

_.eachKey(this._dataValidations.fromModel, function (validators, modelPath) {
const [index, path] = modelPath.split('.');
const data = (this.model.m().get() || [])[index][path];
validators = Array.isArray(validators) ? validators : [validators];

if (validators && validators.length) {
validations.push({
modelPath: modelPath,
data: data,
validators: validators
});
}
}, this);


let allValid = true;
async.each(validations,
function (validation, nextValidation) {
let lastResponse;
async.every(validation.validators,
function (validator, next) {
validator(validation.data, function (err, response) {
lastResponse = response || {};
next(err, lastResponse.valid);
});
},
function (err, valid) {
lastResponse.path = validation.modelPath;
lastResponse.valid = valid;
handleValidatedComponents.call(self, lastResponse, invalidControls);
if (!valid) allValid = false;
nextValidation(null);
}
);
},
function (err) {
invalidControls = Object.assign({}, invalidControls, self._invalidFormControls);
callback && callback({allValid, invalidControls});
}
);
}

function validateRequired(data, callback) {
const valid = typeof data != 'undefined'
&& (typeof data != 'string' || data.trim() != '');
const response = MLForm$$validatorResponse(valid, 'please enter a value', 'REQUIRED');
callback(null, response);
}

function MLForm$$validatorResponse(valid, reason, reasonCode) {
return valid
? { valid: true }
: { valid: false, reason: reason, reasonCode: reasonCode };
}

function handleValidatedComponents(response) {
if (response.valid) {
delete this._invalidFormControls[response.path];
} else {
const [index, modelPath] = response.path.split('.');
let reason = {
label: `List Item ${Number(index)+1}. ${modelPath}`,
reason: response.reason,
reasonCode: response.reasonCode
};
this._invalidFormControls[response.path] = {
reason: reason
};
}
}

17 changes: 17 additions & 0 deletions lib/forms/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ _.extendProto(MLForm, {
viewPathSchema: MLForm$viewPathSchema,
getModelPath: MLForm$getModelPath,
getViewPath: MLForm$getViewPath,
getSubSchemas: MLForm$getSubSchemas,
destroy: MLForm$destroy,
});

Expand Down Expand Up @@ -542,6 +543,22 @@ function MLForm$viewPathComponent(viewPath) {
return viewPathObj && viewPathObj.component;
}

/**
* Returns subSchemas of type formList
*
* @return {Schemas}
*/
function MLForm$getSubSchemas() {
let subSchemas = [];
for(const value in this._formViewPaths) {
if(Object.hasOwn(this._formViewPaths, value) && this._formViewPaths[value].schema && this._formViewPaths[value].schema.type === "formlist") {
subSchemas.push(this._formViewPaths[value]);
}
}
console.log("subSchemas", subSchemas)
return subSchemas;
}


/**
* Returns form schema for a given view path item (path as defined in Data facet)
Expand Down
2 changes: 2 additions & 0 deletions lib/forms/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ formRegistry.setDefaults({


function registry_get(name) {
// debugger
var formItem = name && formTypes[name];

if (!formItem)
Expand All @@ -38,6 +39,7 @@ function registry_get(name) {
}

function registry_add(name, newFormItem) {
// debugger
check(name, String);
check(newFormItem, {
compClass: Match.Optional(String),
Expand Down