all files / src/widgets/ widgetConfigDialog.ui.js

54.55% Statements 36/66
23.81% Branches 5/21
38.89% Functions 7/18
59.32% Lines 35/59
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                                                                                                                                                                                                                       
/* 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 __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var React = require('react');
var modalDialog_ui_tsx_1 = require("../modal/modalDialog.ui.tsx");
var WidgetConfig = require("./widgetConfig");
var react_redux_1 = require("react-redux");
var settingsForm_ui_1 = require("../ui/settingsForm.ui");
var redux_form_1 = require("redux-form");
var ModalIds = require('../modal/modalDialogIds');
var react_1 = require("react");
var DIALOG_ID = ModalIds.WIDGET_CONFIG;
var FORM_ID = "widget-settings-form";
function unshiftIfNotExists(array, element, isEqual) {
    if (isEqual === void 0) { isEqual = function (a, b) { return a.id == b.id; }; }
    if (array.find(function (e) { return isEqual(e, element); }) == undefined) {
        array.unshift(element);
    }
}
exports.unshiftIfNotExists = unshiftIfNotExists;
var WidgetConfigModal = (function (_super) {
    __extends(WidgetConfigModal, _super);
    function WidgetConfigModal(props) {
        _super.call(this, props);
    }
    WidgetConfigModal.prototype.onSubmit = function (formData, dispatch) {
        dispatch(WidgetConfig.createOrUpdateWidget(this.props.widgetId, this.props.widgetType, formData));
        return true;
    };
    WidgetConfigModal.prototype.resetForm = function () {
        this.props.resetForm(FORM_ID);
    };
    WidgetConfigModal.prototype.render = function () {
        var _this = this;
        var props = this.props;
        var actions = [
            {
                className: "ui right button",
                label: "Reset",
                onClick: function () {
                    _this.resetForm();
                    return false;
                }
            },
            {
                className: "ui right red button",
                label: "Cancel",
                onClick: function () {
                    _this.resetForm();
                    return true;
                }
            },
            {
                className: "ui right labeled icon positive button",
                iconClass: "save icon",
                label: "Save",
                onClick: function () {
                    var success = _this.refs.form.submit();
                    if (success)
                        _this.resetForm();
                    return success;
                }
            }
        ];
        //const selectedWidgetPlugin = WidgetPlugins.getPlugin(this.props.widgetType) || {settings: []};
        var selectedWidgetPlugin = props.widgetPlugin;
        // TODO: Get typeInfo from selectedWidgetPlugin.typeInfo
        Eif (!selectedWidgetPlugin) {
            // TODO: Find a better (more generic way) to deal with uninitialized data for modals
            // TODO: The widgetConfig in the state is a bad idea. Solve this via state.modalDialog.data
            // This is needed for the very first time the page is rendered and the selected widget type is undefined
            return React.createElement(modalDialog_ui_tsx_1.default, {id: DIALOG_ID, title: "Configure Widget: " + props.widgetType, actions: actions}, 
                React.createElement("div", null, 
                    "Unknown WidgetType: ", 
                    props.widgetType)
            );
        }
        // Add additional fields
        var settings = selectedWidgetPlugin ? selectedWidgetPlugin.typeInfo.settings.slice() : [];
        unshiftIfNotExists(settings, {
            id: 'name',
            name: 'Name',
            type: 'string',
            defaultValue: selectedWidgetPlugin.typeInfo.name
        });
        var fields = settings.map(function (setting) { return setting.id; });
        var initialValues = settings.reduce(function (initialValues, setting) {
            if (setting.defaultValue !== undefined) {
                initialValues[setting.id] = setting.defaultValue;
            }
            return initialValues;
        }, {});
        // Overwrite with current widget props
        initialValues = Object.assign({}, initialValues, props.widgetSettings);
        return React.createElement(modalDialog_ui_tsx_1.default, {id: DIALOG_ID, title: "Configure Widget: " + selectedWidgetPlugin.typeInfo.name, actions: actions}, 
            React.createElement("div", {className: "ui one column grid"}, 
                React.createElement("div", {className: "column"}, 
                    selectedWidgetPlugin.description ?
                        React.createElement("div", {className: "ui icon message"}, 
                            React.createElement("i", {className: "idea icon"}), 
                            React.createElement("div", {className: "content"}, selectedWidgetPlugin.description))
                        : null, 
                    React.createElement(settingsForm_ui_1.default, {ref: "form", form: FORM_ID, settings: settings, onSubmit: this.onSubmit.bind(this), fields: fields.slice(), initialValues: initialValues}))
            )
        );
    };
    return WidgetConfigModal;
}(React.Component));
WidgetConfigModal.propTypes = {
    widgetId: react_1.PropTypes.string,
    resetForm: react_1.PropTypes.func.isRequired,
    widgetType: react_1.PropTypes.string,
    widgetSettings: react_1.PropTypes.object.isRequired,
    widgetPlugin: react_1.PropTypes.shape({
        id: react_1.PropTypes.string.isRequired,
        typeInfo: react_1.PropTypes.shape({
            type: react_1.PropTypes.string.isRequired,
            name: react_1.PropTypes.string.isRequired,
            settings: react_1.PropTypes.array
        })
    })
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = react_redux_1.connect(function (state) {
    return {
        widgetId: state.widgetConfig.id,
        widgetType: state.widgetConfig.type,
        widgetSettings: state.widgetConfig.settings,
        widgetPlugin: state.widgetPlugins[state.widgetConfig.type]
    };
}, function (dispatch) {
    return {
        resetForm: function (id) { return dispatch(redux_form_1.reset(id)); }
    };
})(WidgetConfigModal);