all files / src/dashboard/ import.ts

48.48% Statements 16/33
25% Branches 1/4
14.29% Functions 1/7
48.48% Lines 16/33
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                                              41×           41×                                                
/* 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 Action = require("../actionNames");
var actionNames_1 = require("../actionNames");
var layouts_js_1 = require("../layouts/layouts.js");
var _ = require("lodash");
var dashboard_1 = require("../dashboard");
/**
 * To extend the import/export by another property you just need to add the property to the exported data
 * See: serialize()
 *
 * If there are any action needed after a property got imported, call them after the import.
 * See: afterImport()
 */
function serialize(state) {
    return JSON.stringify({
        widgets: state.widgets,
        datasources: state.datasources,
        datasourcePlugins: state.datasourcePlugins,
        widgetPlugins: state.widgetPlugins
    });
}
exports.serialize = serialize;
function afterImport(dispatch, getState) {
    var oldDashboard = dashboard_1.default.getInstance();
    oldDashboard.dispose();
    var newDashboard = new dashboard_1.default(oldDashboard.store);
    newDashboard.init();
}
function importReducer(state, action) {
    switch (action.type) {
        case Action.DASHBOARD_IMPORT:
            var newState = _.assign({}, state, action.state);
            console.log("new State:", state, action.state, newState);
            return newState;
        default:
            return state;
    }
}
exports.importReducer = importReducer;
function deserialize(data) {
    if (typeof data === "string") {
        return JSON.parse(data);
    }
    else {
        throw new Error("Dashboard data for import must be of type string but is " + typeof data);
    }
}
exports.deserialize = deserialize;
function doImport(data) {
    var state = deserialize(data);
    return function (dispatch, getState) {
        // Bad hack to force the grid layout to update correctly
        dispatch(layouts_js_1.loadEmptyLayout());
        setTimeout(function () {
            dispatch({
                type: actionNames_1.DASHBOARD_IMPORT,
                state: state
            });
            afterImport(dispatch, getState);
        }, 0);
    };
}
exports.doImport = doImport;