From 6da18fda387584a572e3584d94aab901f672864f Mon Sep 17 00:00:00 2001 From: Victor Manuel Agudelo Date: Thu, 30 May 2019 16:06:33 -0500 Subject: [PATCH] added new Event -onDelete added new method -focus (id) --- README.md | 6 +++++- jquery.selectareas.js | 27 +++++++++++++++++++++------ jquery.selectareas.min.js | 2 +- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bc2819c..196a87f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ Best regards minSize: [30, 30], // Minimum size of a selection maxSize: [400, 300], // Maximum size of a selection onChanged: $.noop, // fired when a selection is released - onChanging: $.noop // fired during the modification of a selection + onChanging: $.noop, // fired during the modification of a selection + onDeleted: $.noop // fired during the item deletion }); @@ -68,6 +69,7 @@ Here is a list of available options for selectAreas, with their *default value*: - **areas** (*[]*) : list of areas to add to the image from the beginning (id will be ignored) - **onChanging** (*null*) : triggered when the event "changing" is fired - **onChanged** (*null*) : triggered when the event "changed" is fired + - **onDeleted** (*null*) : triggered when the event "deleted" is fired - **onLoaded** (*null*) : triggered when the event "loaded" is fired - **width** (*0*) : When not 0, scale the image to this width (px). The coordinates of the areas on the full image can be retrieved with method relativeAreas() @@ -76,6 +78,7 @@ Three events are fired by the plugin: - **loaded** : fired when plugin is loaded - **changing** : fired during the modification of a selection. arguments : (event, id, areas) - **changed** : fired when a selection is released. arguments : (event, id, areas) + - **deleted** : fired when a selection is deleted. arguments : (event, id, areas) ## Methods Once you added a *selectAreas* plugin on an image, several method are exposed to help you @@ -88,3 +91,4 @@ manipulate and retrieve these areas: - **destroy ()** : remove the *selectAreas* plugin - **blurAll ()** : blur (unfocus) all the areas - **contains (point)** : return true or false whether or not a point ({x: X, y: Y}) is included in at least one area + - **focus (id)** : highlights an element by passing its id diff --git a/jquery.selectareas.js b/jquery.selectareas.js index 4665a02..df2715a 100755 --- a/jquery.selectareas.js +++ b/jquery.selectareas.js @@ -3,6 +3,10 @@ * @author 360Learning * @author Catalin Dogaru (https://github.com/cdog - http://code.tutsplus.com/tutorials/how-to-create-a-jquery-image-cropping-plugin-from-scratch-part-i--net-20994) * @author Adrien David-Sivelle (https://github.com/AdrienDS - Refactoring, Multiselections & Mobile compatibility) + * @updates Victor Manuel Agudelo (victor.agudelo@enterdev.com.co) + * changelog + * 2019-05-30: event onDelete,method focus (id) + * */ (function($) { $.imageArea = function(parent, id) { @@ -424,11 +428,11 @@ $.each($resizeHandlers, function(card, $handler) { $handler.remove(); }); - if ($btDelete) { - $btDelete.remove(); - } + $btDelete.remove(); parent._remove(id); fireEvent("changed"); + fireEvent("deleted"); + //alert('deleted'); }, getElementOffset = function (object) { var offset = $(object).offset(); @@ -474,7 +478,7 @@ .addClass("select-areas-background-area") .css({ background : "#fff url(" + $image.attr("src") + ") no-repeat", - backgroundSize : $image.width() + "px " + $image.height() + "px", + backgroundSize : $image.width() + "px", position : "absolute" }) .insertAfter($outline); @@ -573,7 +577,8 @@ overlayOpacity: 0.5, areas: [], onChanging: null, - onChanged: null + onChanged: null, + onDeleted: null }; this.options = $.extend(defaultOptions, customOptions); @@ -599,6 +604,10 @@ if (this.options.onChanged) { this.$image.on("changed", this.options.onChanged); } + + if (this.options.onDeleted) { + this.$image.on("deleted", this.options.onDeleted); + } if (this.options.onLoaded) { this.$image.on("loaded", this.options.onLoaded); } @@ -774,7 +783,6 @@ this.$trigger.remove(); this.$image.css("width", "").css("position", "").unwrap(); this.$image.removeData("mainImageSelectAreas"); - this.$image.off('changing changed loaded'); }; $.imageSelectAreas.prototype.areas = function () { @@ -819,6 +827,13 @@ }); return res; }; + + $.imageSelectAreas.prototype.focus = function (id) { + if (this._areas[id]) { + //this._areas[id].deleteSelection(); + this._areas[id].focus(); + } + }; $.selectAreas = function(object, options) { var $object = $(object); diff --git a/jquery.selectareas.min.js b/jquery.selectareas.min.js index 8b4a441..4a1536f 100644 --- a/jquery.selectareas.min.js +++ b/jquery.selectareas.min.js @@ -1 +1 @@ -!function(a){a.imageArea=function(b,c){var g,h,j,d=b.options,e=b.$image,f=b.$trigger,i={},k=!0,l=!0,m=[0,0],n=[0,0],o={id:c,x:0,y:0,z:0,height:0,width:0},p=function(){o.z=0,A("blur")},q=function(){b.blurAll(),o.z=100,A()},r=function(){return o},s=function(a){e.trigger(a,[o.id,b.areas()])},t=function(a){var b=a||window.event||{};b.cancelBubble=!0,b.returnValue=!1,b.stopPropagation&&b.stopPropagation(),b.preventDefault&&b.preventDefault()},u=function(){a.each(arguments,function(a,b){v(b)})},v=function(b,c){var d,e;switch(b){case"start":d="mousedown",e="touchstart";break;case"move":d="mousemove",e="touchmove";break;case"stop":d="mouseup",e="touchend";break;default:return}c&&jQuery.isFunction(c)?a(window.document).on(d,c).on(e,c):a(window.document).off(d).off(e)},w=function(){g.css({cursor:"default",width:o.width,height:o.height,left:o.x,top:o.y,"z-index":o.z}),h.css({backgroundPosition:-o.x-1+"px "+(-o.y-1)+"px",cursor:d.allowMove?"move":"default",width:o.width-2>0?o.width-2:0,height:o.height-2>0?o.height-2:0,left:o.x+1,top:o.y+1,"z-index":o.z+2})},x=function(b){d.allowResize&&(b?a.each(i,function(a,b){var c,d,e=Math.round(b.width()/2),f=Math.round(b.height()/2),g=a[0],h=a[a.length-1];c="n"===g?-f:"s"===g?o.height-f-1:Math.round(o.height/2)-f-1,d="e"===h?o.width-e-1:"w"===h?-e:Math.round(o.width/2)-e-1,b.css({display:"block",left:o.x+d,top:o.y+c,"z-index":o.z+1})}):a(".select-areas-resize-handler").each(function(){a(this).css({display:"none"})}))},y=function(a){j&&j.css({display:a?"block":"none",left:o.x+o.width+1,top:o.y-j.outerHeight()-1,"z-index":o.z+1})},z=function(a){g.css({cursor:a}),h.css({cursor:a})},A=function(a){switch(a){case"startSelection":b._refresh(),w(),x(),y(!0);break;case"pickSelection":case"pickResizeHandler":x();break;case"resizeSelection":w(),x(),z("crosshair"),y(!0);break;case"moveSelection":w(),x(),z("move"),y(!0);break;case"blur":w(),x(),y();break;default:w(),x(!0),y(!0)}},B=function(a){t(a),o.width=d.minSize[0],o.height=d.minSize[1],q(),v("move",E),v("stop",H),n=K(a),n[0]+o.width>e.width()&&(n[0]=e.width()-o.width),n[1]+o.height>e.height()&&(n[1]=e.height()-o.height),o.x=n[0],o.y=n[1],A("startSelection")},C=function(a){t(a),q(),v("move",F),v("stop",H);var b=K(a);m[0]=b[0]-o.x,m[1]=b[1]-o.y,A("pickSelection")},D=function(a){t(a),q();var b=a.target.className.split(" ")[1];"w"===b[b.length-1]&&(n[0]+=o.width,o.x=n[0]-o.width),"n"===b[0]&&(n[1]+=o.height,o.y=n[1]-o.height),"n"===b||"s"===b?k=!1:"e"!==b&&"w"!==b||(l=!1),v("move",E),v("stop",H),A("pickResizeHandler")},E=function(a){t(a),q();var b=K(a),c=b[1]-n[1],f=b[0]-n[0];Math.abs(f)=0?d.minSize[0]:-d.minSize[0]),Math.abs(c)=0?d.minSize[1]:-d.minSize[1]),(n[0]+f<0||n[0]+f>e.width())&&(f=-f),(n[1]+c<0||n[1]+c>e.height())&&(c=-c),d.maxSize[0]>d.minSize[0]&&d.maxSize[1]>d.minSize[1]&&(Math.abs(f)>d.maxSize[0]&&(f=f>=0?d.maxSize[0]:-d.maxSize[0]),Math.abs(c)>d.maxSize[1]&&(c=c>=0?d.maxSize[1]:-d.maxSize[1])),k&&(o.width=f),l&&(o.height=c),d.aspectRatio&&(f>0&&c>0||f<0&&c<0?k?c=Math.round(f/d.aspectRatio):f=Math.round(c*d.aspectRatio):k?c=-Math.round(f/d.aspectRatio):f=-Math.round(c*d.aspectRatio),n[0]+f>e.width()&&(f=e.width()-n[0],c=c>0?Math.round(f/d.aspectRatio):-Math.round(f/d.aspectRatio)),n[1]+c<0&&(c=-n[1],f=f>0?-Math.round(c*d.aspectRatio):Math.round(c*d.aspectRatio)),n[1]+c>e.height()&&(c=e.height()-n[1],f=f>0?Math.round(c*d.aspectRatio):-Math.round(c*d.aspectRatio)),o.width=f,o.height=c),o.width<0?(o.width=Math.abs(o.width),o.x=n[0]-o.width):o.x=n[0],o.height<0?(o.height=Math.abs(o.height),o.y=n[1]-o.height):o.y=n[1],s("changing"),A("resizeSelection")},F=function(a){if(t(a),d.allowMove){q();var b=K(a);G({x:b[0]-m[0],y:b[1]-m[1]}),s("changing")}},G=function(a){a.x>0?a.x+o.width0?a.y+o.heighte.width()?e.width():c,d=d<0?0:d>e.height()?e.height():d,[c,d]};if(g=a('
').css({opacity:d.outlineOpacity,position:"absolute"}).insertAfter(f),h=a("
").addClass("select-areas-background-area").css({background:"#fff url("+e.attr("src")+") no-repeat",backgroundSize:e.width()+"px "+e.height()+"px",position:"absolute"}).insertAfter(g),d.allowResize&&a.each(["nw","n","ne","e","se","s","sw","w"],function(b,c){i[c]=a('
').css({opacity:.5,position:"absolute",cursor:c+"-resize"}).insertAfter(h).mousedown(D).bind("touchstart",D)}),d.allowDelete){var L=function(a){return a.click(I).bind("touchstart",I).bind("tap",I),a};j=L(a('
')).append(L(a('
'))).insertAfter(h)}return d.allowMove&&h.mousedown(C).bind("touchstart",C),q(),{getData:r,startSelection:B,deleteSelection:I,options:d,blur:p,focus:q,nudge:function(a){a.x=o.x,a.y=o.y,a.d&&(a.y=o.y+a.d),a.u&&(a.y=o.y-a.u),a.l&&(a.x=o.x-a.l),a.r&&(a.x=o.x+a.r),G(a),s("changed")},set:function(b,c){o=a.extend(o,b),n[0]=o.x,n[1]=o.y,c||s("changed")},contains:function(a){return a.x>=o.x&&a.x<=o.x+o.width&&a.y>=o.y&&a.y<=o.y+o.height}}},a.imageSelectAreas=function(){},a.imageSelectAreas.prototype.init=function(b,c){var d=this,e={allowEdit:!0,allowMove:!0,allowResize:!0,allowSelect:!0,allowDelete:!0,allowNudge:!0,aspectRatio:0,minSize:[0,0],maxSize:[0,0],width:0,maxAreas:0,outlineOpacity:.5,overlayOpacity:.5,areas:[],onChanging:null,onChanged:null};this.options=a.extend(e,c),this.options.allowEdit||(this.options.allowSelect=this.options.allowMove=this.options.allowResize=this.options.allowDelete=!1),this._areas={},this.$image=a(b),this.ratio=1,this.options.width&&this.$image.width()&&this.options.width!==this.$image.width()&&(this.ratio=this.options.width/this.$image.width(),this.$image.width(this.options.width)),this.options.onChanging&&this.$image.on("changing",this.options.onChanging),this.options.onChanged&&this.$image.on("changed",this.options.onChanged),this.options.onLoaded&&this.$image.on("loaded",this.options.onLoaded),this.$holder=a("
").css({position:"relative",width:this.$image.width(),height:this.$image.height()}),this.$image.wrap(this.$holder).css({position:"absolute"}),this.$overlay=a('
').css({opacity:this.options.overlayOpacity,position:"absolute",width:this.$image.width(),height:this.$image.height()}).insertAfter(this.$image),this.$trigger=a("
").css({backgroundColor:"#000000",opacity:0,position:"absolute",width:this.$image.width(),height:this.$image.height()}).insertAfter(this.$overlay),a.each(this.options.areas,function(a,b){d._add(b,!0)}),this.blurAll(),this._refresh(),this.options.allowSelect&&this.$trigger.mousedown(a.proxy(this.newArea,this)).on("touchstart",a.proxy(this.newArea,this)),this.options.allowNudge&&a("html").keydown(function(a){var e,b={37:"l",38:"u",39:"r",40:"d"},c=b[a.which];if(c&&(d._eachArea(function(a){if(100===a.getData().z)return e=a,!1}),e)){var f={};f[c]=1,e.nudge(f)}})},a.imageSelectAreas.prototype._refresh=function(){var a=this.areas().length;this.$overlay.css({display:a?"block":"none"}),a?this.$image.addClass("blurred"):this.$image.removeClass("blurred"),this.$trigger.css({cursor:this.options.allowSelect?"crosshair":"default"})},a.imageSelectAreas.prototype._eachArea=function(b){a.each(this._areas,function(a,c){if(c)return b(c,a)})},a.imageSelectAreas.prototype._remove=function(a){delete this._areas[a],this._refresh()},a.imageSelectAreas.prototype.remove=function(a){this._areas[a]&&this._areas[a].deleteSelection()},a.imageSelectAreas.prototype.newArea=function(b){var c=-1;return this.blurAll(),this.options.maxAreas&&this.options.maxAreas<=this.areas().length?c:(this._eachArea(function(a,b){c=Math.max(c,parseInt(b,10))}),c+=1,this._areas[c]=a.imageArea(this,c),b&&this._areas[c].startSelection(b),c)},a.imageSelectAreas.prototype.set=function(a,b,c){this._areas[a]&&(b.id=a,this._areas[a].set(b,c),this._areas[a].focus())},a.imageSelectAreas.prototype._add=function(a,b){var c=this.newArea();this.set(c,a,b)},a.imageSelectAreas.prototype.add=function(b){var c=this;this.blurAll(),a.isArray(b)?a.each(b,function(a,b){c._add(b)}):this._add(b),this._refresh(),this.options.allowSelect||this.options.allowMove||this.options.allowResize||this.options.allowDelete||this.blurAll()},a.imageSelectAreas.prototype.reset=function(){var a=this;this._eachArea(function(b,c){a.remove(c)}),this._refresh()},a.imageSelectAreas.prototype.destroy=function(){this.reset(),this.$holder.remove(),this.$overlay.remove(),this.$trigger.remove(),this.$image.css("width","").css("position","").unwrap(),this.$image.removeData("mainImageSelectAreas"),this.$image.off("changing changed loaded")},a.imageSelectAreas.prototype.areas=function(){var a=[];return this._eachArea(function(b){a.push(b.getData())}),a},a.imageSelectAreas.prototype.relativeAreas=function(){for(var b=this.areas(),c=[],d=this.ratio,e=function(a){return Math.floor(a/d)},f=0;fm.width())&&(a=-a),(x[1]+i<0||x[1]+i>m.height())&&(i=-i),f.maxSize[0]>f.minSize[0]&&f.maxSize[1]>f.minSize[1]&&(Math.abs(a)>f.maxSize[0]&&(a=0<=a?f.maxSize[0]:-f.maxSize[0]),Math.abs(i)>f.maxSize[1]&&(i=0<=i?f.maxSize[1]:-f.maxSize[1])),v&&(b.width=a),S&&(b.height=i),f.aspectRatio&&(0m.width()&&(a=m.width()-x[0],i=0m.height()&&(i=m.height()-x[1],a=0m.width()?m.width():i,a=a<0?0:a>m.height()?m.height():a]};if(u=I('
').css({opacity:f.outlineOpacity,position:"absolute"}).insertAfter(w),g=I("
").addClass("select-areas-background-area").css({background:"#fff url("+m.attr("src")+") no-repeat",backgroundSize:m.width()+"px",position:"absolute"}).insertAfter(u),f.allowResize&&I.each(["nw","n","ne","e","se","s","sw","w"],function(e,t){y[t]=I('
').css({opacity:.5,position:"absolute",cursor:t+"-resize"}).insertAfter(g).mousedown(l).bind("touchstart",l)}),f.allowDelete){function C(e){return e.click(d).bind("touchstart",d).bind("tap",d),e}p=C(I('
')).append(C(I('
'))).insertAfter(g)}return f.allowMove&&g.mousedown(e).bind("touchstart",e),o(),{getData:function(){return b},startSelection:function(e){n(e),b.width=f.minSize[0],b.height=f.minSize[1],o(),z("move",_),z("stop",R),(x=D(e))[0]+b.width>m.width()&&(x[0]=m.width()-b.width),x[1]+b.height>m.height()&&(x[1]=m.height()-b.height),b.x=x[0],b.y=x[1],$("startSelection")},deleteSelection:d,options:f,blur:function(){b.z=0,$("blur")},focus:o,nudge:function(e){e.x=b.x,e.y=b.y,e.d&&(e.y=b.y+e.d),e.u&&(e.y=b.y-e.u),e.l&&(e.x=b.x-e.l),e.r&&(e.x=b.x+e.r),k(e),s("changed")},set:function(e,t){b=I.extend(b,e),x[0]=b.x,x[1]=b.y,t||s("changed")},contains:function(e){return e.x>=b.x&&e.x<=b.x+b.width&&e.y>=b.y&&e.y<=b.y+b.height}}},I.imageSelectAreas=function(){},I.imageSelectAreas.prototype.init=function(e,t){var o=this;this.options=I.extend({allowEdit:!0,allowMove:!0,allowResize:!0,allowSelect:!0,allowDelete:!0,allowNudge:!0,aspectRatio:0,minSize:[0,0],maxSize:[0,0],width:0,maxAreas:0,outlineOpacity:.5,overlayOpacity:.5,areas:[],onChanging:null,onChanged:null,onDeleted:null},t),this.options.allowEdit||(this.options.allowSelect=this.options.allowMove=this.options.allowResize=this.options.allowDelete=!1),this._areas={},this.$image=I(e),this.ratio=1,this.options.width&&this.$image.width()&&this.options.width!==this.$image.width()&&(this.ratio=this.options.width/this.$image.width(),this.$image.width(this.options.width)),this.options.onChanging&&this.$image.on("changing",this.options.onChanging),this.options.onChanged&&this.$image.on("changed",this.options.onChanged),this.options.onDeleted&&this.$image.on("deleted",this.options.onDeleted),this.options.onLoaded&&this.$image.on("loaded",this.options.onLoaded),this.$holder=I("
").css({position:"relative",width:this.$image.width(),height:this.$image.height()}),this.$image.wrap(this.$holder).css({position:"absolute"}),this.$overlay=I('
').css({opacity:this.options.overlayOpacity,position:"absolute",width:this.$image.width(),height:this.$image.height()}).insertAfter(this.$image),this.$trigger=I("
").css({backgroundColor:"#000000",opacity:0,position:"absolute",width:this.$image.width(),height:this.$image.height()}).insertAfter(this.$overlay),I.each(this.options.areas,function(e,t){o._add(t,!0)}),this.blurAll(),this._refresh(),this.options.allowSelect&&this.$trigger.mousedown(I.proxy(this.newArea,this)).on("touchstart",I.proxy(this.newArea,this)),this.options.allowNudge&&I("html").keydown(function(e){var t,i={37:"l",38:"u",39:"r",40:"d"}[e.which];if(i&&(o._eachArea(function(e){if(100===e.getData().z)return t=e,!1}),t)){var a={};a[i]=1,t.nudge(a)}})},I.imageSelectAreas.prototype._refresh=function(){var e=this.areas().length;this.$overlay.css({display:e?"block":"none"}),e?this.$image.addClass("blurred"):this.$image.removeClass("blurred"),this.$trigger.css({cursor:this.options.allowSelect?"crosshair":"default"})},I.imageSelectAreas.prototype._eachArea=function(i){I.each(this._areas,function(e,t){if(t)return i(t,e)})},I.imageSelectAreas.prototype._remove=function(e){delete this._areas[e],this._refresh()},I.imageSelectAreas.prototype.remove=function(e){this._areas[e]&&this._areas[e].deleteSelection()},I.imageSelectAreas.prototype.newArea=function(e){var i=-1;return this.blurAll(),this.options.maxAreas&&this.options.maxAreas<=this.areas().length||(this._eachArea(function(e,t){i=Math.max(i,parseInt(t,10))}),i+=1,this._areas[i]=I.imageArea(this,i),e&&this._areas[i].startSelection(e)),i},I.imageSelectAreas.prototype.set=function(e,t,i){this._areas[e]&&(t.id=e,this._areas[e].set(t,i),this._areas[e].focus())},I.imageSelectAreas.prototype._add=function(e,t){var i=this.newArea();this.set(i,e,t)},I.imageSelectAreas.prototype.add=function(e){var i=this;this.blurAll(),I.isArray(e)?I.each(e,function(e,t){i._add(t)}):this._add(e),this._refresh(),this.options.allowSelect||this.options.allowMove||this.options.allowResize||this.options.allowDelete||this.blurAll()},I.imageSelectAreas.prototype.reset=function(){var i=this;this._eachArea(function(e,t){i.remove(t)}),this._refresh()},I.imageSelectAreas.prototype.destroy=function(){this.reset(),this.$holder.remove(),this.$overlay.remove(),this.$trigger.remove(),this.$image.css("width","").css("position","").unwrap(),this.$image.removeData("mainImageSelectAreas")},I.imageSelectAreas.prototype.areas=function(){var t=[];return this._eachArea(function(e){t.push(e.getData())}),t},I.imageSelectAreas.prototype.relativeAreas=function(){function e(e){return Math.floor(e/a)}for(var t=this.areas(),i=[],a=this.ratio,o=0;o