idraw/examples/core/features/lib/element.js

73 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2021-05-28 04:25:41 +00:00
const dom = document.querySelector('#elem-list');
let hasInited = false;
export function doElemens(core) {
if (hasInited === true) return;
2021-06-04 06:43:42 +00:00
if (!dom) return;
2021-05-28 04:25:41 +00:00
renderElemens(core);
listenElements(core);
}
function renderElemens(core) {
const data = core.getData();
const elems = data.elements;
const items = [];
2021-08-06 01:38:17 +00:00
for (let i = elems.length - 1; i >= 0; i --) {
const ele = elems[i];
items.push(`
2021-05-28 04:25:41 +00:00
<div class="elem-item">
<span class="elem-item-name" data-elem-name="${ele.uuid || ''}">${ele.name || 'Unnamed'}</span>
2021-08-06 01:38:17 +00:00
<span class="elem-item-btn ${i === elems.length - 1 ? 'btn-hidden' : ''}" data-elem-btn-up="${ele.uuid || ''}">Up</span>
<span class="elem-item-btn ${i === 0 ? 'btn-hidden' : '' }" data-elem-btn-down="${ele.uuid || ''}">Down</span>
2021-07-09 10:19:10 +00:00
<span class="elem-item-btn" data-elem-btn-lock="${ele.uuid || ''}">Lock</span>
<span class="elem-item-btn" data-elem-btn-invisible="${ele.uuid || ''}">Invisible</span>
2021-05-28 04:25:41 +00:00
</div>
`);
}
2021-05-28 04:25:41 +00:00
dom.innerHTML = items.join('');
}
function listenElements(core) {
dom.addEventListener('click', (e) => {
if (!e.path[0]) {
return;
}
const el = e.path[0];
if (el.hasAttribute('data-elem-name')) {
2021-05-28 04:25:41 +00:00
const uuid = el.getAttribute('data-elem-name');
2021-07-24 01:38:21 +00:00
core.selectElement(uuid);
} else if (el.hasAttribute('data-elem-btn-up')) {
2021-05-28 04:25:41 +00:00
const uuid = el.getAttribute('data-elem-btn-up');
core.moveUpElement(uuid);
renderElemens(core);
} else if (el.hasAttribute('data-elem-btn-down')) {
2021-05-28 04:25:41 +00:00
const uuid = el.getAttribute('data-elem-btn-down');
core.moveDownElement(uuid);
renderElemens(core);
} else if (el.hasAttribute('data-elem-btn-lock')) {
const uuid = el.getAttribute('data-elem-btn-lock');
const elem = core.getElement(uuid);
if (!elem.operation) {
elem.operation = {};
}
elem.operation.lock = !elem.operation.lock;
core.updateElement(elem);
renderElemens(core);
} else if (el.hasAttribute('data-elem-btn-invisible')) {
const uuid = el.getAttribute('data-elem-btn-invisible');
const elem = core.getElement(uuid);
if (!elem.operation) {
elem.operation = {};
}
elem.operation.invisible = !elem.operation.invisible;
core.updateElement(elem);
renderElemens(core);
}
}, true);
2021-05-28 04:25:41 +00:00
}