Browse Source

Handle merge buttons style

yogevbd 6 years ago
parent
commit
9c705402eb
2 changed files with 167 additions and 0 deletions
  1. 130
    0
      lib/src/commands/LayoutTreeCrawler.test.ts
  2. 37
    0
      lib/src/commands/LayoutTreeCrawler.ts

+ 130
- 0
lib/src/commands/LayoutTreeCrawler.test.ts View File

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

+ 37
- 0
lib/src/commands/LayoutTreeCrawler.ts View File

@@ -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
+      this._normalizeButtons(passedOptions.topBar.leftButtons, (buttons, style) => {
64
+        passedOptions.topBar.leftButtons = buttons;
65
+
66
+        if (staticOptions.topBar) {
67
+          this._applyButtonsStyle(staticOptions.topBar.leftButtons, style);
68
+        }
69
+      });
70
+
71
+      this._normalizeButtons(passedOptions.topBar.rightButtons, (buttons, style) => {
72
+        passedOptions.topBar.rightButtons = buttons;
73
+
74
+        if (staticOptions.topBar) {
75
+          this._applyButtonsStyle(staticOptions.topBar.rightButtons, style);
76
+        }
77
+      });
78
+    }
79
+  }
80
+
81
+  _normalizeButtons(buttons, callback) {
82
+    if (_.isPlainObject(buttons)) {
83
+      callback([], buttons);
84
+    } else {
85
+      callback(buttons);
86
+    }
87
+  }
88
+
89
+  _applyButtonsStyle(buttons, style) {
90
+    if (buttons) {
91
+      buttons.forEach((button) => {
92
+        _.merge(button, style);
93
+      });
94
+    }
95
+  }
96
+
60 97
   _assertKnownLayoutType(type) {
61 98
     if (!LayoutType[type]) {
62 99
       throw new Error(`Unknown layout type ${type}`);