Skip to content

Commit

Permalink
Calculations fix (added rounding).
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Mar 30, 2020
1 parent e3dc508 commit 425d623
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
12 changes: 6 additions & 6 deletions assets/navigationMenu.min.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

return <<<'EOT'
var ivoPetkov=ivoPetkov||{};ivoPetkov.bearFrameworkAddons=ivoPetkov.bearFrameworkAddons||{};
ivoPetkov.bearFrameworkAddons.navigationMenu=ivoPetkov.bearFrameworkAddons.navigationMenu||function(){return{make:function(x,u,y){var d=document.getElementById(x),b=null,p=!1,v=null,k=function(){if(!p){var f=d.getBoundingClientRect(),e=d.getAttribute(u);null===e&&(e="none");var k=JSON.stringify([e,f.top,f.left,f.width,f.height]);if(v!==k){p=!0;d.style.overflow="hidden";var r=function(c,d){for(var f=d.childNodes,k=f.length,g=0;g<k;g++){var l=f[g];if(1<l.childNodes.length){var a=l.firstChild;"a"===
a.tagName.toLowerCase()&&("none"===e?("undefined"!==typeof a.nmts&&delete a.nmts,"undefined"!==typeof a.nmtsh&&(a.removeEventListener("touchstart",a.nmtsh),delete a.nmtsh)):"undefined"===typeof a.nmtsh&&(a.nmtsh=function(a){a=this.parentNode;"undefined"!==typeof this.nmts?(a===b?a.lastChild.style.display="none":0<this.htmlnavth.length&&(this.href=this.htmlnavth),delete this.nmts):(a===b?a.lastChild.style.removeProperty("display"):("undefined"===typeof this.htmlnavth&&(this.htmlnavth=this.href),this.href=
"javascript:void(0);"),this.nmts=1)},a.addEventListener("touchstart",a.nmtsh,!1)));a=l.lastChild;if("none"===e)a.style.removeProperty("left"),a.style.removeProperty("top");else{var h=document.body.clientWidth;a.style.left="-99999px";a.style.display="inline-block";a.style.maxWidth=h+"px";var q=l.getBoundingClientRect(),n=a.getBoundingClientRect(),m=0,p=0;"horizontal-down"===e?m=l===b?m-(n.width-q.width):0<c?q.width:0:"vertical-right"===e?m=q.width:"vertical-left"===e&&(m=-n.width);l=!1;h=n.left+n.width+
99999-h+m;0<h&&(m-=h,l=!0);n=-(n.left+99999+m);0<n&&(m+=n,l=!0);l&&("horizontal-down"===e&&0<c||"vertical-right"===e||"vertical-left"===e)&&(p+=3*q.height/4);a.style.left=Math.floor(m)+"px";a.style.top=Math.floor(("horizontal-down"===e&&0===c?q.height:0)+p)+"px";r(c+1,a);a.style.removeProperty("display")}}}};if(null!==b)for(var c=0;99999>c;c++){var g=b.lastChild;if(null===g.firstChild)break;b.parentNode.appendChild(g.firstChild)}c=!1;if("horizontal-down"===e&&(g=d.lastChild.getBoundingClientRect(),
g.left+g.width>f.left+f.width)){null===b&&(c=document.createElement("div"),c.innerHTML=y,b=c.firstChild,d.appendChild(b));g=b.getBoundingClientRect();var t=d.childNodes,w=null;for(c=0;c<t.length;c++){var h=t[c];if(h!==b&&(h=h.getBoundingClientRect(),h.left+h.width+g.width>f.left+f.width)){w=t[c];break}}d.insertBefore(b,w);for(c=0;99999>c&&b.nextSibling;c++)b.lastChild.appendChild(b.nextSibling);c=!0}c||null===b||(b.parentNode.removeChild(b),b=null);r(0,d);d.style.overflow="initial";v=k;p=!1}}};k();
if("undefined"!==typeof MutationObserver){var r=null;(new MutationObserver(function(){var b=d.getAttribute(u);b!==r&&(r=b,k())})).observe(d,{attributes:!0})}"undefined"!==typeof ResizeObserver&&(new ResizeObserver(k)).observe(d);window.addEventListener("resize",k);window.addEventListener("orientationchange",k)}}}();
ivoPetkov.bearFrameworkAddons.navigationMenu=ivoPetkov.bearFrameworkAddons.navigationMenu||function(){return{make:function(x,u,y){var d=document.getElementById(x),b=null,p=!1,v=null,g=function(){if(!p){var k=d.getBoundingClientRect(),f=d.getAttribute(u);null===f&&(f="none");var g=JSON.stringify([f,k.top,k.left,k.width,k.height]);if(v!==g){p=!0;d.style.overflow="hidden";var r=function(c,e){for(var d=e.childNodes,k=d.length,g=0;g<k;g++){var l=d[g];if(1<l.childNodes.length){var a=l.firstChild;"a"===
a.tagName.toLowerCase()&&("none"===f?("undefined"!==typeof a.nmts&&delete a.nmts,"undefined"!==typeof a.nmtsh&&(a.removeEventListener("touchstart",a.nmtsh),delete a.nmtsh)):"undefined"===typeof a.nmtsh&&(a.nmtsh=function(a){a=this.parentNode;"undefined"!==typeof this.nmts?(a===b?a.lastChild.style.display="none":0<this.htmlnavth.length&&(this.href=this.htmlnavth),delete this.nmts):(a===b?a.lastChild.style.removeProperty("display"):("undefined"===typeof this.htmlnavth&&(this.htmlnavth=this.href),this.href=
"javascript:void(0);"),this.nmts=1)},a.addEventListener("touchstart",a.nmtsh,!1)));a=l.lastChild;if("none"===f)a.style.removeProperty("left"),a.style.removeProperty("top");else{var h=document.body.clientWidth;a.style.left="-99999px";a.style.display="inline-block";a.style.maxWidth=h+"px";var q=l.getBoundingClientRect(),n=a.getBoundingClientRect(),m=0,p=0;"horizontal-down"===f?m=l===b?m-(n.width-q.width):0<c?q.width:0:"vertical-right"===f?m=q.width:"vertical-left"===f&&(m=-n.width);l=!1;h=n.left+n.width+
99999-h+m;0<h&&(m-=h,l=!0);n=-(n.left+99999+m);0<n&&(m+=n,l=!0);l&&("horizontal-down"===f&&0<c||"vertical-right"===f||"vertical-left"===f)&&(p+=3*q.height/4);a.style.left=Math.floor(m)+"px";a.style.top=Math.floor(("horizontal-down"===f&&0===c?q.height:0)+p)+"px";r(c+1,a);a.style.removeProperty("display")}}}};if(null!==b)for(var c=0;99999>c;c++){var e=b.lastChild;if(null===e.firstChild)break;b.parentNode.appendChild(e.firstChild)}c=!1;if("horizontal-down"===f&&(e=d.lastChild,null!==b&&e===b&&(e=e.previousSibling),
null!==e&&(e=e.getBoundingClientRect(),Math.ceil(e.left+e.width)>Math.ceil(k.left+k.width)))){null===b&&(c=document.createElement("div"),c.innerHTML=y,b=c.firstChild,d.appendChild(b));e=b.getBoundingClientRect();var t=d.childNodes,w=null;for(c=0;c<t.length;c++){var h=t[c];if(h!==b&&(h=h.getBoundingClientRect(),Math.ceil(h.left+h.width+e.width)>Math.ceil(k.left+k.width))){w=t[c];break}}d.insertBefore(b,w);for(c=0;99999>c&&b.nextSibling;c++)b.lastChild.appendChild(b.nextSibling);c=!0}c||null===b||(b.parentNode.removeChild(b),
b=null);r(0,d);d.style.overflow="initial";v=g;p=!1}}};g();if("undefined"!==typeof MutationObserver){var r=null;(new MutationObserver(function(){var b=d.getAttribute(u);b!==r&&(r=b,g())})).observe(d,{attributes:!0})}"undefined"!==typeof ResizeObserver&&(new ResizeObserver(g)).observe(d);window.addEventListener("resize",g);window.addEventListener("orientationchange",g)}}}();
EOT;
56 changes: 31 additions & 25 deletions dev/navigationMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,35 +136,41 @@ ivoPetkov.bearFrameworkAddons.navigationMenu = ivoPetkov.bearFrameworkAddons.nav

var hasOverflow = false;
if (type === 'horizontal-down') {
var lastChildRect = element.lastChild.getBoundingClientRect();
if (lastChildRect.left + lastChildRect.width > elementRect.left + elementRect.width) { // has overflow
if (moreElement === null) {
var temp = document.createElement("div");
temp.innerHTML = moreElementHTML;
moreElement = temp.firstChild; // li expected
element.appendChild(moreElement);
}
var moreElementRect = moreElement.getBoundingClientRect();
var children = element.childNodes;
var firstOverflowedChild = null;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child !== moreElement) {
var childRect = child.getBoundingClientRect();
if (childRect.left + childRect.width + moreElementRect.width > elementRect.left + elementRect.width) {
firstOverflowedChild = children[i];
break;
var lastChild = element.lastChild;
if (moreElement !== null && lastChild === moreElement) {
lastChild = lastChild.previousSibling;
}
if (lastChild !== null) {
var lastChildRect = lastChild.getBoundingClientRect();
if (Math.ceil(lastChildRect.left + lastChildRect.width) > Math.ceil(elementRect.left + elementRect.width)) { // has overflow
if (moreElement === null) {
var temp = document.createElement("div");
temp.innerHTML = moreElementHTML;
moreElement = temp.firstChild; // li expected
element.appendChild(moreElement);
}
var moreElementRect = moreElement.getBoundingClientRect();
var children = element.childNodes;
var firstOverflowedChild = null;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (child !== moreElement) {
var childRect = child.getBoundingClientRect();
if (Math.ceil(childRect.left + childRect.width + moreElementRect.width) > Math.ceil(elementRect.left + elementRect.width)) {
firstOverflowedChild = children[i];
break;
}
}
}
}
element.insertBefore(moreElement, firstOverflowedChild); // move the more element at the right position
for (var i = 0; i < 99999; i++) {
if (!moreElement.nextSibling) {
break;
element.insertBefore(moreElement, firstOverflowedChild); // move the more element at the right position
for (var i = 0; i < 99999; i++) {
if (!moreElement.nextSibling) {
break;
}
moreElement.lastChild.appendChild(moreElement.nextSibling);
}
moreElement.lastChild.appendChild(moreElement.nextSibling);
hasOverflow = true;
}
hasOverflow = true;
}
}

Expand Down

0 comments on commit 425d623

Please sign in to comment.