PageRenderTime 94ms CodeModel.GetById 18ms app.highlight 70ms RepoModel.GetById 2ms app.codeStats 0ms

/wp-includes/js/crop/cropper.js

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