You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openNG/src/client/stores/windows.js

106 lines
2.3 KiB
JavaScript

"use strict";
const xtend = require("xtend");
const nanoid = require("nanoid");
const immutable = require("immutable");
module.exports = function createWindowStore({onUpdated}) {
let windows = immutable.OrderedMap([]);
let floatingWindows = immutable.OrderedMap([]);
let leftSidebarWindows = immutable.OrderedMap([]);
let rightSidebarWindows = immutable.OrderedMap([]);
let activeWindow;
let lastZIndex = 0;
function updateWindowIndexes() {
floatingWindows = windows.filter((window_) => window_.sidebar == null);
leftSidebarWindows = windows.filter((window_) => window_.sidebar === "left");
rightSidebarWindows = windows.filter((window_) => window_.sidebar === "right");
}
function setSide(id, side) {
let targetWindow = windows.get(id);
targetWindow.sidebar = side;
updateWindowIndexes();
onUpdated();
}
return {
add: function (window_) {
let id = nanoid();
windows = windows.set(id, xtend(window_, {
id: id,
x: window_.initialX,
y: window_.initialY,
width: window_.initialWidth,
height: window_.initialHeight
}));
updateWindowIndexes();
this.focus(id);
/* NOTE: We let .focus call onUpdated. QUESTION: Is this not deduplicated by React? */
return id;
},
focus: function (id) {
let targetWindow = windows.get(id);
windows.forEach((window_) => {
window_.isActive = false;
});
targetWindow.zIndex = lastZIndex++;
targetWindow.isActive = true;
activeWindow = targetWindow;
onUpdated();
},
setPosition: function (id, x, y) {
let targetWindow = windows.get(id);
targetWindow.x = x;
targetWindow.y = y;
onUpdated();
},
setDimensions: function (id, width, height) {
let targetWindow = windows.get(id);
targetWindow.width = width;
targetWindow.height = height;
onUpdated();
},
moveToSidebar: function (id, side) {
setSide(id, side);
},
makeFloating: function (id) {
setSide(id, null);
},
close: function (id) {
windows = windows.delete(id);
updateWindowIndexes();
onUpdated();
},
getAll: function () {
return windows;
},
getFloating: function () {
return floatingWindows;
},
getLeftSidebar: function () {
return leftSidebarWindows;
},
getRightSidebar: function () {
return rightSidebarWindows;
},
getActiveWindow: function () {
return activeWindow;
}
};
};