forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
190 lines (158 loc) · 5.1 KB
/
queue.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
var Lib = require('../lib');
var dfltConfig = require('../plot_api/plot_config').dfltConfig;
/**
* Copy arg array *without* removing `undefined` values from objects.
*
* @param gd
* @param args
* @returns {Array}
*/
function copyArgArray(gd, args) {
var copy = [];
var arg;
for(var i = 0; i < args.length; i++) {
arg = args[i];
if(arg === gd) copy[i] = arg;
else if(typeof arg === 'object') {
copy[i] = Array.isArray(arg) ?
Lib.extendDeep([], arg) :
Lib.extendDeepAll({}, arg);
} else copy[i] = arg;
}
return copy;
}
// -----------------------------------------------------
// Undo/Redo queue for plots
// -----------------------------------------------------
var queue = {};
// TODO: disable/enable undo and redo buttons appropriately
/**
* Add an item to the undoQueue for a graphDiv
*
* @param gd
* @param undoFunc Function undo this operation
* @param undoArgs Args to supply undoFunc with
* @param redoFunc Function to redo this operation
* @param redoArgs Args to supply redoFunc with
*/
queue.add = function(gd, undoFunc, undoArgs, redoFunc, redoArgs) {
var queueObj,
queueIndex;
// make sure we have the queue and our position in it
gd.undoQueue = gd.undoQueue || {index: 0, queue: [], sequence: false};
queueIndex = gd.undoQueue.index;
// if we're already playing an undo or redo, or if this is an auto operation
// (like pane resize... any others?) then we don't save this to the undo queue
if(gd.autoplay) {
if(!gd.undoQueue.inSequence) gd.autoplay = false;
return;
}
// if we're not in a sequence or are just starting, we need a new queue item
if(!gd.undoQueue.sequence || gd.undoQueue.beginSequence) {
queueObj = {undo: {calls: [], args: []}, redo: {calls: [], args: []}};
gd.undoQueue.queue.splice(queueIndex, gd.undoQueue.queue.length - queueIndex, queueObj);
gd.undoQueue.index += 1;
} else {
queueObj = gd.undoQueue.queue[queueIndex - 1];
}
gd.undoQueue.beginSequence = false;
// we unshift to handle calls for undo in a forward for loop later
if(queueObj) {
queueObj.undo.calls.unshift(undoFunc);
queueObj.undo.args.unshift(undoArgs);
queueObj.redo.calls.push(redoFunc);
queueObj.redo.args.push(redoArgs);
}
if(gd.undoQueue.queue.length > dfltConfig.queueLength) {
gd.undoQueue.queue.shift();
gd.undoQueue.index--;
}
};
/**
* Begin a sequence of undoQueue changes
*
* @param gd
*/
queue.startSequence = function(gd) {
gd.undoQueue = gd.undoQueue || {index: 0, queue: [], sequence: false};
gd.undoQueue.sequence = true;
gd.undoQueue.beginSequence = true;
};
/**
* Stop a sequence of undoQueue changes
*
* Call this *after* you're sure your undo chain has ended
*
* @param gd
*/
queue.stopSequence = function(gd) {
gd.undoQueue = gd.undoQueue || {index: 0, queue: [], sequence: false};
gd.undoQueue.sequence = false;
gd.undoQueue.beginSequence = false;
};
/**
* Move one step back in the undo queue, and undo the object there.
*
* @param gd
*/
queue.undo = function undo(gd) {
var queueObj, i;
if(gd.undoQueue === undefined ||
isNaN(gd.undoQueue.index) ||
gd.undoQueue.index <= 0) {
return;
}
// index is pointing to next *forward* queueObj, point to the one we're undoing
gd.undoQueue.index--;
// get the queueObj for instructions on how to undo
queueObj = gd.undoQueue.queue[gd.undoQueue.index];
// this sequence keeps things from adding to the queue during undo/redo
gd.undoQueue.inSequence = true;
for(i = 0; i < queueObj.undo.calls.length; i++) {
queue.plotDo(gd, queueObj.undo.calls[i], queueObj.undo.args[i]);
}
gd.undoQueue.inSequence = false;
gd.autoplay = false;
};
/**
* Redo the current object in the undo, then move forward in the queue.
*
* @param gd
*/
queue.redo = function redo(gd) {
var queueObj, i;
if(gd.undoQueue === undefined ||
isNaN(gd.undoQueue.index) ||
gd.undoQueue.index >= gd.undoQueue.queue.length) {
return;
}
// get the queueObj for instructions on how to undo
queueObj = gd.undoQueue.queue[gd.undoQueue.index];
// this sequence keeps things from adding to the queue during undo/redo
gd.undoQueue.inSequence = true;
for(i = 0; i < queueObj.redo.calls.length; i++) {
queue.plotDo(gd, queueObj.redo.calls[i], queueObj.redo.args[i]);
}
gd.undoQueue.inSequence = false;
gd.autoplay = false;
// index is pointing to the thing we just redid, move it
gd.undoQueue.index++;
};
/**
* Called by undo/redo to make the actual changes.
*
* Not meant to be called publically, but included for mocking out in tests.
*
* @param gd
* @param func
* @param args
*/
queue.plotDo = function(gd, func, args) {
gd.autoplay = true;
// this *won't* copy gd and it preserves `undefined` properties!
args = copyArgArray(gd, args);
// call the supplied function
func.apply(null, args);
};
module.exports = queue;