all files / src/ dashboard.ts

80.49% Statements 66/82
50% Branches 10/20
78.95% Functions 15/19
80.49% Lines 66/82
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                                                                                        19× 19×                                                                              
"use strict";
var datasourcePluginRegistry_1 = require("./datasource/datasourcePluginRegistry");
var _ = require("lodash");
var Plugins = require("./pluginApi/pluginLoader");
var PluginCache = require("./pluginApi/pluginCache");
var scriptLoader_1 = require("./util/scriptLoader");
var URI = require("urijs");
var widgetPluginRegistry_1 = require("./widgets/widgetPluginRegistry");
/**
 * The root of the Dashboard business Logic
 * Defines the lifecycle of the Dashboard from creation till disposal
 */
var Dashboard = (function () {
    function Dashboard(_store) {
        var _this = this;
        this._store = _store;
        this._initialized = false;
        this._scriptsLoading = {};
        this._datasourcePluginRegistry = new datasourcePluginRegistry_1.default(_store);
        this._widgetPluginRegistry = new widgetPluginRegistry_1.default(_store);
        _store.subscribe(function () {
            // Whenever a datasource is added that is still loading, we create an instance and update the loading state
            var state = _store.getState();
            if (_this._lastLoadingUrls === state.pluginLoader.loadingUrls) {
                return;
            }
            _this._lastLoadingUrls = state.pluginLoader.loadingUrls;
            state.pluginLoader.loadingUrls.forEach(function (urlToLoad) {
                Eif (!_this._scriptsLoading[urlToLoad]) {
                    _this.loadPluginScript(urlToLoad);
                }
            });
        });
    }
    Dashboard.setInstance = function (dashboard) {
        Dashboard._instance = dashboard;
    };
    /**
     * We have some code that depends on this global instance of the Dashboard
     * This is bad, but better that static references
     * we have at least the chance to influence the instance during tests
     *
     * @returns {Dashboard}
     */
    Dashboard.getInstance = function () {
        Iif (!Dashboard._instance) {
            throw new Error("No global dashboard created. Call setInstance(dashboard) before!");
        }
        return Dashboard._instance;
    };
    Object.defineProperty(Dashboard.prototype, "store", {
        get: function () {
            return this._store;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Dashboard.prototype, "datasourcePluginRegistry", {
        get: function () {
            return this._datasourcePluginRegistry;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Dashboard.prototype, "widgetPluginRegistry", {
        get: function () {
            return this._widgetPluginRegistry;
        },
        enumerable: true,
        configurable: true
    });
    Dashboard.prototype.init = function () {
        var _this = this;
        Iif (this._initialized) {
            throw new Error("Dashboard was already initialized. Can not call init() twice.");
        }
        this._initialized = true;
        // TODO: Should not be needed but is still needed for unloading plugins and in some widget code
        Dashboard.setInstance(this);
        var state = this._store.getState();
        var plugins = _.valuesIn(state.datasourcePlugins)
            .concat(_.valuesIn(state.widgetPlugins));
        plugins.forEach(function (plugin) {
            _this._store.dispatch(Plugins.reloadExistingPlugin(plugin.url, plugin.id));
        });
    };
    Dashboard.prototype.dispose = function () {
        this._datasourcePluginRegistry.dispose();
        this._widgetPluginRegistry.dispose();
    };
    /**
     * Load plugin from URL
     */
    Dashboard.prototype.loadPluginScript = function (url) {
        var _this = this;
        var loadScriptsPromise = scriptLoader_1.default.loadScript([url]);
        this._scriptsLoading[url] = loadScriptsPromise.then(function () {
            Eif (PluginCache.hasPlugin()) {
                // TODO: use a reference to the pluginCache and only bind that instance to the window object while the script is loaded
                // TODO: The scriploader can ensure that only one script is loaded at a time
                var plugin = PluginCache.popLoadedPlugin();
                return _this.loadPluginScriptDependencies(plugin, url);
            }
            else {
                return Promise.reject(new Error("Failed to load Plugin. Make sure it called window.iotDashboardApi.register***Plugin from url " + url));
            }
        }).then(function (plugin) {
            Eif (plugin.Datasource) {
                _this._datasourcePluginRegistry.register(plugin);
                _this._store.dispatch(Plugins.datasourcePluginFinishedLoading(plugin, url));
            }
            else if (plugin.Widget) {
                _this._widgetPluginRegistry.register(plugin);
                _this._store.dispatch(Plugins.widgetPluginFinishedLoading(plugin, url));
            }
            delete _this._scriptsLoading[url];
            return Promise.resolve();
        }).catch(function (error) {
            console.warn("Failed to load script: ", error);
            delete _this._scriptsLoading[url];
            _this._store.dispatch(Plugins.pluginFailedLoading(url));
        });
        return this._scriptsLoading[url];
    };
    Dashboard.prototype.loadPluginScriptDependencies = function (plugin, url) {
        // Do not load dependencies of widgets anymore, they are loaded inside the iFrame
        Iif (plugin.TYPE_INFO.kind === "widget") {
            return Promise.resolve(plugin);
        }
        var dependencies = plugin.TYPE_INFO.dependencies;
        Iif (_.isArray(dependencies) && dependencies.length !== 0) {
            var dependencyPaths = dependencies.map(function (dependency) {
                return URI(dependency).absoluteTo(url).toString();
            });
            console.log("Loading Dependencies for Plugin", dependencyPaths);
            return scriptLoader_1.default.loadScript(dependencyPaths).then(function () {
                return Promise.resolve(plugin);
            });
        }
        else {
            return Promise.resolve(plugin);
        }
    };
    return Dashboard;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Dashboard;