root/afridex/plugins/fresh-page/js/cropper.js @ 21

Revision 21, 16.6 kB (checked in by admin, 18 years ago)
RevLine 
[21]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
19var CropDraggable=Class.create();
20Object.extend(Object.extend(CropDraggable.prototype,Draggable.prototype),{initialize:function(_1){
21this.options=Object.extend({drawMethod:function(){
22}},arguments[1]||{});
23this.element=$(_1);
24this.handle=this.element;
25this.delta=this.currentDelta();
26this.dragging=false;
27this.eventMouseDown=this.initDrag.bindAsEventListener(this);
28Event.observe(this.handle,"mousedown",this.eventMouseDown);
29Draggables.register(this);
30},draw:function(_2){
31var _3=Position.cumulativeOffset(this.element);
32var d=this.currentDelta();
33_3[0]-=d[0];
34_3[1]-=d[1];
35var p=[0,1].map(function(i){
36return (_2[i]-_3[i]-this.offset[i]);
37}.bind(this));
38this.options.drawMethod(p);
39}});
40var Cropper={};
41Cropper.Img=Class.create();
42Cropper.Img.prototype={initialize:function(_7,_8){
43this.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||{});
44this.img=$(_7);
45this.clickCoords={x:0,y:0};
46this.dragging=false;
47this.resizing=false;
48this.isWebKit=/Konqueror|Safari|KHTML/.test(navigator.userAgent);
49this.isIE=/MSIE/.test(navigator.userAgent);
50this.isOpera8=/Opera\s[1-8]/.test(navigator.userAgent);
51this.ratioX=0;
52this.ratioY=0;
53this.attached=false;
54this.fixedWidth=(this.options.maxWidth>0&&(this.options.minWidth>=this.options.maxWidth));
55this.fixedHeight=(this.options.maxHeight>0&&(this.options.minHeight>=this.options.maxHeight));
56if(typeof this.img=="undefined"){
57return;
58}
59$A(document.getElementsByTagName("script")).each(function(s){
60if(s.src.match(/cropper\.js/)){
61var _a=s.src.replace(/cropper\.js(.*)?/,"");
62var _b=document.createElement("link");
63_b.rel="stylesheet";
64_b.type="text/css";
65_b.href=_a+"../css/cropper.css";
66_b.media="screen";
67document.getElementsByTagName("head")[0].appendChild(_b);
68}
69});
70if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){
71var _c=this.getGCD(this.options.ratioDim.x,this.options.ratioDim.y);
72this.ratioX=this.options.ratioDim.x/_c;
73this.ratioY=this.options.ratioDim.y/_c;
74}
75this.subInitialize();
76if(this.img.complete||this.isWebKit){
77this.onLoad();
78}else{
79Event.observe(this.img,"load",this.onLoad.bindAsEventListener(this));
80}
81},getGCD:function(a,b){
82if(b==0){
83return a;
84}
85return this.getGCD(b,a%b);
86},onLoad:function(){
87var _f="imgCrop_";
88var _10=this.img.parentNode;
89var _11="";
90if(this.isOpera8){
91_11=" opera8";
92}
93this.imgWrap=Builder.node("div",{"class":_f+"wrap"+_11});
94this.north=Builder.node("div",{"class":_f+"overlay "+_f+"north"},[Builder.node("span")]);
95this.east=Builder.node("div",{"class":_f+"overlay "+_f+"east"},[Builder.node("span")]);
96this.south=Builder.node("div",{"class":_f+"overlay "+_f+"south"},[Builder.node("span")]);
97this.west=Builder.node("div",{"class":_f+"overlay "+_f+"west"},[Builder.node("span")]);
98var _12=[this.north,this.east,this.south,this.west];
99this.dragArea=Builder.node("div",{"class":_f+"dragArea"},_12);
100this.handleN=Builder.node("div",{"class":_f+"handle "+_f+"handleN"});
101this.handleNE=Builder.node("div",{"class":_f+"handle "+_f+"handleNE"});
102this.handleE=Builder.node("div",{"class":_f+"handle "+_f+"handleE"});
103this.handleSE=Builder.node("div",{"class":_f+"handle "+_f+"handleSE"});
104this.handleS=Builder.node("div",{"class":_f+"handle "+_f+"handleS"});
105this.handleSW=Builder.node("div",{"class":_f+"handle "+_f+"handleSW"});
106this.handleW=Builder.node("div",{"class":_f+"handle "+_f+"handleW"});
107this.handleNW=Builder.node("div",{"class":_f+"handle "+_f+"handleNW"});
108this.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"})]);
109this.imgWrap.appendChild(this.img);
110this.imgWrap.appendChild(this.dragArea);
111this.dragArea.appendChild(this.selArea);
112this.dragArea.appendChild(Builder.node("div",{"class":_f+"clickArea"}));
113_10.appendChild(this.imgWrap);
114this.startDragBind=this.startDrag.bindAsEventListener(this);
115Event.observe(this.dragArea,"mousedown",this.startDragBind);
116this.onDragBind=this.onDrag.bindAsEventListener(this);
117Event.observe(document,"mousemove",this.onDragBind);
118this.endCropBind=this.endCrop.bindAsEventListener(this);
119Event.observe(document,"mouseup",this.endCropBind);
120this.resizeBind=this.startResize.bindAsEventListener(this);
121this.handles=[this.handleN,this.handleNE,this.handleE,this.handleSE,this.handleS,this.handleSW,this.handleW,this.handleNW];
122this.registerHandles(true);
123if(this.options.captureKeys){
124this.keysBind=this.handleKeys.bindAsEventListener(this);
125Event.observe(document,"keypress",this.keysBind);
126}
127new CropDraggable(this.selArea,{drawMethod:this.moveArea.bindAsEventListener(this)});
128this.setParams();
129},registerHandles:function(_13){
130for(var i=0;i<this.handles.length;i++){
131var _15=$(this.handles[i]);
132if(_13){
133var _16=false;
134if(this.fixedWidth&&this.fixedHeight){
135_16=true;
136}else{
137if(this.fixedWidth||this.fixedHeight){
138var _17=_15.className.match(/([S|N][E|W])$/);
139var _18=_15.className.match(/(E|W)$/);
140var _19=_15.className.match(/(N|S)$/);
141if(_17){
142_16=true;
143}else{
144if(this.fixedWidth&&_18){
145_16=true;
146}else{
147if(this.fixedHeight&&_19){
148_16=true;
149}
150}
151}
152}
153}
154if(_16){
155_15.hide();
156}else{
157Event.observe(_15,"mousedown",this.resizeBind);
158}
159}else{
160_15.show();
161Event.stopObserving(_15,"mousedown",this.resizeBind);
162}
163}
164},setParams:function(){
165this.imgW=this.img.width;
166this.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();
173var _1a={x1:0,y1:0,x2:0,y2:0};
174var _1b=false;
175if(this.options.onloadCoords!=null){
176_1a=this.cloneCoords(this.options.onloadCoords);
177_1b=true;
178}else{
179if(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}
187this.setAreaCoords(_1a,false,false,1);
188if(this.options.displayOnInit&&_1b){
189this.selArea.show();
190this.drawArea();
191this.endCrop();
192}
193this.attached=true;
194},remove:function(){
195if(this.attached){
196this.attached=false;
197this.imgWrap.parentNode.insertBefore(this.img,this.imgWrap);
198this.imgWrap.parentNode.removeChild(this.imgWrap);
199Event.stopObserving(this.dragArea,"mousedown",this.startDragBind);
200Event.stopObserving(document,"mousemove",this.onDragBind);
201Event.stopObserving(document,"mouseup",this.endCropBind);
202this.registerHandles(false);
203if(this.options.captureKeys){
204Event.stopObserving(document,"keypress",this.keysBind);
205}
206}
207},reset:function(){
208if(!this.attached){
209this.onLoad();
210}else{
211this.setParams();
212}
213this.endCrop();
214},handleKeys:function(e){
215var dir={x:0,y:0};
216if(!this.dragging){
217switch(e.keyCode){
218case (37):
219dir.x=-1;
220break;
221case (38):
222dir.y=-1;
223break;
224case (39):
225dir.x=1;
226break;
227case (40):
228dir.y=1;
229break;
230}
231if(dir.x!=0||dir.y!=0){
232if(e.shiftKey){
233dir.x*=10;
234dir.y*=10;
235}
236this.moveArea([this.areaCoords.x1+dir.x,this.areaCoords.y1+dir.y]);
237Event.stop(e);
238}
239}
240},calcW:function(){
241return (this.areaCoords.x2-this.areaCoords.x1);
242},calcH:function(){
243return (this.areaCoords.y2-this.areaCoords.y1);
244},moveArea:function(_1e){
245this.setAreaCoords({x1:_1e[0],y1:_1e[1],x2:_1e[0]+this.calcW(),y2:_1e[1]+this.calcH()},true,false);
246this.drawArea();
247},cloneCoords:function(_1f){
248return {x1:_1f.x1,y1:_1f.y1,x2:_1f.x2,y2:_1f.y2};
249},setAreaCoords:function(_20,_21,_22,_23,_24){
250if(_21){
251var _25=_20.x2-_20.x1;
252var _26=_20.y2-_20.y1;
253if(_20.x1<0){
254_20.x1=0;
255_20.x2=_25;
256}
257if(_20.y1<0){
258_20.y1=0;
259_20.y2=_26;
260}
261if(_20.x2>this.imgW){
262_20.x2=this.imgW;
263_20.x1=this.imgW-_25;
264}
265if(_20.y2>this.imgH){
266_20.y2=this.imgH;
267_20.y1=this.imgH-_26;
268}
269}else{
270if(_20.x1<0){
271_20.x1=0;
272}
273if(_20.y1<0){
274_20.y1=0;
275}
276if(_20.x2>this.imgW){
277_20.x2=this.imgW;
278}
279if(_20.y2>this.imgH){
280_20.y2=this.imgH;
281}
282if(_23!=null){
283if(this.ratioX>0){
284this.applyRatio(_20,{x:this.ratioX,y:this.ratioY},_23,_24);
285}else{
286if(_22){
287this.applyRatio(_20,{x:1,y:1},_23,_24);
288}
289}
290var _27=[this.options.minWidth,this.options.minHeight];
291var _28=[this.options.maxWidth,this.options.maxHeight];
292if(_27[0]>0||_27[1]>0||_28[0]>0||_28[1]>0){
293var _29={a1:_20.x1,a2:_20.x2};
294var _2a={a1:_20.y1,a2:_20.y2};
295var _2b={min:0,max:this.imgW};
296var _2c={min:0,max:this.imgH};
297if((_27[0]!=0||_27[1]!=0)&&_22){
298if(_27[0]>0){
299_27[1]=_27[0];
300}else{
301if(_27[1]>0){
302_27[0]=_27[1];
303}
304}
305}
306if((_28[0]!=0||_28[0]!=0)&&_22){
307if(_28[0]>0&&_28[0]<=_28[1]){
308_28[1]=_28[0];
309}else{
310if(_28[1]>0&&_28[1]<=_28[0]){
311_28[0]=_28[1];
312}
313}
314}
315if(_27[0]>0){
316this.applyDimRestriction(_29,_27[0],_23.x,_2b,"min");
317}
318if(_27[1]>1){
319this.applyDimRestriction(_2a,_27[1],_23.y,_2c,"min");
320}
321if(_28[0]>0){
322this.applyDimRestriction(_29,_28[0],_23.x,_2b,"max");
323}
324if(_28[1]>1){
325this.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}
331this.areaCoords=_20;
332},applyDimRestriction:function(_2d,val,_2f,_30,_31){
333var _32;
334if(_31=="min"){
335_32=((_2d.a2-_2d.a1)<val);
336}else{
337_32=((_2d.a2-_2d.a1)>val);
338}
339if(_32){
340if(_2f==1){
341_2d.a2=_2d.a1+val;
342}else{
343_2d.a1=_2d.a2-val;
344}
345if(_2d.a1<_30.min){
346_2d.a1=_30.min;
347_2d.a2=val;
348}else{
349if(_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){
356var _37;
357if(_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){
371var _3c=Object.extend(_38,{});
372var _3d=_3c.a2-_3c.a1;
373var _3e=Math.floor(_3d*_39.b/_39.a);
374var _3f;
375var _40;
376var _41=null;
377if(_3a.b==1){
378_3f=_3c.b1+_3e;
379if(_3f>_3b.max){
380_3f=_3b.max;
381_41=_3f-_3c.b1;
382}
383_3c.b2=_3f;
384}else{
385_3f=_3c.b2-_3e;
386if(_3f<_3b.min){
387_3f=_3b.min;
388_41=_3f+_3c.b2;
389}
390_3c.b1=_3f;
391}
392if(_41!=null){
393_40=Math.floor(_41*_39.a/_39.b);
394if(_3a.a==1){
395_3c.a2=_3c.a1+_40;
396}else{
397_3c.a1=_3c.a1=_3c.a2-_40;
398}
399}
400return _3c;
401},drawArea:function(){
402var _42=this.calcW();
403var _43=this.calcH();
404var px="px";
405var _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];
406var _46=this.selArea.style;
407_46.left=_45[0];
408_46.top=_45[1];
409_46.width=_45[2];
410_46.height=_45[3];
411var _47=Math.ceil((_42-6)/2)+px;
412var _48=Math.ceil((_43-6)/2)+px;
413this.handleN.style.left=_47;
414this.handleE.style.top=_48;
415this.handleS.style.left=_47;
416this.handleW.style.top=_48;
417this.north.style.height=_45[1];
418var _49=this.east.style;
419_49.top=_45[1];
420_49.height=_45[3];
421_49.left=_45[4];
422_49.width=_45[6];
423var _4a=this.south.style;
424_4a.top=_45[5];
425_4a.height=_45[7];
426var _4b=this.west.style;
427_4b.top=_45[1];
428_4b.height=_45[3];
429_4b.width=_45[0];
430this.subDrawArea();
431this.forceReRender();
432},forceReRender:function(){
433if(this.isIE||this.isWebKit){
434var n=document.createTextNode(" ");
435var d,el,fixEL,i;
436if(this.isIE){
437fixEl=this.selArea;
438}else{
439if(this.isWebKit){
440fixEl=document.getElementsByClassName("imgCrop_marqueeSouth",this.imgWrap)[0];
441d=Builder.node("div","");
442d.style.visibility="hidden";
443var _4e=["SE","S","SW"];
444for(i=0;i<_4e.length;i++){
445el=document.getElementsByClassName("imgCrop_handle"+_4e[i],this.selArea)[0];
446if(el.childNodes.length){
447el.removeChild(el.childNodes[0]);
448}
449el.appendChild(d);
450}
451}
452}
453fixEl.appendChild(n);
454fixEl.removeChild(n);
455}
456},startResize:function(e){
457this.startCoords=this.cloneCoords(this.areaCoords);
458this.resizing=true;
459this.resizeHandle=Event.element(e).classNames().toString().replace(/([^N|NE|E|SE|S|SW|W|NW])+/,"");
460Event.stop(e);
461},startDrag:function(e){
462this.selArea.show();
463this.clickCoords=this.getCurPos(e);
464this.setAreaCoords({x1:this.clickCoords.x,y1:this.clickCoords.y,x2:this.clickCoords.x,y2:this.clickCoords.y},false,false,null);
465this.dragging=true;
466this.onDrag(e);
467Event.stop(e);
468},getCurPos:function(e){
469var el=this.imgWrap,wrapOffsets=Position.cumulativeOffset(el);
470while(el.nodeName!="BODY"){
471wrapOffsets[1]-=el.scrollTop||0;
472wrapOffsets[0]-=el.scrollLeft||0;
473el=el.parentNode;
474}
475return curPos={x:Event.pointerX(e)-wrapOffsets[0],y:Event.pointerY(e)-wrapOffsets[1]};
476},onDrag:function(e){
477if(this.dragging||this.resizing){
478var _54=null;
479var _55=this.getCurPos(e);
480var _56=this.cloneCoords(this.areaCoords);
481var _57={x:1,y:1};
482if(this.dragging){
483if(_55.x<this.clickCoords.x){
484_57.x=-1;
485}
486if(_55.y<this.clickCoords.y){
487_57.y=-1;
488}
489this.transformCoords(_55.x,this.clickCoords.x,_56,"x");
490this.transformCoords(_55.y,this.clickCoords.y,_56,"y");
491}else{
492if(this.resizing){
493_54=this.resizeHandle;
494if(_54.match(/E/)){
495this.transformCoords(_55.x,this.startCoords.x1,_56,"x");
496if(_55.x<this.startCoords.x1){
497_57.x=-1;
498}
499}else{
500if(_54.match(/W/)){
501this.transformCoords(_55.x,this.startCoords.x2,_56,"x");
502if(_55.x<this.startCoords.x2){
503_57.x=-1;
504}
505}
506}
507if(_54.match(/N/)){
508this.transformCoords(_55.y,this.startCoords.y2,_56,"y");
509if(_55.y<this.startCoords.y2){
510_57.y=-1;
511}
512}else{
513if(_54.match(/S/)){
514this.transformCoords(_55.y,this.startCoords.y1,_56,"y");
515if(_55.y<this.startCoords.y1){
516_57.y=-1;
517}
518}
519}
520}
521}
522this.setAreaCoords(_56,false,e.shiftKey,_57,_54);
523this.drawArea();
524Event.stop(e);
525}
526},transformCoords:function(_58,_59,_5a,_5b){
527var _5c=[_58,_59];
528if(_58>_59){
529_5c.reverse();
530}
531_5a[_5b+"1"]=_5c[0];
532_5a[_5b+"2"]=_5c[1];
533},endCrop:function(){
534this.dragging=false;
535this.resizing=false;
536this.options.onEndCrop(this.areaCoords,{width:this.calcW(),height:this.calcH()});
537},subInitialize:function(){
538},subDrawArea:function(){
539}};
540Cropper.ImgWithPreview=Class.create();
541Object.extend(Object.extend(Cropper.ImgWithPreview.prototype,Cropper.Img.prototype),{subInitialize:function(){
542this.hasPreviewImg=false;
543if(typeof (this.options.previewWrap)!="undefined"&&this.options.minWidth>0&&this.options.minHeight>0){
544this.previewWrap=$(this.options.previewWrap);
545this.previewImg=this.img.cloneNode(false);
546this.previewImg.id="imgCrop_"+this.previewImg.id;
547this.options.displayOnInit=true;
548this.hasPreviewImg=true;
549this.previewWrap.addClassName("imgCrop_previewWrap");
550this.previewWrap.setStyle({width:this.options.minWidth+"px",height:this.options.minHeight+"px"});
551this.previewWrap.appendChild(this.previewImg);
552}
553},subDrawArea:function(){
554if(this.hasPreviewImg){
555var _5d=this.calcW();
556var _5e=this.calcH();
557var _5f={x:this.imgW/_5d,y:this.imgH/_5e};
558var _60={x:_5d/this.options.minWidth,y:_5e/this.options.minHeight};
559var _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"};
560var _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
Note: See TracBrowser for help on using the browser.