Skip to content

Commit df9c4fd

Browse files
Undo/redo buttons add
1 parent 9f900eb commit df9c4fd

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iknow-entity-browser",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Visualizer for iKnow entities",
55
"main": "gulpfile.babel.js",
66
"scripts": {

src/static/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
</head>
1212
<body onload="window.init()">
1313
<div id="windows">
14+
<div id="controlIcons">
15+
<i id="undoButton" class="ui icon-undo"></i>
16+
<i id="redoButton" class="ui icon-redo"></i>
17+
</div>
1418
<svg id="graph"></svg>
1519
<div id="table">
1620
<i id="tableToggle" class="ui icon-window-list"></i>

src/static/js/model/history.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
let history = [],
2-
redoHistory = [];
2+
redoHistory = [],
3+
undoButton = null,
4+
redoButton = null;
35

46
export function createState ({ undo, redo } = {}) {
57
if (typeof undo !== "function" || typeof redo !== "function")
68
throw new Error("Undo and Redo must be both functions");
79
redoHistory = [];
810
history.push({ undo, redo });
11+
updateButtons();
912
}
1013

1114
export function undo () {
@@ -24,5 +27,21 @@ export function redo () {
2427
redoed.redo();
2528
}
2629

27-
window.undo = undo;
28-
window.redo = redo;
30+
export function reset () {
31+
history = [];
32+
redoHistory = [];
33+
updateButtons();
34+
}
35+
36+
function updateButtons () {
37+
undoButton.classList[history.length ? `remove` : `add`](`disabled`);
38+
redoButton.classList[redoHistory.length ? `remove` : `add`](`disabled`);
39+
}
40+
41+
export function init () {
42+
undoButton = document.getElementById(`undoButton`);
43+
redoButton = document.getElementById(`redoButton`);
44+
undoButton.addEventListener(`click`, () => { undo(); updateButtons(); });
45+
redoButton.addEventListener(`click`, () => { redo(); updateButtons(); });
46+
updateButtons();
47+
}

src/static/js/model/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,17 @@ export function update () {
115115
alert(data.error || `No graph data returned`);
116116
} else {
117117
graph = preprocess(data.graph);
118-
updateCallbacks.forEach(cb => cb(graph, true));
118+
dataUpdated(true);
119119
}
120120
});
121121
}
122122

123+
function dataUpdated (reset = false) {
124+
if (reset)
125+
history.reset();
126+
updateCallbacks.forEach(cb => cb(graph, reset));
127+
}
128+
123129
export function getGraphData () {
124130
return graph;
125131
}
@@ -151,7 +157,8 @@ export let uiState = {
151157

152158
export function init () {
153159
graph = preprocess(sampleData.graph);
154-
updateCallbacks.forEach(cb => cb(graph, true));
160+
history.init();
161+
dataUpdated(true);
155162
}
156163

157164
function resetChildrenPosition (parent, children = []) {
@@ -238,7 +245,7 @@ export function unfold (node, children = 20) {
238245
node.children = node.children.concat(next);
239246
node.label = left > 0 ? `${ left } more` : `Others`;
240247
resetChildrenPosition(node, next);
241-
updateCallbacks.forEach(cb => cb(graph));
248+
dataUpdated();
242249
return {
243250
unfolded: next.length,
244251
left: left
@@ -252,7 +259,7 @@ export function unfold (node, children = 20) {
252259
node._children = part.concat(node._children);
253260
node.id = oldId;
254261
node.label = node._children.length > 0 ? `${ node._children.length } more` : `Others`;
255-
updateCallbacks.forEach(cb => cb(graph));
262+
dataUpdated();
256263
}
257264
});
258265
return res.left;

src/static/scss/interface.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@
3636
z-index: $zIndexInterface;
3737
}
3838

39+
#controlIcons {
40+
position: absolute;
41+
left: 0;
42+
bottom: 0;
43+
z-index: $zIndexInterface;
44+
padding-left: 8px;
45+
}

0 commit comments

Comments
 (0)