Can you write me some code?
- User
Can you write me some code?
- Assistant
As an AI language model, I can provide a code example for you. However, I need a specific programming language and programming task to make the code. Can you provide more details?
- User
Can you rewrite this JQuery code into modern and pure JS? .fn.extend({ createBtn: function () { var elmWidth = ("li", (this).listview("option", "inset") ? true : false, btnWidth = elmWidth < 300 && listType ? "35%" : elmWidth > 300 && !listType ? "25%" : "20%"; (this)).each(function () { var text = (this).html(("", { class: "go" }).text("Save").width(btnWidth)).append(("", { class: "del" }).text("Delete").width(btnWidth)).css({ left: "-" + btnWidth }).on("swipeleft swiperight vclick tap", function (e) {
$(this).revealBtn(e, btnWidth); }) /**/ ); }); }, revealBtn: function (e, x) { var check = this.check(x), swipe = e.type; if (check == "closed") { swipe == "swiperight" ? this.open(e, x, "left") : swipe == "swipeleft" ? this.open(e, x, "right") : setTimeout(function () { this.close(e); }, 0); e.stopImmediatePropagation(); } if (check == "right" || check == "left") { swipe == "swiperight" ? this.open(e, "left") : swipe == "swipeleft" ? this.open(e, x, "right") : setTimeout(function () { this.close(e); }, 0); e.stopImmediatePropagation(); } if (check !== "closed" && e.isImmediatePropagationStopped() && (swipe == "vclick" || swipe == "tap")) { this.close(e); } }, close: function (e) { var check = this.check(); this.css({ transform: "translateX(0)" }); }, open: function (e, x, dir) { var posX = dir == "left" ? x : "-" + x; $(this).css({ transform: "translateX(" + posX + ")" }); }, check: function (x) { var matrix = this.css("transform").split(" "), posY = parseInt(matrix[matrix.length - 2], 10), btnW = (this.width() * parseInt(x) / 100) / 1.1; return isNaN(posY) ? "closed" : posY >= btnW ? "left" : posY <= "-" + btnW ? "right" : "closed"; }});
(document).on("pagecreate", function () { ("ul").createBtn(); });
- Assistant
Here is a possible solution using modern JavaScript instead of jQuery:
function createBtn(element) { const listItems = Array.from(element.getElementsByTagName('li')); listItems.forEach(item => { const { width: elmWidth } = item.getBoundingClientRect(); const listType = element.getAttribute('data-inset') !== null; const btnWidth = elmWidth < 300 && listType ? '35%' : elmWidth > 300 && !listType ? '25%' : '20%'; const wrapper = document.createElement('div'); wrapper.className = 'wrapper'; const go = document.createElement('div'); go.className = 'go'; go.innerText = 'Save'; go.style.width = btnWidth; const itemWrapper = document.createElement('div'); itemWrapper.className = 'item'; itemWrapper.innerText = item.innerText; const del = document.createElement('div'); del.className = 'del'; del.innerText = 'Delete'; del.style.width = btnWidth; wrapper.appendChild(go); wrapper.appendChild(itemWrapper); wrapper.appendChild(del); item.appendChild(wrapper); item.style.left = `-${btnWidth}`; item.addEventListener('swipeleft', e => { revealBtn(e, item, btnWidth); }); item.addEventListener('swiperight', e => { revealBtn(e, item, btnWidth); }); item.addEventListener('vclick', e => { revealBtn(e, item, btnWidth); }); item.addEventListener('tap', e => { revealBtn(e, item, btnWidth); }); }); } function revealBtn(event, item, btnWidth) { const check = checkPosition(item, btnWidth); const swipe = event.type; if (check === 'closed') { if (swipe === 'swiperight') { open(item, btnWidth, 'left'); } else if (swipe === 'swipeleft') { open(item, btnWidth, 'right'); } else { setTimeout(() => { close(item); }, 0); } event.stopImmediatePropagation(); } if (check === 'right' || check === 'left') { if (swipe === 'swiperight') { open(item, btnWidth, 'left'); } else if (swipe === 'swipeleft') { open(item, btnWidth, 'right'); } else { setTimeout(() => { close(item); }, 0); } event.stopImmediatePropagation(); } if (check !== 'closed' && event.isImmediatePropagationStopped() && (swipe === 'vclick' || swipe === 'tap')) { close(item); } } function close(item) { item.style.transform = 'translateX(0)'; } function open(item, btnWidth, dir) { const posX = dir === 'left' ? btnWidth : `-${btnWidth}`; item.style.transform = `translateX(${posX})`; } function checkPosition(item, btnWidth) { const matrix = getComputedStyle(item).transform.split(' '); const posY = parseInt(matrix[matrix.length - 2], 10); const btnW = (item.offsetWidth * parseInt(btnWidth, 10)) / 100 / 1.1; if (isNaN(posY)) { return 'closed'; } if (posY >= btnW) { return 'left'; } if (posY <= `-${btnW}`) { return 'right'; } return 'closed'; } window.addEventListener('load', () => { const lists = Array.from(document.getElementsByTagName('ul')); lists.forEach(list => createBtn(list)); });This code implements the same functionality as the original jQuery code, creating buttons and allowing them to be revealed by swiping or tapping on list items. The code uses
addEventListener()to bind event listeners to the list items, and uses modern approaches such asgetBoundingClientRect()for measuring element dimensions andtransformCSS property for animation.