all files / src/widgets/ widgetConfig.js

43.18% Statements 19/44
20% Branches 3/15
11.11% Functions 1/9
41.86% Lines 18/43
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                                                                                                                      43× 43×                               43×        
/* 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;