From f1c3caae2dfcd5cbbe5b01ce727031b0c5d1bae8 Mon Sep 17 00:00:00 2001 From: kgiszewski Date: Wed, 27 May 2015 13:31:25 -0400 Subject: [PATCH] Allow args to be passed to label templates and begin label library --- app/controllers/controller.js | 38 +++++++++++++------ app/helpers/labelhelpers.js | 69 +++++++++++++++++++++++++++++------ 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/app/controllers/controller.js b/app/controllers/controller.js index dd418e9..a4b4b79 100644 --- a/app/controllers/controller.js +++ b/app/controllers/controller.js @@ -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); } } diff --git a/app/helpers/labelhelpers.js b/app/helpers/labelhelpers.js index 599ba83..331d5b4 100644 --- a/app/helpers/labelhelpers.js +++ b/app/helpers/labelhelpers.js @@ -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 ""; } }