/** * @author Mateusz */ ImageEditor.History = { initialize: function() { this.history = new Array(); this.historyPointer = -1; this.isEnabled = true; this.image = ImageEditor.Positioning.addBehaviour($('image')); this.size = new Array(); this.fakeImage = $('fakeImg'); this.image = $('image'); this.adjust = new Array(); this.undo = ImageEditor.History.undo.bind(this); this.redo = ImageEditor.History.redo.bind(this); this.add = ImageEditor.History.add.bind(this); this.addListeners = ImageEditor.History.addListeners.bind(this); this.hasOperation = ImageEditor.History.hasOperation.bind(this); this.isInHistory = ImageEditor.History.isInHistory.bind(this); this.onImageLoad = ImageEditor.History.onImageLoad.bind(this); this.removeLastOperation = ImageEditor.History.removeLastOperation.bind(this); this.getOptimizedHistory = ImageEditor.History.getOptimizedHistory.bind(this); this.addCrop = ImageEditor.History.addCrop.bind(this); this.addResize = ImageEditor.History.addResize.bind(this); this.addEffect = ImageEditor.History.addEffect.bind(this); this.addAdjust = ImageEditor.History.addAdjust.bind(this); this.enable = ImageEditor.History.enable.bind(this); this.disable = ImageEditor.History.disable.bind(this); this.clear = ImageEditor.History.clear.bind(this); this.onlyResized = ImageEditor.History.onlyResized.bind(this); this.optimizeOtherEffects = ImageEditor.History.optimizeOtherEffects.bind(this); this.optimizeCrop = ImageEditor.History.optimizeCrop.bind(this); this.optimizeResize = ImageEditor.History.optimizeResize.bind(this); this.optimizeRotate = ImageEditor.History.optimizeRotate.bind(this); this.checkSpecialOperation = ImageEditor.History.checkSpecialOperation.bind(this); this.addListeners(); }, undo: function() { if(this.isEnabled) { if(this.historyPointer >= 1) { var operation = this.history[this.historyPointer].operation; this.checkSpecialOperation('undo',this.history[this.historyPointer]); Event.observe('image','load',this.onImageLoad); this.historyPointer = this.historyPointer - 1; this.image.src = this.history[this.historyPointer].fileUrl; } else { ImageEditor.statusMessageWrapper.statusMessage("No more undo","bad"); } } }, redo: function() { if(this.isEnabled) { if(this.historyPointer < this.history.length-1) { var operation = this.history[this.historyPointer+1].operation; this.checkSpecialOperation('redo',this.history[this.historyPointer+1]); Event.observe('image','load',this.onImageLoad); this.historyPointer = this.historyPointer + 1; this.image.src = this.history[this.historyPointer].fileUrl; } else { ImageEditor.statusMessageWrapper.statusMessage("No more redo","bad"); } } }, add: function(operation,url,additionalInfo) { var imageWidth = isNaN(parseInt($('image').style.width)) ? Element.getDimensions($('image')).width : parseInt($('image').style.width);//IE hack var imageHeight = isNaN(parseInt($('image').style.height)) ? Element.getDimensions($('image')).height : parseInt($('image').style.height);//IE hack //code above should be moved to Positioning.addBehaviour if(!this.isInHistory(operation,url)) { this.historyPointer++; this.size[this.historyPointer] = {'width': imageWidth,'height': imageHeight}; this.history[this.historyPointer] = {'operation': operation,'fileUrl' : url,'additionalInfo': additionalInfo}; this.size = this.size.slice(0,this.historyPointer+1); this.history = this.history.slice(0,this.historyPointer+1); } }, addCrop: function(url,top,left,width,height) { this.add('crop',url,{ 'top':top, 'left': left, 'width': width, 'height': height }); }, addResize: function(url,width,height) { this.add('resize',url,{ 'width': width, 'height': height }); }, addEffect: function(url,name) { this.add(name,url); }, addAdjust: function(name,value,url) { this.add(name,url,{'value': value}); if(this.adjust[name] == undefined) { this.adjust[name] = {'pointer': 0,'values': Array()} this.adjust[name].values[0] = ImageEditor.effects.getEffect(name).getDefaultValue(); } this.adjust[name].values[this.adjust[name].values.length] = value; this.adjust[name].pointer++; }, addListeners: function() { this.undoListener = Event.observe('UndoButton','click',this.undo); this.redoListener = Event.observe('RedoButton','click',this.redo); }, hasOperation: function(operation,historyPointer) { if(historyPointer == undefined) historyPointer = this.history.length-1; for(i=historyPointer;i>=0;i--) { if(this.history[i].operation == operation) { return true; } } return false; }, getOptimizedHistory: function(without) { var history = this.history.slice(0,this.historyPointer+1); var result = {}; var historyPointer = 1; result[0] = {fileUrl : history[0].fileUrl}; var resize = this.optimizeResize(history,this.size); var rotate = this.optimizeRotate(history); var crop = this.optimizeCrop(history,this.size); var other = this.optimizeOtherEffects(history,without); if(rotate != undefined) { for(var i =0;i=0;i--) { if(this.history[i].operation != 'resize') { return false; } } return true; }, optimizeResize: function(history,size) { var scallingXFactor = 1;var scallingYFactor = 1; var initWidth = size[0].width;var initHeight = size[0].height; for(var i=0;i 0) this.adjust[historyEntry.operation].pointer--; } else { this.adjust[historyEntry.operation].pointer++; } ImageEditor.effects.getEffect(historyEntry.operation).setValue(this.adjust[historyEntry.operation].values[this.adjust[historyEntry.operation].pointer]); } } };