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 = [],
|
2013-08-06 18:59:15 +00:00
|
|
|
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;
|
2013-08-06 18:59:15 +00:00
|
|
|
$element && $element.triggerEvent('add');
|
2013-08-03 13:58:36 +00:00
|
|
|
return history.length;
|
|
|
|
},
|
2013-08-06 18:59:15 +00:00
|
|
|
bindEvent: function() {
|
|
|
|
if (!$element) {
|
|
|
|
$element = Ox.Element();
|
|
|
|
}
|
|
|
|
$element.bindEvent.apply(this, arguments);
|
|
|
|
},
|
2013-08-03 13:58:36 +00:00
|
|
|
clear: function() {
|
|
|
|
history = [];
|
|
|
|
position = 0;
|
2013-08-06 18:59:15 +00:00
|
|
|
$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) {
|
2013-08-06 19:07:09 +00:00
|
|
|
position++;
|
2013-08-06 18:59:15 +00:00
|
|
|
$element && $element.triggerEvent('redo');
|
2013-08-06 19:07:09 +00:00
|
|
|
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) {
|
|
|
|
|
2013-08-06 18:59:15 +00:00
|
|
|
},
|
|
|
|
unbindEvent: function() {
|
|
|
|
$element && $element.unbindEvent.apply(this, arguments);
|
2013-08-03 13:58:36 +00:00
|
|
|
},
|
|
|
|
undo: function() {
|
|
|
|
if (position > 0) {
|
2013-08-06 19:07:09 +00:00
|
|
|
position--;
|
2013-08-06 18:59:15 +00:00
|
|
|
$element && $element.triggerEvent('undo');
|
2013-08-06 19:07:09 +00:00
|
|
|
return history[position];
|
2013-08-03 13:58:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
undoText: function() {
|
|
|
|
if (position > 0) {
|
|
|
|
return options.text(history[position - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|