| 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
7×
7×
7×
7×
6×
7×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
1×
1×
1×
43×
43×
43×
2×
2×
2×
2×
41×
1×
1×
8×
1×
11×
7×
1×
1×
1×
1×
2×
2×
1×
1×
1×
1×
1×
1×
2×
2×
1×
3×
3×
36×
3×
2×
2×
2×
2×
14×
2×
3×
3×
36×
3×
1×
| /* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var Uuid = require("../util/uuid.js");
var _ = require("lodash");
var reducer_js_1 = require("../util/reducer.js");
var Action = require("../actionNames");
exports.HEADER_HEIGHT = 35;
exports.ROW_HEIGHT = 100;
exports.initialWidgets = {
"initial_chart": {
"id": "initial_chart",
"type": "chart",
"settings": {
"name": "Random Values",
"datasource": "initial_random_source",
"chartType": "area-spline",
"dataKeys": "[\"value\"]",
"xKey": "x",
"names": "{\"value\": \"My Value\"}",
"gaugeData": "{\"min\":0,\"max\":100,\"units\":\" %\"}"
},
"row": 0,
"col": 0,
"width": 6,
"height": 2,
"availableHeightPx": 123
},
"initial_text": {
"id": "initial_text",
"type": "text",
"settings": {
"name": "Random data",
"datasource": "initial_random_source"
},
"row": 0,
"col": 6,
"width": 6,
"height": 3,
"availableHeightPx": 223
},
"106913f4-44fb-4f69-ab89-5d5ae857cf3c": {
"id": "106913f4-44fb-4f69-ab89-5d5ae857cf3c",
"type": "chart",
"settings": {
"name": "Random Values",
"datasource": "initial_random_source",
"chartType": "spline",
"dataKeys": "[\"value\", \"value2\"]",
"xKey": "x",
"names": "{\"value\": \"My Value\"}",
"gaugeData": "{\"min\":0,\"max\":100,\"units\":\" %\"}"
},
"row": 2,
"col": 0,
"width": 6,
"height": 2,
"availableHeightPx": 123
}
};
/* // TODO: better explicitly create initial state? But when? ...
export function createInitialWidgets() {
return function(dispatch: AppState.Dispatch) {
dispatch(addWidget('chart', {
"name": "Random Values",
"datasource": "initial_random_source",
"chartType": "area-spline",
"dataKeys": "[\"value\"]",
"xKey": "x",
"names": "{\"value\": \"My Value\"}",
"gaugeData": "{\"min\":0,\"max\":100,\"units\":\" %\"}"
}, 0, 0, 6, 2));
dispatch(addWidget('text', {
"name": "Random data",
"datasource": "initial_random_source"
}, 0, 6, 6, 3));
dispatch(addWidget('text', {
"name": "Bars",
"datasource": "initial_random_source",
"chartType": "spline",
"dataKeys": "[\"value\", \"value2\"]",
"xKey": "x",
"names": "{\"value\": \"My Value\"}",
"gaugeData": "{\"min\":0,\"max\":100,\"units\":\" %\"}"
}, 2, 0, 6, 2));
}
}
*/
function createWidget(widgetType, widgetSettings) {
return function (dispatch, getState) {
var widgets = getState().widgets;
var widgetPositions = calcNewWidgetPosition(widgets);
return dispatch(addWidget(widgetType, widgetSettings, widgetPositions.row, widgetPositions.col));
};
}
exports.createWidget = createWidget;
function addWidget(widgetType, widgetSettings, row, col, width, height, id) {
Iif (widgetSettings === void 0) { widgetSettings = {}; }
if (width === void 0) { width = 3; }
if (height === void 0) { height = 3; }
if (!id) {
id = Uuid.generate();
}
return {
type: Action.ADD_WIDGET,
id: id,
col: col,
row: row,
width: width,
height: height,
widgetType: widgetType,
widgetSettings: widgetSettings
};
}
exports.addWidget = addWidget;
function updateWidgetSettings(id, widgetSettings) {
return {
type: Action.UPDATE_WIDGET_SETTINGS,
id: id,
widgetSettings: widgetSettings
};
}
exports.updateWidgetSettings = updateWidgetSettings;
function updatedSingleSetting(id, settingId, settingValue) {
return {
type: Action.UPDATED_SINGLE_WIDGET_SETTING,
id: id,
settingId: settingId,
settingValue: settingValue
};
}
exports.updatedSingleSetting = updatedSingleSetting;
function deleteWidget(id) {
return {
type: Action.DELETE_WIDGET,
id: id
};
}
exports.deleteWidget = deleteWidget;
function updateLayout(layouts) {
return {
type: Action.UPDATE_WIDGET_LAYOUT,
layouts: layouts
};
}
exports.updateLayout = updateLayout;
var widgetsCrudReducer = reducer_js_1.genCrudReducer([Action.ADD_WIDGET, Action.DELETE_WIDGET], widget);
function widgets(state, action) {
if (state === void 0) { state = exports.initialWidgets; }
state = widgetsCrudReducer(state, action);
switch (action.type) {
case Action.UPDATE_WIDGET_LAYOUT:
return _.valuesIn(state)
.reduce(function (newState, _a) {
var id = _a.id;
newState[id] = widget(newState[id], action);
return newState;
}, _.assign({}, state));
case Action.LOAD_LAYOUT:
console.assert(action.layout.widgets, "Layout is missing Widgets, id: " + action.layout.id);
return action.layout.widgets || {};
case Action.DELETE_WIDGET_PLUGIN:
var toDelete = _.valuesIn(state).filter(function (widgetState) {
return widgetState.type === action.id;
});
var newState_1 = _.assign({}, state);
toDelete.forEach(function (widgetState) {
delete newState_1[widgetState.id];
});
return newState_1;
default:
return state;
}
}
exports.widgets = widgets;
function calcAvaliableHeight(heightUnits) {
// The 10 px extra seem to be based on a bug in the grid layout ...
return (heightUnits * (exports.ROW_HEIGHT + 10)) - exports.HEADER_HEIGHT - 10;
}
function widget(state, action) {
switch (action.type) {
case Action.ADD_WIDGET:
return {
id: action.id,
type: action.widgetType,
settings: action.widgetSettings,
row: action.row,
col: action.col,
width: action.width,
height: action.height,
availableHeightPx: calcAvaliableHeight(action.height)
};
case Action.UPDATE_WIDGET_SETTINGS:
return _.assign({}, state, { settings: action.widgetSettings });
case Action.UPDATED_SINGLE_WIDGET_SETTING: {
var newSettings = _.clone(state.settings);
newSettings[action.settingId] = action.settingValue;
return _.assign({}, state, { settings: newSettings });
}
case Action.UPDATE_WIDGET_LAYOUT:
var layout = layoutById(action.layouts, state.id);
if (layout == null) {
console.warn("No layout for widget. Skipping position update of widget with id: " + state.id);
return state;
}
var heightInPx = calcAvaliableHeight(layout.h);
// Only change state when something actually changed!
Eif (state.row !== layout.y ||
state.col !== layout.x ||
state.width !== layout.w ||
state.height !== layout.h ||
state.availableHeightPx !== heightInPx) {
return _.assign({}, state, {
row: layout.y,
col: layout.x,
width: layout.w,
height: layout.h,
availableHeightPx: heightInPx
});
}
else {
return state;
}
default:
return state;
}
}
// Local functions
function layoutById(layout, id) {
return _.find(layout, function (l) {
return l.i === id;
});
}
function calcNewWidgetPosition(widgets) {
var colHeights = {};
// TODO: Replace 12 with constant for number of columns
// This is different on different breaking points...
for (var i = 0; i < 12; i++) {
colHeights[i] = 0;
}
colHeights = _.valuesIn(widgets).reduce(function (prev, curr) {
prev[curr.col] = prev[curr.col] || 0;
var currHeight = curr.row + curr.height || 0;
Eif (prev[curr.col] < currHeight) {
for (var i = curr.col; i < curr.col + curr.width; i++) {
prev[i] = currHeight;
}
}
return prev;
}, colHeights);
var heights = _.valuesIn(colHeights);
var col = _.keysIn(colHeights).reduce(function (a, b, index, array) {
return Number(colHeights[a] <= colHeights[b] ? a : b);
}, 0);
//Math.min(...colHeights);
return { col: col, row: Math.min.apply(Math, heights) };
}
exports.calcNewWidgetPosition = calcNewWidgetPosition;
|