pointvec/desktop/sources/scripts/lib/history.js

52 lines
946 B
JavaScript
Raw Normal View History

2018-08-28 00:34:17 -04:00
'use strict';
2018-01-31 15:21:59 -05:00
function History()
{
this.index = 0;
this.a = [];
2018-02-04 16:09:46 -05:00
this.clear = function()
{
this.a = [];
this.index = 0;
}
2018-01-31 15:21:59 -05:00
this.push = function(data)
{
if(this.index < this.a.length-1){
this.fork();
}
this.index = this.a.length;
this.a = this.a.slice(0,this.index);
this.a.push(copy(data));
2018-02-06 01:34:45 -05:00
if(this.a.length > 20){
this.a.shift();
}
2018-01-31 15:21:59 -05:00
}
this.fork = function()
{
this.a = this.a.slice(0,this.index+1);
}
this.pop = function()
{
return this.a.pop();
}
this.prev = function()
{
this.index = clamp(this.index-1,0,this.a.length-1);
return copy(this.a[this.index]);
}
this.next = function()
{
this.index = clamp(this.index+1,0,this.a.length-1);
return copy(this.a[this.index]);
}
2018-02-06 01:34:45 -05:00
function copy(data){ return data ? JSON.parse(JSON.stringify(data)) : []; }
2018-01-31 15:21:59 -05:00
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
}