From efe40db233470fa61f90f3a45d42002b45a3fee0 Mon Sep 17 00:00:00 2001 From: Pat Long Date: Tue, 29 Sep 2015 01:07:26 -0700 Subject: [PATCH] fieldId()'s for names and ids --- src/directives/field.js | 29 ++++++++++++++++++++++++++--- src/services/decorators.js | 23 +++++++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/directives/field.js b/src/directives/field.js index 556f30c59..b56efbc68 100644 --- a/src/directives/field.js +++ b/src/directives/field.js @@ -9,9 +9,12 @@ angular.module('schemaForm').directive('sfField', replace: false, transclude: false, scope: true, - require: '^sfSchema', + require: ['^sfSchema', '?^form'], link: { - pre: function(scope, element, attrs, sfSchema) { + pre: function(scope, element, attrs, Ctrl) { + var sfSchema = Ctrl[0]; + var formCtrl = Ctrl[1]; + //The ngModelController is used in some templates and //is needed for error messages, scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) { @@ -23,7 +26,10 @@ angular.module('schemaForm').directive('sfField', // Fetch our form. scope.form = sfSchema.lookup['f' + attrs.sfField]; }, - post: function(scope, element, attrs, sfSchema) { + post: function(scope, element, attrs, Ctrl) { + var sfSchema = Ctrl[0]; + var formCtrl = Ctrl[1]; + //Keep error prone logic from the template scope.showTitle = function() { return scope.form && scope.form.notitle !== true && scope.form.title; @@ -154,6 +160,23 @@ angular.module('schemaForm').directive('sfField', ); }; + scope.fieldId = function(prependFormName, omitNumbers) { + if(scope.form.key){ + var fieldKey = scope.form.key; + if(omitNumbers){ + fieldKey = fieldKey.filter(function(key){ + return !angular.isNumber(key); + }); + } + return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-'); + } + return ''; + }; + + // append the field-id to the htmlClass + if(!scope.form.htmlClass){ scope.form.htmlClass = ''; } + scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true); + var form = scope.form; // Where there is a key there is probably a ngModel diff --git a/src/services/decorators.js b/src/services/decorators.js index bd13a94fc..05d00bae0 100644 --- a/src/services/decorators.js +++ b/src/services/decorators.js @@ -39,8 +39,10 @@ angular.module('schemaForm').provider('schemaFormDecorators', replace: false, transclude: false, scope: true, - require: '?^sfSchema', - link: function(scope, element, attrs, sfSchema) { + require: ['?^sfSchema', '?^form'], + link: function(scope, element, attrs, Ctrl) { + var sfSchema = Ctrl[0]; + var formCtrl = Ctrl[1]; //The ngModelController is used in some templates and //is needed for error messages, @@ -149,6 +151,19 @@ angular.module('schemaForm').provider('schemaFormDecorators', return scope.ngModel.$invalid && !scope.ngModel.$pristine; }; + scope.fieldId = function(prependFormName, omitNumbers) { + if(scope.form.key){ + var fieldKey = scope.form.key; + if(omitNumbers){ + fieldKey = fieldKey.filter(function(key){ + return !angular.isNumber(key); + }); + } + return ((prependFormName && formCtrl && formCtrl.$name)?formCtrl.$name+'-':'')+fieldKey.join('-'); + } + return ''; + }; + /** * DEPRECATED: use sf-messages instead. * Error message handler @@ -174,6 +189,10 @@ angular.module('schemaForm').provider('schemaFormDecorators', form.ngModelOptions = form.ngModelOptions || {}; scope.form = form; + // append the field-id to the htmlClass + if(!scope.form.htmlClass){ scope.form.htmlClass = ''; } + scope.form.htmlClass += (scope.form.htmlClass?' ':'')+scope.fieldId(false, true); + //ok let's replace that template! //We do this manually since we need to bind ng-model properly and also //for fieldsets to recurse properly.