1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| //moveresize.js
function moveResizeModal(modal, buttonModalShow) {
const button_Reduce = modal.querySelector('.reduce-button')
const button_Resize = modal.querySelector('.resize-button')
const button_Close = modal.querySelector('.close-button')
const titre_Bar = modal.querySelector('.titre-Bar')
const window_Resize = modal.querySelector('.resize-window')
const resize_right = modal.querySelector('.resize-right')
const resize_bottom= modal.querySelector('.resize-bottom')
var resizeSelect, offset = { x: 0, y: 0 }, coord = [modal.offsetTop, modal.offsetLeft, modal.offsetHeight, modal.offsetWidth]
button_Resize.style.setProperty('--check-primary', '"◹"');
modalZindex()
//move
titre_Bar.addEventListener('mousedown', moveMouseDown, false)
window.addEventListener('mouseup', moveMouseUp, false)
//resize
window_Resize.addEventListener('mousedown', resizeMouseDown, false)
//resize right
resize_right.addEventListener('mousedown', resizeMouseDown, false)
//resize bottom
resize_bottom.addEventListener('mousedown', resizeMouseDown, false)
//boutons and title bar resize
button_Resize.addEventListener('click', resizeButtom, false)
titre_Bar.addEventListener('dblclick', resizeButtom, false)
//z-index
modal.addEventListener('mousedown', modalZindex, false)
// fenêtre modal centrée après un redimensionnement du navigateur
function centerModal() {
var windowWidth = document.documentElement.clientWidth, windowHeight = document.documentElement.clientHeight,
leftPos = (windowWidth - modal.clientWidth) / 2, topPos = (windowHeight - modal.clientHeight) / 2
modal.style.left = `${leftPos}px`
modal.style.top = `${topPos}px`
}
function modalZindex() {
const modals = document.getElementsByClassName('modal-Window')
for (const selmodal of modals) {
selmodal.style.zIndex = ''
selmodal.classList.add('disable')
}
modal.style.zIndex = '4'
modal.classList.remove('disable')
}
function resizeButtom(e) {
let symbol = button_Resize.style.cssText.replace(/\s/g, '')
button_Reduce.removeAttribute('style')
window_Resize.removeAttribute('style')
modal.classList.remove('disabled-move')
organiseReduce()
if (symbol == '--check-primary:"◹";') {
modal.style.cssText += `top: 0px; left: 0px; height: ${window.innerHeight}px; width: ${window.innerWidth}px;`
button_Resize.style.setProperty('--check-primary', '"◺"');
} else {
modal.style.cssText += `top: ${coord[0]}px; left: ${coord[1]}px; height: ${coord[2]}px; width: ${coord[3]}px;`
button_Resize.style.setProperty('--check-primary', '"◹"');
}
e.stopImmediatePropagation();
}
function moveMouseUp() {
window.removeEventListener('mousemove', modalMove, true)
window.removeEventListener('mousemove', modalResize, true)
modal.style.cursor ='default'
}
function moveMouseDown(e) {
offset.x = e.clientX - modal.offsetLeft
offset.y = e.clientY - modal.offsetTop
modal.classList.contains('disabled-move') == false && window.addEventListener('mousemove', modalMove, true)
}
function modalMove(e) {
if (window.innerWidth > modal.offsetWidth || window.innerHeight > modal.offsetHeight) {
var c = { top: (e.clientY - offset.y), left: (e.clientX - offset.x), minLeft: 0, maxLeft: (window.innerWidth - modal.offsetWidth), minTop: 0, maxTop: (window.innerHeight - modal.clientHeight) }
c.left = c.left < c.minLeft ? c.minLeft : c.left //min left
c.left = c.left > c.maxLeft ? c.maxLeft : c.left //max left
c.top = c.top < c.minTop ? c.minTop : c.top //min top
c.top = c.top > c.maxTop ? c.maxTop : c.top //max top
modal.style.top = `${c.top}px`
modal.style.left = `${c.left}px`
coord = [modal.offsetTop, modal.offsetLeft, modal.offsetHeight, modal.offsetWidth]
}
}
function resizeMouseDown(e) {
resizeSelect = e.target.className
offset.x = (e.clientX - modal.offsetWidth)
offset.y = (e.clientY - modal.offsetHeight)
window.addEventListener('mousemove', modalResize, true)
}
function modalResize(e) {
var c = { height: (e.clientY - offset.y), width: (e.clientX - offset.x), maxWidth: window.innerWidth, maxHeight: window.innerHeight, windowHeight: ((e.clientY - offset.y) + modal.offsetTop), windowWidth: ((e.clientX - offset.x) + modal.offsetLeft) }
c.width = c.windowWidth >= c.maxWidth ? (c.maxWidth - modal.offsetLeft) : c.width
c.height = c.windowHeight >= c.maxHeight ? (c.maxHeight - modal.offsetTop) : c.height
modal.style.cursor = window.getComputedStyle(modal.querySelector('.' + resizeSelect)).cursor
c.height < 100 ? c.height = 100 : null
c.maxWidth == c.width && c.maxHeight == c.height ? button_Resize.style.setProperty('--check-primary', '"◺"'): button_Resize.style.setProperty('--check-primary', '"◹"')
resizeSelect == "resize-window" ? (modal.style.height = `${c.height}px`) & (modal.style.width = `${c.width}px`) : null
resizeSelect == "resize-bottom" ? modal.style.height = `${c.height}px`: null
resizeSelect == "resize-right" ? modal.style.width = `${c.width}px`: null
coord = [modal.offsetTop, modal.offsetLeft, modal.offsetHeight, modal.offsetWidth]
}
function organiseReduce() {
let reduceLeft = 0
for (const modalReduce of document.getElementsByClassName('disabled-move')) {
modalReduce.style.cssText += `height:25px; width:200px; top:auto; bottom:0px;left:${reduceLeft}px;`
reduceLeft += 200
}
}
//reduce
button_Reduce.addEventListener('click', function (e) {
button_Resize.style.setProperty('--check-primary', '"▭"');
coord = [modal.offsetTop, modal.offsetLeft, modal.offsetHeight, modal.offsetWidth]
modal.classList.add('disabled-move')
modal.style.cssText += 'left:0px;'
this.style.cssText = 'pointer-events:none; color:gray'
window_Resize.style.display = 'none'
organiseReduce()
})
//close modal
button_Close.addEventListener('click', function (e) {
button_Resize.removeAttribute('style')
button_Reduce.removeAttribute('style')
window_Resize.removeAttribute('style')
buttonModalShow.removeAttribute('style')
modal.removeAttribute('style')
modal.classList.remove('disabled-move')
organiseReduce()
topleft[0] -= 3
topleft[1] -= 2
e.stopImmediatePropagation();
})
window.onresize = function () {
centerModal(); // Appeler la fonction pour centrer la fenêtre modale lors du redimensionnement de la fenêtre du navigateur
}
} |
Partager