forked from GabiAxel/ng-polymer-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-polymer-elements.js
213 lines (165 loc) · 6.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(function(angular) {
'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: 'inputValue'
},
ngDisabled: {
primitive: 'disabled'
}
};
var selectorMappings = {
ngModel: {
primitive: 'selected'
},
ngChange:
{
event: 'core-select'
}
};
var checkMappings = {
ngModel: {
primitive: 'checked'
},
ngDisabled: {
primitive: 'disabled'
}
};
var openableMappings = {
ngOpened: {
primitive: 'opened'
}
};
var allMappings = {
coreInput: inputMappings,
paperInput: inputMappings,
paperRadioGroup: selectorMappings,
paperTabs: selectorMappings,
coreDropdown: selectorMappings,
paperDropdownMenu: selectorMappings,
paperCheckbox: checkMappings,
paperToggleButton: checkMappings,
coreOverlay: openableMappings,
paperDialog: openableMappings,
paperToast: openableMappings,
paperSlider: {
ngModel: {
primitive: 'value'
},
ngDisabled: {
primitive: 'disabled'
}
},
coreList: {
ngModel: {
array: 'data'
},
ngTap: {
event: 'core-activate'
}
}
};
// The following allows adding and modifying mappings by the application
var extendedMappings = window.NG_POLYMER_ELEMENTS_EXTENDED_MAPPINGS;
if(extendedMappings) {
angular.extend(allMappings, extendedMappings);
}
var module = angular.module('ng-polymer-elements', [])
// A directive is created for each web component according to the mappings
Object.keys(allMappings).forEach(function(tag) {
var mappings = allMappings[tag];
module.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]);
}
});
});
}
});
}
};
}]);
});
// The directives need to be applied to the custom elements after Polymer
// has been loaded and the elements upgraded. Because of this AngularJS
// should be manually bootstrapped only after the the body's 'unresolved'
// tag has been removed and the 'polymer-ready' event has been fired.
// The following code removes any automated bootstrap definition and runs
// manual bootstrap instead.
document.querySelectorAll('[ng-app]').array().forEach(function(element) {
var app = element.getAttribute('ng-app');
element.removeAttribute('ng-app');
function bootstrap() {
angular.bootstrap(wrap(element), [app]);
}
if(angular.isDefined(document.body.attributes['unresolved'])) {
var readyListener = function() {
bootstrap();
window.removeEventListener('polymer-ready', readyListener);
}
window.addEventListener('polymer-ready', readyListener);
} else {
bootstrap();
}
});
})(angular);