popup.rect get & set

This commit is contained in:
Tsanie Lily 2023-04-19 09:27:29 +08:00
parent 046ed387e6
commit 92a360cbc2
5 changed files with 54 additions and 17 deletions

View File

@ -6,7 +6,7 @@ function fillCheckbox(container, type, label, charactor = 'check') {
container.appendChild( container.appendChild(
createElement('layer', 'check-box-inner', createIcon(type, charactor)) createElement('layer', 'check-box-inner', createIcon(type, charactor))
); );
if (label instanceof HTMLElement) { if (label instanceof Element) {
container.appendChild(label); container.appendChild(label);
} else if (label?.length > 0) { } else if (label?.length > 0) {
container.appendChild( container.appendChild(

View File

@ -10,7 +10,7 @@ function createUse(type, id) {
} }
function changeIcon(svg, type, id) { function changeIcon(svg, type, id) {
if (svg instanceof HTMLElement) { if (svg instanceof SVGElement) {
svg.replaceChildren(createUse(type, id)); svg.replaceChildren(createUse(type, id));
} }
return svg; return svg;

View File

@ -38,6 +38,7 @@
onResizeEnded: () => console.log('resize ended.', popup.rect) onResizeEnded: () => console.log('resize ended.', popup.rect)
}); });
popup.show(); popup.show();
window.popup = popup;
}); });
</script> </script>
<style type="text/css"> <style type="text/css">

View File

@ -27,6 +27,17 @@ const ResizeMods = {
// [ResizeMods.topLeft]: 'nwse-resize' // [ResizeMods.topLeft]: 'nwse-resize'
// } // }
function trimPx(px) {
if (typeof px !== 'string') {
return px;
}
if (px.endsWith('px')) {
const size = Number(px.substring(0, px.length - 2));
return isNaN(size) ? px : size;
}
return px;
}
class Popup { class Popup {
#mask; #mask;
#option; #option;
@ -45,11 +56,14 @@ class Popup {
return null; return null;
} }
const style = global.getComputedStyle(container); const style = global.getComputedStyle(container);
const collapsed = container.classList.contains('popup-collapse');
const bounds = this.#bounds;
return { return {
left: style.left, collapsed,
top: style.top, left: trimPx(style.left),
width: style.width, top: trimPx(style.top),
height: style.height width: collapsed === true && bounds != null ? bounds.width : trimPx(style.width),
height: collapsed === true && bounds != null ? bounds.height : trimPx(style.height)
}; };
} }
set rect(r) { set rect(r) {
@ -64,11 +78,26 @@ class Popup {
if (!isNaN(r.top)) { if (!isNaN(r.top)) {
css.push(`top: ${r.top}px`); css.push(`top: ${r.top}px`);
} }
if (!isNaN(r.width) && r.width > 0) { const collapse = container.querySelector('.popup-header>.icon-expand');
css.push(`width: ${r.width}px`); if (r.collapsed === true) {
} css.push('width: 160px', 'height: 40px');
if (!isNaN(r.height) && r.height > 0) { this.#bounds = r;
css.push(`height: ${r.height}px`); container.classList.add('popup-collapse');
if (collapse != null) {
changeIcon(collapse, 'fa-regular', 'expand-alt');
}
} else {
if (!isNaN(r.width) && r.width > 0) {
css.push(`width: ${r.width}px`);
}
if (!isNaN(r.height) && r.height > 0) {
css.push(`height: ${r.height}px`);
}
container.classList.remove('popup-collapse');
this.#bounds = null;
if (collapse != null) {
changeIcon(collapse, 'fa-regular', 'compress-alt');
}
} }
if (css.length > 0) { if (css.length > 0) {
container.style.cssText += css.join('; '); container.style.cssText += css.join('; ');
@ -106,17 +135,20 @@ class Popup {
move.addEventListener('mousedown', e => { move.addEventListener('mousedown', e => {
const x = e.clientX - container.offsetLeft; const x = e.clientX - container.offsetLeft;
const y = e.clientY - container.offsetTop; const y = e.clientY - container.offsetTop;
let moved;
const move = e => { const move = e => {
container.style.left = `${e.clientX - x}px`; container.style.left = `${e.clientX - x}px`;
container.style.top = `${e.clientY - y}px`; container.style.top = `${e.clientY - y}px`;
moved = true;
}; };
mask.addEventListener('mousemove', move, { passive: false }); mask.addEventListener('mousemove', move, { passive: false });
const up = () => { const up = () => {
mask.removeEventListener('mousemove', move, { passive: false }); mask.removeEventListener('mousemove', move, { passive: false });
mask.removeEventListener('mouseup', up); mask.removeEventListener('mouseup', up);
if (typeof this.#option.onMoveEnded === 'function') { if (moved === true && typeof this.#option.onMoveEnded === 'function') {
this.#option.onMoveEnded.call(this); this.#option.onMoveEnded.call(this);
} }
moved = false;
}; };
mask.addEventListener('mouseup', up); mask.addEventListener('mouseup', up);
}); });
@ -128,14 +160,15 @@ class Popup {
if (container.classList.contains('popup-collapse')) { if (container.classList.contains('popup-collapse')) {
const bounds = this.#bounds; const bounds = this.#bounds;
if (bounds != null) { if (bounds != null) {
container.style.cssText += `width: ${bounds.width}; height: ${bounds.height}`; container.style.cssText += `width: ${bounds.width}px; height: ${bounds.height}px`;
this.#bounds = null;
} }
container.classList.remove('popup-collapse'); container.classList.remove('popup-collapse');
changeIcon(collapse, 'fa-regular', 'compress-alt'); changeIcon(collapse, 'fa-regular', 'compress-alt');
} else { } else {
const rect = this.rect; const rect = this.rect;
this.#bounds = rect; this.#bounds = rect;
container.style.cssText += `left: ${rect.left}; top: ${rect.top}; width: 160px; height: 40px`; container.style.cssText += `width: 160px; height: 40px`;
container.classList.add('popup-collapse'); container.classList.add('popup-collapse');
changeIcon(collapse, 'fa-regular', 'expand-alt'); changeIcon(collapse, 'fa-regular', 'expand-alt');
} }
@ -271,6 +304,7 @@ class Popup {
}; };
const minWidth = option.minWidth ?? 200; const minWidth = option.minWidth ?? 200;
const minHeight = option.minHeight ?? 200; const minHeight = option.minHeight ?? 200;
let resized;
const move = e => { const move = e => {
const offsetX = e.clientX - originalX; const offsetX = e.clientX - originalX;
const offsetY = e.clientY - originalY; const offsetY = e.clientY - originalY;
@ -313,6 +347,7 @@ class Popup {
} else { } else {
container.style.cssText += `left: ${x}px; top: ${y}px; width: ${width}px; height: ${height}px`; container.style.cssText += `left: ${x}px; top: ${y}px; width: ${width}px; height: ${height}px`;
} }
resized = true;
} }
const parent = option.mask === false ? mask.parentElement : mask; const parent = option.mask === false ? mask.parentElement : mask;
parent.addEventListener('mousemove', move, { passive: false }); parent.addEventListener('mousemove', move, { passive: false });
@ -320,9 +355,10 @@ class Popup {
parent.removeEventListener('mousemove', move, { passive: false }); parent.removeEventListener('mousemove', move, { passive: false });
parent.removeEventListener('mouseup', up); parent.removeEventListener('mouseup', up);
// mask.style.cursor = this.#cursor; // mask.style.cursor = this.#cursor;
if (typeof option.onResizeEnded === 'function') { if (resized === true && typeof option.onResizeEnded === 'function') {
option.onResizeEnded.call(this); option.onResizeEnded.call(this);
} }
resized = false;
}; };
parent.addEventListener('mouseup', up); parent.addEventListener('mouseup', up);
} }
@ -365,7 +401,7 @@ export function showAlert(title, message, iconType = 'info', parent = document.b
} }
export function showConfirm(title, content, buttons, iconType = 'question', parent = document.body) { export function showConfirm(title, content, buttons, iconType = 'question', parent = document.body) {
return new Promise((resolve, reject) => { return new Promise(resolve => {
const wrapper = createElement('div', 'message-wrapper'); const wrapper = createElement('div', 'message-wrapper');
if (!nullOrEmpty(iconType)) { if (!nullOrEmpty(iconType)) {
wrapper.appendChild(createIcon('fa-solid', iconTypes[iconType] ?? 'question-circle')); wrapper.appendChild(createIcon('fa-solid', iconTypes[iconType] ?? 'question-circle'));

View File

@ -23,7 +23,7 @@ function setTooltip(container, content, flag = false, parent = null) {
createElement('div', 'tooltip-curtain tooltip-color'), createElement('div', 'tooltip-curtain tooltip-color'),
createElement('div', cnt => { createElement('div', cnt => {
cnt.className = 'tooltip-content'; cnt.className = 'tooltip-content';
if (content instanceof HTMLElement) { if (content instanceof Element) {
cnt.appendChild(content); cnt.appendChild(content);
} else { } else {
cnt.innerText = content; cnt.innerText = content;