openct-tasks/_common/modules/pemFioi/drag_lib-1.0.js

3 lines
18 KiB
JavaScript

function _action(t,e,i){this.dstCont=t,this.dstPos=e,this.dropType=i,this.sameAs=function(t){return this.dstCont==t.dstCont&&this.dstPos==t.dstPos&&this.dropType==t.dropType}}function action(t,e,i){return new _action(t,e,i)}function _component(t,e,i,s){var n=this;this.cx=t,this.cy=e,this.elems=i,this.paper=s,this.nbEl=this.elems.length;for(var r=0;r<this.nbEl;r++)if("text"==this.elems[r].type){var o=this.elems[r].getBBox();this.elems.push(s.rect(o.x,o.y,o.width,o.height).attr("fill","red").attr("opacity",0))}this.oldTransforms=new Array;for(var a=0;a<this.elems.length;a++)this.elems[a].toFront(),this.oldTransforms[a]=this.elems[a].transform(),this.elems[a].transform("t"+this.cx+","+this.cy+this.oldTransforms[a]);this.placeAt=function(t,e){this.cx=t,this.cy=e;for(var i=0;i<this.elems.length;i++)this.elems[i].transform("t"+this.cx+","+this.cy+this.oldTransforms[i]);return this};var l=function(t,e){return Raphael.animation({transform:"t"+n.cx+","+n.cy+n.oldTransforms[t]},e,"")};this.placeAtWithAnim=function(t,e,i){this.cx=t,this.cy=e;for(var s=0;s<this.elems.length;s++)this.elems[s].animate(l(s,i));return this},this.move=function(t,e){this.placeAt(this.cx+t,this.cy+e)},this.moveWithAnim=function(t,e,i){this.placeAt(this.cx+t,this.cy+e,i)},this.drag=function(t,e,i){n.hasReallyMoved=!1,this.startDrag=e,this.moveDrag=t,this.upDrag=i;for(var s=0;s<this.elems.length;s++)this.elems[s].drag(function(t,e){n.moveDrag(t,e)},function(){n.startDrag()},function(){n.upDrag()});return this},this.clone=function(){for(var t=new Array,e=0;e<this.nbEl;e++)t[e]=this.elems[e].clone(),void 0!=this.oldTransforms[e]&&(t[e]=t[e].attr("transform",this.oldTransforms[e]));return new _component(this.cx,this.cy,t,this.paper)},this.remove=function(){for(var t=0;t<this.elems.length;t++)this.elems[t].remove()},this.toFront=function(){for(var t=0;t<this.elems.length;t++)this.elems[t].toFront()},this.show=function(){for(var t=0;t<this.nbEl;t++)this.elems[t].attr("transparent")||this.elems[t].attr("opacity","1")},this.hide=function(){for(var t=0;t<this.nbEl;t++)this.elems[t].attr("opacity","0")},this.halfHide=function(){for(var t=0;t<this.nbEl;t++)this.elems[t].attr("transparent")||this.elems[t].attr("opacity","0.3")}}function component(t,e,i,s){return new _component(t,e,i,s)}function container(t){var e=t;if(void 0==e.dragAndDropSystem&&alert("no dragAndDropSystem is specified"),void 0==e.ident&&(e.ident=""),void 0==e.type&&(e.type="list"),"source"!=e.type&&"list"!=e.type&&alert("type should be 'source' or 'list'"),(void 0==e.cx||void 0==e.cy)&&alert("cx and cy are not specified"),void 0==e.widthPlace&&(e.widthPlace=40),void 0==e.heightPlace&&(e.heightPlace=40),void 0!=e.align?("top"!=e.align&&"bottom"!=e.align&&"left"!=e.align&&"right"!=e.align&&alert("align should be 'top' or 'bottom' or 'left' or 'right'"),"top"==e.align||"bottom"==e.align?e.direction="vertical":e.direction="horizontal"):void 0==e.direction?(e.direction="horizontal",e.align="left"):"vertical"==e.direction?e.align="top":"horizontal"==e.direction?e.align="left":alert("direction should be 'vertical' or 'horizontal' "),void 0==e.dragDisplayMode?e.dragDisplayMode="preview":"preview"!=e.dragDisplayMode&&"marker"!=e.dragDisplayMode&&alert("dragDisplayMode should be 'preview' or 'marker' "),void 0==e.placeBackgroundArray){var i=e.dragAndDropSystem.paper,s=e.widthPlace,n=e.heightPlace;e.placeBackgroundArray=[i.rect(-s/2,-n/2,s,n).attr("fill","blue")]}return"source"==e.type&&(void 0==e.dropMode&&(e.dropMode="replace"),e.nbPlaces=1,void 0==e.sourceElemArray&&alert("sourceElemArray should be defined")),"list"==e.type&&(void 0==e.dropMode&&(e.dropMode="insert"),void 0==e.nbPlaces&&(e.nbPlaces=5)),new _container(e.dragAndDropSystem,e.ident,e.cx,e.cy,e.nbPlaces,e.widthPlace,e.heightPlace,e.direction,e.align,e.dropMode,e.dragDisplayMode,e.placeBackgroundArray,e.type,e.sourceElemArray,e.places)}function _DragAndDropSystem(t){"use strict";this.paper=t,this.keepLastGoodAction=!0,this.containers=new Array,this.addContainer=function(t){return t.dragAndDropSystem=this,this.containers.push(container(t)),this.containers[this.containers.length-1]},this.removeContainer=function(t){for(var e=0;e<this.containers.length;e++)this.containers[e]==t&&(this.containers[e]=this.containers[this.containers.length-1],this.containers.pop())},this.addContainer({ident:"temporaryContainer",cx:-1e3,cy:-1e3,nbPlaces:1,widthPlace:10,heigthPlace:10,direction:"vertical",align:"top",dropMode:"replace",dragDiplayMode:"marker",placeBackgroundArray:[],type:"list"}),this.addDraggableElement=function(t,e,i,s){var n=new _draggableElement(t,e,i,s);s.draggableElement=n,e.draggableElements[i]=n;var r=this,o=function(){this.hasReallyMoved&&this.upDrag(),r.canBeTaken(this.draggableElement.container.ident,this.draggableElement.position)&&(this.startcx=this.cx,this.startcy=this.cy,this.hasReallyMoved=!1,this.toFront())},a=function(t,e){isNaN(t)||isNaN(e)||r.canBeTaken(this.draggableElement.container.ident,this.draggableElement.position)&&(this.placeAt(this.startcx+t,this.startcy+e),(Math.abs(this.cx-this.startcx)>5||Math.abs(this.cy-this.startcy)>5)&&(this.hasReallyMoved=!0,r.hasBeenTaken(n)),this.hasReallyMoved&&r.hasBeenMoved(n,this.cx,this.cy))},l=function(){if(r.canBeTaken(this.draggableElement.container.ident,this.draggableElement.position)){if(!this.hasReallyMoved)return void this.placeAt(this.startcx,this.startcy);this.hasReallyMoved=!1,r.hasBeenDropped(n,this.cx,this.cy)}};return s.drag(a,o,l),n},this.removeDraggableElement=function(t){for(var e=0;e<this.draggableElements.length;e++)this.draggableElements[e]==t&&(this.draggableElements[e]=this.draggableElements[this.draggableElements.length-1],this.draggableElements.pop())},this.getObjects=function(t){for(var e=0;e<this.containers.length;e++)if(this.containers[e].ident==t)return this.containers[e].getObjects()},this.insertObject=function(t,e,i){for(var s=0;s<this.containers.length;s++)this.containers[s].ident==t&&this.containers[s].createDraggable(i.ident,e,i.elements)},this.insertObjects=function(t,e,i){for(var s=0;s<this.containers.length;s++)if(this.containers[s].ident==t)for(var n=this.containers[s],r=0;r<i.length;r++){var o=i[r];null!=o&&n.createDraggable(o.ident,e+r,o.elements)}},this.removeObject=function(t,e){for(var i=0;i<this.containers.length;i++)if(this.containers[i].ident==t){var s=this.containers[i].draggableElements[e];this.containers[i].draggableElements[e]=null,s.remove()}},this.removeAllObjects=function(t){for(var e=0;e<this.containers.length;e++)if(this.containers[e].ident==t)for(var i=this.containers[e].draggableElements,s=0;s<i.length;s++){var n=i[s];null!=n&&(i[s]=null,n.remove())}},this.userActionToAction=function(t){for(var e=0;e<this.containers.length;e++)if(this.containers[e].ident==t.dstCont)return action(this.containers[e],t.dstPos,t.dropType);return action(null,t.dstPos,t.dropType)},this.getCorrespondingAction=function(t,e,i){for(var s=t.container,n=t.position,r=0;r<this.containers.length;r++){var o=this.containers[r];if(o.isInContainer(e,i)){var a=o.getCorrespondingAction(t,e,i);if(null==a)continue;var l=this.actionIfDropped(s.ident,n,a.dstCont.ident,a.dstPos,a.dropType);if(1==l)return a;if(0!=l)return this.userActionToAction(l)}}var l=this.actionIfDropped(s.ident,n,null,null,"insert");return 1==l?action(null,null,"insert"):0!=l?this.userActionToAction(l):this.keepLastGoodAction&&null!=this.lastDisplayedAction?this.lastDisplayedAction:action(s,n,s.dropMode)},this.hideIndicators=function(){for(var t=0;t<this.containers.length;t++)this.containers[t].hideIndicator()},this.updateDisplay=function(){for(var t=0;t<this.containers.length;t++)this.containers[t].updateDisplay()},this.updateIntermediateDisplay=function(t,e,i,s,n){for(var r=0;r<this.containers.length;r++)this.containers[r].updateIntermediateDisplay(t,e,i,s,n)},this.getElementOver=function(t,e,i){for(var s=0;s<this.containers.length;s++){var n=this.containers[s].getElementOver(t,e,i);if(null!=n)return n}return null},this.hasBeenTaken=function(t){},this.lastDisplayedAction=null,this.lastOver=-1,this.hasBeenMoved=function(t,e,i){var s=this.getCorrespondingAction(t,e,i),n=this.getElementOver(t,e,i);this.lastOver!==n&&(this.lastOver=n,null!=this.lastOver?this.over(t.container.ident,t.position,n.container.ident,n.position):this.over(t.container.ident,t.position,null,0)),null!=this.lastDisplayedAction&&s.sameAs(this.lastDisplayedAction)||(this.lastDisplayedAction=s,this.hideIndicators(),null!=s.dstCont&&(s.dstCont.showIndicator(s),t.component.toFront()),this.updateIntermediateDisplay(t.container,t.position,s.dstCont,s.dstPos,s.dropType))},this.hasBeenDropped=function(t,e,i){this.hideIndicators();var s=this.getCorrespondingAction(t,e,i),n=t.container,r=t.position;this.processDeplacement(n,r,s.dstCont,s.dstPos,s.dropType),this.lastDisplayedAction=null,this.lastOver=-1},this.canBeTaken=function(t,e){return!0},this.actionIfDropped=function(t,e,i,s,n){return!0},this.drop=function(t,e,i,s,n){},this.over=function(t,e,i,s){},this.actionIfEjected=function(t,e,i){return null},this.ejected=function(t,e,i){},this.processDeplacement=function(t,e,i,s,n){for(var r=new Array,o=0;o<this.containers.length;o++)r[o]=this.containers[o].getElementsAfterDrop(t,e,i,s,n);for(var a=null,o=0;o<this.containers.length;o++)if(null!=r[o][this.containers[o].nbPlaces]){var l=r[o][this.containers[o].nbPlaces],h=this.containers[o];a={refEl:l,previousCont:h,previousPos:this.containers[o].nbPlaces-1}}for(var c=-1,o=0;o<this.containers.length;o++)this.containers[o]==i&&(c=o);if(-1!=c&&"replace"==n&&null!=r[c][s]&&null!=i.draggableElements[s]&&r[c][s]!=i.draggableElements[s]){var l=i.draggableElements[s],h=this.containers[c],d=s;a={refEl:l,previousCont:h,previousPos:d}}null==i&&(a={refEl:t.draggableElements[e],previousCont:t,previousPos:e});for(var o=0;o<this.containers.length;o++)for(var p=this.containers[o],g=0;g<p.nbPlaces;g++)p.draggableElements[g]=r[o][g],null!=p.draggableElements[g]&&(p.draggableElements[g].container=p,p.draggableElements[g].position=g);this.updateDisplay(),null!=i?this.drop(t.ident,e,i.ident,s,n):this.drop(t.ident,e,null),null!=a&&this.manageEjection(a.refEl,a.previousCont,a.previousPos)},this.manageEjection=function(t,e,i){var s=this.actionIfEjected(t,e.ident,i);t.show(),null==s?t.remove():(s=this.userActionToAction(s),this.containers[0].draggableElements[0]=t,t.container=this.containers[0],t.position=0,this.processDeplacement(this.containers[0],0,s.dstCont,s.dstPos,s.dropType)),this.ejected(t,e.ident,i)}}function DragAndDropSystem(t){void 0==t.paper&&alert("paper should be defined");var e=new _DragAndDropSystem(t.paper);return void 0!=t.keepLastGoodAction&&(e.keepLastGoodAction=t.keepLastGoodAction),void 0!=t.canBeTaken&&(e.canBeTaken=t.canBeTaken),void 0!=t.actionIfDropped&&(e.actionIfDropped=t.actionIfDropped),void 0!=t.drop&&(e.drop=t.drop),void 0!=t.actionIfEjected&&(e.actionIfEjected=t.actionIfEjected),void 0!=t.ejected&&(e.ejected=t.ejected),void 0!=t.over&&(e.over=t.over),e}function _draggableElement(t,e,i,s){this.ident=t,this.container=e,this.position=i,this.component=s,this.crossShape=null,this.remove=function(){null!=this.crossShape&&this.crossShape.remove(),this.crossShape=null,this.component.remove()},this.cross=function(){null!=this.crossShape&&this.crossShape.remove(),this.component.halfHide()},this.show=function(){null!=this.crossShape&&this.crossShape.remove(),this.crossShape=null,this.component.show()},this.hide=function(){null!=this.crossShape&&this.crossShape.remove(),this.crossShape=null,this.component.hide()}}var _container=function(t,e,i,s,n,r,o,a,l,h,c,d,p,g,u){"use strict";this.dragAndDropSystem=t,this.ident=e,this.cx=i,this.cy=s,this.nbPlaces=n,this.widthPlace=r,this.heightPlace=o,this.direction=a,this.align=l,this.dropMode=h,this.dragDisplayMode=c,this.type=p,this.places=u,this.placeHolder=component(0,0,[t.paper.rect(-r/2,-o/2,r,o).attr({stroke:"yellow","stroke-width":"2","stroke-dasharray":"-"})],this.dragAndDropSystem.paper),this.placeHolder.hide(),this.sourceElemArray=g,this.sanityCheck=function(){"vertical"!=a&&"horizontal"!=a&&alert("direction should be 'vertical' or 'horizontal'!"),"vertical"==a&&"top"!=l&&"bottom"!=l&&alert("Since direction is vertical, align should be 'top' or 'bottom'"),"horizontal"==a&&"left"!=l&&"right"!=l&&alert("Since direction is horizontal, align should be 'left' or 'right'"),"replace"!=h&&"insert-replace"!=h&&"insert"!=h&&"insertBefore"!=h&&alert("dropMode should be 'replace' or 'insert' or 'insert-replace' or 'insertBefore'"),"preview"!=c&&"marker"!=c&&alert("dragDisplayMode should be 'preview' or 'marker' "),"list"!=p&&"source"!=p&&alert("type should be 'list' or 'source'")},this.sanityCheck(),this.placeCenter=function(t){var e=this.widthPlace,i=this.heightPlace;return void 0!=this.places?this.places[t]:"horizontal"==a?"left"==l?[this.cx+(2*t+1-this.nbPlaces)*e/2,this.cy]:[this.cx+(this.nbPlaces-2*t-1)*e/2,this.cy]:"top"==l?[this.cx,this.cy+(2*t+1-this.nbPlaces)*i/2]:[this.cx,this.cy+(this.nbPlaces-2*t-1)*i/2]},this.placeId=function(t,e){for(var i=0;i<this.nbPlaces;i++){var s=this.placeCenter(i),n=this.widthPlace,r=this.heightPlace;if(t>=s[0]-n/2&&t<=s[0]+n/2&&e>=s[1]-r/2&&e<=s[1]+r/2)return i}return-1},this.isInContainer=function(t,e){return-1!=this.placeId(t,e)},this.ratioPositionInPlace=function(t,e){var i=this.placeCenter(0),s=this.placeCenter(1),n=[t-i[0],e-i[1]],r=[s[0]-i[0],s[1]-i[1]],o=n[0]*r[0]+n[1]*r[1],a=parseFloat(o)/parseFloat(r[0]*r[0]+r[1]*r[1])+.5;return a-this.placeId(t,e)},this.getCorrespondingAction=function(t,e,i){var s=this.placeId(e,i),n=this.ratioPositionInPlace(e,i);return-1==s?null:"replace"==this.dropMode?action(this,s,"replace"):"insert-replace"==this.dropMode?.25>n?action(this,s,"insert"):n>.75&&s+1<this.nbPlaces?action(this,s+1,"insert"):action(this,s,"replace"):"insert"==this.dropMode?.25>n?action(this,s,"insert"):n>.75&&s+1<this.nbPlaces?action(this,s+1,"insert"):null:"insertBefore"==this.dropMode?.75>n?action(this,s,"insert"):n>.75&&s+1<this.nbPlaces?action(this,s+1,"insert"):null:void alert("dropMode ?")},this.getElementsAfterDrop=function(t,e,i,s,n){for(var r=[],o=0;o<this.nbPlaces;o++)r[o]=this.draggableElements[o];if(r[this.nbPlaces]=null,this==t)if("replace"==this.dropMode)r[e]=null;else for(o=e;o+1<=this.nbPlaces&&null!=this.draggableElements[o];)r[o]=r[o+1],o++;var a=t.draggableElements[e];if(this==i)if("replace"==n)r[s]=a;else{for(var l=s;l<this.nbPlaces&&null!=r[l];)l++;for(o=l;o>s;o--)r[o]=r[o-1];r[s]=a}return r},this.placeBG=component(0,0,d,this.dragAndDropSystem.paper);for(var m=0;m<this.nbPlaces;m++){var f=this.placeCenter(m);0==m?this.placeBG.placeAt(f[0],f[1]):this.placeBG.clone().placeAt(f[0],f[1])}this.draggableElements=[];for(var v=0;v<this.nbMax;v++)this.draggableElements[v]=null;if(this.createDraggable=function(t,e,i){this.dragAndDropSystem.addDraggableElement(t,this,e,new _component(this.placeCenter(e)[0],this.placeCenter(e)[1],i,this.dragAndDropSystem.paper)),"source"==this.type&&(this.sourceCompo=this.draggableElements[0].component.clone(),this.sourceCompo.move(-1e4,-1e4),this.sourceIdent=t,this.sourcePos=e)},"source"==this.type){var y=[];for(v=0;v<this.sourceElemArray.length;v++)y[v]=this.sourceElemArray[v].clone();component(this.cx,this.cy,y,this.dragAndDropSystem.paper),this.createDraggable(this.ident,0,this.sourceElemArray)}this.getObjects=function(){for(var t=[],e=0;e<this.nbPlaces;e++)null!=this.draggableElements[e]?t[e]=this.draggableElements[e].ident:t[e]=null;return t},this.getElementOver=function(t,e,i){for(var s=0;s<this.nbPlaces;s++)if(null!=this.draggableElements[s]&&this.draggableElements[s]!=t){var n=this.draggableElements[s];if(e>=n.component.cx-this.widthPlace/2-1&&e<=n.component.cx+this.widthPlace/2+1&&i>=n.component.cy-this.heightPlace/2-1&&i<=n.component.cy+this.heightPlace/2+1)return n}return null},this.indicator=null,this.showIndicator=function(t){if("marker"==this.dragDisplayMode){var e=this.dragAndDropSystem.paper,i=this.placeCenter(t.dstPos),s=this.widthPlace,n=this.heightPlace;if("replace"==t.dropType&&(this.indicator=e.rect(i[0]-s/2,i[1]-n/2,s,n).attr({stroke:"red","stroke-width":"4"})),"insert"==t.dropType){var r=this.placeCenter(t.dstPos-1);if("vertical"==this.direction){var o=(r[1]+i[1])/2;this.indicator=e.rect(i[0]-3*s/4,o,3*s/2,1).attr({stroke:"red","stroke-width":"4"})}else{var a=(r[0]+i[0])/2;this.indicator=e.rect(a,i[1]-3*n/4,1,3*n/2).attr({stroke:"red","stroke-width":"4"})}}}},this.hideIndicator=function(){null!=this.indicator&&this.indicator.remove(),this.indicator=null},this.updateSource=function(){if("source"==this.type&&null==this.draggableElements[0]){this.sourceCompo.placeAt(0,0);var t=this.sourceCompo.elems.length;for(v=this.sourceCompo.nbEl;t>v;v++)this.sourceCompo.elems[v].remove();for(v=this.sourceCompo.nbEl;t>v;v++)this.sourceCompo.elems.pop();this.createDraggable(this.sourceIdent,0,this.sourceCompo.elems)}},this.timeAnim=100,this.updateDisplay=function(){this.updateSource(),this.placeHolder.hide();for(var t=0;t<this.draggableElements.length;t++){var e=this.placeCenter(t);null!=this.draggableElements[t]&&(this.draggableElements[t].component.placeAtWithAnim(e[0],e[1],this.timeAnim),this.draggableElements[t].component.show())}},this.updateIntermediateDisplay=function(t,e,i,s,n){this.placeHolder.hide();var r,o,a=this.getElementsAfterDrop(t,e,i,s,n);if("preview"==this.dragDisplayMode)for(r=0;r<=this.nbPlaces;r++)o=this.placeCenter(r),null!=a[r]&&(a[r]==t.draggableElements[e]?(this.placeHolder.show(),this.placeHolder.placeAt(o[0],o[1]),this.placeHolder.toFront(),t.draggableElements[e].show(),t.draggableElements[e].component.toFront()):(a[r].component.placeAtWithAnim(o[0],o[1],this.timeAnim),a[r].show()));if("marker"==this.dragDisplayMode){if("replace"==this.dropMode)return;for(r=0;r<this.nbPlaces;r++)null!=this.draggableElements[r]&&this.draggableElements[r].show();if(this==t)for(var l=e;l+1<this.nbPlaces&&null!=this.draggableElements[l+1];)o=this.placeCenter(l),this.draggableElements[l+1].component.placeAtWithAnim(o[0],o[1],this.timeAnim),l++}null!=a[this.nbPlaces]&&a[this.nbPlaces].cross()}};
//# sourceMappingURL=drag-and-drop.min.js.map