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

Commit 842205f

Browse files
author
Kamil Kisiela
committed
Add support for md-theme (templateOptions.theme)
1 parent 070a682 commit 842205f

File tree

6 files changed

+109
-19
lines changed

6 files changed

+109
-19
lines changed

lib/client/formly-material.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var {SetModule} = angular2now;
2+
3+
SetModule('formlyMaterial')
4+
.provider('formlyMaterial', formlyMaterial);
5+
6+
function formlyMaterial() {
7+
var self = this;
8+
9+
this.templateUrl = function (templateUrl) {
10+
return '/packages/wieldo:angular-formly-templates-material/' + templateUrl.replace(/^\//, "");
11+
};
12+
13+
this.$get = function () {
14+
return {
15+
templateUrl: self.templateUrl
16+
}
17+
};
18+
}

lib/client/main.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,4 @@ SetModule('formlyMaterial', [
44
'ngMessages',
55
'ngMaterial',
66
'formly'
7-
])
8-
.provider('formlyMaterial', formlyMaterial);
9-
10-
function formlyMaterial() {
11-
var self = this;
12-
13-
this.templateUrl = function (templateUrl) {
14-
return '/packages/wieldo:angular-formly-templates-material/' + templateUrl.replace(/^\//, "");
15-
};
16-
17-
this.$get = function () {
18-
return {
19-
templateUrl: self.templateUrl
20-
}
21-
};
22-
}
7+
]);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
var {SetModule} = angular2now;
2+
3+
SetModule('formlyMaterial')
4+
.config((formlyConfigProvider) => {
5+
6+
const addIfNotPresent = (nodes, attr, val) => {
7+
angular.forEach(nodes, node => {
8+
if (!node.getAttribute(attr)) {
9+
node.setAttribute(attr, val)
10+
}
11+
})
12+
};
13+
14+
const nodeMatches = (node, selector) => {
15+
const div = document.createElement('div');
16+
17+
div.innerHTML = node.outerHTML;
18+
return div.querySelector(selector)
19+
};
20+
21+
const getNgModelNodes = (node, skip) => {
22+
const selectorNot = angular.isString(skip) ? `:not(${skip})` : '';
23+
const skipNot = ':not([formly-skip-ng-model-attrs-manipulator])';
24+
const query = `[ng-model]${selectorNot}${skipNot}, [data-ng-model]${selectorNot}${skipNot}`;
25+
26+
try {
27+
return node.querySelectorAll(query)
28+
} catch (e) {
29+
//this code is needed for IE8, as it does not support the CSS3 ':not' selector
30+
//it should be removed when IE8 support is dropped
31+
return getNgModelNodesFallback(node, skip)
32+
}
33+
};
34+
35+
const getNgModelNodesFallback = (node, skip) => {
36+
const allNgModelNodes = node.querySelectorAll('[ng-model], [data-ng-model]');
37+
const matchingNgModelNodes = [];
38+
39+
//make sure this array is compatible with NodeList type by adding an 'item' function
40+
matchingNgModelNodes.item = function (i) {
41+
return this[i]
42+
};
43+
44+
for (let i = 0; i < allNgModelNodes.length; i++) {
45+
const ngModelNode = allNgModelNodes[i];
46+
47+
if (!ngModelNode.hasAttribute('formly-skip-ng-model-attrs-manipulator') && !(angular.isString(skip) && nodeMatches(ngModelNode, skip))) {
48+
matchingNgModelNodes.push(ngModelNode)
49+
}
50+
}
51+
52+
return matchingNgModelNodes
53+
};
54+
55+
formlyConfigProvider.templateManipulators.preWrapper.push((template, options, scope) => {
56+
if (angular.isDefined(options.templateOptions.theme)) {
57+
const node = document.createElement('div');
58+
const skip = options.extras && options.extras.skipNgModelAttrsManipulator;
59+
if (skip === true) {
60+
return template
61+
}
62+
node.innerHTML = template;
63+
const modelNodes = getNgModelNodes(node, skip);
64+
65+
if (!modelNodes || !modelNodes.length) {
66+
return template;
67+
}
68+
69+
addIfNotPresent(modelNodes, 'md-theme', options.templateOptions.theme);
70+
71+
return node.innerHTML;
72+
73+
} else {
74+
return template;
75+
}
76+
});
77+
78+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div>
2-
<md-checkbox ng-model="model[options.key]" md-theme="{{to.mdTheme || 'primary'}}">
2+
<md-checkbox ng-model="model[options.key]">
33
{{to.label}}
44
</md-checkbox>
55
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<md-switch ng-model="model[options.key]" md-theme="{{to.mdTheme || 'primary'}}">
1+
<md-switch ng-model="model[options.key]">
22
{{to.label}}
33
</md-switch>

package.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ Package.onUse(function (api) {
3636

3737
api.addFiles([
3838
'lib/client/main.js',
39+
// formlyMaterial prodiver
40+
'lib/client/formly-material.js',
41+
42+
//
43+
// templateManipulators
44+
//
45+
46+
// md-theme
47+
'lib/client/run/md-theme-manipulator.js',
3948

4049
//
4150
// wrappers
@@ -70,7 +79,7 @@ Package.onUse(function (api) {
7079
// select
7180
'lib/client/types/select/select.js',
7281
'lib/client/types/select/select.ng.html',
73-
82+
7483
// radio
7584
'lib/client/types/radio/radio.js',
7685
'lib/client/types/radio/radio.ng.html',

0 commit comments

Comments
 (0)