-
Notifications
You must be signed in to change notification settings - Fork 52
/
ng-polymer-elements.js
299 lines (263 loc) · 9.85 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*!
* ng-polymer-elements 0.3.0
* https://gabiaxel.github.io/ng-polymer-elements/
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
(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: '=value',
ngDisabled: '=disabled',
ngFocused: '=focused'
};
var selectorMappings = {
ngModel: '=selected'
};
var multiSelectableMappings = {
ngModel: function property(element) {
return element.hasAttribute('multi') ? 'selectedValues' : 'selected';
}
};
var checkMappings = {
ngModel: '=checked',
ngDisabled: '=disabled',
ngChange: '&iron-change'
};
var allMappings = {
ironSelector: multiSelectableMappings,
paperInput: inputMappings,
paperTextarea: inputMappings,
paperRadioGroup: selectorMappings,
paperTabs: selectorMappings,
paperMenu: multiSelectableMappings,
paperCheckbox: checkMappings,
paperToggleButton: checkMappings,
paperDialog: {
ngOpened: '=opened',
ngOverlayOpened: '&iron-overlay-opened',
ngOverlayClosed: '&iron-overlay-closed'
},
paperSlider: {
ngModel: '=value',
ngChange: '&value-change',
ngDisabled: '=disabled'
},
goldEmailInput: inputMappings,
goldPhoneInput: inputMappings,
goldCcInput: {
ngModel: '=value',
ngDisabled: '=disabled',
ngFocused: '=focused',
ngCardType: '=cardType'
},
goldCcExpirationInput: inputMappings,
goldCcCvcInput: inputMappings,
goldZipInput: inputMappings,
googleFeeds: {
ngModel: '=results',
loading: '=loading',
ngError: '&google-feeds-error',
ngQueryError: '&google-feeds-queryerror',
ngQueryResponse: '&google-feeds-queryresponse',
ngResponse: '&google-feeds-response',
ngMultiResponse: '&google-multi-feeds-response'
},
googleMap: {
ngMap: '=map',
ngLatitude: '=latitude',
ngLongitude: '=longitude'
},
googleSheets: {
ngRows: '=rows',
ngSheet: '=sheet',
ngTab: '=tab'
}
};
// 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 mapped = mappings[attr];
// For constant mapping, prefix "=" for property mapping and "&" for
// event mapping.
// For dynamic mapping, name the function "property" or "event".
var mappingType;
switch(typeof mapped) {
case 'string':
mappingType = mapped.charAt(0);
if(mappingType !== '=' && mappingType !== '&') {
throw 'Invalid mapping: "' + mapped
+ '" - must begin with "=" or "&"';
}
mapped = mapped.substr(1);
break;
case 'function':
switch(mapped.name) {
case 'property':
mappingType = '=';
break;
case 'event':
mappingType = '&';
break;
default:
throw 'Invalid mapping for "' + attr
+ '" - function name must be "property" or "event"';
}
break;
default:
throw 'Invalid mapped type for "' + attr
+ '" - must be string or function';
}
scopeDefinition[attr] = mappingType;
});
return {
restrict: 'E',
scope: scopeDefinition,
link: function (scope, element, attrs) {
var el = element[0];
var observers = {}
scope.$on('$destroy', function () {
Object.keys(observers).forEach(function(key) {
var observer = observers[key];
Object.unobserve(el[key], observer);
});
});
keys.forEach(function(attr) {
// Don't create bindings for non-existent attributes
if(!attrs[attr]) {
return;
}
var mapped = mappings[attr];
var mappingType;
if(typeof mapped === 'function') {
mappingType = mapped.name === 'property' ? '=' : '&';
mapped = mapped(el);
} else {
mappingType = mapped.charAt(0);
mapped = mapped.substr(1);
}
if(mappingType === '&') {
// Event mapping
var fn = $parse(attrs[attr]);
el.addEventListener(mapped, function (e) {
scope.$apply(function() {
fn(scope.$parent, {$event: e});
});
});
} else {
// Property mapping
var propertyName = mapped;
var propertyInfo = el.getPropertyInfo(mapped);
var propertyType = propertyInfo.type;
var readOnly = propertyInfo.readOnly;
// For object and array property types, if the element has no
// initial value - set it to empty object/array.
if(!readOnly && !el[propertyName]) {
switch(propertyType) {
case Array:
el[propertyName] = [];
break;
case Object:
el[propertyName] = {};
break;
}
}
// Observe changes to the array/object, and copy its content
// to the directive attribute.
var attachObserver = function() {
if(!readOnly) {
if(observers[propertyName]) {
Object.unobserve(el[propertyName],
observers[propertyName]);
delete observers[propertyName];
}
switch(propertyType) {
case Array:
case Object:
observers[propertyName] = function() {
scope.$apply(function() {
if(!scope[attr]) {
scope[attr] = propertyType === Array ? [] : {};
}
angular.copy(el[propertyName], scope[attr]);
});
}
Object.observe(el[propertyName], observers[propertyName]);
break;
}
}
};
attachObserver();
// Copy the directive attribute value to the element's property.
// For arrays and objects, copy the content.
// The copying is deferred to the next event loop because some
// elements (eg. gold-cc-input) may change the property value
// immediately after inputting it, and we want to use only the
// latest value.
var handler = function() {
setTimeout(function() {
var value = scope[attr];
if(propertyType != Array && propertyType != Object) {
// Undefined value is ignored in order to allow binding to
// values without initiallizing them with an "empty"
// value. Some elements try to process the value on any
// change without safety check.
if(value !== undefined) {
el[propertyName] = value;
}
} else if(value) {
el[propertyName] = angular.copy(value);
attachObserver();
}
});
};
if(!readOnly) {
scope.$watch(attr, handler, true);
handler(scope[attr]);
}
// When the property value changes, copy its new value to the
// directive attribute.
var eventName = propertyName.replace(/([A-Z])/g, function($1) {
return '-' + $1.toLowerCase();
}) + '-changed';
el.addEventListener(eventName, function(event) {
var value = el[propertyName]; //event.detail.value;
el.async(function() {
scope.$apply(function () {
if(propertyType === Array || propertyType === Object) {
scope[attr] = angular.copy(value);
} else {
if(scope[attr] != value) {
scope[attr] = value;
}
}
});
attachObserver();
});
});
}
});
}
};
}]);
});
}]);
})(angular);