-
Notifications
You must be signed in to change notification settings - Fork 9
/
abstractComponent.js
815 lines (709 loc) · 20.8 KB
/
abstractComponent.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/**
* @copyright Copyright (c) 2019 Georg Ehrke
*
* @author Georg Ehrke <georg-nextcloud@ehrke.email>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import lockableTrait from '../traits/lockable.js'
import ExpectedICalJSError from '../errors/expectedICalJSError.js'
import { lc, uc, ucFirst } from '../helpers/stringHelper.js'
import { createComponent } from '../factories/icalFactory.js'
import { getConstructorForPropertyName } from '../properties/index.js'
import Property from '../properties/property.js'
import Parameter from '../parameters/parameter.js'
import observerTrait from '../traits/observer.js'
import ICAL from 'ical.js'
/**
* @class AbstractComponent
*/
export default class AbstractComponent extends observerTrait(lockableTrait(class {})) {
/**
* Constructor
*
* @param {string} name - Name of component
* @param {Property[]} properties - Array of properties stored inside the component
* @param {AbstractComponent[]} components - Array of subcomponents stored inside this component
* @param {CalendarComponent|null} root - The root of this calendar document
* @param {AbstractComponent|null} parent - The parent component of this element
*/
constructor(name, properties = [], components = [], root = null, parent = null) {
super()
/**
* Name of component
*
* @type {string}
* @private
*/
this._name = uc(name)
/**
* All properties in this component
*
* @type {Map<string, Property[]>}
* @private
*/
this._properties = new Map()
/**
* All subcomponents of this component
*
* @type {Map<string, AbstractComponent[]>}
* @private
*/
this._components = new Map()
/**
* Root node of ical document
*
* @type {CalendarComponent|null}
* @private
*/
this._root = root
/**
* Parent node
*
* @type {AbstractComponent|null}
* @private
*/
this._parent = parent
this._setPropertiesFromConstructor(properties)
this._setComponentsFromConstructor(components)
}
/**
* Get the component's name
*
* @return {string}
*/
get name() {
return this._name
}
/**
* Gets the root of this calendar-document
*
* @return {CalendarComponent}
*/
get root() {
return this._root
}
/**
* Sets the root of this calendar-document
*
* @param {CalendarComponent} root The new root element
*/
set root(root) {
this._modify()
this._root = root
for (const property of this.getPropertyIterator()) {
property.root = root
}
for (const component of this.getComponentIterator()) {
component.root = root
}
}
/**
* Gets the parent component
*
* @return {AbstractComponent}
*/
get parent() {
return this._parent
}
/**
* Sets the parent component
*
* @param {AbstractComponent} parent The new parent element
*/
set parent(parent) {
this._modify()
this._parent = parent
}
/**
* Gets the first property that matches the given propertyName
*
* @param {string} propertyName Name of the property to get
* @return {Property|null}
*/
getFirstProperty(propertyName) {
if (!this._properties.has(uc(propertyName))) {
return null
}
return this._properties.get(uc(propertyName))[0]
}
/**
* Gets the first value of the first property matching that name
*
* @param {string} propertyName Name of the property to get first value of
* @return {string | number | AbstractValue | string[] | number[] | AbstractValue[] | null}
*/
getFirstPropertyFirstValue(propertyName) {
const property = this.getFirstProperty(propertyName)
if (!property) {
return null
}
return property.getFirstValue()
}
/**
* update a property if it exists,
* create a new one if it doesn't
*
* @param {string} propertyName Name of the property to update / create
* @param {string | number | AbstractValue | string[] | number[] | AbstractValue[] | null} value The value to set
*/
updatePropertyWithValue(propertyName, value) {
this._modify()
const property = this.getFirstProperty(propertyName)
if (property) {
property.value = value
} else {
const constructor = getConstructorForPropertyName(propertyName)
const newProperty = new constructor(propertyName, value, [], this, this.root)
this.addProperty(newProperty)
}
}
/**
* Returns iterator for all properties of a given propertyName
* or if no propertyName was given over all available properties
*
* @param {string=} propertyName Name of the property to get an iterator for
*/
* getPropertyIterator(propertyName = null) {
if (propertyName) {
if (!this.hasProperty(propertyName)) {
return
}
// this._properties.get() returns an array
// [Symbol.iterator]() creates an iterator from that array
yield * this._properties.get(uc(propertyName)).slice()[Symbol.iterator]()
} else {
for (const key of this._properties.keys()) {
yield * this.getPropertyIterator(key)
}
}
}
/**
* Get all properties by name that match the given LANG parameter
*
* @param {string} propertyName The name of the property
* @param {string | null} lang The lang to query
* @private
*/
* _getAllOfPropertyByLang(propertyName, lang) {
for (const property of this.getPropertyIterator(propertyName)) {
// getParameterFirstValue will return null if language not set, so no language parameter will match lang=null
if (property.getParameterFirstValue('LANGUAGE') === lang) {
yield property
}
}
}
/**
* Get the first property by name that matches the given LANG parameter
*
* @param {string} propertyName The name of the property
* @param {string | null} lang The lang to query
* @return {Property|null}
* @private
*/
_getFirstOfPropertyByLang(propertyName, lang) {
const iterator = this._getAllOfPropertyByLang(propertyName, lang)
return iterator.next().value || null
}
/**
* Adds a property
*
* @param {Property} property The property to add
* @return {boolean}
*/
addProperty(property) {
this._modify()
property.root = this.root
property.parent = this
if (this._properties.has(property.name)) {
const arr = this._properties.get(property.name)
if (arr.indexOf(property) !== -1) {
// If the property is already part of this component,
// return false to indicate an error
return false
}
arr.push(property)
} else {
this._properties.set(property.name, [property])
}
property.subscribe(() => this._notifySubscribers())
return true
}
/**
* Checks if this component has a property of the given name
*
* @param {string} propertyName The name of the property
* @return {boolean}
*/
hasProperty(propertyName) {
return this._properties.has(uc(propertyName))
}
/**
* Removes the given property from this component
*
* @param {Property} property The property to delete
* @return {boolean}
*/
deleteProperty(property) {
this._modify()
if (!this._properties.has(property.name)) {
return false
}
const arr = this._properties.get(property.name)
const index = arr.indexOf(property)
if (index === -1) {
return false
}
if (index !== -1 && arr.length === 1) {
// If this is the last property of the given name,
// remove the entire array from _properties
// This is required for hasProperty to work properly
this._properties.delete(property.name)
} else {
arr.splice(index, 1)
}
return true
}
/**
* Removes all properties of a given name
*
* @param {string} propertyName The name of the property
* @return {boolean}
*/
deleteAllProperties(propertyName) {
this._modify()
return this._properties.delete(uc(propertyName))
}
/**
* Gets the first component of a given name
*
* @param {string} componentName The name of the component
* @return {AbstractComponent|null}
*/
getFirstComponent(componentName) {
if (!this.hasComponent(componentName)) {
return null
}
return this._components.get(uc(componentName))[0]
}
/**
* Returns iterator for all components of a given componentName
* or if no componentName was given over all available components
*
* @param {string=} componentName The name of the component
*/
* getComponentIterator(componentName) {
if (componentName) {
if (!this.hasComponent(componentName)) {
return
}
// this._components.get() returns an array
// [Symbol.iterator]() creates an iterator from that array
yield * this._components.get(uc(componentName)).slice()[Symbol.iterator]()
} else {
for (const key of this._components.keys()) {
yield * this.getComponentIterator(key)
}
}
}
/**
* Adds a new component to this component
*
* @param {AbstractComponent} component The component to add
* @return {boolean}
*/
addComponent(component) {
this._modify()
component.root = this.root
component.parent = this
if (this._components.has(component.name)) {
const arr = this._components.get(component.name)
if (arr.indexOf(component) !== -1) {
// If the property is already part of this component,
// return false to indicate an error
return false
}
arr.push(component)
} else {
this._components.set(component.name, [component])
}
component.subscribe(() => this._notifySubscribers())
return true
}
/**
* Checks if this component has a component of the given name
*
* @param {string} componentName The name of the component
* @return {boolean}
*/
hasComponent(componentName) {
return this._components.has(uc(componentName))
}
/**
* Removes the given component from this component
*
* @param {AbstractComponent} component The component to delete
* @return {boolean}
*/
deleteComponent(component) {
this._modify()
if (!this._components.has(component.name)) {
return false
}
const arr = this._components.get(component.name)
const index = arr.indexOf(component)
if (index === -1) {
return false
}
if (index !== -1 && arr.length === 1) {
// If this is the last component of the given name,
// remove the entire array from _components
// This is required for hasComponent to work properly
this._components.delete(component.name)
} else {
arr.splice(index, 1)
}
return true
}
/**
* Removes all components of a given name
*
* @param {string} componentName The name of the component
* @return {boolean}
*/
deleteAllComponents(componentName) {
this._modify()
return this._components.delete(uc(componentName))
}
/**
* Marks this parameter is immutable
* locks it against further modification
*/
lock() {
super.lock()
for (const property of this.getPropertyIterator()) {
property.lock()
}
for (const component of this.getComponentIterator()) {
component.lock()
}
}
/**
* Marks this parameter as mutable
* allowing further modification
*/
unlock() {
super.unlock()
for (const property of this.getPropertyIterator()) {
property.unlock()
}
for (const component of this.getComponentIterator()) {
component.unlock()
}
}
/**
* Creates a copy of this parameter
*
* @return {AbstractComponent}
*/
clone() {
const properties = []
for (const property of this.getPropertyIterator()) {
properties.push(property.clone())
}
const components = []
for (const component of this.getComponentIterator()) {
components.push(component.clone())
}
return new this.constructor(this.name, properties, components, this.root, this.parent)
}
/**
* Adds properties from constructor to this._properties
*
* @param {Property[]} properties Array of properties
* @private
*/
_setPropertiesFromConstructor(properties) {
for (let property of properties) {
if (Array.isArray(property)) {
const constructor = getConstructorForPropertyName(property[0])
property = new constructor(property[0], property[1])
}
this.addProperty(property)
}
}
/**
* Adds components from constructor to this._components
*
* @param {AbstractComponent[]} components Array of components
* @private
*/
_setComponentsFromConstructor(components) {
for (const component of components) {
this.addComponent(component)
}
}
/**
* Creates a new Component based on an ical object
*
* @param {ICAL.Component} icalValue The ical.js component to initialise from
* @param {CalendarComponent=} root The root of the Calendar Document
* @param {AbstractComponent=} parent The parent element of this component
* @return {AbstractComponent}
*/
static fromICALJs(icalValue, root = null, parent = null) {
if (!(icalValue instanceof ICAL.Component)) {
throw new ExpectedICalJSError()
}
const name = icalValue.name
const newComponent = new this(name, [], [], root, parent)
for (const icalProp of icalValue.getAllProperties()) {
const constructor = getConstructorForPropertyName(icalProp.name)
const property = constructor.fromICALJs(icalProp, root, newComponent)
newComponent.addProperty(property)
}
for (const icalComp of icalValue.getAllSubcomponents()) {
const constructor = this._getConstructorForComponentName(icalComp.name)
const component = constructor.fromICALJs(icalComp, root, newComponent)
newComponent.addComponent(component)
}
return newComponent
}
/**
* Gets a constructor for a give component name
*
* @param {string} componentName The name of the component
* @return {AbstractComponent}
* @protected
*/
static _getConstructorForComponentName(componentName) {
return AbstractComponent
}
/**
* turns this Component into an ICAL.js component
*
* @return {ICAL.Component}
*/
toICALJs() {
const component = createComponent(lc(this.name))
for (const prop of this.getPropertyIterator()) {
component.addProperty(prop.toICALJs())
}
for (const comp of this.getComponentIterator()) {
component.addSubcomponent(comp.toICALJs())
}
return component
}
}
/**
* Advertise properties that may at most occur once
*
* Properties, which may at most occur once, get a simple getter and setter
*
* @param {object} prototype The object's prototype
* @param {object} options The options for advertising properties
* @param {boolean} advertiseValueOnly Whether to advertise the value only or the entire property
*/
export function advertiseSingleOccurrenceProperty(prototype, options, advertiseValueOnly = true) {
options = getDefaultOncePropConfig(options)
Object.defineProperty(prototype, options.name, {
get() {
const value = this.getFirstPropertyFirstValue(options.iCalendarName)
if (!value) {
return options.defaultValue
} else {
if (Array.isArray(options.allowedValues) && !options.allowedValues.includes(value)) {
return options.unknownValue
}
return value
}
},
set(value) {
this._modify()
if (value === null) {
this.deleteAllProperties(options.iCalendarName)
return
}
if (Array.isArray(options.allowedValues) && !options.allowedValues.includes(value)) {
throw new TypeError('Illegal value')
}
this.updatePropertyWithValue(options.iCalendarName, value)
},
})
}
/**
* Advertise properties that may occur more than once
*
* Properties, which may occur more than once, won't get simple getter / setter,
* but rather a more advanced set of get{name}Iterator, get{name}List, add{name},
* remove{name} and clearAll{name} methods
*
* @param {object} prototype The object's prototype
* @param {object} options The options for advertising properties
*/
export function advertiseMultipleOccurrenceProperty(prototype, options) {
options = getDefaultMultiplePropConfig(options)
prototype['get' + ucFirst(options.name) + 'Iterator'] = function * () {
yield * this.getPropertyIterator(options.iCalendarName)
}
prototype['get' + ucFirst(options.name) + 'List'] = function() {
return Array.from(this['get' + ucFirst(options.name) + 'Iterator']())
}
prototype['remove' + ucFirst(options.name)] = function(property) {
this.deleteProperty(property)
}
prototype['clearAll' + ucFirst(options.pluralName)] = function() {
this.deleteAllProperties(options.iCalendarName)
}
}
/**
* advertises a multi-value string property enabling simple access by language
* This is used for:
* - CATEGORIES
* - RESOURCES
*
* @param {object} prototype The object's prototype
* @param {object} options The options for advertising properties
*/
export function advertiseMultiValueStringPropertySeparatedByLang(prototype, options) {
options = getDefaultMultiplePropConfig(options)
prototype['get' + ucFirst(options.name) + 'Iterator'] = function * (lang = null) {
for (const property of this._getAllOfPropertyByLang(options.iCalendarName, lang)) {
yield * property.getValueIterator()
}
}
prototype['get' + ucFirst(options.name) + 'List'] = function(lang = null) {
return Array.from(this['get' + ucFirst(options.name) + 'Iterator'](lang))
}
prototype['add' + ucFirst(options.name)] = function(value, lang = null) {
const property = this._getFirstOfPropertyByLang(options.iCalendarName, lang)
if (property) {
property.addValue(value)
} else {
const newProperty = new Property(options.iCalendarName, [value])
if (lang) {
const languageParameter = new Parameter('LANGUAGE', lang)
newProperty.setParameter(languageParameter)
}
this.addProperty(newProperty)
}
}
prototype['remove' + ucFirst(options.name)] = function(value, lang = null) {
for (const property of this._getAllOfPropertyByLang(options.iCalendarName, lang)) {
if (property.isMultiValue() && property.hasValue(value)) {
if (property.value.length === 1) {
this.deleteProperty(property)
return true
}
property.removeValue(value)
return true
}
}
return false
}
prototype['clearAll' + ucFirst(options.pluralName)] = function(lang = null) {
for (const property of this._getAllOfPropertyByLang(options.iCalendarName, lang)) {
this.deleteProperty(property)
}
}
}
/**
* advertise a component
*
* @param {object} prototype The object's prototype
* @param {object} options The options for advertising components
*/
export function advertiseComponent(prototype, options) {
options = getDefaultMultipleCompConfig(options)
prototype['get' + ucFirst(options.name) + 'Iterator'] = function * () {
yield * this.getComponentIterator(options.iCalendarName)
}
prototype['get' + ucFirst(options.name) + 'List'] = function() {
return Array.from(this['get' + ucFirst(options.name) + 'Iterator']())
}
prototype['remove' + ucFirst(options.name)] = function(component) {
this.deleteComponent(component)
}
prototype['clearAll' + ucFirst(options.pluralName)] = function() {
this.deleteAllComponents(options.iCalendarName)
}
}
/**
* Fill up the options object for advertiseProperty
*
* @param {object | string} options The options object
* @param {string} options.name Advertised name of the property
* @param {string=} options.iCalendarName The iCalendar name of the property
* @param {string[]=} options.allowedValues A list of allowed values
* @param {string | number=} options.defaultValue The default value if unset
* @param {string | number=} options.unknownValue The fallback value if unknown value
* @return {object}
*/
function getDefaultOncePropConfig(options) {
if (typeof options === 'string') {
options = {
name: options,
}
}
return Object.assign({}, {
iCalendarName: uc(options.name),
pluralName: options.name + 's',
allowedValues: null,
defaultValue: null,
unknownValue: null,
}, options)
}
/**
* Fill up the options object for advertiseProperty
*
* @param {object | string} options The options object
* @param {string} options.name Advertised name of property
* @param {string=} options.iCalendarName The iCalendar name of the property
* @param {boolean=} options.customAddMethod Whether or not to use a custom add method
* @return {object}
*/
function getDefaultMultiplePropConfig(options) {
if (typeof options === 'string') {
options = {
name: options,
}
}
return Object.assign({}, {
iCalendarName: uc(options.name),
pluralName: options.name + 's',
}, options)
}
/**
* Fill up the options object for advertiseComponent
*
* @param {object | string} options Options destructuring object
* @param {string} options.name Advertised name of component
* @param {string=} options.iCalendarName The iCalendar name of the component
* @param {boolean=} options.customAddMethod Whether or not to use a custom add method
* @return {object}
*/
function getDefaultMultipleCompConfig(options) {
if (typeof options === 'string') {
options = {
name: options,
}
}
return Object.assign({}, {
iCalendarName: 'V' + uc(options.name),
pluralName: options.name + 's',
}, options)
}