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 | "use strict" ;
|
28 |
|
29 |
|
30 |
|
31 | const ColumnMenu = require( './ColumnMenu.js' ) ;
|
32 | const ToggleButton = require( './ToggleButton.js' ) ;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | function ColumnMenuMulti( options ) {
|
39 |
|
40 | options = ! options ? {} : options.internal ? options : Object.create( options ) ;
|
41 | options.internal = true ;
|
42 |
|
43 |
|
44 | if ( options.value && typeof options.value === 'object' ) {
|
45 | this.setValue( options.value , true ) ;
|
46 | }
|
47 | else {
|
48 | this.value = {} ;
|
49 | }
|
50 |
|
51 | ColumnMenu.call( this , options ) ;
|
52 |
|
53 |
|
54 | if ( this.elementType === 'ColumnMenuMulti' && ! options.noDraw ) { this.draw() ; }
|
55 | }
|
56 |
|
57 | module.exports = ColumnMenuMulti ;
|
58 |
|
59 | ColumnMenuMulti.prototype = Object.create( ColumnMenu.prototype ) ;
|
60 | ColumnMenuMulti.prototype.constructor = ColumnMenuMulti ;
|
61 | ColumnMenuMulti.prototype.elementType = 'ColumnMenuMulti' ;
|
62 |
|
63 |
|
64 |
|
65 | ColumnMenuMulti.prototype.ButtonClass = ToggleButton ;
|
66 | ColumnMenuMulti.prototype.childUseParentKeyValue = true ;
|
67 |
|
68 |
|
69 |
|
70 | ColumnMenuMulti.prototype.initChildren = function() {
|
71 | ColumnMenu.prototype.initChildren.call( this ) ;
|
72 |
|
73 |
|
74 | if ( this.elementType === 'ColumnMenuMulti' ) { this.initPage() ; }
|
75 | } ;
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | ColumnMenuMulti.prototype.setValue = function( value , noDraw ) {
|
81 | if ( ! value || typeof value !== 'object' ) { return ; }
|
82 |
|
83 | if ( Array.isArray( value ) || value instanceof Set ) {
|
84 | this.value = {} ;
|
85 | for ( let key of value ) {
|
86 | if ( key && typeof key === 'string' ) {
|
87 | this.value[ key ] = true ;
|
88 | }
|
89 | }
|
90 | }
|
91 | else {
|
92 | this.value = value ;
|
93 | }
|
94 |
|
95 |
|
96 | if ( ! this.buttons ) { return ; }
|
97 |
|
98 | this.buttons.forEach( button => {
|
99 | if ( button.internalRole || ! button.key || ! ( button instanceof ToggleButton ) ) { return ; }
|
100 | button.setValue( this.value[ button.key ] ) ;
|
101 | } ) ;
|
102 |
|
103 | if ( ! noDraw ) { this.draw() ; }
|
104 | } ;
|
105 |
|
106 |
|
107 |
|
108 | ColumnMenuMulti.prototype.onKey = function( key , altKeys , data ) {
|
109 | switch( this.keyBindings[ key ] ) {
|
110 | case 'previous' :
|
111 | this.focusChild = this.focusPreviousChild( ! this.maxPage ) ;
|
112 | if ( this.focusChild === this.children[ 0 ] && this.maxPage && this.page > 0 ) {
|
113 | this.previousPage( 'cycle' ) ;
|
114 | }
|
115 | break ;
|
116 | case 'next' :
|
117 | this.focusChild = this.focusNextChild( ! this.maxPage ) ;
|
118 | if ( this.focusChild === this.children[ this.children.length - 1 ] && this.maxPage && this.page < this.maxPage ) {
|
119 | this.nextPage( 'cycle' ) ;
|
120 | }
|
121 | break ;
|
122 | case 'previousPage' :
|
123 | if ( this.maxPage && this.page > 0 ) {
|
124 | this.previousPage( 'cycle' ) ;
|
125 | }
|
126 | break ;
|
127 | case 'nextPage' :
|
128 | if ( this.maxPage && this.page < this.maxPage ) {
|
129 | this.nextPage( 'cycle' ) ;
|
130 | }
|
131 | break ;
|
132 | case 'firstPage' :
|
133 | if ( this.maxPage && this.page !== 0 ) {
|
134 | this.toPage( 0 , 'cycle' ) ;
|
135 | }
|
136 | break ;
|
137 | case 'lastPage' :
|
138 | if ( this.maxPage && this.page !== this.maxPage ) {
|
139 | this.toPage( this.maxPage , 'cycle' ) ;
|
140 | }
|
141 | break ;
|
142 | case 'submit' :
|
143 | this.emit( 'submit' , this.value , undefined , this ) ;
|
144 | break ;
|
145 | default :
|
146 | return ;
|
147 | }
|
148 |
|
149 | return true ;
|
150 | } ;
|
151 |
|
152 |
|
153 |
|
154 | ColumnMenuMulti.prototype.onButtonSubmit = function( buttonValue , action , button ) {
|
155 | switch ( button.internalRole ) {
|
156 | case 'previousPage' :
|
157 | this.previousPage() ;
|
158 | break ;
|
159 | case 'nextPage' :
|
160 | this.nextPage() ;
|
161 | break ;
|
162 | default :
|
163 | this.emit( 'submit' , this.value , action , this ) ;
|
164 | }
|
165 | } ;
|
166 |
|
167 |
|
168 |
|
169 | ColumnMenuMulti.prototype.onButtonToggle = function( buttonValue , action , button ) {
|
170 | if ( ! button.key ) { return ; }
|
171 | if ( buttonValue ) { this.value[ button.key ] = true ; }
|
172 | else { delete this.value[ button.key ] ; }
|
173 | } ;
|
174 |
|