2007-09-15 03:08:25 +02:00
|
|
|
/**
|
|
|
|
* @author Mateusz
|
|
|
|
*/
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.Crop = {
|
2007-09-15 03:08:25 +02:00
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
this.cropBox = $('cropBox');
|
2007-10-03 01:26:35 +02:00
|
|
|
new ImageEditor.Positioning.addBehaviour(this.cropBox);
|
2007-09-15 03:08:25 +02:00
|
|
|
this.imageContainer = $('imageContainer');
|
|
|
|
this.leftGreyBox = $('leftGreyBox');
|
|
|
|
this.rightGreyBox = $('rightGreyBox');
|
|
|
|
this.upperGreyBox = $('upperGreyBox');
|
|
|
|
this.lowerGreyBox = $('lowerGreyBox');
|
2007-10-03 01:26:35 +02:00
|
|
|
this.centerCropBox = ImageEditor.Crop.centerCropBox.bind(this);
|
|
|
|
this.placeGreyBox = ImageEditor.Crop.placeGreyBox.bind(this);
|
|
|
|
this.setListeners = ImageEditor.Crop.setListeners.bind(this);
|
|
|
|
this.onCropStart = ImageEditor.Crop.onCropStart.bind(this);
|
|
|
|
this.onCropOk = ImageEditor.Crop.onCropOk.bind(this);
|
|
|
|
this.onCropCancel = ImageEditor.Crop.onCropCancel.bind(this);
|
|
|
|
this.doCrop = ImageEditor.Crop.doCrop.bind(this);
|
|
|
|
this.setVisible = ImageEditor.Crop.setVisible.bind(this);
|
|
|
|
this.enable = ImageEditor.Crop.enable.bind(this);
|
|
|
|
this.disable = ImageEditor.Crop.disable.bind(this);
|
|
|
|
this.onImageLoadCallback = ImageEditor.Crop.onImageLoadCallback.bind(this);
|
2007-09-15 03:08:25 +02:00
|
|
|
Event.observe('image','load',this.centerCropBox);
|
2007-10-03 01:26:35 +02:00
|
|
|
var options = {
|
|
|
|
resizeStop: ImageEditor.Crop.resizeStop.bind(this),
|
|
|
|
onDrag: ImageEditor.Crop.onDrag.bind(this),
|
|
|
|
onResize: ImageEditor.Crop.onResize.bind(this),
|
|
|
|
getMousePos: ImageEditor.Crop.getMousePos.bind(this)
|
2007-09-15 03:19:43 +02:00
|
|
|
};
|
2007-10-03 01:26:35 +02:00
|
|
|
this.resizeCropBox = new ImageEditor.Resizeable.initialize(this.cropBox,options);
|
2007-09-16 02:50:48 +02:00
|
|
|
Event.observe(this.cropBox,'dblclick',this.onCropOk.bind(this));
|
2007-09-15 03:08:25 +02:00
|
|
|
this.setListeners();
|
2007-09-15 22:54:37 +02:00
|
|
|
this.isVisible = false;
|
|
|
|
this.setVisible(this.isVisible);
|
2007-09-15 03:31:31 +02:00
|
|
|
this.isEnabled = true;
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
resizeStop: function(event) {
|
2007-09-15 22:54:37 +02:00
|
|
|
if(this.isVisible) {
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.EventStack.clearStack();
|
2007-09-15 22:54:37 +02:00
|
|
|
this.resizeCropBox.originalHeight = this.cropBox.getHeight();
|
|
|
|
this.resizeCropBox.originalWidth = this.cropBox.getWidth();
|
|
|
|
}
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
2007-09-15 03:19:43 +02:00
|
|
|
onDrag: function(event) {
|
2007-09-15 03:08:25 +02:00
|
|
|
if(this.cropBox.getLeft() <= 0 ) this.cropBox.style.left = '0px';
|
|
|
|
if(this.cropBox.getTop() <= 0 ) this.cropBox.style.top = '0px';
|
|
|
|
if(this.cropBox.getLeft() + this.cropBox.getWidth() > this.cropBox.getParentWidth()) this.cropBox.style.left = this.cropBox.getParentWidth()- this.cropBox.getWidth() + 'px';
|
|
|
|
if(this.cropBox.getTop() + this.cropBox.getHeight() > this.cropBox.getParentHeight()) this.cropBox.style.top = this.cropBox.getParentHeight() - this.cropBox.getHeight() + 'px';
|
2007-09-15 03:19:43 +02:00
|
|
|
this.placeGreyBox(this.cropBox.getWidth(),this.cropBox.getHeight());
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
centerCropBox: function() {
|
|
|
|
this.cropBox.style.width = this.cropBox.getParentWidth()/2 + 'px';
|
|
|
|
this.cropBox.style.height = this.cropBox.getParentHeight()/2 + 'px';
|
|
|
|
this.cropBox.style.left = (this.cropBox.getParentWidth() - this.cropBox.getWidth())/2 + "px";
|
|
|
|
this.cropBox.style.top = (this.cropBox.getParentHeight() - this.cropBox.getHeight())/2 + "px";
|
2007-09-15 03:19:43 +02:00
|
|
|
this.placeGreyBox(this.cropBox.getWidth(),this.cropBox.getHeight());
|
2007-09-15 03:08:25 +02:00
|
|
|
this.leftBoxConstraint = this.cropBox.getParentLeft();
|
|
|
|
this.topBoxConstraint = this.cropBox.getParentTop();
|
|
|
|
this.rightBoxConstraint = this.cropBox.getParentLeft() + this.cropBox.getParentWidth();
|
|
|
|
this.bottomBoxConstraint = this.cropBox.getParentTop() + this.cropBox.getParentHeight()-1;//hack without 1 doesn't work;
|
|
|
|
},
|
|
|
|
|
2007-09-15 03:19:43 +02:00
|
|
|
placeGreyBox: function(width,height) {
|
2007-09-15 22:54:37 +02:00
|
|
|
if(this.isVisible) {
|
|
|
|
this.lowerGreyBox.style.left = this.cropBox.getLeft() + 'px';
|
|
|
|
this.lowerGreyBox.style.width = width + 'px';
|
|
|
|
this.lowerGreyBox.style.height = this.cropBox.getParentHeight() - this.cropBox.getTop() - height + "px";
|
|
|
|
this.lowerGreyBox.style.top = this.cropBox.getTop() + height + "px";
|
|
|
|
this.leftGreyBox.style.width = this.cropBox.getLeft() + "px";
|
|
|
|
this.leftGreyBox.style.height = $('imageContainer').getHeight() + 'px';
|
|
|
|
this.rightGreyBox.style.width = this.cropBox.getParentWidth() - this.cropBox.getLeft() - width + "px";
|
|
|
|
this.rightGreyBox.style.height = $('imageContainer').getHeight() + 'px';
|
|
|
|
this.rightGreyBox.style.left = this.cropBox.getLeft() + width + "px";
|
|
|
|
this.upperGreyBox.style.width = width + 'px';
|
|
|
|
this.upperGreyBox.style.left = this.cropBox.getLeft() + 'px';
|
|
|
|
this.upperGreyBox.style.height = this.cropBox.getTop() + 'px';
|
|
|
|
this.resizeCropBox.placeClickBox();
|
|
|
|
}
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
2007-09-15 03:19:43 +02:00
|
|
|
onResize: function(width,height) {
|
2007-09-16 04:24:11 +02:00
|
|
|
if(width + parseInt(this.cropBox.style.left) > Element.getDimensions(this.imageContainer).width) {
|
|
|
|
this.cropBox.style.left = parseInt(this.cropBox.style.left) - Math.abs(Element.getDimensions(this.imageContainer).width - (width + parseInt(this.cropBox.style.left))) + "px";
|
|
|
|
}
|
|
|
|
if(parseInt(this.cropBox.style.left) < 0) {
|
|
|
|
this.cropBox.style.left = "0px";
|
|
|
|
}
|
|
|
|
if(width > Element.getDimensions(this.imageContainer).width) {
|
|
|
|
this.cropBox.style.width = Element.getDimensions(this.imageContainer).width + "px";
|
|
|
|
width = Element.getDimensions(this.imageContainer).width;
|
|
|
|
}
|
2007-09-15 03:19:43 +02:00
|
|
|
this.placeGreyBox(width,height);
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
2007-10-03 01:26:35 +02:00
|
|
|
|
2007-09-15 03:08:25 +02:00
|
|
|
getMousePos: function(event) {
|
2007-10-03 01:26:35 +02:00
|
|
|
var x = Event.pointerX(event) + $('imageEditorContainer').scrollLeft;
|
|
|
|
var y = Event.pointerY(event) + $('imageEditorContainer').scrollTop;
|
2007-09-15 03:08:25 +02:00
|
|
|
if(x <= this.leftBoxConstraint) x = this.leftBoxConstraint;
|
|
|
|
if(y <= this.topBoxConstraint) y = this.topBoxConstraint;
|
|
|
|
if(x >= this.rightBoxConstraint) x = this.rightBoxConstraint;
|
|
|
|
if(y >= this.bottomBoxConstraint) y = this.bottomBoxConstraint;
|
|
|
|
return {x: x,y: y};
|
|
|
|
},
|
|
|
|
|
|
|
|
doCrop: function() {
|
2007-09-15 03:31:31 +02:00
|
|
|
if(this.isEnabled) {
|
2007-10-03 01:26:35 +02:00
|
|
|
var newWidth = this.cropBox.getWidth()
|
|
|
|
var newHeight = this.cropBox.getHeight() ;
|
|
|
|
var startTop = this.cropBox.getTop() ;
|
|
|
|
var startLeft = this.cropBox.getLeft() ;
|
2007-09-15 03:31:31 +02:00
|
|
|
if(newWidth > 35 && newHeight > 35) {
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.imageTransformation.crop(startTop,startLeft,newWidth,newHeight,ImageEditor.Crop.cropCallback.bind(this));
|
2007-09-16 02:50:48 +02:00
|
|
|
this.disable();
|
2007-09-15 03:31:31 +02:00
|
|
|
} else {
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.statusMessageWrapper.statusMessage("Crop area too small","bad");
|
2007-09-16 02:50:48 +02:00
|
|
|
return false;
|
2007-09-15 03:31:31 +02:00
|
|
|
}
|
2007-09-15 23:54:51 +02:00
|
|
|
$('image').style.visibility = 'visible';//hack for IE for not selecting image during crop
|
2007-09-16 02:50:48 +02:00
|
|
|
return true;
|
2007-09-15 03:08:25 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-09-15 03:31:31 +02:00
|
|
|
cropCallback: function() {
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.resize.imageContainerResize.placeClickBox();
|
|
|
|
ImageEditor.resize.imageContainerResize.setVisible(true);
|
2007-09-16 04:23:10 +02:00
|
|
|
Element.show($('CropText'));
|
|
|
|
Element.hide(this.cropBox,this.leftGreyBox,this.rightGreyBox,this.upperGreyBox,this.lowerGreyBox,$('CurrentAction'));
|
2007-09-15 03:31:31 +02:00
|
|
|
},
|
|
|
|
|
2007-09-15 03:08:25 +02:00
|
|
|
setListeners: function() {
|
2007-09-16 04:23:10 +02:00
|
|
|
Event.observe('CropButton','click',this.onCropStart);
|
|
|
|
Event.observe('CancelButton','click',this.onCropCancel);
|
|
|
|
Event.observe('ApplyButton','click',this.onCropOk);
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
onCropStart: function() {
|
2007-09-15 03:31:31 +02:00
|
|
|
if(this.isEnabled) {
|
2007-09-15 23:54:51 +02:00
|
|
|
$('image').style.visibility = "hidden";//hack for IE for not selecting image during crop
|
2007-09-15 03:31:31 +02:00
|
|
|
this.setVisible(true);
|
2007-09-16 04:23:10 +02:00
|
|
|
Element.show($('CurrentAction'));
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.imageHistory.disable();
|
|
|
|
ImageEditor.effects.disableRotate();
|
2007-09-15 22:54:37 +02:00
|
|
|
this.enable();
|
2007-09-15 03:31:31 +02:00
|
|
|
}
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onCropOk: function() {
|
2007-09-15 03:31:31 +02:00
|
|
|
if(this.isEnabled) {
|
2007-09-16 04:23:10 +02:00
|
|
|
if(this.doCrop()) Element.hide($('CurrentAction'));
|
2007-09-15 03:31:31 +02:00
|
|
|
}
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:23:10 +02:00
|
|
|
onCropCancel: function(event) {
|
2007-09-15 03:31:31 +02:00
|
|
|
if(this.isEnabled) {
|
2007-09-16 04:23:10 +02:00
|
|
|
Element.hide($('CurrentAction'));
|
|
|
|
Element.show($('CropText'));
|
2007-09-15 03:31:31 +02:00
|
|
|
this.setVisible(false);
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.imageHistory.enable();
|
|
|
|
ImageEditor.effects.enableRotate();
|
2007-09-15 22:54:37 +02:00
|
|
|
this.enable();
|
2007-09-15 23:54:51 +02:00
|
|
|
}
|
2007-09-16 04:23:10 +02:00
|
|
|
$('image').style.visibility = 'visible';//hack for IE for not selecting image during crop
|
2007-09-15 03:08:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setVisible: function(setVisible) {
|
2007-09-15 22:54:37 +02:00
|
|
|
this.isVisible = setVisible;
|
2007-09-15 03:08:25 +02:00
|
|
|
if(setVisible) {
|
|
|
|
Element.show(this.cropBox,this.leftGreyBox,this.rightGreyBox,this.upperGreyBox,this.lowerGreyBox);
|
|
|
|
this.centerCropBox();
|
2007-09-15 03:19:43 +02:00
|
|
|
this.placeGreyBox(this.cropBox.getWidth(),this.cropBox.getHeight());
|
2007-09-15 03:08:25 +02:00
|
|
|
} else {
|
2007-09-16 04:23:10 +02:00
|
|
|
Element.hide(this.cropBox,this.leftGreyBox,this.rightGreyBox,this.upperGreyBox,this.lowerGreyBox,$('CurrentAction'));
|
2007-09-15 03:08:25 +02:00
|
|
|
}
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.resize.imageContainerResize.setVisible(!setVisible);
|
2007-09-15 03:08:25 +02:00
|
|
|
this.resizeCropBox.setVisible(setVisible);
|
2007-09-15 03:31:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
enable: function() {
|
|
|
|
this.isEnabled = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function() {
|
|
|
|
this.isEnabled = false;
|
2007-09-15 22:54:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onImageLoadCallback: function() {
|
2007-10-03 01:26:35 +02:00
|
|
|
ImageEditor.crop.setVisible(false);
|
2007-09-15 03:31:31 +02:00
|
|
|
}
|
|
|
|
|
2007-09-15 03:08:25 +02:00
|
|
|
}
|