Fixing display constraint text

This commit is contained in:
Eloízio Salgado 2017-02-22 18:43:04 -03:00
parent 6ff8a57f50
commit 0d44906fe9
3 changed files with 60 additions and 25 deletions

View File

@ -14,7 +14,7 @@
<td>
<i class="fa fa-pencil editRule"></i>
<i class="fa fa-trash deleteRule"></i>
{{displayConstraint attribute constraint}}
{{displayConstraint attribute constraint ../attributes}}
{{ #unless rulePassed }}<small class="failWarning">Fail</small>{{ /unless }}
</td>
<td><input class="ruleWeight" type="number" value={{weight}} min="1" step="1" max="1000"/></td>

View File

@ -1,19 +1,22 @@
UI.registerHelper('viewportSettingsTypes', function() {
import { Blaze } from 'meteor/blaze';
Blaze.registerHelper('viewportSettingsTypes', function() {
return HP.viewportSettingsTypes;
});
UI.registerHelper('toolSettingsTypes', function() {
Blaze.registerHelper('toolSettingsTypes', function() {
return HP.toolSettingsTypes;
});
UI.registerHelper('studyAttributes', function() {
Blaze.registerHelper('studyAttributes', function() {
return HP.studyAttributes;
});
UI.registerHelper('seriesAttributes', function() {
Blaze.registerHelper('seriesAttributes', function() {
return HP.seriesAttributes;
});
UI.registerHelper('instanceAttributes', function() {
Blaze.registerHelper('instanceAttributes', function() {
return HP.instanceAttributes;
});

View File

@ -1,39 +1,71 @@
function humanize(text) {
var humanized = text.replace(/([A-Z])/g, ' $1'); // insert a space before all caps
humanized = humanized.replace(/^./, function(str) { // uppercase the first character
import { Blaze } from 'meteor/blaze';
const attributeCache = Object.create(null);
const REGEXP = /^\([x0-9a-f]+\)/;
const humanize = text => {
let humanized = text.replace(/([A-Z])/g, ' $1'); // insert a space before all caps
humanized = humanized.replace(/^./, str => { // uppercase the first character
return str.toUpperCase();
})
});
return humanized;
}
};
UI.registerHelper('displayConstraint', function(attribute, constraint) {
if (!constraint) {
/**
* Get the text of an attribute for a given attribute
* @param {String} attributeId The attribute ID
* @param {Array} attributes Array of attributes objects with id and text properties
* @return {String} If found return the attribute text or an empty string otherwise
*/
const getAttributeText = (attributeId, attributes) => {
// If the attribute is already in the cache, return it
if (attributeId in attributeCache) {
return attributeCache[attributeId];
}
// Find the attribute with given attributeId
const attribute = attributes.find(attribute => attribute.id === attributeId);
let attributeText;
// If attribute was found get its text and save it on the cache
if (attribute) {
attributeText = attribute.text.replace(REGEXP, '');
attributeCache[attributeId] = attributeText;
}
return attributeText || '';
};
Blaze.registerHelper('displayConstraint', (attributeId, constraint, attributes) => {
if (!constraint || !attributeId) {
return;
}
if (!attribute) {
const validatorType = Object.keys(constraint)[0];
if (!validatorType) {
return;
}
var validatorType = Object.keys(constraint)[0];
if (!attribute) {
const validator = Object.keys(constraint[validatorType])[0];
if (!validator) {
return;
}
var validator = Object.keys(constraint[validatorType])[0];
if (!attribute) {
const value = constraint[validatorType][validator];
if (value === void 0) {
return;
}
var value = constraint[validatorType][validator];
if (value === undefined) {
return;
}
var comparator = validator;
let comparator = validator;
if (validator === 'value') {
comparator = validatorType;
}
return humanize(attribute) + ' ' + humanize(comparator).toLowerCase() + ' ' + value;
const attributeText = getAttributeText(attributeId, attributes);
const constraintText = attributeText + ' ' + humanize(comparator).toLowerCase() + ' ' + value;
return constraintText;
});