/* 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;
|