-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CU-mhbk6g Add a resize event and viewport correcting code (#384)
- Loading branch information
Showing
3 changed files
with
73 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export default class KeepInViewPort { | ||
constructor() { | ||
this.VIEWPORTRESIZE = 'js-keep-in-viewport-after-resize'; | ||
this.VIEWPORT = 'js-keep-in-viewport'; | ||
|
||
// Get containers. | ||
const ViewPortResizeContainer = document.querySelectorAll(`[${this.VIEWPORTRESIZE}]`); | ||
|
||
if(ViewPortResizeContainer) { | ||
ViewPortResizeContainer.forEach(item => { | ||
item.addEventListener('resizeByChildren', KeepInViewPort.resizeEvent); | ||
}); | ||
} | ||
|
||
// Get containers. | ||
const ViewPortContainer = document.querySelectorAll(`[${this.VIEWPORT}]`); | ||
|
||
if(ViewPortContainer) { | ||
ViewPortContainer.forEach(item => { | ||
KeepInViewPort.moveInsideViewPort(item, 8); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Keep the actual viewport function cleaned from event objects. | ||
* @param {object} event | ||
* @return void | ||
*/ | ||
static resizeEvent(event) { | ||
KeepInViewPort.moveInsideViewPort(event.target, 8); | ||
} | ||
|
||
/** | ||
* Move element inside viewport. | ||
* @param {object} element | ||
* @param {integer} margin | ||
* @return void | ||
*/ | ||
static moveInsideViewPort(element, margin) { | ||
// Enable calculations. | ||
element.classList.add('u-display--block'); | ||
|
||
// Compare window width with right most position. | ||
const viewPortRightDistance = window.innerWidth - element.getBoundingClientRect().right; | ||
if (viewPortRightDistance < 0) { | ||
element.setAttribute('style', `${element.getAttribute('style')} left: ${viewPortRightDistance - margin}px;`); | ||
} | ||
|
||
// Disable calculations. | ||
element.classList.remove('u-display--block'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
export default class ResizeByChildren { | ||
constructor() { | ||
this.RESIZE = 'js-resize-by-children'; | ||
|
||
//Get containers | ||
let resizeContainer = document.querySelectorAll(`[${this.RESIZE}]`); | ||
|
||
// Get containers | ||
const resizeContainer = document.querySelectorAll(`[${this.RESIZE}]`); | ||
|
||
if(resizeContainer) { | ||
|
||
resizeContainer.forEach(item => { | ||
|
||
//Enale calculations | ||
// Enable calculations | ||
item.classList.add('u-display--block'); | ||
|
||
//Declare | ||
let currentChilds = item.querySelectorAll('li > a'); | ||
let widthStack = []; | ||
|
||
//Get all widts as array | ||
// Declare | ||
const currentChilds = item.querySelectorAll('li > a'); | ||
const widthStack = []; | ||
|
||
// Get all widts as array | ||
currentChilds.forEach(child => { | ||
widthStack.push(child.getBoundingClientRect().width); | ||
}); | ||
|
||
|
||
//Get largest | ||
let maxSize = Math.round(Math.max.apply(null, widthStack)); | ||
// Get largest | ||
const maxSize = Math.round(Math.max.apply(null, widthStack)); | ||
|
||
if(item.getBoundingClientRect().width > maxSize) { | ||
item.setAttribute("style", "width:" + maxSize + "px !important;"); | ||
item.setAttribute('style', 'width:' + maxSize + 'px !important;'); | ||
item.dispatchEvent(new Event('resizeByChildren')); | ||
} | ||
|
||
//Disable calculations | ||
// Disable calculations | ||
item.classList.remove('u-display--block'); | ||
|
||
//Add class as calc | ||
// Add class as calc | ||
item.classList.add(item.classList[0] + '--calculated'); | ||
|
||
}); | ||
}); | ||
} | ||
} | ||
} |