1/*! 2 * undone.js 0.0.1 - https://github.com/yckart/undone.js 3 * The undo/redo manager for well formed javascript applications. 4 * 5 * Inspired by: http://blog.asgaard.co.uk/2012/11/21/undo-redo-in-javascript 6 * 7 * Copyright (c) 2012 Yannick Albert (http://yckart.com) 8 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php). 9 * 2013/03/16 10 **/ 11;(function(window){ 12 13 /** 14 * @constructor 15 */ 16 var Undone = function(options) { 17 18 // define the defaults 19 this.options = { 20 buffer: 1000 21 }; 22 23 // extend the defaults 24 for(var i in options) this.options[i] = options[i]; 25 26 this.history = { 27 undo: [], 28 redo: [] 29 }; 30 }; 31 32 /** 33 * Executes an action and adds it to the undo stack. 34 * @param {Function} action - Action function. 35 * @param {Function} reverse - Reverse function. 36 * @param {Object} ctx - The 'this' argument for the action/reverse functions. 37 */ 38 Undone.prototype.register = function(action, reverse, ctx) { 39 if(this.history.undo.length >= this.options.buffer) this.history.undo.unshift(); 40 this.history.undo.push( {action: action, reverse: reverse, ctx: ctx} ); 41 action.call(ctx); 42 this.history.redo.length = 0; 43 this.change("register", this.history.undo.length, this.history.redo.length); 44 }; 45 46 Undone.prototype.undo = function(n) { 47 var len = this.history.undo.length; 48 49 n = n || 1; 50 if(n > len) n = len; 51 if(len < this.options.buffer) { 52 while(n--) { 53 var c = this.history.undo.pop(); 54 if (!c) return; 55 56 c.reverse.call(c.ctx); 57 this.history.redo.push(c); 58 } 59 if( len !== this.history.undo.length ) this.change("undo", this.history.undo.length, this.history.redo.length); 60 } 61 }; 62 63 Undone.prototype.redo = function(n) { 64 65 var len = this.history.redo.length; 66 67 n = n || 1; 68 if(n > len) n = len; 69 if(len < this.options.buffer) { 70 while(n--) { 71 var c = this.history.redo.pop(); 72 if (!c) return; 73 c.action.call(c.ctx); 74 this.history.undo.push(c); 75 } 76 if( len !== this.history.redo.length ) this.change("redo", this.history.undo.length, this.history.redo.length); 77 } 78 }; 79 80 Undone.prototype.onChange = function(){}; 81 Undone.prototype.change = function(event, undoLen, redoLen){ 82 if (this.onChange) this.onChange(event, undoLen, redoLen); 83 if(window.jQuery) $(window).trigger("undone:change", [event, undoLen, redoLen]); 84 }; 85 86 Undone.prototype.clear = function(){ 87 this.history = { 88 undo: [], 89 redo: [] 90 }; 91 this.change("clear", 0, 0); 92 }; 93 94 window.Undone = Undone; 95}(window));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.