This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-element-wrapper.service.js
634 lines (566 loc) · 26.3 KB
/
custom-element-wrapper.service.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
* @ngdoc service
* @module shared
* @name customElementWrapper
* @subtitle A helper service to wrap AngularJS components/directives with custom elements
*
* @description
* We would like to reuse some of existing AngularJS components in Elm.
* Thus we need a way to wrap AngularJS components with custom elements.
* So this service contains helper functions for that.
*/
angular
.module('shared', [])
.factory('customElementWrapper', function (_, $rootScope, $compile, $window, $injector) {
'use strict';
var service = {
create: create,
helper: {
decode: decode,
},
decode: {
/**
* @ngdoc method
* @name customElementWrapper#decode.boolean
*
* @description
* Creates a setter to decode boolean
*
* @param {boolean} value Boolean value.
*/
boolean: decode({
assert: _.isBoolean,
type: 'boolean',
}),
/**
* @ngdoc method
* @name customElementWrapper#decode.integer
*
* @description
* Creates a setter to decode integer
*
* @param {number} value Integer value.
*/
integer: decode({
assert: _.isInteger,
type: 'integer',
}),
/**
* @ngdoc method
* @name customElementWrapper#decode.string
*
* @description
* Creates a setter to decode string
*
* @param {string} value String value.
*/
string: decode({
assert: _.isString,
type: 'string',
}),
/**
* @ngdoc method
* @name customElementWrapper#decode.object
*
* @description
* Creates a setter to decode object
*
* @param {Object} value Object value.
*/
object: decode({
assert: _.isPlainObject,
type: 'object',
}),
},
encode: {
/**
* @ngdoc method
* @name customElementWrapper#encode.identity
*
* @description
* Encodes value as is.
*
* @param {*} value Any value to encode.
* @returns {*} Same value as given. If value is undefined `null` will be returns.
*/
identity: _.identity,
},
};
service.private = {
customElements: {},
};
return service;
/**
* @ngdoc type
* @module shared
* @name CustomElementWrapperCreateOptions
* @subtitle Represents create options for custom element wrapper
*
* @description
* This type is used for {@link customElementWrapper} service `create` method.
*
* @property {Array} [attrs] An array of attribute strings. If attributes from the list will be found in custom element
* they will be replicated to AngularJS component. This is useful when in AngularJS component one uses `$attrs[attrName]` directly.
*
* Example usage:
* ```
* attrs: ['attr-1', 'attr-2',...]
* ```
*
* On Elm side to specify an attribute one can do following
* ```
* import Shared.Html.Attributes.Extra as Attrs
*
* ...
*
* [ Attrs.conditional condition <|
* Attrs.attribute "attr-1" ""
* , Attrs.conditional condition <|
* Attrs.attribute "attr-2" ""
* , ...
* ]
* ```
* @property {Array} [interpolations] An array of interpolation strings.
* An interpolation is a `@` binding in AngularJS component.
* String decoder always will be used for interpolations.
*
* Example usage:
* ```
* interpolations: ['interpolationAttr1', 'interpolationAttr2',...]
* ```
*
* On Elm side to specify an interpolation one can do following
* ```
* ...
*
* [ Attrs.property "interpolationAttr1" (Encode.string "interpolated value 1")
* , Attrs.property "interpolationAttr2" (Encode.string "interpolated value 2")
* , ...
* ]
* ```
* @property {Object} [bindings] An object of bindings. Key of binding will be used as a name for scope property.
* A value of binding is either a getter/setter or function which returns a getter/setter.
* Most of the time we care about setter only. Because this is what will be called when value changes of Elm side.
* On this stage we have to decode a value received from Elm to value for AngularJS component.
* This is useful when in AngularJS component one specifies a binding.
*
* Example usage:
* ```
* bindings: {
* integerBinding: customElementWrapper.decode.integer,
* booleanBinding: customElementWrapper.decode.boolean,
* }
* ```
*
* On Elm side to specify a property one can do following
* ```
* [ Attrs.property "integerBinding" (Encode.int 123)
* , Attrs.property "booleanBinding" (Encode.bool True)
* , ...
* ]
* ```
* @property {Object} [customBindings] An object of custom bindings.
* A custom binding is a binding with custom attribute name and attribute value.
* Key of binding will be used as a name for scope property.
* A value of binding should be an object with three properties.
* - `attr` - name of attribute which will be added to AngularJS component.
* - `attrValue` - a value of attribute
* - `decode` - either a getter/setter or function which returns a getter setter.
* Most of the time we care about setter only. Because this is what will be called when value changes of Elm side.
* On this stage we have to decode a value received from Elm to value for AngularJS component.
* This is useful when in AngularJS component one specifies a binding.
*
* Example usage:
* ```
* customBindings: {
* customBinding: {
* attr: 'custom-attribute-name',
* attrValue: '{{ someCustomValue }}',
* decode: {
* set: function (value) {
* this.$scope.someCustomValue = value;
* }
* }
* }
* }
* ```
*
* On Elm side to specify a property one can do following
* ```
* [ Attrs.property "customBinding" (Encode.string "some custom value")
* , ...
* ]
* ```
* @property {Object} [events] An object of events.
* When AngularJS calls a callback function a new custom event will be dispatched with injectable details.
* Let's say AngularJS component has a `onChange` callback with two injectables, `model` and init`.
* We can describe it in following way
* ```
* events: {
* onChange: {
* model: function (value) {
* return value === 'my string' : 'Yes, I have got what I wanted' : 'Oops, failed';
* },
* init: customElementWrapper.encode.identity
* }
* }
* ```
*
* On Elm side to specify an event one can do following
* ```
* type Msg = MyMessage String Bool
*
* [ Events.on "onChange"
* (Decode.decode MyMessage
* |> Decode.requiredAt [ "detail", "model" ] Decode.string
* |> Decode.requiredAt [ "detail", "init" ] Decode.bool
* )
* , ...
* ]
* ```
*/
/**
* @ngdoc method
* @name customElementWrapper#create
*
* @description
* Creates a custom element wrapper for AngularJS component.
*
* Basic usage from JS:
* ```
* customElementWrapper.create('component-name', {
* attrs: ['required', 'change-year'],
* interpolations: ['apiUrl', 'mainAddressLabel'],
* bindings: {
* integerBinding: customElementWrapper.decode.integer,
* booleanBinding: customElementWrapper.decode.boolean,
* },
* customBindings: {
* customBinding: {
* attr: 'custom-attribute',
* attrValue: '{{ someCustomValue }}',
* decode: {
* set: function (value) {
* this.$scope.someCustomValue = value;
* }
* }
* }
* },
* events: {
* onChange: {
* model: function (value) {
* return value === 'my string' : 'Yes, I have got what I wanted' : 'Oops, failed';
* },
* init: customElementWrapper.encode.identity
* }
* }
* });
* ```
*
* Basic usage from Elm:
* ```
* import Shared.Html.Extra as Html
*
* viewComponent : Html Msg
* viewComponent =
* Html.node "custom-component-name"
* [ Attrs.attribute "required" ""
* , Attrs.attribute "change-year" ""
* , Attrs.property "apiUrl" (Encode.string "some/url")
* , Attrs.property "mainAddressLabel" (Encode.string "My address in Oslo")
* , Attrs.property "integerBinding" (Encode.int 42)
* , Attrs.property "booleanBinding" (Encode.bool True)
* , Attrs.property "customBinding" (Encode.string "My custom string")
* , Events.on "onChange"
* (Decode.decode MyMessage
* |> Decode.requiredAt [ "detail", "model" ] Decode.string
* |> Decode.requiredAt [ "detail", "init" ] Decode.bool
* )
* ]
* [ Html.customElementComponentWrapper
* -- `Html.customElementTranscludeWrapper` takes one parameter with type `List (Html Never)`.
* -- Which means any content which does not produce any messages.
* , Html.customElementTranscludeWrapper [ Html.text "Add some extra content here." ]
* ]
* ```
*
* From `component-name` a custom element will be created with name `custom-component-name`.
*
* @param {string} componentName AngularJS component name.
* @param {CustomElementWrapperCreateOptions} options Custom element options.
* Find out more {@link CustomElementWrapperCreateOptions here}.
*/
function create(componentName, options) {
options = options || {};
// ASSERT CHECKS
assert(service.private.customElements.hasOwnProperty(componentName) === false,
'Component with name ' + componentName + ' already registered');
assert($injector.has(_.camelCase(componentName) + 'Directive'), 'There is no component defined for name: ' + componentName);
assert(_.isPlainObject(options), 'Options should be an object');
var attrs = options.attrs;
var interpolations = options.interpolations;
var bindings = options.bindings;
var customBindings = options.customBindings;
var events = options.events;
assert(Array.isArray(attrs) && attrs.every(_.isString) || angular.isUndefined(attrs),
'Options attributes should be an array of strings');
assert(Array.isArray(interpolations) && interpolations.every(_.isString) || angular.isUndefined(interpolations),
'Options interpolations should be an array of strings');
assert(_.isPlainObject(bindings) || angular.isUndefined(bindings),
'Options bindings should be an object');
assert(_.isPlainObject(customBindings) || angular.isUndefined(customBindings),
'Options custom bindings should be an object');
assert(_.isPlainObject(events) || angular.isUndefined(events),
'Options events should be an object');
if (events) {
Object.keys(events).forEach(function (event) {
assert(_.isPlainObject(events[event]), 'Value of event should be an object with injectables');
assert(Object.keys(events[event]).map(_.propertyOf(events[event])).every(_.isFunction),
'Value of injectable should be a function');
});
}
// CUSTOM ELEMENT CONSTRUCTOR
service.private.customElements[componentName] = function () {
// Super call
var self = HTMLElement.call(this) || this;
self.$scope = $rootScope.$new();
return self;
};
// CUSTOM ELEMENT PROTOTYPE
var prototype = {
constructor: {
value: service.private.customElements[componentName],
},
connectedCallback: {
/**
* Connected callback.
*/
value: function () {
var self = this;
// ANGULAR COMPONENT ELEMENT
var componentElementWrapper = self.querySelector('custom-element-component');
var componentElementTransclude = self.querySelector('custom-element-transclude');
assert(componentElementWrapper !== null, '`custom-element-component` element should be defined');
self.componentElement = angular.element(document.createElement(componentName));
// TRANSCLUDE
if (componentElementTransclude !== null) {
// Copy content for transclusion
self.componentElement[0].innerHTML = componentElementTransclude.innerHTML;
}
// ATTRIBUTES
if (attrs) {
attrs.forEach(function (attr) {
// If attribute exist on custom element then replicate it to AngularJS component
if (self.hasAttribute(attr)) {
self.componentElement.attr(attr, '');
}
});
}
// INTERPOLATIONS
if (interpolations) {
// Adds each interpolation to AngularJS component in following way `attr-name="{{ attrName }}"`
interpolations.forEach(function (interpolation) {
self.componentElement.attr(_.kebabCase(interpolation), '{{ ' + interpolation + ' }}');
});
}
// BINDINGS
if (bindings) {
// Adds each binding to AngularJS component in following way `attr-name="attrName"`
Object.keys(bindings).forEach(function (binding) {
self.componentElement.attr(_.kebabCase(binding), binding);
});
}
// CUSTOM BINDINGS
if (customBindings) {
// Adds each custom binding to AngularJS component in following way
// `{{ binding.attr }}="{{ binding.attrValue }}"`
Object.keys(customBindings).forEach(function (binding) {
self.componentElement.attr(customBindings[binding].attr, customBindings[binding].attrValue);
});
}
// EVENTS
if (events) {
// Adds each event to AngularJS component in following way
// `event-name="eventName(injectable1, injectable2, ...)"`
// Custom event will be dispatched with encoded injectables as detail for each AngularJS component event
Object.keys(events).forEach(function (event) {
var injectables = Object.keys(events[event]);
self.$scope[event] = function () {
var args = arguments;
self.dispatchEvent(new CustomEvent(event, {
detail: injectables.reduce(function (detail, injectable, index) {
// Encode each injectable
detail[injectable] = events[event][injectable](args[index]);
// Elm understands only null and not undefined
if (angular.isUndefined(detail[injectable])) {
detail[injectable] = null;
}
return detail;
}, {}),
}));
};
self.componentElement.attr(_.kebabCase(event), event + '(' + injectables.join(', ') + ')');
});
}
// COMPILE AND APPEND
$compile(self.componentElement)(self.$scope);
angular.element(componentElementWrapper).append(self.componentElement);
},
},
disconnectedCallback: {
/**
* Disconnected callback.
*/
value: function () {
this.$scope.$destroy();
},
},
};
if (interpolations) {
// Extends prototype with getter/setter for each interpolation
interpolations.forEach(function (interpolation) {
prototype[interpolation] = service.decode.string(interpolation);
});
}
if (bindings) {
// Extends prototype with getter/setter for each binding
Object.keys(bindings).forEach(function (binding) {
var isBindingFunction = _.isFunction(bindings[binding]);
assert(isBindingFunction || _.isPlainObject(bindings[binding]),
'Value of binding should be a function or an object');
if (isBindingFunction) {
prototype[binding] = bindings[binding](binding);
} else {
prototype[binding] = bindings[binding];
}
hasGetterOrSetter(prototype[binding], binding);
});
}
if (customBindings) {
// Extends prototype with getter/setter for each custom binding
Object.keys(customBindings).forEach(function (binding) {
assert(_.isPlainObject(customBindings[binding]), 'Value of custom binding should be an object');
assert(_.isString(customBindings[binding].attr),
'Value of custom binding should have a string value of "attr" property');
assert(_.isString(customBindings[binding].attrValue),
'Value of custom binding should have a string value of "attrValue" property');
var isDecodeFunction = _.isFunction(customBindings[binding].decode);
assert(isDecodeFunction || _.isPlainObject(customBindings[binding].decode),
'Value of custom binding should have a "decode" method or property with object type');
if (isDecodeFunction) {
prototype[binding] = customBindings[binding].decode(binding);
} else {
prototype[binding] = customBindings[binding].decode;
}
hasGetterOrSetter(prototype[binding], binding);
});
}
service.private.customElements[componentName].prototype = Object.create(HTMLElement.prototype, prototype);
// REGISTER CUSTOM ELEMENT
$window.customElements.define('custom-' + componentName, service.private.customElements[componentName]);
}
// CREATE HELPERS
/**
* Checks if property has getter or setter or both.
*
* @param {Object} property Property to check.
* @param {string} bindingName Binding name.
*/
function hasGetterOrSetter(property, bindingName) {
var getSetKeys = Object.keys(property);
var hasGetSetProperties = _.isEqual(getSetKeys, ['set', 'get']) ||
_.isEqual(getSetKeys, ['get', 'set']) ||
_.isEqual(getSetKeys, ['get']) ||
_.isEqual(getSetKeys, ['set']);
assert(_.isPlainObject(property) && hasGetSetProperties,
'Binding "' + bindingName + '" should be have either setter or getter or both');
}
// DECODERS
/**
* @ngdoc type
* @module shared
* @name CustomElementWrapperDecodeOptions
* @subtitle Represents decode options
*
* @description
* This type is used for {@link customElementWrapper} service `helper.decode` method.
*
* @property {Function} [valueTransformation] A function to transform a value before equality check.
* @property {Function} [equalityCheck=_.isEqual] A function to perform equality check for.
* @property {Function} assert An assertion function to check that incoming value has correct type.
* @property {string} type A type of incoming value. Used for error message.
* @property {*} [nullValue=undefined] A null value which is used when incoming value is `null`.
*/
/**
* @ngdoc method
* @name customElementWrapper#helper.decode
*
* @description
* Helper function to decode value from Elm to values for AngularJS.
*
* @param {CustomElementWrapperDecodeOptions} options Decode options.
* Find out more {@link CustomElementWrapperDecodeOptions here}.
* @returns {Function} Function to decode incoming value.
*/
function decode(options) {
var isValueTransformationFunction = _.isFunction(options.valueTransformation);
var equalityCheck = options.equalityCheck || _.isEqual;
assert(isValueTransformationFunction || angular.isUndefined(options.valueTransformation),
'Value transformation should be a function');
assert(_.isFunction(equalityCheck), 'Equality check should be a function');
assert(_.isFunction(options.assert), 'Assertion should be a function');
assert(_.isString(options.type), 'Value type should be a string');
var nullValue;
if (options.hasOwnProperty('nullValue')) {
nullValue = options.nullValue;
}
return function (bindingName) {
assert(_.isString(bindingName), 'Binding name should be a string');
return {
/**
* Setter.
*/
set: function (value) {
var shouldDigestChanges = false;
if (value === null) {
this.$scope[bindingName] = nullValue;
shouldDigestChanges = true;
} else {
if (options.assert(value)) {
var newValue = isValueTransformationFunction ? options.valueTransformation(value) : value;
if (equalityCheck(newValue, this.$scope[bindingName]) === false) {
this.$scope[bindingName] = newValue;
shouldDigestChanges = true;
}
} else {
// Means something went wrong in the code when sending value from ELm,
// because actual type of a value does not equal to expected one.
// So we notify ourselves about it. Self compiler ;)
throw new Error('Expected a ' + options.type + ' or null, but got ' + typeof value);
}
}
if (shouldDigestChanges) {
// Let Angular digest changes
this.$scope.$evalAsync();
}
},
};
};
}
// HELPERS
/**
* Checks the assertion and if it's false then throws an error with given message,
* otherwise does nothing.
*
* @param {boolean} assertion The assertion to be checked.
* @param {string} message Message to be thrown.
*
* @usage
* assert(angular.isObject(result), 'Result should be and object');
*/
function assert(assertion, message) {
if (!assertion) {
throw new Error(message);
}
}
});