App-SocialCalc-Multiplayer
view release on metacpan or search on metacpan
socialcalc/socialcalc-3.js view on Meta::CPAN
}
// Return next token as a string
SocialCalc.Parse.prototype.NextToken = function() {
if (this.pos < 0) return "";
var pos2 = this.str.indexOf(this.delimiter, this.pos);
var pos1 = this.pos;
if (pos2 > this.lineEnd) { // don't go past end of line
pos2 = this.lineEnd;
}
if (pos2 >= 0) {
this.pos = pos2 + 1;
return this.str.substring(pos1, pos2);
}
else {
this.pos = this.lineEnd;
return this.str.substring(pos1, this.lineEnd);
}
}
// Return everything from current point until end of line
SocialCalc.Parse.prototype.RestOfString = function() {
var oldpos = this.pos;
if (this.pos < 0 || this.pos >= this.lineEnd) return "";
this.pos = this.lineEnd;
return this.str.substring(oldpos, this.lineEnd);
}
SocialCalc.Parse.prototype.RestOfStringNoMove = function() {
if (this.pos < 0 || this.pos >= this.lineEnd) return "";
return this.str.substring(this.pos, this.lineEnd);
}
// Move current point to next line
SocialCalc.Parse.prototype.NextLine = function() {
this.pos = this.lineEnd + 1;
this.lineEnd = this.str.indexOf("\n", this.pos);
if (this.lineEnd < 0) {
this.lineEnd = this.str.length;
}
}
// Check to see if at end of string with no more to process
SocialCalc.Parse.prototype.EOF = function() {
if (this.pos < 0 || this.pos >= this.str.length) return true;
return false;
}
// *************************************
//
// UndoStack class:
//
// Implements the behavior needed for a normal application's undo/redo stack.
// You add a new change sequence with PushChange.
// The type argument is a string that can be used to lookup some general string
// like "typing" or "setting attribute" for the menu prompts for undo/redo.
//
// You add the "do" steps with AddDo. The non-null, non-undefined arguments are
// joined together with " " to make a command string to be saved.
//
// You add the undo steps as commands for the most recent change with AddUndo.
// The non-null, non-undefined arguments are joined together with " " to make
// a command string to be saved.
//
// The Undo and Redo functions move the Top Of Stack pointer through the changes stack
// so you can undo and redo. Doing a new PushChange removes all undone items
// after TOS.
//
// You can push more things than you can undo if you want.
// There is a maximum to remember as the "did" stack for an audit trail (and as redo). This may be unlimited.
// There is a separate maximum to remember that can be undone. This may be smaller than maxRedo.
//
// *************************************
SocialCalc.UndoStack = function() {
// properties:
this.stack = []; // {command: [], type: type, undo: []} -- multiple dos and undos allowed
this.tos = -1; // top of stack position, used for undo/redo
this.maxRedo = 0; // Maximum size of redo stack (and audit trail which is this.stack[n].command) or zero if no limit
this.maxUndo = 50; // Maximum number of steps kept for undo (usually the memory intensive part) or zero if no limit
}
SocialCalc.UndoStack.prototype.PushChange = function(type) { // adding a new thing to the stack
while (this.stack.length > 0 && this.stack.length-1 > this.tos) { // pop off things not redone
this.stack.pop();
}
this.stack.push({command: [], type: type, undo: []});
if (this.maxRedo && this.stack.length > this.maxRedo) { // limit number kept as audit trail
this.stack.shift(); // remove the extra one
}
if (this.maxUndo && this.stack.length > this.maxUndo) { // need to trim excess undo info
this.stack[this.stack.length - this.maxUndo - 1].undo = []; // only need to remove one
}
this.tos = this.stack.length - 1;
}
SocialCalc.UndoStack.prototype.AddDo = function() {
var args = [];
for (var i=0; i<arguments.length; i++) {
if (arguments[i]!=null) args.push(arguments[i]); // ignore null or undefined
}
var cmd = args.join(" ");
this.stack[this.stack.length-1].command.push(cmd);
}
SocialCalc.UndoStack.prototype.AddUndo = function() {
var args = [];
for (var i=0; i<arguments.length; i++) {
if (arguments[i]!=null) args.push(arguments[i]); // ignore null or undefined
}
var cmd = args.join(" ");
this.stack[this.stack.length-1].undo.push(cmd);
}
( run in 3.367 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )