Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 64c1345

Browse files
author
Kent C. Dodds
committed
Adding formlyProvider allowing developers to override templates and make custom types.
1 parent b7cbe31 commit 64c1345

File tree

10 files changed

+156
-45
lines changed

10 files changed

+156
-45
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Version numbers correspond to `bower.json` version
22

3+
# 0.0.11
4+
5+
## Features
6+
7+
Adding the `formlyProvider` which allows developers to set templateUrls for types. Also allows them to make custom types.
8+
9+
## Bug Fixes
10+
11+
## Breaking Changes
12+
313
# 0.0.10
414

515
## Features

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,37 @@ _Example password field_
396396
}
397397
```
398398

399+
## Other Notes
400+
401+
### Global Config
402+
403+
You can configure formly to use custom templates for specified types (your own "text" template) by injecting the `formlyProvider` in your app's `config` function. The `formlyProvider` has the following functions:
404+
405+
#### setTemplateUrl
406+
407+
Allows you to set a template
408+
409+
```javascript
410+
formlyProvider.setTemplateUrl('radio', 'views/custom-formly-radio.html');
411+
formlyProvider.setTemplateUrl('checkbox', 'views/custom-formly-checkbox.html');
412+
413+
// the same can be accomplished with
414+
415+
formlyProvider.setTemplate({
416+
radio: 'views/custom-formly-radio.html',
417+
checkbox: 'views/custom-formly-checkbox.html'
418+
});
419+
```
420+
421+
#### getTemplateUrl
422+
423+
Allows you to get the template
424+
425+
```javascript
426+
formlyProvider.setTemplateUrl('foo', 'bar');
427+
formlyProvider.getTemplateUrl('foo') === 'bar'; // true
428+
```
429+
399430
## Roadmap
400431

401432
## Release Notes

src/app.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ var app = angular.module('app', ['ng',
66
'formly'
77
]);
88

9-
app.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
9+
app.constant('usingCustomTypeTemplates', window.localStorage.getItem('useCustomTypeTemplates') === 'true');
10+
11+
app.config(function($stateProvider, $urlRouterProvider, $locationProvider, formlyProvider, usingCustomTypeTemplates) {
1012
$locationProvider.html5Mode(false);
1113
$locationProvider.hashPrefix('!');
1214

@@ -18,6 +20,14 @@ app.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
1820
templateUrl: 'views/home.html',
1921
controller: 'home'
2022
});
23+
if (usingCustomTypeTemplates) {
24+
formlyProvider.setTemplateUrl('text', 'views/custom-field-text.html');
25+
// or
26+
formlyProvider.setTemplateUrl({
27+
radio: 'views/custom-field-radio.html',
28+
checkbox: 'views/custom-field-checkbox.html'
29+
});
30+
}
2131
});
2232

2333
app.run(function($rootScope, $state, $stateParams, $window) {

src/directives/formly-field.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,6 @@
11
'use strict';
22
angular.module('formly.render')
3-
.directive('formlyField', function formlyField($http, $compile, $templateCache) {
4-
5-
var getTemplateUrl = function(type) {
6-
var templateUrl = '';
7-
8-
switch(type) {
9-
case 'textarea':
10-
templateUrl = 'directives/formly-field-textarea.html';
11-
break;
12-
case 'radio':
13-
templateUrl = 'directives/formly-field-radio.html';
14-
break;
15-
case 'select':
16-
templateUrl = 'directives/formly-field-select.html';
17-
break;
18-
case 'number':
19-
templateUrl = 'directives/formly-field-number.html';
20-
break;
21-
case 'checkbox':
22-
templateUrl = 'directives/formly-field-checkbox.html';
23-
break;
24-
case 'password' :
25-
templateUrl = 'directives/formly-field-password.html';
26-
break;
27-
case 'hidden' :
28-
templateUrl = 'directives/formly-field-hidden.html';
29-
break;
30-
case 'email':
31-
templateUrl = 'directives/formly-field-email.html';
32-
break;
33-
case 'text':
34-
templateUrl = 'directives/formly-field-text.html';
35-
break;
36-
default :
37-
templateUrl = null;
38-
break;
39-
}
40-
41-
return templateUrl;
42-
};
43-
3+
.directive('formlyField', function formlyField($http, $compile, $templateCache, formly) {
444
return {
455
restrict: 'AE',
466
transclude: true,
@@ -55,7 +15,7 @@ angular.module('formly.render')
5515
if (template) {
5616
setElementTemplate(template);
5717
} else {
58-
var templateUrl = $scope.options.templateUrl || getTemplateUrl($scope.options.type);
18+
var templateUrl = $scope.options.templateUrl || formly.getTemplateUrl($scope.options.type);
5919
if (templateUrl) {
6020
$http.get(templateUrl, {
6121
cache: $templateCache

src/modules/formly-render.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
// Render module for formly to display forms
2-
angular.module('formly.render', []);
2+
angular.module('formly.render', []).provider('formly', function() {
3+
4+
var templateMap = {
5+
textarea: 'directives/formly-field-textarea.html',
6+
radio: 'directives/formly-field-radio.html',
7+
select: 'directives/formly-field-select.html',
8+
number: 'directives/formly-field-number.html',
9+
checkbox: 'directives/formly-field-checkbox.html',
10+
password: 'directives/formly-field-password.html',
11+
hidden: 'directives/formly-field-hidden.html',
12+
email: 'directives/formly-field-email.html',
13+
text: 'directives/formly-field-text.html'
14+
};
15+
16+
function setTemplateUrl(name, templateUrl) {
17+
if (typeof name === 'string') {
18+
templateMap[name] = templateUrl;
19+
} else {
20+
angular.forEach(name, function(templateUrl, name) {
21+
setTemplateUrl(name, templateUrl)
22+
});
23+
}
24+
}
25+
26+
function getTemplateUrl(type) {
27+
return templateMap[type];
28+
};
29+
30+
this.setTemplateUrl = setTemplateUrl;
31+
this.getTemplateUrl = getTemplateUrl;
32+
this.$get = function formly() {
33+
return this;
34+
}
35+
36+
});

src/views/custom-field-checkbox.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="checkbox">
2+
<label>
3+
<input type="checkbox"
4+
ng-required="options.required"
5+
ng-disabled="options.disabled"
6+
ng-model="value">
7+
Custom field of type "checkbox"
8+
{{options.label || 'Checkbox'}}
9+
{{options.required ? '*' : ''}}
10+
</label>
11+
</div>

src/views/custom-field-radio.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<div class="radio-group">
2+
Custom field of type "radio"
3+
<label class="control-label">
4+
{{options.label}}
5+
{{options.required ? '*' : ''}}
6+
</label>
7+
<div class="radio"
8+
ng-repeat="(key, option) in options.options">
9+
<label>
10+
<input type="radio"
11+
name="{{id}}"
12+
id="{{id + '_'+ $index}}"
13+
ng-value="option.value"
14+
ng-required="options.required"
15+
ng-model="$parent.value">
16+
{{option.name}}
17+
</label>
18+
</div>
19+
20+
</div>

src/views/custom-field-text.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div class="form-group">
2+
Custom field of type "text"
3+
<label for="{{id}}">
4+
{{options.label || 'Text'}}
5+
{{options.required ? '*' : ''}}
6+
</label>
7+
<input type="text"
8+
class="form-control"
9+
id="{{id}}"
10+
placeholder="{{options.placeholder}}"
11+
ng-required="options.required"
12+
ng-disabled="options.disabled"
13+
ng-model="value">
14+
</div>

src/views/home.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ <h1>Formly for Angular</h1>
1616
Edit JSON
1717
</button>
1818

19+
<button class="btn btn-success"
20+
ng-click="toggleCustomTypeTemplates()">
21+
{{typeTemplatesButton}}
22+
</button>
23+
1924
<h3>Options</h3>
2025
<div source="toPrettyJSON(formOptions, 4)"
2126
hljs>

src/views/home.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
app.controller('home', function($scope, $parse, $rootScope) {
2+
app.controller('home', function($scope, $parse, $rootScope, $window, usingCustomTypeTemplates) {
33
// Public Methods
44
$scope.onSubmit = function onSubmit() {
55
$scope.submittedData = $scope.formData;
@@ -11,6 +11,16 @@ app.controller('home', function($scope, $parse, $rootScope) {
1111
return result;
1212
};
1313

14+
$scope.toggleCustomTypeTemplates = function() {
15+
if (usingCustomTypeTemplates) {
16+
$window.localStorage.removeItem('useCustomTypeTemplates');
17+
} else {
18+
$window.localStorage.setItem('useCustomTypeTemplates', 'true');
19+
}
20+
// reload state
21+
$window.location.reload();
22+
};
23+
1424
// Private Methods
1525

1626
// Events
@@ -36,6 +46,12 @@ app.controller('home', function($scope, $parse, $rootScope) {
3646
});
3747

3848
// Public Vars
49+
if (usingCustomTypeTemplates) {
50+
$scope.typeTemplatesButton = 'Use Built-in Type Templates';
51+
} else {
52+
$scope.typeTemplatesButton = 'Use Custom Type Templates';
53+
}
54+
3955
$scope.formFields = [{
4056
key: 'firstName',
4157
type: 'text',

0 commit comments

Comments
 (0)