//FUNCTION FOR RESIZING
this.makeResizableDiv = function(div) {
const element = document.querySelector(div);
const resizers = document.querySelectorAll(div + ' .resizer')
const minimum_size = 40;
let original_width = 0;
let original_height = 0;
let original_x = 0;
let original_y = 0;
let original_mouse_x = 0;
let original_mouse_y = 0;
var funcAtResize = this.config.onresizeEl;
element.addEventListener('mousedown', function(e) {
e.preventDefault();
e.stopPropagation();
//disabling others
var res = document.querySelectorAll('[class~="resizer"]');
for(var i=0;i<res.length;i++){
res[i].style.display = 'none';
}
var res = this.querySelector('.resizers');
for(var i=0;i<res.children.length;i++){
res.children[i].style.display = 'block';
}
});
for (let i = 0;i < resizers.length; i++) {
const currentResizer = resizers[i];
currentResizer.addEventListener('mousedown', function(e) {
e.preventDefault();
e.stopPropagation();
original_width = parseFloat(getComputedStyle(element, null).getPropertyValue('width').replace('px', ''));
original_height = parseFloat(getComputedStyle(element, null).getPropertyValue('height').replace('px', ''));
original_x = element.offsetLeft ;
original_y = element.offsetTop;
var rect = element.parentNode.getBoundingClientRect();
var padX = e.pageX - rect.left;
var padY = e.pageY - rect.top;
original_mouse_x = padX;
original_mouse_y = padY;
window.addEventListener('mousemove', resize)
window.addEventListener('mouseup', stopResize)
});
function resize(e) {
e.preventDefault();
e.stopPropagation();
var rect = element.parentNode.getBoundingClientRect();
var padX = e.pageX - rect.left;
var padY = e.pageY - rect.top;
var wPad = getComputedStyle(element.parentNode, null).getPropertyValue('width');
var wPad = parseFloat(wPad.substring(0, wPad.length - 2));
var hPad = getComputedStyle(element.parentNode, null).getPropertyValue('height');
var hPad = parseFloat(hPad.substring(0, hPad.length - 2));
var xHijo = getComputedStyle(element, null).getPropertyValue('left');
var xHijo = parseFloat(xHijo.substring(0, xHijo.length - 2));
var yHijo = getComputedStyle(element, null).getPropertyValue('top');
var yHijo = parseFloat(yHijo.substring(0, yHijo.length - 2));
var yElAct = original_y + (padY - original_mouse_y);
var xElAct = original_x + (padX - original_mouse_x);
var xElDer = xElAct + element.offsetWidth;
var yElDer = yElAct + element.offsetHeight;
if (currentResizer.classList.contains('top')) {
const height = original_height - (padY - original_mouse_y);
if (height > minimum_size && yElAct > 0) {
element.style.height = height + 'px';
element.style.top = original_y + (padY - original_mouse_y) + 'px';
}
}else
if (currentResizer.classList.contains('right')) {
const width = original_width + (padX - original_mouse_x);
var lim = wPad - xHijo;
if (width > minimum_size && width < lim ) {
element.style.width = width + 'px';
}
}else
if (currentResizer.classList.contains('bottom')) {
const height = original_height + (padY - original_mouse_y);
var lim = hPad - yHijo;
if (height > minimum_size && height < lim ) {
element.style.height = height + 'px'
}
}else
if (currentResizer.classList.contains('left')){
const width = original_width - (padX - original_mouse_x);
if (width > minimum_size && xElAct > 0) {
element.style.width = width + 'px'
element.style.left = original_x + (padX - original_mouse_x) + 'px';
}
}else
if (currentResizer.classList.contains('bottom-right')) {
const width = original_width + (padX - original_mouse_x);
const height = original_height + (padY - original_mouse_y);
var lim = hPad - yHijo;
var limr = wPad - xHijo;
if (width > minimum_size && width < limr) {
element.style.width = width + 'px'
}
if (height > minimum_size && height < lim ) {
element.style.height = height + 'px'
}
}else
if (currentResizer.classList.contains('bottom-left')) {
const height = original_height + (padY - original_mouse_y);
const width = original_width - (padX - original_mouse_x);
var lim = hPad - yHijo;
if (height > minimum_size && height < lim ) {
element.style.height = height + 'px'
}
if (width > minimum_size && xElAct > 0) {
element.style.width = width + 'px'
element.style.left = original_x + (padX - original_mouse_x) + 'px'
}
}else
if (currentResizer.classList.contains('top-right')) {
const width = original_width + (padX - original_mouse_x);
const height = original_height - (padY - original_mouse_y);
var lim = wPad - xHijo;
if (width > minimum_size && width < lim ) {
element.style.width = width + 'px'
}
if (height > minimum_size && yElAct > 0) {
element.style.height = height + 'px'
element.style.top = original_y + (padY - original_mouse_y) + 'px'
}
}else
if (currentResizer.classList.contains('top-left')){
const width = original_width - (padX - original_mouse_x);
const height = original_height - (padY - original_mouse_y);
if (width > minimum_size && xElAct > 0) {
element.style.width = width + 'px'
element.style.left = original_x + (padX - original_mouse_x) + 'px'
}
if (height > minimum_size && yElAct > 0 ) {
element.style.height = height + 'px'
element.style.top = original_y + (padY - original_mouse_y) + 'px'
}
}
//If a function was defined to be fired when resizing
funcAtResize(element);
}
function stopResize(e) {
e.preventDefault();
e.stopPropagation();
window.removeEventListener('mousemove', resize)
}
}
},
Inside we will see several functions to call when user clicks on a corner dot and resize.