@@ -42,7 +42,6 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
4242 //rebind our part of the form to the scope.
4343 var once = scope . $watch ( attrs . form , function ( form ) {
4444
45-
4645 if ( form ) {
4746 scope . form = form ;
4847
@@ -51,7 +50,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
5150 //for fieldsets to recurse properly.
5251 var url = templateUrl ( name , form ) ;
5352 $http . get ( url , { cache : $templateCache } ) . then ( function ( res ) {
54- var template = res . data . replace ( / \$ \$ v a l u e \$ \$ / g, 'model.' + form . key ) ;
53+ var template = res . data . replace ( / \$ \$ v a l u e \$ \$ / g, 'model.' + ( form . key || "" ) ) ;
5554 $compile ( template ) ( scope , function ( clone ) {
5655 element . replaceWith ( clone ) ;
5756 } ) ;
@@ -87,24 +86,78 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
8786 } ] ) ;
8887 } ;
8988
89+ var createManualDirective = function ( type , templateUrl , transclude ) {
90+ transclude = angular . isDefined ( transclude ) ? transclude : false ;
91+ $compileProvider . directive ( 'sf' + angular . uppercase ( type [ 0 ] ) + type . substr ( 1 ) , function ( ) {
92+ return {
93+ restrict : "EAC" ,
94+ scope : true ,
95+ replace : true ,
96+ transclude : transclude ,
97+ template : '<sf-decorator form="form"></sf-decorator>' ,
98+ link : function ( scope , element , attrs ) {
99+ var watchThis = {
100+ 'items' : 'c' ,
101+ 'titleMap' : 'c' ,
102+ 'schema' : 'c'
103+ } ;
104+ var form = { type : type } ;
105+ var once = true ;
106+ angular . forEach ( attrs , function ( value , name ) {
107+ if ( name [ 0 ] !== '$' && name . indexOf ( 'ng' ) !== 0 && name !== 'sfField' ) {
108+
109+ var updateForm = function ( val ) {
110+ if ( angular . isDefined ( val ) && val !== form [ name ] ) {
111+ form [ name ] = val ;
112+
113+ //when we have type, and if specified key we apply it on scope.
114+ if ( once && form . type && ( form . key || angular . isUndefined ( attrs . key ) ) ) {
115+ scope . form = form ;
116+ once = false ;
117+ }
118+ }
119+ } ;
120+
121+ if ( name === 'model' ) {
122+ //"model" is bound to scope under the name "model" since this is what the decorators
123+ //know and love.
124+ scope . $watch ( value , function ( val ) {
125+ if ( val && scope . model !== val ) {
126+ scope . model = val ;
127+ }
128+ } ) ;
129+ } else if ( watchThis [ name ] === 'c' ) {
130+ //watch collection
131+ scope . $watchCollection ( value , updateForm ) ;
132+ } else {
133+ //$observe
134+ attrs . $observe ( name , updateForm ) ;
135+ }
136+ }
137+ } ) ;
138+ }
139+ } ;
140+ } ) ;
141+ } ;
142+
143+
144+
90145 /**
91- * Create a decorator directive
146+ * Create a decorator directive and its sibling "manual" use directives.
92147 * The directive can be used to create form fields or other form entities.
93148 * It can be used in conjunction with <schema-form> directive in which case the decorator is
94149 * given it's configuration via a the "form" attribute.
95150 *
96- * ex. Basic usage with form and schema
97- * <sf-decorator form="myform" schema="myschema"></sf-decorator>
98- *
99- * ex. "Manual" usage
100- * <sf-decorator sf-type="" sf-title=""
151+ * ex. Basic usage
152+ * <sf-decorator form="myform"></sf-decorator>
153+ **
101154 * @param {string } name directive name (CamelCased)
102155 * @param {Object } mappings, an object that maps "type" => "templateUrl"
103156 * @param {Array } rules (optional) a list of functions, function(form){}, that are each tried in turn,
104157 * if they return a string then that is used as the templateUrl. Rules come before
105158 * mappings.
106159 */
107- this . create = function ( name , mappings , rules ) {
160+ this . createDecorator = function ( name , mappings , rules ) {
108161 directives [ name ] = {
109162 mappings : mappings || { } ,
110163 rules : rules || [ ]
@@ -116,6 +169,30 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
116169 createDirective ( name ) ;
117170 } ;
118171
172+ /**
173+ * Creates a directive of a decorator
174+ * Usable when you want to use the decorators without using <schema-form> directive.
175+ * Specifically when you need to reuse styling.
176+ *
177+ * ex. createDirective('text','...')
178+ * <sf-text title="foobar" model="person" key="name" schema="schema"></sf-text>
179+ *
180+ * @param {string } type The type of the directive, resulting directive will have sf- prefixed
181+ * @param {string } templateUrl
182+ * @param {boolean } transclude (optional) sets transclude option of directive, defaults to false.
183+ */
184+ this . createDirective = createManualDirective ;
185+
186+ /**
187+ * Same as createDirective, but takes an object where key is 'type' and value is 'templateUrl'
188+ * Useful for batching.
189+ * @param {Object } mappings
190+ */
191+ this . createDirectives = function ( mappings ) {
192+ angular . forEach ( mappings , function ( url , type ) {
193+ createManualDirective ( type , url ) ;
194+ } ) ;
195+ } ;
119196
120197 /**
121198 * Getter for directive mappings
0 commit comments