all files / src/pluginApi/ pluginsDialog.ui.tsx

40.23% Statements 70/174
18.42% Branches 7/38
16.67% Functions 13/78
43.4% Lines 69/159
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
/* 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 react_redux_1 = require("react-redux");
var _ = require("lodash");
var Modal = require("../modal/modalDialog.js");
var Config = require("../config");
var Plugins = require("../pluginApi/pluginLoader");
var WidgetsPlugins = require("../widgets/widgetPlugins");
var DatasourcePlugins = require("../datasource/datasourcePlugins");
var PluginsModal = (function (_super) {
    __extends(PluginsModal, _super);
    function PluginsModal(props) {
        _super.call(this, props);
        this.state = {
            pluginUrl: "",
            isSearchOpen: false
        };
    }
    PluginsModal.prototype.pluginSearchValueChange = function (e) {
        var url = this.pluginUrlValue();
        if (this.isUrl(url)) {
            this.setState({ isSearchOpen: false });
        }
        else {
            this.setState({ isSearchOpen: true });
        }
        this.setState({ pluginUrl: this.pluginUrlValue() });
    };
    PluginsModal.prototype.onBlurPluginSearchInput = function (e) {
        var _this = this;
        setTimeout(function () {
            _this.setState({ isSearchOpen: false });
        }, 300);
    };
    PluginsModal.prototype.isUrl = function (url) {
        return url === '' || _.startsWith(url, ".") || _.startsWith(url, "/") || _.startsWith(url, "http:") || _.startsWith(url, "https:");
    };
    PluginsModal.prototype.onFocusPluginSearchInput = function (e) {
        var url = this.pluginUrlValue();
        if (this.isUrl(url)) {
            this.setState({ isSearchOpen: false });
        }
        else {
            this.setState({ isSearchOpen: true });
        }
    };
    PluginsModal.prototype.pluginUrlValue = function () {
        var pluginUrlInput = this.refs['pluginUrl'];
        return _.trim(pluginUrlInput.value);
    };
    PluginsModal.prototype.clearUrl = function () {
        var pluginUrlInput = this.refs['pluginUrl'];
        pluginUrlInput.value = "";
        this.setState({ pluginUrl: "" });
    };
    PluginsModal.prototype.render = function () {
        var _this = this;
        var props = this.props;
        var actions = [
            {
                className: "ui right labeled icon positive button",
                iconClass: "save icon",
                label: "Close",
                onClick: function () {
                    props.closeDialog();
                }
            }
        ];
        var datasourcePluginStates = _.valuesIn(props.datasourcePlugins);
        var widgetPluginStates = _.valuesIn(props.widgetPlugins);
        var pluginUrlInput = this.refs['pluginUrl']; // HTMLInputElement
        return React.createElement(modalDialog_ui_1.default, {id: "plugins-dialog", title: "Plugin Manager", actions: actions}, 
            React.createElement("div", {className: "slds-grid"}, 
                React.createElement("div", {className: "slds-size--1-of-1"}, 
                    React.createElement("h2", {className: "slds-section-title--divider slds-m-bottom--medium"}, 
                        "Load Plugin ", 
                        React.createElement(PluginRegistrySettings, {pluginRegistryApiKey: props.pluginRegistryApiKey, pluginRegistryUrl: props.pluginRegistryUrl, onApiKeyChanged: function (key) { return _this.props.setConfigValue("pluginRegistryApiKey", key); }, onRegistryUrlChanged: function (url) { return _this.props.setConfigValue("pluginRegistryUrl", url); }})), 
                    React.createElement("form", {className: "slds-form--inline slds-grid", onSubmit: function (e) {
                        props.loadPlugin(pluginUrlInput.value);
                        _this.clearUrl();
                        e.preventDefault();
                    }}, 
                        React.createElement("div", {className: "slds-form-element slds-has-flexi-truncate slds-lookup" + (this.state.isSearchOpen ? " slds-is-open" : ""), "data-select": "single"}, 
                            React.createElement("div", {className: "slds-form-element__control slds-size--1-of-1"}, 
                                React.createElement("div", {className: "slds-input-has-icon slds-input-has-icon--right"}, 
                                    React.createElement("svg", {"aria-hidden": "true", className: "slds-input__icon"}, 
                                        React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#search"})
                                    ), 
                                    React.createElement("input", {className: "slds-lookup__search-input slds-input", type: "search", placeholder: "URL or Id from Plugin Registry", id: "plugin-lookup-menu", ref: "pluginUrl", autoComplete: (this.state.isSearchOpen ? " off" : "on"), defaultValue: "", onChange: function (e) { return _this.pluginSearchValueChange(e); }, onBlur: function (e) { return _this.onBlurPluginSearchInput(e); }, onFocus: function (e) { return _this.onFocusPluginSearchInput(e); }, "aria-owns": "plugin-lookup-menu", role: "combobox", "aria-activedescendent": "", "aria-expanded": (this.state.isSearchOpen ? "true" : "false"), "aria-autocomplete": "list"}))
                            ), 
                            React.createElement(LookupMenu, {id: "plugin-lookup-menu", searchString: this.state.pluginUrl, pluginRegistryUrl: this.props.pluginRegistryUrl, onItemClicked: function (item) { return props.loadPlugin('plugin://' + item.type); }})), 
                        React.createElement("div", {className: "slds-form-element slds-no-flex"}, 
                            React.createElement("button", {className: "slds-button slds-button--brand", type: "submit", tabIndex: 0}, "Load Plugin")
                        )), 
                    React.createElement("h4", {className: "slds-section-title--divider slds-m-top--medium slds-m-bottom--medium"}, "Datasource Plugins (Installed)"), 
                    React.createElement("div", {className: "slds-grid slds-grid--vertical-stretch slds-wrap slds-has-dividers--around-space"}, datasourcePluginStates.map(function (dsState) {
                        return React.createElement(DatasourcePluginTile, {key: dsState.id, pluginId: dsState.id});
                    })), 
                    React.createElement("h4", {className: "slds-section-title--divider slds-m-top--medium slds-m-bottom--medium"}, "Widget Plugins (Installed)"), 
                    React.createElement("div", {className: "slds-grid slds-grid--vertical-stretch slds-wrap slds-has-dividers--around-space"}, widgetPluginStates.map(function (dsState) {
                        return React.createElement(WidgetPluginTile, {key: dsState.id, pluginId: dsState.id});
                    })))
            )
        );
    };
    return PluginsModal;
}(React.Component));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = react_redux_1.connect(function (state) {
    return {
        widgetPlugins: state.widgetPlugins,
        datasourcePlugins: state.datasourcePlugins,
        pluginRegistryApiKey: state.config.pluginRegistryApiKey,
        pluginRegistryUrl: state.config.pluginRegistryUrl
    };
}, function (dispatch) {
    return {
        closeDialog: function () { return dispatch(Modal.closeModal()); },
        // TODO: Render loading indicator while Plugin loads
        // maybe build some generic solution for Ajax calls where the state can hold all information to render loading indicators / retry buttons etc...
        loadPlugin: function (url) { return dispatch(Plugins.startLoadingPluginFromUrl(url)); },
        setConfigValue: function (key, value) { return dispatch(Config.setConfigValue(key, value)); }
    };
})(PluginsModal);
var PluginTileProps = (function () {
    function PluginTileProps() {
    }
    return PluginTileProps;
}());
var PluginTile = (function (_super) {
    __extends(PluginTile, _super);
    function PluginTile(props) {
        _super.call(this, props);
        this.state = { actionMenuOpen: false };
    }
    PluginTile.prototype._copyUrl = function () {
        var urlInput = this.refs['url'];
        urlInput.focus();
        urlInput.select();
        document.execCommand('copy');
    };
    PluginTile.prototype.toggleActionMenu = function () {
        this.setState({ actionMenuOpen: !this.state.actionMenuOpen });
    };
    PluginTile.prototype.closeActionMenu = function () {
        this.setState({ actionMenuOpen: false });
    };
    PluginTile.prototype.render = function () {
        var _this = this;
        var props = this.props;
        var pluginState = props.pluginState;
        var description = pluginState.typeInfo.description ? pluginState.typeInfo.description : "No Description.";
        var url = pluginState.url ? pluginState.url : "Packaged";
        return React.createElement("div", {className: "slds-tile slds-item slds-size--1-of-5 slds-m-around--x-small", style: { marginTop: "0.5rem" }}, 
            React.createElement("div", {className: "slds-grid slds-grid--align-spread slds-has-flexi-truncate slds-m-bottom--x-small"}, 
                React.createElement("h3", {className: "slds-text-heading--medium"}, pluginState.typeInfo.name), 
                React.createElement("div", {className: "slds-shrink-none slds-dropdown-trigger slds-dropdown-trigger--click" + (this.state.actionMenuOpen ? " slds-is-open" : "")}, 
                    React.createElement("button", {className: "slds-button slds-button--icon-border-filled slds-button--icon-x-small", "aria-haspopup": "true", onClick: function () { return _this.toggleActionMenu(); }, onBlur: function () { return setTimeout(function () { return _this.closeActionMenu(); }, 200); }}, 
                        React.createElement("svg", {"aria-hidden": "true", className: "slds-button__icon slds-button__icon--hint"}, 
                            React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#down"})
                        ), 
                        React.createElement("span", {className: "slds-assistive-text"}, "Actions")), 
                    React.createElement("div", {className: "slds-dropdown slds-dropdown--left slds-dropdown--actions", style: { zIndex: 9003 }}, 
                        React.createElement("ul", {className: "dropdown__list", role: "menu"}, 
                            React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                                React.createElement("a", {href: "javascript:void(0);", role: "menuitem", tabIndex: 0, onClick: function () { return props.publishPlugin(pluginState.id); }}, 
                                    React.createElement("svg", {"aria-hidden": "true", className: "slds-icon slds-icon--x-small slds-icon-text-default slds-m-right--x-small slds-shrink-none"}, 
                                        React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#upload"})
                                    ), 
                                    React.createElement("span", {className: "slds-truncate"}, "Publish"))
                            ), 
                            React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                                React.createElement("a", {href: "javascript:void(0);", role: "menuitem", tabIndex: 0, onClick: function () { return props.publishAndUsePlugin(pluginState.id); }}, 
                                    React.createElement("svg", {"aria-hidden": "true", className: "slds-icon slds-icon--x-small slds-icon-text-default slds-m-right--x-small slds-shrink-none"}, 
                                        React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#upload"})
                                    ), 
                                    React.createElement("span", {className: "slds-truncate"}, "Publish and use"))
                            ), 
                            React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                                React.createElement("a", {href: "javascript:void(0);", role: "menuitem", tabIndex: 0, onClick: function () { return props.removePlugin(pluginState.id); }}, 
                                    React.createElement("svg", {"aria-hidden": "true", className: "slds-icon slds-icon--x-small slds-icon-text-default slds-m-right--x-small slds-shrink-none"}, 
                                        React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#delete"})
                                    ), 
                                    React.createElement("span", {className: "slds-truncate"}, "Remove"))
                            ))
                    ))), 
            React.createElement("div", {className: "slds-tile__detail slds-is-relative"}, 
                pluginState.isLoading ?
                    React.createElement("div", {className: "slds-spinner_container"}, 
                        React.createElement("div", {className: "slds-spinner slds-spinner--small", role: "alert"}, 
                            React.createElement("span", {className: "slds-assistive-text"}, "Loading"), 
                            React.createElement("div", {className: "slds-spinner__dot-a"}), 
                            React.createElement("div", {className: "slds-spinner__dot-b"}))
                    )
                    : null, 
                React.createElement("dl", {className: "slds-dl--horizontal"}, 
                    React.createElement("dt", {className: "slds-dl--horizontal__label"}, 
                        React.createElement("p", {className: "slds-truncate", title: "Type"}, "Type:")
                    ), 
                    React.createElement("dd", {className: "slds-dl--horizontal__detail slds-tile__meta"}, 
                        React.createElement("p", {className: "slds-truncate", title: pluginState.typeInfo.type}, pluginState.typeInfo.type)
                    ), 
                    React.createElement("dt", {className: "slds-dl--horizontal__label"}, 
                        React.createElement("p", {className: "slds-truncate", title: "Version"}, "Version:")
                    ), 
                    React.createElement("dd", {className: "slds-dl--horizontal__detail slds-tile__meta"}, 
                        React.createElement("p", {className: "slds-truncate", title: pluginState.typeInfo.version}, pluginState.typeInfo.version)
                    ), 
                    React.createElement("dt", {className: "slds-dl--horizontal__label"}, 
                        React.createElement("p", {className: "slds-truncate", title: "Author"}, "Author:")
                    ), 
                    React.createElement("dd", {className: "slds-dl--horizontal__detail slds-tile__meta"}, 
                        React.createElement("p", {className: "slds-truncate", title: pluginState.typeInfo.author}, pluginState.typeInfo.author)
                    ), 
                    React.createElement("dt", {className: "slds-dl--horizontal__label"}, 
                        React.createElement("p", {className: "slds-truncate", title: "Type"}, "Url:")
                    ), 
                    React.createElement("dd", {className: "slds-dl--horizontal__detail slds-tile__meta"}, 
                        React.createElement("div", {className: "slds-form-element__control slds-input-has-icon slds-input-has-icon--left", title: url}, 
                            React.createElement("svg", {"aria-hidden": "true", className: "slds-input__icon slds-icon-text-default", onClick: function () { return _this._copyUrl(); }}, 
                                React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#copy"})
                            ), 
                            React.createElement("input", {className: "slds-input", type: "text", ref: "url", readOnly: true, style: { width: "100%", paddingRight: 0 }, placeholder: "Plugin Url ...", value: url}))
                    )), 
                React.createElement("p", {className: "slds-m-top--x-small"}, description)));
    };
    return PluginTile;
}(React.Component));
var WidgetPluginTile = react_redux_1.connect(function (state, ownProps) {
    return {
        pluginState: state.widgetPlugins[ownProps.pluginId]
    };
}, function (dispatch) {
    return {
        removePlugin: function (type) { return dispatch(WidgetsPlugins.unloadPlugin(type)); },
        publishPlugin: function (type) { return dispatch(Plugins.publishPlugin(type, false)); },
        publishAndUsePlugin: function (type) { return dispatch(Plugins.publishPlugin(type, true)); }
    };
})(PluginTile);
var DatasourcePluginTile = react_redux_1.connect(function (state, ownProps) {
    return {
        pluginState: state.datasourcePlugins[ownProps.pluginId]
    };
}, function (dispatch) {
    return {
        removePlugin: function (type) { return dispatch(DatasourcePlugins.unloadPlugin(type)); },
        publishPlugin: function (type) { return dispatch(Plugins.publishPlugin(type, false)); },
        publishAndUsePlugin: function (type) { return dispatch(Plugins.publishPlugin(type, true)); }
    };
})(PluginTile);
var LookupMenu = (function (_super) {
    __extends(LookupMenu, _super);
    function LookupMenu(props) {
        _super.call(this, props);
        this.state = {
            searchResult: []
        };
    }
    LookupMenu.prototype.componentWillReceiveProps = function (nextProps) {
        var _this = this;
        if (!nextProps.searchString) {
            this.setState({
                searchResult: []
            });
            return;
        }
        fetch(nextProps.pluginRegistryUrl + "/api/plugins/?q=" + nextProps.searchString)
            .then(function (result) {
            return result.json();
        })
            .then(function (json) {
            _this.setState({
                searchResult: json.plugins || []
            });
        });
    };
    LookupMenu.prototype.render = function () {
        var _this = this;
        var props = this.props;
        /*Icons for Widget = dashboard, report, poll / Datasource = feed */
        return React.createElement("div", {className: "slds-lookup__menu", id: props.id}, 
            React.createElement("ul", {className: "slds-lookup__list", role: "presentation"}, 
                React.createElement("li", {role: "presentation"}, 
                    React.createElement("span", {className: "slds-lookup__item-action slds-lookup__item-action--label", id: props.id + "-header", role: "option"}, 
                        React.createElement("svg", {"aria-hidden": "true", className: "slds-icon slds-icon--x-small slds-icon-text-default"}, 
                            React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#search"})
                        ), 
                        React.createElement("span", {className: "slds-truncate"}, 
                            "\"", 
                            props.searchString, 
                            "\" in plugin registry"))
                ), 
                this.state.searchResult.map(function (item, i) {
                    return React.createElement("li", {role: "presentation", key: item.type, onClick: function (e) { return _this.props.onItemClicked(item); }}, 
                        React.createElement("span", {className: "slds-lookup__item-action slds-media slds-media--center", id: props.id + "-" + i, role: "option"}, 
                            React.createElement("svg", {"aria-hidden": "true", className: "slds-icon slds-icon-standard-account slds-icon--small slds-media__figure"}, 
                                React.createElement("use", {xlinkHref: "assets/icons/standard-sprite/svg/symbols.svg#dashboard"})
                            ), 
                            React.createElement("div", {className: "slds-media__body"}, 
                                React.createElement("div", {className: "slds-lookup__result-text"}, 
                                    React.createElement("mark", null, item.name), 
                                    " (", 
                                    item.type, 
                                    ")"), 
                                React.createElement("span", {className: "slds-lookup__result-meta slds-text-body--small"}, 
                                    "DS/Widget • by ", 
                                    item.author, 
                                    " • ", 
                                    item.description)))
                    );
                }))
        );
    };
    return LookupMenu;
}(React.Component));
var PluginRegistrySettings = (function (_super) {
    __extends(PluginRegistrySettings, _super);
    function PluginRegistrySettings(props) {
        _super.call(this, props);
        this.state = { actionMenuOpen: false };
    }
    PluginRegistrySettings.prototype.toggleActionMenu = function () {
        this.clearTimeout();
        this.setState({ actionMenuOpen: !this.state.actionMenuOpen });
    };
    PluginRegistrySettings.prototype.closeActionMenu = function () {
        this.clearTimeout();
        this.setState({ actionMenuOpen: false });
    };
    PluginRegistrySettings.prototype.closeActionMenuIn = function (ms) {
        var _this = this;
        this.clearTimeout();
        this.timeout = setTimeout(function () { return _this.closeActionMenu(); }, ms);
    };
    PluginRegistrySettings.prototype.clearTimeout = function () {
        if (this.timeout) {
            clearTimeout(this.timeout);
        }
    };
    PluginRegistrySettings.prototype.onRegistryUrlChanged = function (e) {
        var target = e.target;
        this.props.onRegistryUrlChanged(target.value);
    };
    PluginRegistrySettings.prototype.onApiKeyChanged = function (e) {
        var target = e.target;
        this.props.onApiKeyChanged(target.value);
    };
    PluginRegistrySettings.prototype.render = function () {
        var _this = this;
        return React.createElement("div", {className: "slds-shrink-none slds-dropdown-trigger slds-dropdown-trigger--click" + (this.state.actionMenuOpen ? " slds-is-open" : "")}, 
            React.createElement("button", {className: "slds-button slds-button--icon-border-filled slds-button--icon-x-small", "aria-haspopup": "true", onClick: function () { return _this.toggleActionMenu(); }, onBlur: function () { return _this.closeActionMenuIn(200); }}, 
                React.createElement("svg", {"aria-hidden": "true", className: "slds-button__icon slds-button__icon--hint"}, 
                    React.createElement("use", {xlinkHref: "assets/icons/utility-sprite/svg/symbols.svg#settings"})
                ), 
                React.createElement("span", {className: "slds-assistive-text"}, "Actions")), 
            React.createElement("div", {className: "slds-dropdown slds-dropdown--left slds-dropdown--large"}, 
                React.createElement("ul", {className: "dropdown__list", role: "menu"}, 
                    React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                        React.createElement("span", {className: "slds-truncate slds-m-around--x-small"}, "Registry Url")
                    ), 
                    React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                        React.createElement("div", {className: "slds-form-element__control"}, 
                            React.createElement("input", {className: "slds-input", type: "text", placeholder: "http://dashboard.lobaro.com", defaultValue: this.props.pluginRegistryUrl, onFocus: function () { return _this.clearTimeout(); }, onBlur: function () { return _this.closeActionMenuIn(200); }, onChange: function (e) { return _this.onRegistryUrlChanged(e); }})
                        )
                    ), 
                    React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                        React.createElement("span", {className: "slds-truncate slds-m-around--x-small"}, "Api Key")
                    ), 
                    React.createElement("li", {className: "slds-dropdown__item", role: "presentation"}, 
                        React.createElement("div", {className: "slds-form-element__control"}, 
                            React.createElement("input", {className: "slds-input", type: "text", placeholder: "Api Key", defaultValue: this.props.pluginRegistryApiKey, onFocus: function () { return _this.clearTimeout(); }, onBlur: function () { return _this.closeActionMenuIn(200); }, onChange: function (e) { return _this.onApiKeyChanged(e); }})
                        )
                    ))
            ));
    };
    return PluginRegistrySettings;
}(React.Component));