Stackedit/src/store/contextMenu.js
Benoit Schweblin e05e7717eb Added tests
2018-06-07 23:56:11 +00:00

56 lines
1.5 KiB
JavaScript

const setter = propertyName => (state, value) => {
state[propertyName] = value;
};
export default {
namespaced: true,
state: {
coordinates: {
left: 0,
top: 0,
},
items: [],
resolve: () => {},
},
mutations: {
setCoordinates: setter('coordinates'),
setItems: setter('items'),
setResolve: setter('resolve'),
},
actions: {
open({ commit, rootState }, { coordinates, items }) {
commit('setItems', items);
// Place the context menu outside the screen
commit('setCoordinates', { top: 0, left: -9999 });
// Let the UI refresh itself
setTimeout(() => {
// Take the size of the context menu and place it
const elt = document.querySelector('.context-menu__inner');
if (elt) {
const height = elt.offsetHeight;
if (coordinates.top + height > rootState.layout.bodyHeight) {
coordinates.top -= height;
}
if (coordinates.top < 0) {
coordinates.top = 0;
}
const width = elt.offsetWidth;
if (coordinates.left + width > rootState.layout.bodyWidth) {
coordinates.left -= width;
}
if (coordinates.left < 0) {
coordinates.left = 0;
}
commit('setCoordinates', coordinates);
}
}, 1);
return new Promise(resolve => commit('setResolve', resolve));
},
close({ commit }) {
commit('setItems', []);
commit('setResolve', () => {});
},
},
};