all files / src/layouts/ layouts.js

57.89% Statements 33/57
40% Branches 6/15
16.67% Functions 2/12
56.36% Lines 31/55
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                                                                                                                          43× 43× 43×   43×                                   43× 43×                         43×        
/* 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;