forked from GabiAxel/ng-polymer-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-polymer-elements.js
177 lines (134 loc) · 5.86 KB
/
ng-polymer-elements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(function(angular) {
angular.module('ng-polymer-elements', [])
.config(['$compileProvider', '$injector', function($compileProvider, $injector) {
'use strict';
// Each mapping is an object where the key is the directive/custom element
// name in camel case and the value is an object where the keys are the
// AngularJS attributes in camel case and the values are objects where the
// key is the type which can be 'primitive', 'object', 'array' or 'event'
// and the value is the name of the attribute in the web component.
var inputMappings = {
ngModel: {
primitive: 'value'
},
ngDisabled: {
primitive: 'disabled'
}
};
var selectorMappings = {
ngModel: {
primitive: 'selected'
}
};
var checkMappings = {
ngModel: {
primitive: 'checked'
},
ngDisabled: {
primitive: 'disabled'
}
};
var openableMappings = {
ngOpened: {
primitive: 'opened'
}
};
var allMappings = {
paperInput: inputMappings,
paperRadioGroup: selectorMappings,
paperTabs: selectorMappings,
coreSelector: selectorMappings,
coreMenu: selectorMappings,
paperCheckbox: checkMappings,
paperToggleButton: checkMappings,
coreOverlay: openableMappings,
paperDialog: openableMappings,
paperActionDialog: openableMappings,
paperToast: openableMappings,
paperSlider: inputMappings,
coreList: {
ngModel: {
array: 'data'
},
ngTap: {
event: 'core-activate'
}
}
};
// Extension point for overriding mappings
if($injector.has('$ngPolymerMappings')) {
var extendedMappings = $injector.get('$ngPolymerMappings');
angular.extend(allMappings, extendedMappings);
}
// A directive is created for each web component according to the mappings
Object.keys(allMappings).forEach(function(tag) {
var mappings = allMappings[tag];
$compileProvider.directive(tag, ['$parse', '$window', function($parse, $window) {
var scopeDefinition = {};
var keys = Object.keys(mappings);
keys.forEach(function(attr) {
var conf = mappings[attr];
if(conf.primitive || conf.object || conf.array) {
scopeDefinition[attr] = '=';
} else if(!conf.event) {
throw 'Invalid mapping for ' + attr +
' - must contain primitive | object | array | event';
}
});
return {
restrict: 'E',
scope: scopeDefinition,
link: function (scope, element, attrs) {
var el = element[0];
keys.forEach(function(attr) {
// Don't create bindings for non-existent attributes
if(!attrs[attr]) {
return;
}
var conf = mappings[attr];
if(conf.event) {
var fn = $parse(attrs[attr]);
el.addEventListener(conf.event, function (e) {
scope.$apply(function() {
fn(scope.$parent, {$event: e});
});
});
} else {
var propertyName =
conf.primitive || conf.object || conf.array;
if(conf.object) {
el[propertyName] = {};
} else if(conf.array) {
el[propertyName] = [];
}
// Copy the scope property value to the web
// component's value
var handler = function(value) {
if(conf.primitive) {
el[propertyName] = value;
} else {
angular.copy(value, el[propertyName]);
}
};
scope.$watch(attr, handler, true);
handler(scope[attr]);
// Copy the web component's value to the scope
// property value
var observer = new PathObserver(el, propertyName);
observer.open(function (value) {
scope.$apply(function () {
if(conf.primitive) {
scope[attr] = value;
} else {
angular.copy(value, scope[attr]);
}
});
});
}
});
}
};
}]);
});
}]);
})(angular);