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