@@ -5,119 +5,120 @@ import { EventType } from './EventType';
5
5
* @class ArrayElement
6
6
*
7
7
* This class is a store of arrays of objects of one type.
8
- *
8
+ *
9
9
* @property {String } provider - The provider for data
10
10
*
11
11
* @example
12
12
* <js-array provider="provider"></js-array>
13
13
*/
14
14
export class ArrayElement extends LitElement {
15
- #data = new Array ( ) ;
16
- #newdata = new Array ( ) ;
17
- #provider = null ;
15
+ #data = [ ] ;
18
16
19
- static get localName ( ) {
20
- return 'js-array' ;
21
- }
17
+ #newdata = [ ] ;
22
18
23
- static get properties ( ) {
24
- return {
25
- provider : { type : String , reflect : true } ,
26
- select : { type : String , reflect : true } ,
27
- } ;
28
- }
19
+ #provider = null ;
29
20
30
- firstUpdated ( ) {
31
- // Set up a listener for data changes
32
- this . addEventListener ( EventType . CHANGE , ( ) => {
33
- this . requestUpdate ( ) ;
34
- } ) ;
35
- }
21
+ static get localName ( ) {
22
+ return 'js-array' ;
23
+ }
36
24
37
- render ( ) {
38
- return html `< div > Array contains ${ this . length } elements, provider=${ this . provider } </ div > ` ;
39
- }
25
+ static get properties ( ) {
26
+ return {
27
+ provider : { type : String , reflect : true } ,
28
+ select : { type : String , reflect : true } ,
29
+ } ;
30
+ }
40
31
41
- attributeChangedCallback ( name , oldVal , newVal ) {
42
- super . attributeChangedCallback ( name , oldVal , newVal ) ;
43
- if ( name === 'provider' ) {
44
- this . #providerChanged ( newVal , oldVal ) ;
45
- }
46
- }
32
+ firstUpdated ( ) {
33
+ // Set up a listener for data changes
34
+ this . addEventListener ( EventType . CHANGE , ( ) => {
35
+ this . requestUpdate ( ) ;
36
+ } ) ;
37
+ }
47
38
48
- get length ( ) {
49
- return this . #data . length ;
50
- }
39
+ render ( ) {
40
+ return html ` < div > Array contains ${ this . length } elements, provider= ${ this . provider } </ div > ` ;
41
+ }
51
42
52
- at ( index ) {
53
- return this . #data[ index ] ;
43
+ attributeChangedCallback ( name , oldVal , newVal ) {
44
+ super . attributeChangedCallback ( name , oldVal , newVal ) ;
45
+ if ( name === 'provider' ) {
46
+ this . #providerChanged( newVal , oldVal ) ;
54
47
}
48
+ }
55
49
56
- #providerChanged( newVal , oldVal ) {
57
- if ( oldVal != null && this . #provider && newVal !== oldVal ) {
58
- this . #provider. removeEventListener ( EventType . FETCH , this . #providerFetch. bind ( this ) ) ;
59
- this . #provider. removeEventListener ( EventType . OBJECT , this . #providerObject. bind ( this ) ) ;
60
- this . #provider. removeEventListener ( EventType . DONE , this . #providerDone. bind ( this ) ) ;
61
- this . #provider = null ;
62
- }
63
- if ( newVal != null && newVal !== oldVal ) {
64
- this . #provider = document . querySelector ( newVal ) ;
65
- if ( this . #provider) {
66
- this . #provider. addEventListener ( EventType . FETCH , this . #providerFetch. bind ( this ) ) ;
67
- this . #provider. addEventListener ( EventType . OBJECT , this . #providerObject. bind ( this ) ) ;
68
- this . #provider. addEventListener ( EventType . DONE , this . #providerDone. bind ( this ) ) ;
69
- } else {
70
- throw new Error ( `Provider "${ newVal } " not found` ) ;
71
- }
72
- }
73
- }
50
+ get length ( ) {
51
+ return this . #data. length ;
52
+ }
53
+
54
+ at ( index ) {
55
+ return this . #data[ index ] ;
56
+ }
74
57
75
- #providerFetch( ) {
76
- // Reset the data container
77
- this . #newdata = new Array ( ) ;
58
+ #providerChanged( newVal , oldVal ) {
59
+ if ( oldVal != null && this . #provider && newVal !== oldVal ) {
60
+ this . #provider. removeEventListener ( EventType . FETCH , this . #providerFetch. bind ( this ) ) ;
61
+ this . #provider. removeEventListener ( EventType . OBJECT , this . #providerObject. bind ( this ) ) ;
62
+ this . #provider. removeEventListener ( EventType . DONE , this . #providerDone. bind ( this ) ) ;
63
+ this . #provider = null ;
78
64
}
65
+ if ( newVal != null && newVal !== oldVal ) {
66
+ this . #provider = document . querySelector ( newVal ) ;
67
+ if ( this . #provider) {
68
+ this . #provider. addEventListener ( EventType . FETCH , this . #providerFetch. bind ( this ) ) ;
69
+ this . #provider. addEventListener ( EventType . OBJECT , this . #providerObject. bind ( this ) ) ;
70
+ this . #provider. addEventListener ( EventType . DONE , this . #providerDone. bind ( this ) ) ;
71
+ } else {
72
+ throw new Error ( `Provider "${ newVal } " not found` ) ;
73
+ }
74
+ }
75
+ }
76
+
77
+ #providerFetch( ) {
78
+ // Reset the data container
79
+ this . #newdata = [ ] ;
80
+ }
79
81
80
- #providerObject( event ) {
81
- var data = event . detail ;
82
- if ( this . select ) {
83
- if ( data instanceof Object ) {
84
- data = data [ this . select ] ;
85
- if ( data === undefined ) {
86
- throw new Error ( `Property "${ this . select } " not found in object` ) ;
87
- } else if ( Array . isArray ( data ) ) {
88
- this . #newdata = this . #newdata. concat ( data ) ;
89
- } else {
90
- this . #newdata. push ( data ) ;
91
- }
92
- }
82
+ #providerObject( event ) {
83
+ let data = event . detail ;
84
+ if ( this . select ) {
85
+ if ( data instanceof Object ) {
86
+ data = data [ this . select ] ;
87
+ if ( data === undefined ) {
88
+ throw new Error ( `Property "${ this . select } " not found in object` ) ;
89
+ } else if ( Array . isArray ( data ) ) {
90
+ this . #newdata = this . #newdata. concat ( data ) ;
93
91
} else {
94
- // Add the object to the data container
95
- this . #newdata. push ( data ) ;
92
+ this . #newdata. push ( data ) ;
96
93
}
94
+ }
95
+ } else {
96
+ // Add the object to the data container
97
+ this . #newdata. push ( data ) ;
97
98
}
99
+ }
98
100
99
- #providerDone( ) {
100
- let modified = false ;
101
- if ( this . #newdata. length !== this . #data. length ) {
102
- modified = true ;
103
- } else {
104
- for ( let i = 0 ; i < this . #newdata. length ; i ++ ) {
105
- if ( this . #newdata[ i ] !== this . #data[ i ] ) {
106
- modified = true ;
107
- break ;
108
- }
109
- }
101
+ #providerDone( ) {
102
+ let modified = false ;
103
+ if ( this . #newdata. length !== this . #data. length ) {
104
+ modified = true ;
105
+ } else {
106
+ for ( let i = 0 ; i < this . #newdata. length ; i += 1 ) {
107
+ if ( this . #newdata[ i ] !== this . #data[ i ] ) {
108
+ modified = true ;
109
+ break ;
110
110
}
111
+ }
112
+ }
111
113
112
- // Copy over the data
113
- this . #data = this . #newdata;
114
-
115
- // Emit a change event if the data was modified
116
- if ( modified ) {
117
- this . dispatchEvent ( new CustomEvent ( EventType . CHANGE , {
118
- detail : this
119
- } ) ) ;
120
- }
114
+ // Copy over the data
115
+ this . #data = this . #newdata;
121
116
117
+ // Emit a change event if the data was modified
118
+ if ( modified ) {
119
+ this . dispatchEvent ( new CustomEvent ( EventType . CHANGE , {
120
+ detail : this ,
121
+ } ) ) ;
122
122
}
123
+ }
123
124
}
0 commit comments