Parcourir la source

Handle merge buttons style

yogevbd il y a 6 ans
Parent
révision
e682776ca4
2 fichiers modifiés avec 159 ajouts et 0 suppressions
  1. 122
    0
      lib/src/commands/LayoutTreeCrawler.test.ts
  2. 37
    0
      lib/src/commands/LayoutTreeCrawler.ts

+ 122
- 0
lib/src/commands/LayoutTreeCrawler.test.ts Voir le fichier

@@ -104,6 +104,128 @@ describe('LayoutTreeCrawler', () => {
104 104
     });
105 105
   });
106 106
 
107
+  it('Components: extract button style from passedOptions buttons array and merge it to all buttons', () => {
108
+    const MyComponent = class {
109
+      static get options() {
110
+        return {
111
+          topBar: {
112
+            leftButtons: [{
113
+              id: 'id'
114
+            }],
115
+            rightButtons: [{
116
+              id: 'id2'
117
+            }]
118
+          }
119
+        };
120
+      }
121
+    };
122
+
123
+    const passedOptions = {
124
+      topBar: {
125
+        leftButtons: [{
126
+          font: 'Helvetica'
127
+        }],
128
+        rightButtons: []
129
+      }
130
+    };
131
+
132
+    const node = { type: LayoutType.Component, data: { name: 'theComponentName', options: passedOptions } };
133
+    store.setOriginalComponentClassForName('theComponentName', MyComponent);
134
+
135
+    uut.crawl(node);
136
+
137
+    expect(node.data.options).toEqual({
138
+      topBar: {
139
+        leftButtons: [{
140
+          id: 'id',
141
+          font: 'Helvetica'
142
+        }],
143
+        rightButtons: [{
144
+          id: 'id2'
145
+        }]
146
+      }
147
+    });
148
+  });
149
+
150
+  it('Components: empty buttons array should not affect static buttons', () => {
151
+    const MyComponent = class {
152
+      static get options() {
153
+        return {
154
+          topBar: {
155
+            leftButtons: [{
156
+              id: 'id'
157
+            }],
158
+            rightButtons: [{
159
+              id: 'id2'
160
+            }]
161
+          }
162
+        };
163
+      }
164
+    };
165
+
166
+    const passedOptions = {
167
+      topBar: {
168
+        leftButtons: [{
169
+          id: 'id'
170
+        }],
171
+        rightButtons: []
172
+      }
173
+    };
174
+
175
+    const node = { type: LayoutType.Component, data: { name: 'theComponentName', options: passedOptions } };
176
+    store.setOriginalComponentClassForName('theComponentName', MyComponent);
177
+
178
+    uut.crawl(node);
179
+
180
+    expect(node.data.options).toEqual({
181
+      topBar: {
182
+        leftButtons: [{
183
+          id: 'id'
184
+        }],
185
+        rightButtons: [{
186
+          id: 'id2'
187
+        }]
188
+      }
189
+    });
190
+  });
191
+
192
+  it('Components: undefined passed buttons should not affect static buttons', () => {
193
+    const MyComponent = class {
194
+      static get options() {
195
+        return {
196
+          topBar: {
197
+            leftButtons: [{
198
+              id: 'id'
199
+            }],
200
+            rightButtons: [{
201
+              id: 'id2'
202
+            }]
203
+          }
204
+        };
205
+      }
206
+    };
207
+
208
+    const passedOptions = {
209
+      topBar: {}
210
+    };
211
+
212
+    const node = { type: LayoutType.Component, data: { name: 'theComponentName', options: passedOptions } };
213
+    store.setOriginalComponentClassForName('theComponentName', MyComponent);
214
+
215
+    uut.crawl(node);
216
+
217
+    expect(node.data.options).toEqual({
218
+      topBar: {
219
+        leftButtons: [{
220
+          id: 'id'
221
+        }],
222
+        rightButtons: [{
223
+          id: 'id2'
224
+        }]
225
+      }
226
+    });
227
+  });
228
+
107 229
   it('Component: deepClones options', () => {
108 230
     const theStyle = {};
109 231
     const MyComponent = class {

+ 37
- 0
lib/src/commands/LayoutTreeCrawler.ts Voir le fichier

@@ -54,9 +54,46 @@ export class LayoutTreeCrawler {
54 54
     const clazz = this.store.getOriginalComponentClassForName(node.data.name) || {};
55 55
     const staticOptions = _.cloneDeep(clazz.options) || {};
56 56
     const passedOptions = node.data.options || {};
57
+    this._mergeButtonsStyles(passedOptions, staticOptions);
57 58
     node.data.options = _.merge({}, staticOptions, passedOptions);
58 59
   }
59 60
 
61
+  _mergeButtonsStyles(passedOptions, staticOptions) {
62
+    if (passedOptions.topBar) {
63
+      const leftButtons = passedOptions.topBar.leftButtons;
64
+      if (leftButtons) {
65
+        const leftButtonsStyle = this._extractButtonsStyle(leftButtons);
66
+        this._applyButtonsStyle(staticOptions.topBar.leftButtons, leftButtonsStyle);
67
+        this._applyButtonsStyle(passedOptions.topBar.leftButtons, leftButtonsStyle);
68
+      }
69
+
70
+      const rightButtons = passedOptions.topBar.rightButtons;
71
+      if (rightButtons) {
72
+        const rightButtonsStyle = this._extractButtonsStyle(rightButtons);
73
+        this._applyButtonsStyle(staticOptions.topBar.rightButtons, rightButtonsStyle);
74
+        this._applyButtonsStyle(passedOptions.topBar.rightButtons, rightButtonsStyle);
75
+      }
76
+    }
77
+  }
78
+
79
+  _extractButtonsStyle(buttons) {
80
+    return buttons.find((button) => {
81
+      if (button.id === undefined) {
82
+        const index = buttons.indexOf(button);
83
+        buttons.splice(index, 1);
84
+        return button;
85
+      } else {
86
+        return undefined;
87
+      }
88
+    });
89
+  }
90
+
91
+  _applyButtonsStyle(buttons, style) {
92
+    buttons.forEach((button) => {
93
+      _.merge(button, style);
94
+    });
95
+  }
96
+
60 97
   _assertKnownLayoutType(type) {
61 98
     if (!LayoutType[type]) {
62 99
       throw new Error(`Unknown layout type ${type}`);