diff --git a/Gruntfile.js b/Gruntfile.js index 8e7f186..0cd74cb 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -60,7 +60,8 @@ module.exports = function(grunt) { 'app/directives/localize.js', 'app/services/localization.js', 'app/helpers/labelhelpers.js', - 'app/resources/propertyeditor.js' + 'app/resources/propertyeditor.js', + 'app/services/archetypeService.js' ], dest: '<%= basePath %>/js/archetype.js' } diff --git a/app/controllers/controller.js b/app/controllers/controller.js index a4b4b79..ea84fc8 100644 --- a/app/controllers/controller.js +++ b/app/controllers/controller.js @@ -1,4 +1,4 @@ -angular.module("umbraco").controller("Imulus.ArchetypeController", function ($scope, $http, assetsService, angularHelper, notificationsService, $timeout, entityResource) { +angular.module("umbraco").controller("Imulus.ArchetypeController", function ($scope, $http, assetsService, angularHelper, notificationsService, $timeout, entityResource, archetypeService) { //$scope.model.value = ""; $scope.model.hideLabel = $scope.model.config.hideLabel == 1; @@ -17,90 +17,14 @@ init(); - //hold references to helper resources + //hold references to helper resources for the label helpers $scope.resources = { entityResource: entityResource } //helper to get $eval the labelTemplate - $scope.getFieldsetTitle = function(fieldsetConfigModel, fieldsetIndex) { - if(!fieldsetConfigModel) - return ""; - var fieldset = $scope.model.value.fieldsets[fieldsetIndex]; - var fieldsetConfig = $scope.getConfigFieldsetByAlias(fieldset.alias); - var template = fieldsetConfigModel.labelTemplate; - - if (template.length < 1) - return fieldsetConfig.label; - - 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 templates = results[0].replace("{{", '').replace("}}", '').split("|"); - var templateLabelValue = ""; - - 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 template = templates[i]; - - //test for function - var beginParamsIndexOf = template.indexOf("("); - var endParamsIndexOf = template.indexOf(")"); - - if(beginParamsIndexOf != -1 && endParamsIndexOf != -1) - { - 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 = template; - templateLabelValue = $scope.getPropertyValueByAlias(fieldset, propertyAlias); - } - } - parsedTemplate = parsedTemplate.replace(results[0], templateLabelValue); - } - - return parsedTemplate; - }; - - function executeFunctionByName(functionName, context) { - var args = Array.prototype.slice.call(arguments).splice(2); - - var namespaces = functionName.split("."); - var func = namespaces.pop(); - - for(var i = 0; i < namespaces.length; i++) { - context = context[namespaces[i]]; - } - - if(context && context[func]) { - return context[func].apply(this, args); - } - - return ""; + $scope.getFieldsetTitle = function (fieldsetConfigModel, fieldsetIndex) { + return archetypeService.getFieldsetTitle($scope, fieldsetConfigModel, fieldsetIndex); } var draggedRteSettings; diff --git a/app/helpers/labelhelpers.js b/app/helpers/labelhelpers.js index 3ebe03e..0f086e0 100644 --- a/app/helpers/labelhelpers.js +++ b/app/helpers/labelhelpers.js @@ -34,7 +34,7 @@ var ArchetypeLabels = (function() { //public functions return { - GetEntityProperty: function (value, scope, args) { + Entity: function (value, scope, args) { if(!args.entityType) { args = {entityType: "Document", propertyName: "name"} diff --git a/app/services/archetypeService.js b/app/services/archetypeService.js new file mode 100644 index 0000000..adcfddb --- /dev/null +++ b/app/services/archetypeService.js @@ -0,0 +1,85 @@ +angular.module('umbraco.services').factory('archetypeService', function () { + //private + function executeFunctionByName(functionName, context) { + var args = Array.prototype.slice.call(arguments).splice(2); + + var namespaces = functionName.split("."); + var func = namespaces.pop(); + + for(var i = 0; i < namespaces.length; i++) { + context = context[namespaces[i]]; + } + + if(context && context[func]) { + return context[func].apply(this, args); + } + + return ""; + } + + //public + return { + getFieldsetTitle: function($scope, fieldsetConfigModel, fieldsetIndex) { + if(!fieldsetConfigModel) + return ""; + var fieldset = $scope.model.value.fieldsets[fieldsetIndex]; + var fieldsetConfig = $scope.getConfigFieldsetByAlias(fieldset.alias); + var template = fieldsetConfigModel.labelTemplate; + + if (template.length < 1) + return fieldsetConfig.label; + + 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 templates = results[0].replace("{{", '').replace("}}", '').split("|"); + var templateLabelValue = ""; + + 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 template = templates[i]; + + //test for function + var beginParamsIndexOf = template.indexOf("("); + var endParamsIndexOf = template.indexOf(")"); + + if(beginParamsIndexOf != -1 && endParamsIndexOf != -1) + { + 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 = template; + templateLabelValue = $scope.getPropertyValueByAlias(fieldset, propertyAlias); + } + } + parsedTemplate = parsedTemplate.replace(results[0], templateLabelValue); + } + + return parsedTemplate; + } + } +}); \ No newline at end of file