-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddi.min.js
8 lines (8 loc) · 3.47 KB
/
addi.min.js
1
2
3
4
5
6
7
8
/* https://www.axel-hahn.de/ */
var _addi_zIndex=100,addi=function(){return{_saveData:[],_dragClass:"draggable",_dragOpacity:.8,oFence:{bFullscreen:!0,top:0,left:0,width:window.innerWidth,height:window.innerHeight},_savstyles:{transition:"auto"},init:function(a){a&&(this._dragClass=a);if((a=document.getElementsByClassName(this._dragClass))&&a.length)for(var b=0;b<a.length;b++)this.initDiv(a[b],!1)},_fixVisiblePosition:function(a,b,c){this._updateFence(a.style.paddingLeft);var d=window.getComputedStyle(a),e=0+parseInt(d.borderLeftWidth)+
parseInt(d.borderRightWidth)+parseInt(d.marginLeft)+parseInt(d.marginRight)+parseInt(d.paddingLeft)+parseInt(d.paddingRight);d=0+parseInt(d.borderTopWidth)+parseInt(d.borderBottomWidth)+parseInt(d.marginTop)+parseInt(d.marginBottom)+parseInt(d.paddingTop)+parseInt(d.paddingBottom);b=Math.max(this.oFence.left,b);b=Math.min(this.oFence.left+this.oFence.width-a.clientWidth-e,b);c=Math.max(this.oFence.top,c);c=Math.min(this.oFence.top+this.oFence.height-a.clientHeight-d,c);return{xpos:b,ypos:c}},_getVarname:function(a){return"addi.saveddiv-"+
a},_styleSave:function(a){var b=window.getComputedStyle(a);void 0===this._saveData[a.id]&&(this._saveData[a.id]={});for(var c in this._savstyles)this._saveData[a.id][c]=b.getPropertyValue(c),a.style[c]=this._savstyles[c];return!0},_styleRestore:function(a){for(var b in this._savstyles)a.style[b]=this._saveData[a.id][b];return!0},_updateFence:function(){this.oFence.bFullscreen&&(this.oFence={bFullscreen:!0,top:0,left:0,width:window.innerWidth,height:window.innerHeight})},initDiv:function(a,b){var c=
a.id;if(!c)return!1;o=b?b:a;o.onmousedown=function(a){addi._isDragging=!0;addi.startMoving(document.getElementById(c),a)};o.onmouseup=function(){addi._isDragging=!1;addi.stopMoving(document.getElementById(c))};!1!==a.className.indexOf("saveposition")&&this.load(a)},load:function(a){var b=this._getVarname(a.id),c=a.currentStyle||window.getComputedStyle(a);c.zIndex&&0<c.zIndex&&(_addi_zIndex=Math.max(parseInt(c.zIndex),_addi_zIndex));a.style.zIndex=_addi_zIndex++;(b=localStorage.getItem(b)?JSON.parse(localStorage.getItem(b)):
!1)&&b.left&&b.top&&this.move(a,b.left,b.top,!0);return b},move:function(a,b,c,d){a.style.bottom="auto";b=d?{xpos:b,ypos:c}:this._fixVisiblePosition(a,b,c);a.style.left=b.xpos+"px";a.style.top=b.ypos+"px";this.save(a);return!0},save:function(a){aData={left:a.style.left.replace("px",""),top:a.style.top.replace("px","")};localStorage.setItem(this._getVarname(a.id),JSON.stringify(aData))},startMoving:function(a,b){if(!1===a.className.indexOf(this._dragClass))return!1;b=b||window.event;var c=b.clientX;
b=b.clientY;this._styleSave(a);document.body.style.userSelect="none";iDivWidth=parseInt(a.style.width);iDivHeight=parseInt(a.style.height);a.style.cursor="move";a.style.zIndex=_addi_zIndex++;iDivLeft=a.style.left?a.style.left.replace("px",""):a.offsetLeft;iDivTop=a.style.top?a.style.top.replace("px",""):a.offsetTop;var d=c-iDivLeft,e=b-iDivTop;document.onmousemove=function(b){b=b||window.event;var c=b.clientX-d;b=b.clientY-e;a.style.opacity=addi._dragOpacity;addi.move(a,c,b)};return!0},stopMoving:function(a){a.style.cursor=
"default";this._styleRestore(a);document.body.style.userSelect="auto";a.style.opacity=1;document.onmousemove=function(){}},reset:function(a,b){a||(a=this._dragClass);if((a=document.getElementsByClassName(a))&&a.length)for(var c=0;c<a.length;c++)this.resetDiv(a[c],b)},resetDiv:function(a,b){a.onmousemove=null;a.onmouseup=null;a.onmousedown=null;b&&localStorage.delItem(this._getVarname(a.id))}}}();