oxjs/source/UI/js/Core/History.js

74 lines
2 KiB
JavaScript
Raw Permalink Normal View History

2013-08-03 13:58:36 +00:00
'use strict';
Ox.History = function(options) {
options = Ox.extend({
text: function(item) {
return item.text;
}
}, options || {});
var history = [],
position = 0,
$element;
2013-08-03 13:58:36 +00:00
return {
_print: function() {
Ox.print(JSON.stringify({history: history, position: position}));
},
add: function(items) {
items = Ox.makeArray(items);
history = history.slice(0, position).concat(items);
position += items.length;
$element && $element.triggerEvent('add');
2013-08-03 13:58:36 +00:00
return history.length;
},
bindEvent: function() {
if (!$element) {
$element = Ox.Element();
}
2014-09-20 10:29:03 +00:00
$element.bindEvent.apply($element, arguments);
},
2013-08-03 13:58:36 +00:00
clear: function() {
history = [];
position = 0;
$element && $element.triggerEvent('clear');
2013-08-03 13:58:36 +00:00
return history.length;
},
items: function() {
return history.length;
},
redo: function() {
if (position < history.length) {
position++;
$element && $element.triggerEvent('redo');
return history[position - 1];
2013-08-03 13:58:36 +00:00
}
},
redoText: function() {
if (position < history.length) {
return options.text(history[position]);
}
},
remove: function(test) {
},
unbindEvent: function() {
2014-09-20 10:29:03 +00:00
$element && $element.unbindEvent.apply($element, arguments);
2013-08-03 13:58:36 +00:00
},
undo: function() {
if (position > 0) {
position--;
$element && $element.triggerEvent('undo');
return history[position];
2013-08-03 13:58:36 +00:00
}
},
undoText: function() {
if (position > 0) {
return options.text(history[position - 1]);
}
}
};
};