/* 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 Widgets = require('../widgets/widgets');
var uuid_1 = require('../util/uuid');
var reducer_1 = require('../util/reducer');
var actionNames_1 = require('../actionNames');
var initialLayouts = {
"default": {
id: "default",
name: "Default Layout",
widgets: Widgets.initialWidgets
}
};
function addLayout(name, widgets) {
return function (dispatch) {
var addLayout = dispatch({
type: actionNames_1.ADD_LAYOUT,
id: uuid_1.generate(),
name: name,
widgets: widgets
});
dispatch(setCurrentLayout(addLayout.id));
};
}
exports.addLayout = addLayout;
function updateLayout(id, widgets) {
return {
type: actionNames_1.UPDATE_LAYOUT,
id: id,
widgets: widgets
};
}
exports.updateLayout = updateLayout;
function deleteLayout(id) {
return {
type: actionNames_1.DELETE_LAYOUT,
id: id
};
}
exports.deleteLayout = deleteLayout;
function setCurrentLayout(id) {
return {
type: actionNames_1.SET_CURRENT_LAYOUT,
id: id
};
}
exports.setCurrentLayout = setCurrentLayout;
function loadEmptyLayout() {
return {
type: actionNames_1.LOAD_LAYOUT,
layout: {
id: "empty",
widgets: {}
}
};
}
exports.loadEmptyLayout = loadEmptyLayout;
function loadLayout(id) {
return function (dispatch, getState) {
var state = getState();
var layout = state.layouts[id];
// Bad hack to force the grid layout to update correctly
dispatch(loadEmptyLayout());
if (!layout) {
return;
}
setTimeout(function () {
dispatch(setCurrentLayout(layout.id));
dispatch({
type: actionNames_1.LOAD_LAYOUT,
layout: layout
});
}, 0);
};
}
exports.loadLayout = loadLayout;
var layoutCrudReducer = reducer_1.genCrudReducer([actionNames_1.ADD_LAYOUT, actionNames_1.DELETE_LAYOUT], layout);
function layouts(state, action) {
if (state === void 0) { state = initialLayouts; }
state = layoutCrudReducer(state, action);
switch (action.type) {
default:
return state;
}
}
exports.layouts = layouts;
function layout(state, action) {
switch (action.type) {
case actionNames_1.ADD_LAYOUT:
return {
id: action.id,
name: action.name,
widgets: action.widgets
};
case actionNames_1.UPDATE_LAYOUT:
return Object.assign({}, state, {
widgets: action.widgets
});
default:
return state;
}
}
exports.layout = layout;
function currentLayout(state, action) {
if (state === void 0) { state = {}; }
switch (action.type) {
case actionNames_1.SET_CURRENT_LAYOUT:
return Object.assign({}, state, {
id: action.id
});
case actionNames_1.DELETE_LAYOUT:
if (action.id == state.id) {
return Object.assign({}, state, {
id: undefined
});
}
return state;
default:
return state;
}
}
exports.currentLayout = currentLayout;
|