2013-06-24 23:57:42 +08:00
|
|
|
(function(a,b,c,d,e,f){function g(a,b,c){return 3>=c.length?a.call(b,c[0],c[1],c[2]):a.apply(b,c)}function h(a,b){if(null==a)return!1;d.isArray(b)||(b=p(arguments,1));for(var c=0,e=b.length;e>c;c++)if(!(b[c]in a))return!1;return!0}function j(){this.registeredObjects=[]}function k(a,b,c,e){for(var h,f=0,g=b.length;g>f;f++)if(h=b[f]){if("on"===a){if(e.objectRegistry.isRegistered(h))continue;e.objectRegistry.register(h)}else{if(!e.objectRegistry.isRegistered(h))continue;e.objectRegistry.unregister(h)}d.isFunction(h[a])&&h[a]("all",c,e)}}function l(a,b,c){var e=b.type,f=!c[e]||c[e][a];d.isFunction(f)&&f(b.object,b.before,b.after,b)}function m(a,b){if(!(b.isCurrentlyUndoRedoing||"undo"===a&&-1===b.pointer||"redo"===a&&b.pointer===b.length-1)){b.isCurrentlyUndoRedoing=!0;var c,d,e="undo"===a;for(e?(c=b.at(b.pointer),b.pointer--):(b.pointer++,c=b.at(b.pointer)),d=b.where({cycleIndex:c.get("cycleIndex")}),b.pointer+=(e?-1:1)*(d.length-1);c=e?d.pop():d.shift();)c[a](b.undoTypes);b.isCurrentlyUndoRedoing=!1}}function n(a,b,c,d,e){if(a.track&&!a.isCurrentlyUndoRedoing&&b in d){var j,f=g(d[b].on,null,c);if(h(f,"object","before","after")){if(f.type=b,f.cycleIndex=i(),a.pointer<a.length-1)for(var j=a.length-a.pointer-1;j--;)a.pop();a.pointer=a.length,a.add(f),a.length>e&&(a.shift(),a.pointer--)}}}function u(){}function v(a,b,c,e){if("object"==typeof b)return d.each(type,function(b,d){2===a?v(a,b,c,e):v(a,d,b,e)});switch(a){case 0:h(c,"undo","redo","on")&&d.all(d.pick(c,"undo","redo","on"),d.isFunction)&&(e[b]=c);break;case 1:e[b]&&d.isObject(c)&&d.extend(e[b],c);break;case 2:delete e[b]}}var i=function(){function c(){b++,a=!0,d.defer(function(){a=!1})}var a=!1,b=-1;return function(){return a||c(),0|b}}();j.prototype={isRegistered:function(a){return a&&a.cid?this.registeredObjects[a.cid]:d.contains(this.registeredObjects,a)},register:function(a){a&&a.cid?this.registeredObjects[a.cid]=a:this.registeredObjects.push(a)},unregister:function(a){if(a&&a.cid)delete this.registeredObjects[a.cid];else{var b=d.indexOf(this.registeredObjects,a);b>-1&&this.registeredObjects.splice(b,1)}}};var o=Array.prototype.slice,p=function(a,b){return o.call(a,b)},q={add:{undo:function(a,b,c,d){a.remove(c,d.options)},redo:function(a,b,c,d){var e=d.options;e.index&&(e.at=e.index),a.add(c,d.options)},on:function(a,b,c){return{object:b,before:f,after:a,options:d.clone(c)}}},remove:{undo:function(a,b,c,d){var e=d.options;e.index&&(e.at=e.index),a.add(b,e)},redo:function(a,b,c,d){a.remove(b,d.options)},on:function(a,b,c){return{object:b,before:a,after:f,options:d.clone(c)}}},change:{undo:function(a,b,c){d.isEmpty(b)?d.each(d.keys(c),a.unset,a):a.set(b)},redo:function(a,b,c){d.isEmpty(c)?d.each(d.keys(b),a.unset,a):a.set(c)},on:function(a){var c=a.changedAttributes(),e=d.pick(a.previousAttributes(),d.keys(c));return{object:a,before:e,after:c}}},reset:{undo:function(a,b){a.reset(b)},redo:function(a,b,c){a.reset(c)},on:function(a,b){return{object:a,before:b.previousModels,after:d.clone(a.models)}}}},r=e.Model.extend({defaults:{type:null,object:null,before:null,after:null,cycleIndex:null},undo:function(a){l("undo",this.attributes,a)},redo:function(a){l("redo",this.attributes,a)}}),s=e.Collection.extend({model:r,pointer:-1,track:!1,isCurrentlyUndoRedoing:!1,maximumStackLength:1/0,initialize:function(){this.objectRegistry=new j,this.undoTypes=new u},setMaxLength:function(a){this.maximumStackLength=a},addToStack:function(a){n(this,a,p(arguments,1),this.undoTypes,this.maximumStackLength)}}),t=e.Model.extend({defaults:{maximumStackLength:1/0},initialize:function(){this.stack=new s,this.stack.setMaxLength(this.get("maximumStackLength")),this.on("change:maximumStackLength",function(a,b){this.stack.setMaxLength(b)},this)},startTracking:function(){this.stack.track=!0},stopTracking:function(){this.stack.track=!1},register:function(){k("on",arguments,this.stack.addToStack,this.stack)},unregister:function(){k("off",arguments,this.stack.addToStack,this.stack)},undo:function(){m("undo",this.stack)},redo:function(){m("redo",this.stack)},addUndoType:function(a,b){v(0,a,b,this.stack.u
|