@@ -2,125 +2,177 @@ import actions from 'actions';
22
33export default store => callback => {
44 const state = store . getState ( ) ;
5- const headerGroups = [ 'default' , 'tools' ] ;
6- let header = Object . create ( Header ) . initialize ( state ) ;
5+ const headerGroups = Object . keys ( state . viewer . headers ) ;
6+ let header = Object . create ( Header ) . initialize ( state . viewer , headerGroups ) ;
77
88 callback ( header ) ;
99 headerGroups . forEach ( headerGroup => {
10- store . dispatch ( actions . setHeaderItems ( headerGroup , [ ...header . getItems ( headerGroup ) ] ) ) ;
10+ store . dispatch ( actions . setHeaderItems ( headerGroup , [ ...header . headers [ headerGroup ] ] ) ) ;
1111 } ) ;
1212} ;
1313
1414const Header = {
15- initialize ( state ) {
16- this . state = state ;
17- this . headerGroups = [ 'default' , 'tools' ] ;
15+ initialize ( viewerState ) {
16+ this . headers = viewerState . headers ;
17+ this . toolButtonObjects = viewerState . toolButtonObjects ;
1818 this . headerGroup = 'default' ;
19- this . headers = state . viewer . headers ;
20- this . items = this . headers [ this . headerGroup ] ;
2119 this . index = - 1 ;
2220
2321 return this ;
2422 } ,
25- getHeader ( headerGroup ) {
26- if ( this . headerGroups . includes ( headerGroup ) ) {
27- this . update ( this . items ) ;
28- this . headerGroup = headerGroup ;
29- this . items = this . headers [ headerGroup ] ;
23+ get ( dataElement ) {
24+ if ( this . index !== - 1 ) {
25+ // get(dataElement) has been called before so we need to reset this
26+ const item = this . headers [ this . headerGroup ] [ this . index ] ;
27+ Object . keys ( item ) . forEach ( key => {
28+ delete this [ key ] ;
29+ } ) ;
30+ }
31+
32+ this . _setIndex ( dataElement ) ;
33+
34+ if ( this . index === - 1 ) {
35+ console . warn ( `${ dataElement } does not exist in ${ this . headerGroup } header` ) ;
3036 } else {
31- console . warn ( 'Header must be either default or tools' ) ;
37+ const item = this . headers [ this . headerGroup ] [ this . index ] ;
38+ Object . keys ( item ) . forEach ( key => this [ key ] = item [ key ] ) ;
3239 }
33-
40+
3441 return this ;
3542 } ,
36- get ( dataElement ) {
37- this . index = this . getIndexOfElement ( dataElement ) ;
38-
39- if ( ! this . dataElementExists ( ) ) {
40- console . warn ( `${ dataElement } does not exist in current header` ) ;
41- }
43+ getItems ( ) {
44+ return this . headers [ this . headerGroup ] ;
45+ } ,
46+ getHeader ( headerGroup ) {
47+ const headerGroups = Object . keys ( this . headers ) ;
4248
49+ if ( headerGroups . includes ( headerGroup ) ) {
50+ this . headerGroup = headerGroup ;
51+ this . _resetIndex ( ) ;
52+ } else {
53+ console . warn ( `Header must be one of: ${ headerGroups . join ( ' or ' ) } .` ) ;
54+ }
55+
4356 return this ;
4457 } ,
4558 insertBefore ( newItem ) {
46- if ( ! this . dataElementExists ( ) ) {
47- console . warn ( 'Please use .get(dataElement) first before use insertBefore' ) ;
59+ if ( this . index === - 1 ) {
60+ console . warn ( 'Please use .get(dataElement) first before using insertBefore' ) ;
4861 } else {
49- this . items . splice ( this . index , 0 , newItem ) ;
62+ this . headers [ this . headerGroup ] . splice ( this . index , 0 , newItem ) ;
5063 }
5164
52- return this . update ( this . items ) ;
65+ return this ;
5366 } ,
5467 insertAfter ( newItem ) {
55- if ( ! this . dataElementExists ( ) ) {
56- console . warn ( 'Please use .get(dataElement) first before use insertAfter' ) ;
68+ if ( this . index === - 1 ) {
69+ console . warn ( 'Please use .get(dataElement) first before using insertAfter' ) ;
5770 } else {
58- this . items . splice ( this . index + 1 , 0 , newItem ) ;
71+ this . index ++ ;
72+ this . headers [ this . headerGroup ] . splice ( this . index , 0 , newItem ) ;
5973 }
6074
61- return this . update ( this . items ) ;
75+ return this ;
6276 } ,
63- delete ( dataElement ) {
64- const index = this . getIndexOfElement ( dataElement ) ;
65- const dataElementExists = index !== - 1 ;
77+ delete ( arg ) {
78+ let index ;
6679
67- if ( ! dataElementExists ) {
68- console . warn ( `${ dataElement } does not exist in current header` ) ;
80+ if ( typeof arg === 'number' ) {
81+ index = arg ;
82+ } else if ( typeof arg === 'string' ) {
83+ if ( this . _getIndexOfElement ( arg ) === - 1 ) {
84+ console . warn ( `${ arg } does not exist in ${ this . headerGroup } header` ) ;
85+ } else {
86+ index = this . _getIndexOfElement ( arg ) ;
87+ }
88+ } else if ( typeof arg === 'undefined' ) {
89+ if ( this . index === - 1 ) {
90+ console . warn ( 'Please use .get(dataElement) first before using delete()' ) ;
91+ } else {
92+ index = this . index ;
93+ }
94+ } else if ( Array . isArray ( arg ) ) {
95+ arg . forEach ( arg => {
96+ if ( typeof arg === 'number' || typeof arg === 'string' ) {
97+ this . delete ( arg ) ;
98+ }
99+ } ) ;
69100 } else {
70- this . deleteElementAtIndex ( index ) ;
101+ console . warn ( 'Argument must be empty, a number, a string or an array' ) ;
71102 }
72103
73- return this . update ( this . items ) ;
74- } ,
75- deleteElementAtIndex ( index ) {
76- if ( index === this . index ) {
77- this . index = - 1 ;
104+ if ( index ) {
105+ this . headers [ this . headerGroup ] . splice ( index , 1 ) ;
106+ this . _resetIndex ( ) ;
78107 }
79- this . items . splice ( index , 1 ) ;
80108 } ,
81109 shift ( ) {
82- this . items . shift ( ) ;
83-
84- return this . update ( this . items ) ;
110+ this . headers [ this . headerGroup ] . shift ( ) ;
111+
112+ return this ;
85113 } ,
86- unshift ( ... newItems ) {
87- this . items . unshift ( ... newItems ) ;
114+ unshift ( newItem ) {
115+ this . headers [ this . headerGroup ] . unshift ( newItem ) ;
88116
89- return this . update ( this . items ) ;
117+ return this ;
90118 } ,
91- push ( ... newItems ) {
92- this . items . push ( ... newItems ) ;
119+ push ( newItem ) {
120+ this . headers [ this . headerGroup ] . push ( newItem ) ;
93121
94- return this . update ( this . items ) ;
122+ return this ;
95123 } ,
96124 pop ( ) {
97- this . items . pop ( ) ;
98-
99- return this . update ( this . items ) ;
100- } ,
101- update ( items ) {
102- this . items = items ;
103- this . headers [ this . headerGroup ] = items ;
125+ this . headers [ this . headerGroup ] . pop ( ) ;
104126
105127 return this ;
106128 } ,
107- getIndexOfElement ( dataElement ) {
108- return this . items . findIndex ( item => {
129+ /* rethink update later */
130+ // update(arg) {
131+ // if (typeof arg === 'object') {
132+ // this._updateItem(arg);
133+ // } else if (Array.isArray(arg)) {
134+ // this._updateItems(arg);
135+ // } else {
136+ // console.warn('Argument must be either an object or an array');
137+ // }
138+
139+ // return this;
140+ // },
141+ // _updateItem(obj) {
142+ // if (this.index === -1) {
143+ // console.warn('Please use .get(dataElement) first before using update');
144+ // } else {
145+ // const item = this.headers[this.headerGroup][this.index];
146+
147+ // Object.keys(obj).forEach(key => {
148+ // item[key] = obj[key];
149+ // });
150+ // }
151+
152+ // return this;
153+ // },
154+ // _updateItems(items) {
155+ // this.headers[this.headerGroup] = items;
156+
157+ // return this;
158+ // },
159+ _setIndex ( dataElement ) {
160+ this . index = this . _getIndexOfElement ( dataElement ) ;
161+ } ,
162+ _getIndexOfElement ( dataElement ) {
163+ return this . headers [ this . headerGroup ] . findIndex ( item => {
109164 let dataElementOfItem ;
110165
111166 if ( item . type === 'toolButton' ) {
112- dataElementOfItem = this . state . viewer . toolButtonObjects [ item . toolName ] . dataElement ;
167+ dataElementOfItem = this . toolButtonObjects [ item . toolName ] . dataElement ;
113168 } else {
114169 dataElementOfItem = item . dataElement ;
115170 }
116171
117172 return dataElementOfItem === dataElement ;
118- } ) ;
173+ } ) ;
119174 } ,
120- getItems ( headerGroup = this . headerGroup ) {
121- return [ ...this . headers [ headerGroup ] ] ;
122- } ,
123- dataElementExists ( ) {
124- return this . index !== - 1 ;
175+ _resetIndex ( ) {
176+ this . index = - 1 ;
125177 }
126178} ;
0 commit comments