From 89f4ac89219791a2ef8ea71480b422c4224919f4 Mon Sep 17 00:00:00 2001 From: Pat Long Date: Tue, 29 Sep 2015 00:44:46 -0700 Subject: [PATCH 1/2] fieldId()'s for names and ids --- dist/bootstrap-decorator.js | 10 ++-- dist/schema-form.js | 53 +++++++++++++++++-- .../decorators/bootstrap/checkbox.html | 2 +- .../decorators/bootstrap/checkboxes.html | 2 +- .../decorators/bootstrap/default.html | 16 +++--- .../decorators/bootstrap/select.html | 2 +- .../decorators/bootstrap/textarea.html | 10 ++-- src/directives/field.js | 24 ++++++++- src/services/decorators.js | 24 ++++++++- 9 files changed, 113 insertions(+), 30 deletions(-) diff --git a/dist/bootstrap-decorator.js b/dist/bootstrap-decorator.js index eabcc2c56..e68795921 100644 --- a/dist/bootstrap-decorator.js +++ b/dist/bootstrap-decorator.js @@ -10,9 +10,9 @@ angular.module("schemaForm").run(["$templateCache", function($templateCache) {$templateCache.put("directives/decorators/bootstrap/actions-trcl.html","
"); $templateCache.put("directives/decorators/bootstrap/actions.html","
"); $templateCache.put("directives/decorators/bootstrap/array.html","
"); -$templateCache.put("directives/decorators/bootstrap/checkbox.html","
"); -$templateCache.put("directives/decorators/bootstrap/checkboxes.html","
"); -$templateCache.put("directives/decorators/bootstrap/default.html","
{{ hasSuccess() ? \'(success)\' : \'(error)\' }}
"); +$templateCache.put("directives/decorators/bootstrap/checkbox.html","
"); +$templateCache.put("directives/decorators/bootstrap/checkboxes.html","
"); +$templateCache.put("directives/decorators/bootstrap/default.html","
{{ hasSuccess() ? \'(success)\' : \'(error)\' }}
"); $templateCache.put("directives/decorators/bootstrap/fieldset-trcl.html","
{{ form.title }}
"); $templateCache.put("directives/decorators/bootstrap/fieldset.html","
{{ form.title }}
"); $templateCache.put("directives/decorators/bootstrap/help.html","
"); @@ -20,11 +20,11 @@ $templateCache.put("directives/decorators/bootstrap/radio-buttons.html","
"); $templateCache.put("directives/decorators/bootstrap/radios.html","
"); $templateCache.put("directives/decorators/bootstrap/section.html","
"); -$templateCache.put("directives/decorators/bootstrap/select.html","
"); +$templateCache.put("directives/decorators/bootstrap/select.html","
"); $templateCache.put("directives/decorators/bootstrap/submit.html","
"); $templateCache.put("directives/decorators/bootstrap/tabarray.html","
"); $templateCache.put("directives/decorators/bootstrap/tabs.html","
"); -$templateCache.put("directives/decorators/bootstrap/textarea.html","
");}]); +$templateCache.put("directives/decorators/bootstrap/textarea.html","
");}]); angular.module('schemaForm').config(['schemaFormDecoratorsProvider', function(decoratorsProvider) { var base = 'directives/decorators/bootstrap/'; diff --git a/dist/schema-form.js b/dist/schema-form.js index 4eb6ed5ba..b44c0e57e 100644 --- a/dist/schema-form.js +++ b/dist/schema-form.js @@ -463,8 +463,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, @@ -589,6 +591,19 @@ angular.module('schemaForm').provider('schemaFormDecorators', ); }; + 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 ''; + }; + // Rebind our part of the form to the scope. var once = scope.$watch(attrs.form, function(form) { if (form) { @@ -598,6 +613,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. @@ -1928,9 +1947,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) { @@ -1942,12 +1964,16 @@ 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; }; + scope.listToCheckboxValues = function(list) { var values = {}; angular.forEach(list, function(v) { @@ -2073,6 +2099,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/directives/decorators/bootstrap/checkbox.html b/src/directives/decorators/bootstrap/checkbox.html index d6ad64d4b..3022c5d0c 100644 --- a/src/directives/decorators/bootstrap/checkbox.html +++ b/src/directives/decorators/bootstrap/checkbox.html @@ -8,7 +8,7 @@ ng-model-options="form.ngModelOptions" schema-validate="form" class="{{form.fieldHtmlClass}}" - name="{{form.key.slice(-1)[0]}}"> + name="{{fieldId()}}">
diff --git a/src/directives/decorators/bootstrap/checkboxes.html b/src/directives/decorators/bootstrap/checkboxes.html index 45b514135..c99eba9b0 100644 --- a/src/directives/decorators/bootstrap/checkboxes.html +++ b/src/directives/decorators/bootstrap/checkboxes.html @@ -9,7 +9,7 @@ sf-changed="form" class="{{form.fieldHtmlClass}}" ng-model="titleMapValues[$index]" - name="{{form.key.slice(-1)[0]}}"> + name="{{fieldId()}}"> diff --git a/src/directives/decorators/bootstrap/default.html b/src/directives/decorators/bootstrap/default.html index b5685042b..a093163c0 100644 --- a/src/directives/decorators/bootstrap/default.html +++ b/src/directives/decorators/bootstrap/default.html @@ -1,6 +1,6 @@
- + + name="{{fieldId()}}" + aria-describedby="{{fieldId(true) + 'Status'}}">
@@ -28,13 +28,13 @@ sf-changed="form" placeholder="{{form.placeholder}}" class="form-control {{form.fieldHtmlClass}}" - id="{{form.key.slice(-1)[0]}}" + id="{{fieldId(true)}}" ng-model-options="form.ngModelOptions" ng-model="$$value$$" ng-disabled="form.readonly" schema-validate="form" - name="{{form.key.slice(-1)[0]}}" - aria-describedby="{{form.key.slice(-1)[0] + 'Status'}}"> + name="{{fieldId()}}" + aria-describedby="{{fieldId(true) + 'Status'}}"> {{ hasSuccess() ? '(success)' : '(error)' }}
diff --git a/src/directives/decorators/bootstrap/select.html b/src/directives/decorators/bootstrap/select.html index b4b3296e0..dd02bb42c 100644 --- a/src/directives/decorators/bootstrap/select.html +++ b/src/directives/decorators/bootstrap/select.html @@ -10,7 +10,7 @@ class="form-control {{form.fieldHtmlClass}}" schema-validate="form" ng-options="item.value as item.name group by item.group for item in form.titleMap" - name="{{form.key.slice(-1)[0]}}"> + name="{{fieldId()}}">
diff --git a/src/directives/decorators/bootstrap/textarea.html b/src/directives/decorators/bootstrap/textarea.html index 06364edfc..ed7ec02f7 100644 --- a/src/directives/decorators/bootstrap/textarea.html +++ b/src/directives/decorators/bootstrap/textarea.html @@ -1,16 +1,16 @@
- + + name="{{fieldId()}}">
@@ -18,14 +18,14 @@ class="input-group-addon" ng-bind-html="form.fieldAddonLeft"> + name="{{fieldId()}}"> diff --git a/src/directives/field.js b/src/directives/field.js index 556f30c59..706527adb 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) { @@ -154,6 +157,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..6cc99d949 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,20 @@ 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 +190,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. From 92d1b6cef904ab948d643c01fc1d2888bf3a2b63 Mon Sep 17 00:00:00 2001 From: Pat Long Date: Tue, 29 Sep 2015 01:15:36 -0700 Subject: [PATCH 2/2] missed Ctrl in post link --- src/directives/field.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/directives/field.js b/src/directives/field.js index 706527adb..b56efbc68 100644 --- a/src/directives/field.js +++ b/src/directives/field.js @@ -26,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;