// Jcrop API methods $.extend(Jcrop.prototype,{ //init: function(){{{ init: function(){ this.event = new this.opt.eventManagerComponent(this); this.ui.keyboard = new this.opt.keyboardComponent(this); this.ui.manager = new this.opt.stagemanagerComponent(this); this.applyFilters(); if ($.Jcrop.supportsTouch) new $.Jcrop.component.Touch(this); this.initEvents(); }, //}}} // applySizeConstraints: function(){{{ applySizeConstraints: function(){ var o = this.opt, img = this.opt.imgsrc; if (img){ var iw = img.naturalWidth || img.width, ih = img.naturalHeight || img.height, bw = o.boxWidth || iw, bh = o.boxHeight || ih; if (img && ((iw > bw) || (ih > bh))){ var bx = Jcrop.getLargestBox(iw/ih,bw,bh); $(img).width(bx[0]).height(bx[1]); this.resizeContainer(bx[0],bx[1]); this.opt.xscale = iw / bx[0]; this.opt.yscale = ih / bx[1]; } } if (this.opt.trueSize){ var dw = this.opt.trueSize[0]; var dh = this.opt.trueSize[1]; var cs = this.getContainerSize(); this.opt.xscale = dw / cs[0]; this.opt.yscale = dh / cs[1]; } }, // }}} initComponent: function(name){ if (Jcrop.component[name]) { var args = Array.prototype.slice.call(arguments); var obj = new Jcrop.component[name]; args.shift(); args.unshift(this); obj.init.apply(obj,args); return obj; } }, // setOptions: function(opt){{{ setOptions: function(opt,proptype){ if (!$.isPlainObject(opt)) opt = {}; $.extend(this.opt,opt); // Handle a setSelect value if (this.opt.setSelect) { // If there is no current selection // passing setSelect will create one if (!this.ui.multi.length) this.newSelection(); // Use these values to update the current selection this.setSelect(this.opt.setSelect); // Set to null so it doesn't get called again this.opt.setSelect = null; } this.event.trigger('configupdate'); return this; }, // }}} //destroy: function(){{{ destroy: function(){ if (this.opt.imgsrc) { this.container.before(this.opt.imgsrc); this.container.remove(); $(this.opt.imgsrc).removeData('Jcrop').show(); } else { // @todo: more elegant destroy() process for non-image containers this.container.remove(); } }, // }}} // applyFilters: function(){{{ applyFilters: function(){ var obj; for(var i=0,f=this.opt.applyFilters,l=f.length; i this.elw) { b.x2 = this.elw; b.x = b.x2 - b.w; } if (b.y < 0) { b.y2 = b.h; b.y = 0; } else if (b.y2 > this.elh) { b.y2 = this.elh; b.y = b.y2 - b.h; } s.element.trigger('cropstart',[s,this.unscale(b)]); s.updateRaw(b,'move'); s.element.trigger('cropend',[s,this.unscale(b)]); }, // }}} // refresh: function(){{{ refresh: function(){ for(var i=0,s=this.ui.multi,l=s.length;i 1) && (this.ui.selection.canDelete)) return this.deleteSelection(); }, // }}} // deleteSelection: function(){{{ deleteSelection: function(){ if (this.ui.selection) { this.removeSelection(this.ui.selection); if (this.ui.multi.length) this.ui.multi[0].focus(); this.ui.selection.refresh(); } }, // }}} // animateTo: function(box){{{ animateTo: function(box){ if (this.ui.selection) this.ui.selection.animateTo(box); return this; }, // }}} // setselect: function(box){{{ setSelect: function(box){ if (this.ui.selection) this.ui.selection.update(Jcrop.wrapFromXywh(box)); return this; }, // }}} //startDrag: function(){{{ startDrag: function(){ var t = this; return function(e){ var $targ = $(e.target); var selection = $targ.closest('.'+t.opt.css_selection).data('selection'); var ord = $targ.data('ord'); t.container.trigger('cropstart',[selection,t.unscale(selection.get())]); selection.startDrag(e,ord); return false; }; }, //}}} // getContainerSize: function(){{{ getContainerSize: function(){ return [ this.container.width(), this.container.height() ]; }, // }}} // resizeContainer: function(w,h){{{ resizeContainer: function(w,h){ this.container.width(w).height(h); this.refresh(); }, // }}} // setImage: function(src,cb){{{ setImage: function(src,cb){ var t = this, targ = t.opt.imgsrc; if (!targ) return false; new $.Jcrop.component.ImageLoader(src,null,function(w,h){ t.resizeContainer(w,h); targ.src = src; $(targ).width(w).height(h); t.applySizeConstraints(); t.refresh(); t.container.trigger('cropimage',[t,targ]); if (typeof cb == 'function') cb.call(t,w,h); }); }, // }}} // update: function(b){{{ update: function(b){ if (this.ui.selection) this.ui.selection.update(b); } // }}} });