Allow args to be passed to label templates and begin label library

This commit is contained in:
kgiszewski
2015-05-27 13:31:25 -04:00
parent 8f8078e12f
commit f1c3caae2d
2 changed files with 85 additions and 22 deletions
+27 -11
View File
@@ -33,34 +33,50 @@
if (template.length < 1)
return fieldsetConfig.label;
var rgx = /{{(.*?)}}*/g;
var rgx = /{{([^)].*)}}/g;
var results;
var parsedTemplate = template;
while ((results = rgx.exec(template)) !== null) {
// split the template in case it consists of multiple property aliases and/or functions
var parts = results[1].split("|");
var templates = results[0].replace("{{", '').replace("}}", '').split("|");
var templateLabelValue = "";
for(var i = 0; i < parts.length; i++) {
for(var i = 0; i < templates.length; i++) {
// stop looking for a template label value if a previous template part already yielded a value
if(templateLabelValue != "") {
break;
}
var part = parts[i];
var template = templates[i];
//test for function
var beginIndexOf = part.indexOf("(");
var endIndexOf = part.indexOf(")");
var beginParamsIndexOf = template.indexOf("(");
var endParamsIndexOf = template.indexOf(")");
if(beginIndexOf != -1 && endIndexOf != -1)
if(beginParamsIndexOf != -1 && endParamsIndexOf != -1)
{
var functionName = part.substring(0, beginIndexOf);
var propertyAlias = part.substring(beginIndexOf + 1, endIndexOf);
templateLabelValue = executeFunctionByName(functionName, window, $scope.getPropertyValueByAlias(fieldset, propertyAlias), $scope);
var functionName = template.substring(0, beginParamsIndexOf);
var propertyAlias = template.substring(beginParamsIndexOf + 1, endParamsIndexOf).split(',')[0];
var args = {};
var beginArgsIndexOf = template.indexOf(',');
if(beginArgsIndexOf != -1) {
var argsString = template.substring(beginArgsIndexOf + 1, endParamsIndexOf).trim();
var normalizedJsonString = argsString.replace(/(\w+)\s*:/g, '"$1":');
args = JSON.parse(normalizedJsonString);
}
templateLabelValue = executeFunctionByName(functionName, window, $scope.getPropertyValueByAlias(fieldset, propertyAlias), $scope, args);
}
else {
propertyAlias = part;
propertyAlias = template;
templateLabelValue = $scope.getPropertyValueByAlias(fieldset, propertyAlias);
}
}
+58 -11
View File
@@ -1,40 +1,87 @@
var ArchetypeLabels = (function() {
//private vars
var isEntityLookupLoading = false;
var entityNameLookupCache = [];
var entityCache = [];
//private functions
function getEntityById(scope, id, type) {
for (var i in entityNameLookupCache) {
if (entityNameLookupCache[i].id == id) {
return entityNameLookupCache[i].value;
}
var cachedEntity = _.find(entityCache, function (e){
return e.id == id;
});
if(cachedEntity) {
return cachedEntity;
}
//go get it from server
if (!isEntityLookupLoading) {
isEntityLookupLoading = true;
scope.resources.entityResource.getById(id, type).then(function(entity) {
entityNameLookupCache.push({id: id, value: entity.name});
entityCache.push(entity);
isEntityLookupLoading = false;
return entity.name;
return entity;
});
}
return "";
}
//public functions
return {
GetFirstDocumentEntityName: function ($scope, value) {
GetEntityProperty: function (value, scope, args) {
if(!args) {
args = {entityType: "Document", propertyName: "name"}
}
if (value) {
//if handed a csv list, take the first only
var id = value.split(",")[0];
if (id) {
return getEntityById($scope, id, "Document");
return getEntityById(scope, id, args.entityType)[args.propertyName];
}
}
return "";
},
UrlPickerTitle: function(value, scope, args) {
if(!args) {
args = {propertyName: "name"}
}
var entity;
switch (value.type) {
case "content":
if(value.typeData.contentId) {
entity = getEntityById(scope, value.typeData.contentId, "Document");
}
break;
case "media":
if(value.typeData.mediaId) {
entity = getEntityById(scope, value.typeData.mediaId, "Media");
}
break;
case "url":
return value.typeData.url;
default:
break;
}
if(entity) {
return entity[args.propertyName];
}
return "";
}
}