/* 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 Widgets = require("./widgets");
var actionNames_1 = require("../actionNames");
var Modal = require("../modal/modalDialog");
var ModalIds = require("../modal/modalDialogIds.ts");
var initialState = {
type: null,
name: null,
settings: {}
};
/**
* Triggered when the user intends to create a widget of a certain type
*/
function createWidget(type) {
return function (dispatch, getState) {
var state = getState();
var widgetPlugin = state.widgetPlugins[type];
if (!widgetPlugin.typeInfo.settings && widgetPlugin.typeInfo.settings.length > 0) {
dispatch(Widgets.createWidget(type));
return;
}
dispatch(openWidgetCreateDialog(type));
};
}
exports.createWidget = createWidget;
/**
* Creates or updates an actual widget
*/
function createOrUpdateWidget(id, type, settings) {
return function (dispatch, getState) {
var state = getState();
var widget = state.widgets[id];
if (widget && widget.type !== type) {
throw new Error("Can not update widget of type " + widget.type + " with props of type " + type);
}
if (widget) {
dispatch(Widgets.updateWidgetSettings(id, settings));
}
else {
dispatch(Widgets.createWidget(type, settings));
}
};
}
exports.createOrUpdateWidget = createOrUpdateWidget;
function openWidgetCreateDialog(type) {
return function (dispatch) {
dispatch({
type: actionNames_1.START_CREATE_WIDGET,
widgetType: type
});
dispatch(Modal.showModal(ModalIds.WIDGET_CONFIG));
};
}
exports.openWidgetCreateDialog = openWidgetCreateDialog;
/**
* Open the dialog with the settings and values of the given widget
*/
function openWidgetConfigDialog(id) {
return function (dispatch, getState) {
var state = getState();
var widget = state.widgets[id];
dispatch({
type: actionNames_1.START_CONFIGURE_WIDGET,
widget: widget
});
dispatch(Modal.showModal(ModalIds.WIDGET_CONFIG));
};
}
exports.openWidgetConfigDialog = openWidgetConfigDialog;
function widgetConfigDialog(state, action) {
if (state === void 0) { state = initialState; }
switch (action.type) {
case actionNames_1.START_CREATE_WIDGET:
return Object.assign({}, state, {
type: action.widgetType,
id: null,
name: action.widgetType,
settings: {}
});
case actionNames_1.START_CONFIGURE_WIDGET:
return Object.assign({}, state, {
type: action.widget.type,
id: action.widget.id,
name: action.widget.name,
settings: action.widget.settings
});
default:
return state;
}
}
exports.widgetConfigDialog = widgetConfigDialog;
|