| 1 | /** |
|---|
| 2 | * Copyright (c) 2006, David Spurr (http://www.defusion.org.uk/) |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
|---|
| 6 | * |
|---|
| 7 | * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
|---|
| 8 | * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
|---|
| 9 | * * Neither the name of the David Spurr nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
|---|
| 10 | * |
|---|
| 11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 12 | * |
|---|
| 13 | * http://www.opensource.org/licenses/bsd-license.php |
|---|
| 14 | * |
|---|
| 15 | * See scriptaculous.js for full scriptaculous licence |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | var CropDraggable=Class.create(); |
|---|
| 20 | Object.extend(Object.extend(CropDraggable.prototype,Draggable.prototype),{initialize:function(_1){ |
|---|
| 21 | this.options=Object.extend({drawMethod:function(){ |
|---|
| 22 | }},arguments[1]||{}); |
|---|
| 23 | this.element=$(_1); |
|---|
| 24 | this.handle=this.element; |
|---|
| 25 | this.delta=this.currentDelta(); |
|---|
| 26 | this.dragging=false; |
|---|
| 27 | this.eventMouseDown=this.initDrag.bindAsEventListener(this); |
|---|
| 28 | Event.observe(this.handle,"mousedown",this.eventMouseDown); |
|---|
| 29 | Draggables.register(this); |
|---|
| 30 | },draw:function(_2){ |
|---|
| 31 | var _3=Position.cumulativeOffset(this.element); |
|---|
| 32 | var d=this.currentDelta(); |
|---|
| 33 | _3[0]-=d[0]; |
|---|
| 34 | _3[1]-=d[1]; |
|---|
| 35 | var p=[0,1].map(function(i){ |
|---|
| 36 | return (_2[i]-_3[i]-this.offset[i]); |
|---|
| 37 | }.bind(this)); |
|---|
| 38 | this.options.drawMethod(p); |
|---|
| 39 | }}); |
|---|
| 40 | var Cropper={}; |
|---|
| 41 | Cropper.Img=Class.create(); |
|---|
| 42 | Cropper.Img.prototype={initialize:function(_7,_8){ |
|---|
| 43 | this.options=Object.extend({ratioDim:{x:0,y:0},minWidth:0,minHeight:0,displayOnInit:false,onEndCrop:Prototype.emptyFunction,captureKeys:true,onloadCoords:null,maxWidth:0,maxHeight:0},_8||{}); |
|---|
| 44 | this.img=$(_7); |
|---|
| 45 | this.clickCoords={x:0,y:0}; |
|---|
| 46 | this.dragging=false; |
|---|
| 47 | this.resizing=false; |
|---|
| 48 | this.isWebKit=/Konqueror|Safari|KHTML/.test(navigator.userAgent); |
|---|
| 49 | this.isIE=/MSIE/.test(navigator.userAgent); |
|---|
| 50 | this.isOpera8=/Opera\s[1-8]/.test(navigator.userAgent); |
|---|
| 51 | this.ratioX=0; |
|---|
| 52 | this.ratioY=0; |
|---|
| 53 | this.attached=false; |
|---|
| 54 | this.fixedWidth=(this.options.maxWidth>0&&(this.options.minWidth>=this.options.maxWidth)); |
|---|
| 55 | this.fixedHeight=(this.options.maxHeight>0&&(this.options.minHeight>=this.options.maxHeight)); |
|---|
| 56 | if(typeof this.img=="undefined"){ |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | $A(document.getElementsByTagName("script")).each(function(s){ |
|---|
| 60 | if(s.src.match(/cropper\.js/)){ |
|---|
| 61 | var _a=s.src.replace(/cropper\.js(.*)?/,""); |
|---|
| 62 | var _b=document.createElement("link"); |
|---|
| 63 | _b.rel="stylesheet"; |
|---|
| 64 | _b.type="text/css"; |
|---|
| 65 | _b.href=_a+"../css/cropper.css"; |
|---|
| 66 | _b.media="screen"; |
|---|
| 67 | document.getElementsByTagName("head")[0].appendChild(_b); |
|---|
| 68 | } |
|---|
| 69 | }); |
|---|
| 70 | if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){ |
|---|
| 71 | var _c=this.getGCD(this.options.ratioDim.x,this.options.ratioDim.y); |
|---|
| 72 | this.ratioX=this.options.ratioDim.x/_c; |
|---|
| 73 | this.ratioY=this.options.ratioDim.y/_c; |
|---|
| 74 | } |
|---|
| 75 | this.subInitialize(); |
|---|
| 76 | if(this.img.complete||this.isWebKit){ |
|---|
| 77 | this.onLoad(); |
|---|
| 78 | }else{ |
|---|
| 79 | Event.observe(this.img,"load",this.onLoad.bindAsEventListener(this)); |
|---|
| 80 | } |
|---|
| 81 | },getGCD:function(a,b){ |
|---|
| 82 | if(b==0){ |
|---|
| 83 | return a; |
|---|
| 84 | } |
|---|
| 85 | return this.getGCD(b,a%b); |
|---|
| 86 | },onLoad:function(){ |
|---|
| 87 | var _f="imgCrop_"; |
|---|
| 88 | var _10=this.img.parentNode; |
|---|
| 89 | var _11=""; |
|---|
| 90 | if(this.isOpera8){ |
|---|
| 91 | _11=" opera8"; |
|---|
| 92 | } |
|---|
| 93 | this.imgWrap=Builder.node("div",{"class":_f+"wrap"+_11}); |
|---|
| 94 | this.north=Builder.node("div",{"class":_f+"overlay "+_f+"north"},[Builder.node("span")]); |
|---|
| 95 | this.east=Builder.node("div",{"class":_f+"overlay "+_f+"east"},[Builder.node("span")]); |
|---|
| 96 | this.south=Builder.node("div",{"class":_f+"overlay "+_f+"south"},[Builder.node("span")]); |
|---|
| 97 | this.west=Builder.node("div",{"class":_f+"overlay "+_f+"west"},[Builder.node("span")]); |
|---|
| 98 | var _12=[this.north,this.east,this.south,this.west]; |
|---|
| 99 | this.dragArea=Builder.node("div",{"class":_f+"dragArea"},_12); |
|---|
| 100 | this.handleN=Builder.node("div",{"class":_f+"handle "+_f+"handleN"}); |
|---|
| 101 | this.handleNE=Builder.node("div",{"class":_f+"handle "+_f+"handleNE"}); |
|---|
| 102 | this.handleE=Builder.node("div",{"class":_f+"handle "+_f+"handleE"}); |
|---|
| 103 | this.handleSE=Builder.node("div",{"class":_f+"handle "+_f+"handleSE"}); |
|---|
| 104 | this.handleS=Builder.node("div",{"class":_f+"handle "+_f+"handleS"}); |
|---|
| 105 | this.handleSW=Builder.node("div",{"class":_f+"handle "+_f+"handleSW"}); |
|---|
| 106 | this.handleW=Builder.node("div",{"class":_f+"handle "+_f+"handleW"}); |
|---|
| 107 | this.handleNW=Builder.node("div",{"class":_f+"handle "+_f+"handleNW"}); |
|---|
| 108 | this.selArea=Builder.node("div",{"class":_f+"selArea"},[Builder.node("div",{"class":_f+"marqueeHoriz "+_f+"marqueeNorth"},[Builder.node("span")]),Builder.node("div",{"class":_f+"marqueeVert "+_f+"marqueeEast"},[Builder.node("span")]),Builder.node("div",{"class":_f+"marqueeHoriz "+_f+"marqueeSouth"},[Builder.node("span")]),Builder.node("div",{"class":_f+"marqueeVert "+_f+"marqueeWest"},[Builder.node("span")]),this.handleN,this.handleNE,this.handleE,this.handleSE,this.handleS,this.handleSW,this.handleW,this.handleNW,Builder.node("div",{"class":_f+"clickArea"})]); |
|---|
| 109 | this.imgWrap.appendChild(this.img); |
|---|
| 110 | this.imgWrap.appendChild(this.dragArea); |
|---|
| 111 | this.dragArea.appendChild(this.selArea); |
|---|
| 112 | this.dragArea.appendChild(Builder.node("div",{"class":_f+"clickArea"})); |
|---|
| 113 | _10.appendChild(this.imgWrap); |
|---|
| 114 | this.startDragBind=this.startDrag.bindAsEventListener(this); |
|---|
| 115 | Event.observe(this.dragArea,"mousedown",this.startDragBind); |
|---|
| 116 | this.onDragBind=this.onDrag.bindAsEventListener(this); |
|---|
| 117 | Event.observe(document,"mousemove",this.onDragBind); |
|---|
| 118 | this.endCropBind=this.endCrop.bindAsEventListener(this); |
|---|
| 119 | Event.observe(document,"mouseup",this.endCropBind); |
|---|
| 120 | this.resizeBind=this.startResize.bindAsEventListener(this); |
|---|
| 121 | this.handles=[this.handleN,this.handleNE,this.handleE,this.handleSE,this.handleS,this.handleSW,this.handleW,this.handleNW]; |
|---|
| 122 | this.registerHandles(true); |
|---|
| 123 | if(this.options.captureKeys){ |
|---|
| 124 | this.keysBind=this.handleKeys.bindAsEventListener(this); |
|---|
| 125 | Event.observe(document,"keypress",this.keysBind); |
|---|
| 126 | } |
|---|
| 127 | new CropDraggable(this.selArea,{drawMethod:this.moveArea.bindAsEventListener(this)}); |
|---|
| 128 | this.setParams(); |
|---|
| 129 | },registerHandles:function(_13){ |
|---|
| 130 | for(var i=0;i<this.handles.length;i++){ |
|---|
| 131 | var _15=$(this.handles[i]); |
|---|
| 132 | if(_13){ |
|---|
| 133 | var _16=false; |
|---|
| 134 | if(this.fixedWidth&&this.fixedHeight){ |
|---|
| 135 | _16=true; |
|---|
| 136 | }else{ |
|---|
| 137 | if(this.fixedWidth||this.fixedHeight){ |
|---|
| 138 | var _17=_15.className.match(/([S|N][E|W])$/); |
|---|
| 139 | var _18=_15.className.match(/(E|W)$/); |
|---|
| 140 | var _19=_15.className.match(/(N|S)$/); |
|---|
| 141 | if(_17){ |
|---|
| 142 | _16=true; |
|---|
| 143 | }else{ |
|---|
| 144 | if(this.fixedWidth&&_18){ |
|---|
| 145 | _16=true; |
|---|
| 146 | }else{ |
|---|
| 147 | if(this.fixedHeight&&_19){ |
|---|
| 148 | _16=true; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | if(_16){ |
|---|
| 155 | _15.hide(); |
|---|
| 156 | }else{ |
|---|
| 157 | Event.observe(_15,"mousedown",this.resizeBind); |
|---|
| 158 | } |
|---|
| 159 | }else{ |
|---|
| 160 | _15.show(); |
|---|
| 161 | Event.stopObserving(_15,"mousedown",this.resizeBind); |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | },setParams:function(){ |
|---|
| 165 | this.imgW=this.img.width; |
|---|
| 166 | this.imgH=this.img.height; |
|---|
| 167 | $(this.north).setStyle({height:0}); |
|---|
| 168 | $(this.east).setStyle({width:0,height:0}); |
|---|
| 169 | $(this.south).setStyle({height:0}); |
|---|
| 170 | $(this.west).setStyle({width:0,height:0}); |
|---|
| 171 | $(this.imgWrap).setStyle({"width":this.imgW+"px","height":this.imgH+"px"}); |
|---|
| 172 | $(this.selArea).hide(); |
|---|
| 173 | var _1a={x1:0,y1:0,x2:0,y2:0}; |
|---|
| 174 | var _1b=false; |
|---|
| 175 | if(this.options.onloadCoords!=null){ |
|---|
| 176 | _1a=this.cloneCoords(this.options.onloadCoords); |
|---|
| 177 | _1b=true; |
|---|
| 178 | }else{ |
|---|
| 179 | if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){ |
|---|
| 180 | _1a.x1=Math.ceil((this.imgW-this.options.ratioDim.x)/2); |
|---|
| 181 | _1a.y1=Math.ceil((this.imgH-this.options.ratioDim.y)/2); |
|---|
| 182 | _1a.x2=_1a.x1+this.options.ratioDim.x; |
|---|
| 183 | _1a.y2=_1a.y1+this.options.ratioDim.y; |
|---|
| 184 | _1b=true; |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | this.setAreaCoords(_1a,false,false,1); |
|---|
| 188 | if(this.options.displayOnInit&&_1b){ |
|---|
| 189 | this.selArea.show(); |
|---|
| 190 | this.drawArea(); |
|---|
| 191 | this.endCrop(); |
|---|
| 192 | } |
|---|
| 193 | this.attached=true; |
|---|
| 194 | },remove:function(){ |
|---|
| 195 | if(this.attached){ |
|---|
| 196 | this.attached=false; |
|---|
| 197 | this.imgWrap.parentNode.insertBefore(this.img,this.imgWrap); |
|---|
| 198 | this.imgWrap.parentNode.removeChild(this.imgWrap); |
|---|
| 199 | Event.stopObserving(this.dragArea,"mousedown",this.startDragBind); |
|---|
| 200 | Event.stopObserving(document,"mousemove",this.onDragBind); |
|---|
| 201 | Event.stopObserving(document,"mouseup",this.endCropBind); |
|---|
| 202 | this.registerHandles(false); |
|---|
| 203 | if(this.options.captureKeys){ |
|---|
| 204 | Event.stopObserving(document,"keypress",this.keysBind); |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | },reset:function(){ |
|---|
| 208 | if(!this.attached){ |
|---|
| 209 | this.onLoad(); |
|---|
| 210 | }else{ |
|---|
| 211 | this.setParams(); |
|---|
| 212 | } |
|---|
| 213 | this.endCrop(); |
|---|
| 214 | },handleKeys:function(e){ |
|---|
| 215 | var dir={x:0,y:0}; |
|---|
| 216 | if(!this.dragging){ |
|---|
| 217 | switch(e.keyCode){ |
|---|
| 218 | case (37): |
|---|
| 219 | dir.x=-1; |
|---|
| 220 | break; |
|---|
| 221 | case (38): |
|---|
| 222 | dir.y=-1; |
|---|
| 223 | break; |
|---|
| 224 | case (39): |
|---|
| 225 | dir.x=1; |
|---|
| 226 | break; |
|---|
| 227 | case (40): |
|---|
| 228 | dir.y=1; |
|---|
| 229 | break; |
|---|
| 230 | } |
|---|
| 231 | if(dir.x!=0||dir.y!=0){ |
|---|
| 232 | if(e.shiftKey){ |
|---|
| 233 | dir.x*=10; |
|---|
| 234 | dir.y*=10; |
|---|
| 235 | } |
|---|
| 236 | this.moveArea([this.areaCoords.x1+dir.x,this.areaCoords.y1+dir.y]); |
|---|
| 237 | Event.stop(e); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | },calcW:function(){ |
|---|
| 241 | return (this.areaCoords.x2-this.areaCoords.x1); |
|---|
| 242 | },calcH:function(){ |
|---|
| 243 | return (this.areaCoords.y2-this.areaCoords.y1); |
|---|
| 244 | },moveArea:function(_1e){ |
|---|
| 245 | this.setAreaCoords({x1:_1e[0],y1:_1e[1],x2:_1e[0]+this.calcW(),y2:_1e[1]+this.calcH()},true,false); |
|---|
| 246 | this.drawArea(); |
|---|
| 247 | },cloneCoords:function(_1f){ |
|---|
| 248 | return {x1:_1f.x1,y1:_1f.y1,x2:_1f.x2,y2:_1f.y2}; |
|---|
| 249 | },setAreaCoords:function(_20,_21,_22,_23,_24){ |
|---|
| 250 | if(_21){ |
|---|
| 251 | var _25=_20.x2-_20.x1; |
|---|
| 252 | var _26=_20.y2-_20.y1; |
|---|
| 253 | if(_20.x1<0){ |
|---|
| 254 | _20.x1=0; |
|---|
| 255 | _20.x2=_25; |
|---|
| 256 | } |
|---|
| 257 | if(_20.y1<0){ |
|---|
| 258 | _20.y1=0; |
|---|
| 259 | _20.y2=_26; |
|---|
| 260 | } |
|---|
| 261 | if(_20.x2>this.imgW){ |
|---|
| 262 | _20.x2=this.imgW; |
|---|
| 263 | _20.x1=this.imgW-_25; |
|---|
| 264 | } |
|---|
| 265 | if(_20.y2>this.imgH){ |
|---|
| 266 | _20.y2=this.imgH; |
|---|
| 267 | _20.y1=this.imgH-_26; |
|---|
| 268 | } |
|---|
| 269 | }else{ |
|---|
| 270 | if(_20.x1<0){ |
|---|
| 271 | _20.x1=0; |
|---|
| 272 | } |
|---|
| 273 | if(_20.y1<0){ |
|---|
| 274 | _20.y1=0; |
|---|
| 275 | } |
|---|
| 276 | if(_20.x2>this.imgW){ |
|---|
| 277 | _20.x2=this.imgW; |
|---|
| 278 | } |
|---|
| 279 | if(_20.y2>this.imgH){ |
|---|
| 280 | _20.y2=this.imgH; |
|---|
| 281 | } |
|---|
| 282 | if(_23!=null){ |
|---|
| 283 | if(this.ratioX>0){ |
|---|
| 284 | this.applyRatio(_20,{x:this.ratioX,y:this.ratioY},_23,_24); |
|---|
| 285 | }else{ |
|---|
| 286 | if(_22){ |
|---|
| 287 | this.applyRatio(_20,{x:1,y:1},_23,_24); |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | var _27=[this.options.minWidth,this.options.minHeight]; |
|---|
| 291 | var _28=[this.options.maxWidth,this.options.maxHeight]; |
|---|
| 292 | if(_27[0]>0||_27[1]>0||_28[0]>0||_28[1]>0){ |
|---|
| 293 | var _29={a1:_20.x1,a2:_20.x2}; |
|---|
| 294 | var _2a={a1:_20.y1,a2:_20.y2}; |
|---|
| 295 | var _2b={min:0,max:this.imgW}; |
|---|
| 296 | var _2c={min:0,max:this.imgH}; |
|---|
| 297 | if((_27[0]!=0||_27[1]!=0)&&_22){ |
|---|
| 298 | if(_27[0]>0){ |
|---|
| 299 | _27[1]=_27[0]; |
|---|
| 300 | }else{ |
|---|
| 301 | if(_27[1]>0){ |
|---|
| 302 | _27[0]=_27[1]; |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | if((_28[0]!=0||_28[0]!=0)&&_22){ |
|---|
| 307 | if(_28[0]>0&&_28[0]<=_28[1]){ |
|---|
| 308 | _28[1]=_28[0]; |
|---|
| 309 | }else{ |
|---|
| 310 | if(_28[1]>0&&_28[1]<=_28[0]){ |
|---|
| 311 | _28[0]=_28[1]; |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | if(_27[0]>0){ |
|---|
| 316 | this.applyDimRestriction(_29,_27[0],_23.x,_2b,"min"); |
|---|
| 317 | } |
|---|
| 318 | if(_27[1]>1){ |
|---|
| 319 | this.applyDimRestriction(_2a,_27[1],_23.y,_2c,"min"); |
|---|
| 320 | } |
|---|
| 321 | if(_28[0]>0){ |
|---|
| 322 | this.applyDimRestriction(_29,_28[0],_23.x,_2b,"max"); |
|---|
| 323 | } |
|---|
| 324 | if(_28[1]>1){ |
|---|
| 325 | this.applyDimRestriction(_2a,_28[1],_23.y,_2c,"max"); |
|---|
| 326 | } |
|---|
| 327 | _20={x1:_29.a1,y1:_2a.a1,x2:_29.a2,y2:_2a.a2}; |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | this.areaCoords=_20; |
|---|
| 332 | },applyDimRestriction:function(_2d,val,_2f,_30,_31){ |
|---|
| 333 | var _32; |
|---|
| 334 | if(_31=="min"){ |
|---|
| 335 | _32=((_2d.a2-_2d.a1)<val); |
|---|
| 336 | }else{ |
|---|
| 337 | _32=((_2d.a2-_2d.a1)>val); |
|---|
| 338 | } |
|---|
| 339 | if(_32){ |
|---|
| 340 | if(_2f==1){ |
|---|
| 341 | _2d.a2=_2d.a1+val; |
|---|
| 342 | }else{ |
|---|
| 343 | _2d.a1=_2d.a2-val; |
|---|
| 344 | } |
|---|
| 345 | if(_2d.a1<_30.min){ |
|---|
| 346 | _2d.a1=_30.min; |
|---|
| 347 | _2d.a2=val; |
|---|
| 348 | }else{ |
|---|
| 349 | if(_2d.a2>_30.max){ |
|---|
| 350 | _2d.a1=_30.max-val; |
|---|
| 351 | _2d.a2=_30.max; |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | },applyRatio:function(_33,_34,_35,_36){ |
|---|
| 356 | var _37; |
|---|
| 357 | if(_36=="N"||_36=="S"){ |
|---|
| 358 | _37=this.applyRatioToAxis({a1:_33.y1,b1:_33.x1,a2:_33.y2,b2:_33.x2},{a:_34.y,b:_34.x},{a:_35.y,b:_35.x},{min:0,max:this.imgW}); |
|---|
| 359 | _33.x1=_37.b1; |
|---|
| 360 | _33.y1=_37.a1; |
|---|
| 361 | _33.x2=_37.b2; |
|---|
| 362 | _33.y2=_37.a2; |
|---|
| 363 | }else{ |
|---|
| 364 | _37=this.applyRatioToAxis({a1:_33.x1,b1:_33.y1,a2:_33.x2,b2:_33.y2},{a:_34.x,b:_34.y},{a:_35.x,b:_35.y},{min:0,max:this.imgH}); |
|---|
| 365 | _33.x1=_37.a1; |
|---|
| 366 | _33.y1=_37.b1; |
|---|
| 367 | _33.x2=_37.a2; |
|---|
| 368 | _33.y2=_37.b2; |
|---|
| 369 | } |
|---|
| 370 | },applyRatioToAxis:function(_38,_39,_3a,_3b){ |
|---|
| 371 | var _3c=Object.extend(_38,{}); |
|---|
| 372 | var _3d=_3c.a2-_3c.a1; |
|---|
| 373 | var _3e=Math.floor(_3d*_39.b/_39.a); |
|---|
| 374 | var _3f; |
|---|
| 375 | var _40; |
|---|
| 376 | var _41=null; |
|---|
| 377 | if(_3a.b==1){ |
|---|
| 378 | _3f=_3c.b1+_3e; |
|---|
| 379 | if(_3f>_3b.max){ |
|---|
| 380 | _3f=_3b.max; |
|---|
| 381 | _41=_3f-_3c.b1; |
|---|
| 382 | } |
|---|
| 383 | _3c.b2=_3f; |
|---|
| 384 | }else{ |
|---|
| 385 | _3f=_3c.b2-_3e; |
|---|
| 386 | if(_3f<_3b.min){ |
|---|
| 387 | _3f=_3b.min; |
|---|
| 388 | _41=_3f+_3c.b2; |
|---|
| 389 | } |
|---|
| 390 | _3c.b1=_3f; |
|---|
| 391 | } |
|---|
| 392 | if(_41!=null){ |
|---|
| 393 | _40=Math.floor(_41*_39.a/_39.b); |
|---|
| 394 | if(_3a.a==1){ |
|---|
| 395 | _3c.a2=_3c.a1+_40; |
|---|
| 396 | }else{ |
|---|
| 397 | _3c.a1=_3c.a1=_3c.a2-_40; |
|---|
| 398 | } |
|---|
| 399 | } |
|---|
| 400 | return _3c; |
|---|
| 401 | },drawArea:function(){ |
|---|
| 402 | var _42=this.calcW(); |
|---|
| 403 | var _43=this.calcH(); |
|---|
| 404 | var px="px"; |
|---|
| 405 | var _45=[this.areaCoords.x1+px,this.areaCoords.y1+px,_42+px,_43+px,this.areaCoords.x2+px,this.areaCoords.y2+px,(this.img.width-this.areaCoords.x2)+px,(this.img.height-this.areaCoords.y2)+px]; |
|---|
| 406 | var _46=this.selArea.style; |
|---|
| 407 | _46.left=_45[0]; |
|---|
| 408 | _46.top=_45[1]; |
|---|
| 409 | _46.width=_45[2]; |
|---|
| 410 | _46.height=_45[3]; |
|---|
| 411 | var _47=Math.ceil((_42-6)/2)+px; |
|---|
| 412 | var _48=Math.ceil((_43-6)/2)+px; |
|---|
| 413 | this.handleN.style.left=_47; |
|---|
| 414 | this.handleE.style.top=_48; |
|---|
| 415 | this.handleS.style.left=_47; |
|---|
| 416 | this.handleW.style.top=_48; |
|---|
| 417 | this.north.style.height=_45[1]; |
|---|
| 418 | var _49=this.east.style; |
|---|
| 419 | _49.top=_45[1]; |
|---|
| 420 | _49.height=_45[3]; |
|---|
| 421 | _49.left=_45[4]; |
|---|
| 422 | _49.width=_45[6]; |
|---|
| 423 | var _4a=this.south.style; |
|---|
| 424 | _4a.top=_45[5]; |
|---|
| 425 | _4a.height=_45[7]; |
|---|
| 426 | var _4b=this.west.style; |
|---|
| 427 | _4b.top=_45[1]; |
|---|
| 428 | _4b.height=_45[3]; |
|---|
| 429 | _4b.width=_45[0]; |
|---|
| 430 | this.subDrawArea(); |
|---|
| 431 | this.forceReRender(); |
|---|
| 432 | },forceReRender:function(){ |
|---|
| 433 | if(this.isIE||this.isWebKit){ |
|---|
| 434 | var n=document.createTextNode(" "); |
|---|
| 435 | var d,el,fixEL,i; |
|---|
| 436 | if(this.isIE){ |
|---|
| 437 | fixEl=this.selArea; |
|---|
| 438 | }else{ |
|---|
| 439 | if(this.isWebKit){ |
|---|
| 440 | fixEl=document.getElementsByClassName("imgCrop_marqueeSouth",this.imgWrap)[0]; |
|---|
| 441 | d=Builder.node("div",""); |
|---|
| 442 | d.style.visibility="hidden"; |
|---|
| 443 | var _4e=["SE","S","SW"]; |
|---|
| 444 | for(i=0;i<_4e.length;i++){ |
|---|
| 445 | el=document.getElementsByClassName("imgCrop_handle"+_4e[i],this.selArea)[0]; |
|---|
| 446 | if(el.childNodes.length){ |
|---|
| 447 | el.removeChild(el.childNodes[0]); |
|---|
| 448 | } |
|---|
| 449 | el.appendChild(d); |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | } |
|---|
| 453 | fixEl.appendChild(n); |
|---|
| 454 | fixEl.removeChild(n); |
|---|
| 455 | } |
|---|
| 456 | },startResize:function(e){ |
|---|
| 457 | this.startCoords=this.cloneCoords(this.areaCoords); |
|---|
| 458 | this.resizing=true; |
|---|
| 459 | this.resizeHandle=Event.element(e).classNames().toString().replace(/([^N|NE|E|SE|S|SW|W|NW])+/,""); |
|---|
| 460 | Event.stop(e); |
|---|
| 461 | },startDrag:function(e){ |
|---|
| 462 | this.selArea.show(); |
|---|
| 463 | this.clickCoords=this.getCurPos(e); |
|---|
| 464 | this.setAreaCoords({x1:this.clickCoords.x,y1:this.clickCoords.y,x2:this.clickCoords.x,y2:this.clickCoords.y},false,false,null); |
|---|
| 465 | this.dragging=true; |
|---|
| 466 | this.onDrag(e); |
|---|
| 467 | Event.stop(e); |
|---|
| 468 | },getCurPos:function(e){ |
|---|
| 469 | var el=this.imgWrap,wrapOffsets=Position.cumulativeOffset(el); |
|---|
| 470 | while(el.nodeName!="BODY"){ |
|---|
| 471 | wrapOffsets[1]-=el.scrollTop||0; |
|---|
| 472 | wrapOffsets[0]-=el.scrollLeft||0; |
|---|
| 473 | el=el.parentNode; |
|---|
| 474 | } |
|---|
| 475 | return curPos={x:Event.pointerX(e)-wrapOffsets[0],y:Event.pointerY(e)-wrapOffsets[1]}; |
|---|
| 476 | },onDrag:function(e){ |
|---|
| 477 | if(this.dragging||this.resizing){ |
|---|
| 478 | var _54=null; |
|---|
| 479 | var _55=this.getCurPos(e); |
|---|
| 480 | var _56=this.cloneCoords(this.areaCoords); |
|---|
| 481 | var _57={x:1,y:1}; |
|---|
| 482 | if(this.dragging){ |
|---|
| 483 | if(_55.x<this.clickCoords.x){ |
|---|
| 484 | _57.x=-1; |
|---|
| 485 | } |
|---|
| 486 | if(_55.y<this.clickCoords.y){ |
|---|
| 487 | _57.y=-1; |
|---|
| 488 | } |
|---|
| 489 | this.transformCoords(_55.x,this.clickCoords.x,_56,"x"); |
|---|
| 490 | this.transformCoords(_55.y,this.clickCoords.y,_56,"y"); |
|---|
| 491 | }else{ |
|---|
| 492 | if(this.resizing){ |
|---|
| 493 | _54=this.resizeHandle; |
|---|
| 494 | if(_54.match(/E/)){ |
|---|
| 495 | this.transformCoords(_55.x,this.startCoords.x1,_56,"x"); |
|---|
| 496 | if(_55.x<this.startCoords.x1){ |
|---|
| 497 | _57.x=-1; |
|---|
| 498 | } |
|---|
| 499 | }else{ |
|---|
| 500 | if(_54.match(/W/)){ |
|---|
| 501 | this.transformCoords(_55.x,this.startCoords.x2,_56,"x"); |
|---|
| 502 | if(_55.x<this.startCoords.x2){ |
|---|
| 503 | _57.x=-1; |
|---|
| 504 | } |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | if(_54.match(/N/)){ |
|---|
| 508 | this.transformCoords(_55.y,this.startCoords.y2,_56,"y"); |
|---|
| 509 | if(_55.y<this.startCoords.y2){ |
|---|
| 510 | _57.y=-1; |
|---|
| 511 | } |
|---|
| 512 | }else{ |
|---|
| 513 | if(_54.match(/S/)){ |
|---|
| 514 | this.transformCoords(_55.y,this.startCoords.y1,_56,"y"); |
|---|
| 515 | if(_55.y<this.startCoords.y1){ |
|---|
| 516 | _57.y=-1; |
|---|
| 517 | } |
|---|
| 518 | } |
|---|
| 519 | } |
|---|
| 520 | } |
|---|
| 521 | } |
|---|
| 522 | this.setAreaCoords(_56,false,e.shiftKey,_57,_54); |
|---|
| 523 | this.drawArea(); |
|---|
| 524 | Event.stop(e); |
|---|
| 525 | } |
|---|
| 526 | },transformCoords:function(_58,_59,_5a,_5b){ |
|---|
| 527 | var _5c=[_58,_59]; |
|---|
| 528 | if(_58>_59){ |
|---|
| 529 | _5c.reverse(); |
|---|
| 530 | } |
|---|
| 531 | _5a[_5b+"1"]=_5c[0]; |
|---|
| 532 | _5a[_5b+"2"]=_5c[1]; |
|---|
| 533 | },endCrop:function(){ |
|---|
| 534 | this.dragging=false; |
|---|
| 535 | this.resizing=false; |
|---|
| 536 | this.options.onEndCrop(this.areaCoords,{width:this.calcW(),height:this.calcH()}); |
|---|
| 537 | },subInitialize:function(){ |
|---|
| 538 | },subDrawArea:function(){ |
|---|
| 539 | }}; |
|---|
| 540 | Cropper.ImgWithPreview=Class.create(); |
|---|
| 541 | Object.extend(Object.extend(Cropper.ImgWithPreview.prototype,Cropper.Img.prototype),{subInitialize:function(){ |
|---|
| 542 | this.hasPreviewImg=false; |
|---|
| 543 | if(typeof (this.options.previewWrap)!="undefined"&&this.options.minWidth>0&&this.options.minHeight>0){ |
|---|
| 544 | this.previewWrap=$(this.options.previewWrap); |
|---|
| 545 | this.previewImg=this.img.cloneNode(false); |
|---|
| 546 | this.previewImg.id="imgCrop_"+this.previewImg.id; |
|---|
| 547 | this.options.displayOnInit=true; |
|---|
| 548 | this.hasPreviewImg=true; |
|---|
| 549 | this.previewWrap.addClassName("imgCrop_previewWrap"); |
|---|
| 550 | this.previewWrap.setStyle({width:this.options.minWidth+"px",height:this.options.minHeight+"px"}); |
|---|
| 551 | this.previewWrap.appendChild(this.previewImg); |
|---|
| 552 | } |
|---|
| 553 | },subDrawArea:function(){ |
|---|
| 554 | if(this.hasPreviewImg){ |
|---|
| 555 | var _5d=this.calcW(); |
|---|
| 556 | var _5e=this.calcH(); |
|---|
| 557 | var _5f={x:this.imgW/_5d,y:this.imgH/_5e}; |
|---|
| 558 | var _60={x:_5d/this.options.minWidth,y:_5e/this.options.minHeight}; |
|---|
| 559 | var _61={w:Math.ceil(this.options.minWidth*_5f.x)+"px",h:Math.ceil(this.options.minHeight*_5f.y)+"px",x:"-"+Math.ceil(this.areaCoords.x1/_60.x)+"px",y:"-"+Math.ceil(this.areaCoords.y1/_60.y)+"px"}; |
|---|
| 560 | var _62=this.previewImg.style; |
|---|
| 561 | _62.width=_61.w; |
|---|
| 562 | _62.height=_61.h; |
|---|
| 563 | _62.left=_61.x; |
|---|
| 564 | _62.top=_61.y; |
|---|
| 565 | } |
|---|
| 566 | }}); |
|---|
| 567 | |
|---|