all files / src/datasource/ datasourceConfigDialog.ui.js

66.67% Statements 64/96
43.9% Branches 18/41
48.15% Functions 13/27
66.67% Lines 60/90
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                                                                                                                                                                                                                                                                                                 
/* 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_1 = require('../modal/modalDialog.ui');
var Datasource = require('./datasource');
var react_redux_1 = require('react-redux');
var _ = require('lodash');
var ui = require('../ui/elements.ui');
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.DATASOURCE_CONFIG;
var FORM_ID = "datasource-settings-form";
function unshiftIfNotExists(array, element, isEqual) {
    Eif (isEqual === void 0) { isEqual = function (a, b) { return a.id == b.id; }; }
    Eif (array.find(function (e) { return isEqual(e, element); }) == undefined) {
        array.unshift(element);
    }
}
exports.unshiftIfNotExists = unshiftIfNotExists;
var DatasourceConfigModal = (function (_super) {
    __extends(DatasourceConfigModal, _super);
    function DatasourceConfigModal(props) {
        _super.call(this, props);
        this.state = {
            selectedType: ''
        };
    }
    DatasourceConfigModal.prototype.componentWillReceiveProps = function (nextProps) {
        if (nextProps.dialogData.datasource) {
            var selectedType = nextProps.dialogData.datasource.type;
            this.state = {
                selectedType: selectedType
            };
        }
    };
    DatasourceConfigModal.prototype.onSubmit = function (formData, dispatch) {
        if (this._isEditing()) {
            var id = this._getEditingDatasource().id;
            this.props.updateDatasource(id, this.state.selectedType, formData);
        }
        else {
            this.props.createDatasource(this.state.selectedType, formData);
        }
        return true;
    };
    DatasourceConfigModal.prototype.resetForm = function () {
        this.props.resetForm(FORM_ID);
    };
    DatasourceConfigModal.prototype._isEditing = function () {
        return !!this.props.dialogData.datasource;
    };
    DatasourceConfigModal.prototype._getEditingDatasource = function () {
        return this.props.dialogData.datasource;
    };
    DatasourceConfigModal.prototype.clearData = function () {
        this.props.clearData(this._getEditingDatasource().id);
    };
    DatasourceConfigModal.prototype.render = function () {
        /*
         { this._isEditing() ?
         <div className="ui right red button" onClick={(e) => this.clearData()}>
         Clear Data
         </div>
         : null }
         */
        var _this = this;
        var props = this.props;
        var actions = [
            {
                className: "ui button",
                label: "Clear Data",
                onClick: function () {
                    _this.clearData();
                    return false;
                }
            },
            {
                className: "ui right button",
                label: "Reset Form",
                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: this._isEditing() ? "Save" : "Create",
                onClick: function () {
                    var success = _this.refs.form.submit();
                    if (success)
                        _this.resetForm();
                    return success;
                }
            }
        ];
        var selectedDsPluginState;
        Iif (this.state.selectedType) {
            selectedDsPluginState = props.datasourcePlugins[this.state.selectedType];
        }
        var settings = [];
        Iif (selectedDsPluginState && selectedDsPluginState.typeInfo.settings) {
            settings = selectedDsPluginState.typeInfo.settings.slice();
        }
        else {
            settings = [];
        }
        unshiftIfNotExists(settings, {
            id: 'maxValues',
            name: 'MaxValues',
            type: 'number',
            description: "Maximum number of values stored",
            defaultValue: 100
        });
        unshiftIfNotExists(settings, {
            id: 'name',
            name: 'Name',
            type: 'string',
            defaultValue: selectedDsPluginState ? selectedDsPluginState.typeInfo.name : ""
        });
        var initialValues = {};
        Iif (this._isEditing()) {
            initialValues = Object.assign({}, this._getEditingDatasource().settings);
        }
        else {
            initialValues = settings.reduce(function (initialValues, setting) {
                Eif (setting.defaultValue !== undefined) {
                    initialValues[setting.id] = setting.defaultValue;
                }
                return initialValues;
            }, {});
        }
        var title = "Create Datasource";
        Iif (this._isEditing()) {
            title = "Edit Datasource";
        }
        return React.createElement(modalDialog_ui_1.default, {id: DIALOG_ID, title: title, actions: actions}, 
            React.createElement("div", {className: "ui one column grid"}, 
                React.createElement("div", {className: "column"}, 
                    selectedDsPluginState && selectedDsPluginState.typeInfo.description ?
                        React.createElement("div", {className: "ui icon message"}, 
                            React.createElement("i", {className: "idea icon"}), 
                            React.createElement("div", {className: "content"}, selectedDsPluginState.typeInfo.description))
                        : null, 
                    React.createElement("div", {className: "field"}, 
                        React.createElement("label", null, "Type"), 
                        React.createElement("select", {className: "ui fluid dropdown", name: "type", value: this.state.selectedType, onChange: function (e) {
                            _this.setState({ selectedType: e.target.value });
                        }}, 
                            React.createElement("option", {key: "none", value: ""}, "Select Type..."), 
                            _.valuesIn(props.datasourcePlugins).map(function (dsPlugin) {
                                return (!dsPlugin.isLoading ? React.createElement("option", {key: dsPlugin.id, value: dsPlugin.id}, dsPlugin.typeInfo.name)
                                    : null);
                            }))), 
                    React.createElement(ui.Divider, null), 
                    React.createElement(settingsForm_ui_1.default, {ref: "form", form: FORM_ID, onSubmit: this.onSubmit.bind(this), settings: settings, initialValues: initialValues}))
            )
        );
    };
    return DatasourceConfigModal;
}(React.Component));
DatasourceConfigModal.propTypes = {
    createDatasource: react_1.PropTypes.func.isRequired,
    updateDatasource: react_1.PropTypes.func.isRequired,
    resetForm: react_1.PropTypes.func.isRequired,
    dialogData: react_1.PropTypes.object.isRequired,
    datasourcePlugins: react_1.PropTypes.object.isRequired
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = react_redux_1.connect(function (state) {
    return {
        dialogData: state.modalDialog.data || {},
        datasourcePlugins: state.datasourcePlugins
    };
}, function (dispatch) {
    return {
        resetForm: function (id) { return dispatch(redux_form_1.reset(id)); },
        clearData: function (id) { return dispatch(Datasource.clearData(id)); },
        createDatasource: function (type, dsSettings) {
            dispatch(Datasource.createDatasource(type, dsSettings));
        },
        updateDatasource: function (id, type, dsSettings) {
            dispatch(Datasource.updateDatasource(id, type, dsSettings));
        }
    };
})(DatasourceConfigModal);