Commit inicial powerservice
This commit is contained in:
@@ -0,0 +1,418 @@
|
||||
/* WPCode Packs — pack-specific styles only.
|
||||
Sidebar, grid, list items, and buttons use the existing plugin styles
|
||||
(wpcode-items-sidebar, wpcode-items-list-category, wpcode-list-item,
|
||||
wpcode-button*) — no overrides needed there. */
|
||||
|
||||
/* ===========================================================
|
||||
Pack card icon + category badge (inside wpcode-list-item)
|
||||
=========================================================== */
|
||||
|
||||
/* Installed-state indicator: subtle 3px green left accent strip. */
|
||||
.wpcode-pack-list-item[data-installed="1"] {
|
||||
border-left: 3px solid var(--wpcode-notice-success-bg, #009966);
|
||||
}
|
||||
|
||||
.wpcode-pack-list-item .wpcode-pack-head {
|
||||
display: flex;
|
||||
align-items: stretch;
|
||||
gap: 12px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
.wpcode-pack-list-item .wpcode-pack-head-text {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
gap: 4px;
|
||||
}
|
||||
.wpcode-pack-list-item h3 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.wpcode-pack-icon {
|
||||
width: 48px;
|
||||
min-height: 48px;
|
||||
border-radius: 6px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
flex-shrink: 0;
|
||||
background: var(--wpcode-background-highlight, #F6FAFF);
|
||||
color: var(--wpcode-color-primary, #3568B7);
|
||||
align-self: stretch;
|
||||
}
|
||||
.wpcode-pack-icon .dashicons {
|
||||
font-size: 24px;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.wpcode-pack-icon-lg {
|
||||
width: 56px;
|
||||
height: 56px;
|
||||
min-height: 56px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
.wpcode-pack-icon-lg .dashicons {
|
||||
font-size: 30px;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
}
|
||||
|
||||
.cat-badge {
|
||||
display: inline-block;
|
||||
padding: 2px 8px;
|
||||
border-radius: 999px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.02em;
|
||||
background: var(--wpcode-background-highlight, #F6FAFF);
|
||||
color: var(--wpcode-color-primary, #3568B7);
|
||||
align-self: flex-start;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
/* Snippet name chips below the title (max 2 + "+N more"). */
|
||||
.wpcode-pack-snippets {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin: 6px 0 10px;
|
||||
}
|
||||
.wpcode-pack-snippet-chip {
|
||||
display: inline-block;
|
||||
max-width: 100%;
|
||||
padding: 3px 8px;
|
||||
border-radius: 4px;
|
||||
border: 1px solid var(--wpcode-border-color, #e5e7eb);
|
||||
background: var(--wpcode-background-gray, #f6f7f7);
|
||||
color: var(--wpcode-text-color-paragraph, #50575e);
|
||||
font-size: 11.5px;
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.wpcode-pack-snippet-chip-more {
|
||||
background: transparent;
|
||||
border-color: transparent;
|
||||
color: var(--wpcode-text-color-highlight, #2271b1);
|
||||
}
|
||||
|
||||
/* Pack card description (sits between the title row and the snippet chips). */
|
||||
.wpcode-pack-desc {
|
||||
margin: 0 0 10px;
|
||||
font-size: 13px;
|
||||
line-height: 1.45;
|
||||
color: var(--wpcode-text-color-paragraph, #50575e);
|
||||
}
|
||||
|
||||
/* Pack card footer — snippet chips on the left, button on the right, same row. */
|
||||
.wpcode-pack-foot {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
.wpcode-pack-foot .wpcode-pack-snippets {
|
||||
margin: 0;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.wpcode-pack-foot .wpcode-pack-actions {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* ----- NEW pill on the Packs tab button ----- */
|
||||
.wpcode-library-tab-button .wpcode-tab-new-pill {
|
||||
display: inline-block;
|
||||
margin-left: 6px;
|
||||
font-size: 10px;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
background: var(--wpcode-color-primary, #3568B7);
|
||||
padding: 2px 6px;
|
||||
border-radius: 3px;
|
||||
letter-spacing: 0.04em;
|
||||
vertical-align: 2px;
|
||||
}
|
||||
|
||||
/* ----- Danger variant on the secondary button (Remove pack) ----- */
|
||||
.wpcode-button.wpcode-button-danger {
|
||||
color: var(--wpcode-color-red, #DF2A35);
|
||||
border-color: var(--wpcode-color-red, #DF2A35);
|
||||
}
|
||||
.wpcode-button.wpcode-button-danger:hover {
|
||||
background: var(--wpcode-color-red, #DF2A35);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
/* ===========================================================
|
||||
Detail view (post-install)
|
||||
=========================================================== */
|
||||
|
||||
.wpcode-pack-detail-back {
|
||||
display: inline-block;
|
||||
margin: 0 0 16px;
|
||||
font-size: 13px;
|
||||
color: var(--wpcode-text-color-highlight, #2271b1);
|
||||
text-decoration: none;
|
||||
}
|
||||
.wpcode-pack-detail-back:hover { text-decoration: underline; }
|
||||
|
||||
.wpcode-pack-detail {
|
||||
background: var(--wpcode-background-white, #fff);
|
||||
border: 1px solid var(--wpcode-border-color, #dcdcde);
|
||||
border-radius: 4px;
|
||||
padding: 24px;
|
||||
}
|
||||
.wpcode-detail-desc {
|
||||
margin: 0 0 16px;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
color: var(--wpcode-text-color-paragraph, #50575e);
|
||||
}
|
||||
.wpcode-detail-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
margin-top: 16px;
|
||||
}
|
||||
|
||||
.wpcode-detail-head {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 14px;
|
||||
margin-bottom: 22px;
|
||||
}
|
||||
.wpcode-detail-head .info { flex: 1; min-width: 0; }
|
||||
.wpcode-detail-head h2 {
|
||||
margin: 0 0 6px;
|
||||
font-size: var(--wpcode-font-size-xl, 22px);
|
||||
font-weight: 600;
|
||||
color: var(--wpcode-text-color-heading, #1d2327);
|
||||
}
|
||||
.wpcode-detail-head .meta-row {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
font-size: var(--wpcode-font-size-s, 14px);
|
||||
color: var(--wpcode-text-color-paragraph, #646970);
|
||||
}
|
||||
.wpcode-detail-head .meta-row .dot {
|
||||
width: 3px; height: 3px;
|
||||
border-radius: 50%;
|
||||
background: var(--wpcode-border-color, #c3c4c7);
|
||||
}
|
||||
.wpcode-detail-head .cat-name {
|
||||
font-weight: 600;
|
||||
color: var(--wpcode-text-color-highlight, #2271b1);
|
||||
}
|
||||
|
||||
/* Snippets card */
|
||||
.wpcode-snippets-card {
|
||||
background: #fff;
|
||||
border: 1px solid var(--wpcode-border-color, #dcdcde);
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.wpcode-snippets-card .head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 12px 16px;
|
||||
background: var(--wpcode-background-gray, #f8f8f8);
|
||||
border-bottom: 1px solid var(--wpcode-border-color, #dcdcde);
|
||||
}
|
||||
.wpcode-snippets-card .head h3 {
|
||||
margin: 0;
|
||||
font-size: var(--wpcode-font-size-s, 14px);
|
||||
font-weight: 600;
|
||||
color: var(--wpcode-text-color-heading, #1d2327);
|
||||
}
|
||||
.wpcode-snippets-card .head .meta {
|
||||
font-size: 12px;
|
||||
color: var(--wpcode-text-color-paragraph, #646970);
|
||||
}
|
||||
|
||||
.wpcode-snippet-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
padding: 14px 16px;
|
||||
border-bottom: 1px solid var(--wpcode-border-color, #dcdcde);
|
||||
}
|
||||
.wpcode-snippet-row:last-child { border-bottom: 0; }
|
||||
.wpcode-snippet-row .toggle-wrap { flex-shrink: 0; }
|
||||
.wpcode-snippet-row .snippet-info { flex: 1; min-width: 0; }
|
||||
.wpcode-snippet-row h5 {
|
||||
margin: 0 0 2px;
|
||||
font-size: var(--wpcode-font-size-s, 14px);
|
||||
font-weight: 600;
|
||||
color: var(--wpcode-text-color-heading, #1d2327);
|
||||
}
|
||||
.wpcode-snippet-row .actions { flex-shrink: 0; }
|
||||
.wpcode-snippet-row.is-inactive h5 { color: #8c8f94; }
|
||||
|
||||
/* Hint shown on options flagged as needing personalization on the library. */
|
||||
.wpcode-snippet-personalize-note {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
margin-top: 2px;
|
||||
font-size: var(--wpcode-font-size-xs, 12px);
|
||||
color: #b26200;
|
||||
}
|
||||
.wpcode-snippet-personalize-note .dashicons {
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
/* Note shown when a snippet is shared with (also part of) another pack. */
|
||||
.wpcode-snippet-shared-note {
|
||||
display: block;
|
||||
margin-top: 2px;
|
||||
font-size: var(--wpcode-font-size-xs, 12px);
|
||||
color: var(--wpcode-text-color-paragraph, #646970);
|
||||
}
|
||||
|
||||
/* Toggle switch */
|
||||
.wpcode-toggle {
|
||||
width: 36px; height: 20px;
|
||||
border-radius: 999px;
|
||||
background: #c3c4c7;
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
transition: background 150ms ease;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.wpcode-toggle::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 2px; left: 2px;
|
||||
width: 16px; height: 16px;
|
||||
border-radius: 50%;
|
||||
background: #fff;
|
||||
transition: transform 180ms cubic-bezier(0.4, 0, 0.2, 1);
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
.wpcode-toggle.is-on { background: var(--wpcode-color-primary, #3568B7); }
|
||||
.wpcode-toggle.is-on::after { transform: translateX(16px); }
|
||||
|
||||
/* ===========================================================
|
||||
Install confirmation modal (renamed to wpcode-rmodal-* to
|
||||
avoid colliding with the existing .wpcode-modal class in
|
||||
src/admin/modal.css which is display:none by default).
|
||||
=========================================================== */
|
||||
|
||||
.wpcode-rmodal-backdrop {
|
||||
position: fixed; inset: 0;
|
||||
background: rgba(16, 24, 40, 0.45);
|
||||
display: grid;
|
||||
place-items: center;
|
||||
z-index: 100000;
|
||||
}
|
||||
.wpcode-rmodal {
|
||||
background: #fff;
|
||||
border-radius: 6px;
|
||||
width: 520px;
|
||||
max-width: calc(100vw - 40px);
|
||||
box-shadow: 0 24px 48px -12px rgba(0, 0, 0, 0.25);
|
||||
overflow: hidden;
|
||||
}
|
||||
.wpcode-rmodal-head {
|
||||
padding: 18px 20px 12px;
|
||||
border-bottom: 1px solid var(--wpcode-border-color, #f0f0f1);
|
||||
}
|
||||
.wpcode-rmodal-head h3 {
|
||||
margin: 0;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: var(--wpcode-text-color-heading, #1d2327);
|
||||
}
|
||||
.wpcode-rmodal-head .sub {
|
||||
margin-top: 4px;
|
||||
font-size: 12px;
|
||||
color: var(--wpcode-text-color-paragraph, #646970);
|
||||
}
|
||||
|
||||
.wpcode-rmodal-body { padding: 16px 20px; }
|
||||
.wpcode-rmodal-body .js-wpcode-rmodal-lead {
|
||||
margin: 0 0 12px;
|
||||
font-size: 13px;
|
||||
color: var(--wpcode-text-color-paragraph, #50575e);
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-checklist {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
border: 1px solid var(--wpcode-border-color, #f0f0f1);
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-check-row {
|
||||
padding: 9px 14px;
|
||||
font-size: 13px;
|
||||
border-bottom: 1px solid var(--wpcode-border-color, #f0f0f1);
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-check-row:last-child { border-bottom: 0; }
|
||||
.wpcode-rmodal-body .wpcode-check-row.pending .label-name { color: #8c8f94; }
|
||||
.wpcode-rmodal-body .wpcode-check-row.active { background: #f6faff; }
|
||||
.wpcode-rmodal-body .wpcode-check-row.done { color: var(--wpcode-text-color-heading, #1d2327); }
|
||||
|
||||
.wpcode-rmodal-body .wpcode-progress-track {
|
||||
height: 4px;
|
||||
background: #f0f0f1;
|
||||
border-radius: 999px;
|
||||
margin: 14px 0 6px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-progress-fill {
|
||||
height: 100%;
|
||||
background: var(--wpcode-color-primary, #3568B7);
|
||||
transition: width 320ms cubic-bezier(0.4, 0, 0.2, 1);
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-progress-label {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
font-size: 12px;
|
||||
color: var(--wpcode-text-color-paragraph, #646970);
|
||||
}
|
||||
|
||||
.wpcode-rmodal-body .wpcode-success-state {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
padding: 24px 16px 8px;
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-success-state[hidden] {
|
||||
display: none;
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-success-state .wpcode-icon-checkmark {
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-success-state h4 {
|
||||
margin: 0 0 6px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: var(--wpcode-text-color-heading, #1d2327);
|
||||
}
|
||||
.wpcode-rmodal-body .wpcode-success-state p {
|
||||
margin: 0;
|
||||
max-width: 340px;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
color: var(--wpcode-text-color-paragraph, #50575e);
|
||||
}
|
||||
|
||||
.wpcode-rmodal-foot {
|
||||
padding: 12px 20px 16px;
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
border-top: 1px solid var(--wpcode-border-color, #f0f0f1);
|
||||
background: var(--wpcode-background-gray, #fafafa);
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
/* global ajaxurl */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
const payloadEl = document.getElementById('wpcode-packs-payload');
|
||||
if (!payloadEl) {
|
||||
return;
|
||||
}
|
||||
const payload = JSON.parse(payloadEl.textContent);
|
||||
const packs = payload.packs;
|
||||
const nonce = payload.nonce;
|
||||
const i18n = payload.i18n;
|
||||
|
||||
// ----- Install modal -----
|
||||
|
||||
function openInstallModal(slug) {
|
||||
const data = packs[slug];
|
||||
if (!data || !data.snippets || !data.snippets.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const tpl = document.getElementById('wpcode-packs-install-modal');
|
||||
const node = tpl.content.firstElementChild.cloneNode(true);
|
||||
document.body.appendChild(node);
|
||||
|
||||
const titleEl = node.querySelector('.js-wpcode-rmodal-title');
|
||||
const subEl = node.querySelector('.js-wpcode-rmodal-sub span');
|
||||
const leadEl = node.querySelector('.js-wpcode-rmodal-lead');
|
||||
const listEl = node.querySelector('.wpcode-checklist');
|
||||
const trackEl = node.querySelector('.wpcode-progress-track');
|
||||
const labelEl = node.querySelector('.wpcode-progress-label');
|
||||
const progressFill = node.querySelector('.wpcode-progress-fill');
|
||||
const progressText = node.querySelector('.js-wpcode-progress-text');
|
||||
const progressPercent = node.querySelector('.js-wpcode-progress-percent');
|
||||
const successEl = node.querySelector('.wpcode-success-state');
|
||||
const successHeading = node.querySelector('.js-wpcode-success-heading');
|
||||
const successBody = node.querySelector('.js-wpcode-success-body');
|
||||
const cancelBtn = node.querySelector('.js-wpcode-rmodal-cancel');
|
||||
const primaryBtn = node.querySelector('.js-wpcode-rmodal-primary');
|
||||
const primaryLabel = primaryBtn.querySelector('span');
|
||||
|
||||
titleEl.textContent = i18n.install_title.replace('%s', data.name);
|
||||
subEl.textContent = i18n.snippets_subtitle
|
||||
.replace('%1$d', data.snippets.length)
|
||||
.replace('%2$s', data.category);
|
||||
leadEl.textContent = i18n.confirm_lead;
|
||||
|
||||
data.snippets.forEach(function (s) {
|
||||
const row = document.createElement('li');
|
||||
row.className = 'wpcode-check-row pending';
|
||||
const label = document.createElement('span');
|
||||
label.className = 'label-name';
|
||||
label.textContent = s;
|
||||
row.appendChild(label);
|
||||
listEl.appendChild(row);
|
||||
});
|
||||
|
||||
cancelBtn.textContent = i18n.cancel;
|
||||
primaryLabel.textContent = i18n.install_button;
|
||||
|
||||
function close() {
|
||||
node.remove();
|
||||
}
|
||||
node.addEventListener('click', function (e) {
|
||||
if (e.target === node) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
cancelBtn.addEventListener('click', close);
|
||||
|
||||
// Guard so the success-state primary button (rebound to a redirect) can't
|
||||
// re-trigger an install when clicked.
|
||||
let installing = false;
|
||||
primaryBtn.addEventListener('click', function () {
|
||||
if (installing) {
|
||||
return;
|
||||
}
|
||||
installing = true;
|
||||
titleEl.textContent = i18n.installing_title.replace('%s', data.name);
|
||||
primaryBtn.disabled = true;
|
||||
primaryLabel.textContent = i18n.installing_button;
|
||||
cancelBtn.style.display = 'none';
|
||||
|
||||
const rows = listEl.querySelectorAll('.wpcode-check-row');
|
||||
let step = 0;
|
||||
trackEl.hidden = false;
|
||||
labelEl.hidden = false;
|
||||
progressText.textContent = i18n.creating_snippets;
|
||||
|
||||
function tick() {
|
||||
if (step >= rows.length) {
|
||||
return;
|
||||
}
|
||||
rows[step].classList.remove('pending');
|
||||
rows[step].classList.add('active');
|
||||
}
|
||||
tick();
|
||||
const interval = setInterval(function () {
|
||||
const r = rows[step];
|
||||
if (!r) {
|
||||
return;
|
||||
}
|
||||
r.classList.remove('active');
|
||||
r.classList.add('done');
|
||||
step += 1;
|
||||
const pct = Math.round((step / rows.length) * 100);
|
||||
progressFill.style.width = pct + '%';
|
||||
progressPercent.textContent = pct + '%';
|
||||
if (step >= rows.length) {
|
||||
clearInterval(interval);
|
||||
} else {
|
||||
tick();
|
||||
}
|
||||
}, 520);
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('action', 'wpcode_install_pack');
|
||||
fd.append('nonce', nonce);
|
||||
fd.append('slug', slug);
|
||||
|
||||
fetch(ajaxurl, { method: 'POST', body: fd })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (resp) {
|
||||
if (!resp.success) {
|
||||
clearInterval(interval);
|
||||
alert(resp.data && resp.data.message ? resp.data.message : 'Install failed');
|
||||
close();
|
||||
return;
|
||||
}
|
||||
function showSuccess() {
|
||||
if (step < rows.length) {
|
||||
setTimeout(showSuccess, 200);
|
||||
return;
|
||||
}
|
||||
titleEl.textContent = i18n.installed_title.replace('%s', data.name);
|
||||
leadEl.hidden = true;
|
||||
listEl.hidden = true;
|
||||
trackEl.hidden = true;
|
||||
labelEl.hidden = true;
|
||||
successEl.hidden = false;
|
||||
|
||||
// Report the real outcome, not an optimistic "all activated".
|
||||
// A snippet can fail to create (failed_options) or fail its
|
||||
// activation safety check (activation_failed).
|
||||
const result = (resp.data && resp.data.result) || {};
|
||||
const total = data.snippets.length;
|
||||
const issues =
|
||||
((result.failed_options || []).length) +
|
||||
((result.activation_failed || []).length);
|
||||
// Prefer the server's real active count; fall back to total - issues.
|
||||
const active = typeof result.active_count === 'number'
|
||||
? result.active_count
|
||||
: Math.max(0, total - issues);
|
||||
|
||||
if (active < total) {
|
||||
successHeading.textContent = i18n.success_heading_partial
|
||||
.replace('%1$d', active)
|
||||
.replace('%2$d', total);
|
||||
successBody.textContent = i18n.success_body_partial;
|
||||
} else {
|
||||
successHeading.textContent = i18n.success_heading.replace('%d', total);
|
||||
successBody.textContent = i18n.success_body;
|
||||
}
|
||||
primaryBtn.disabled = false;
|
||||
primaryLabel.textContent = i18n.open_dashboard;
|
||||
cancelBtn.style.display = '';
|
||||
cancelBtn.textContent = i18n.back_to_packs;
|
||||
|
||||
primaryBtn.onclick = function () {
|
||||
window.location.href = resp.data.redirect_url;
|
||||
};
|
||||
}
|
||||
showSuccess();
|
||||
})
|
||||
.catch(function () {
|
||||
clearInterval(interval);
|
||||
alert('Install failed');
|
||||
close();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ----- Card click → install -----
|
||||
document.querySelectorAll('.js-wpcode-open-install').forEach(function (btn) {
|
||||
btn.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const card = btn.closest('.wpcode-list-item');
|
||||
if (!card) {
|
||||
return;
|
||||
}
|
||||
openInstallModal(card.dataset.packSlug);
|
||||
});
|
||||
});
|
||||
|
||||
// ----- Toggle -----
|
||||
document.querySelectorAll('.js-wpcode-toggle-option').forEach(function (el) {
|
||||
el.addEventListener('click', function () {
|
||||
const row = el.closest('.wpcode-snippet-row');
|
||||
const detail = el.closest('.wpcode-pack-detail');
|
||||
if (!row || !detail) {
|
||||
return;
|
||||
}
|
||||
const libraryId = row.dataset.libraryId;
|
||||
const slug = detail.dataset.packSlug;
|
||||
const turnOn = !el.classList.contains('is-on');
|
||||
|
||||
function setToggle(state) {
|
||||
el.classList.toggle('is-on', state);
|
||||
el.setAttribute('aria-checked', state ? 'true' : 'false');
|
||||
row.classList.toggle('is-inactive', !state);
|
||||
}
|
||||
|
||||
// Optimistic update, reconciled to the server's real state below.
|
||||
setToggle(turnOn);
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('action', 'wpcode_toggle_pack_option');
|
||||
fd.append('nonce', detail.dataset.nonce);
|
||||
fd.append('slug', slug);
|
||||
fd.append('library_id', libraryId);
|
||||
fd.append('active', turnOn ? '1' : '0');
|
||||
|
||||
fetch(ajaxurl, { method: 'POST', body: fd })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (resp) {
|
||||
if (resp && resp.success && resp.data) {
|
||||
// Sync to the real state — activation can be refused.
|
||||
setToggle(!!resp.data.active);
|
||||
if (resp.data.activation_refused) {
|
||||
window.alert(i18n.activation_refused);
|
||||
}
|
||||
} else {
|
||||
setToggle(!turnOn);
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
setToggle(!turnOn);
|
||||
});
|
||||
});
|
||||
el.addEventListener('keydown', function (e) {
|
||||
if (e.key === ' ' || e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
el.click();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ----- Add / Reinstall option -----
|
||||
document.querySelectorAll('.js-wpcode-add-option').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () {
|
||||
const row = btn.closest('.wpcode-snippet-row');
|
||||
const detail = btn.closest('.wpcode-pack-detail');
|
||||
if (!row || !detail) {
|
||||
return;
|
||||
}
|
||||
btn.disabled = true;
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('action', 'wpcode_add_pack_option');
|
||||
fd.append('nonce', detail.dataset.nonce);
|
||||
fd.append('slug', detail.dataset.packSlug);
|
||||
fd.append('library_id', row.dataset.libraryId);
|
||||
|
||||
fetch(ajaxurl, { method: 'POST', body: fd })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (resp) {
|
||||
// Already installed (e.g. a concurrent add) is not an error —
|
||||
// the snippet is present, so just refresh to show it.
|
||||
if (resp.success || (resp.data && resp.data.code === 'already_installed')) {
|
||||
window.location.reload();
|
||||
} else {
|
||||
btn.disabled = false;
|
||||
alert(resp.data && resp.data.message ? resp.data.message : 'Could not add option');
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
btn.disabled = false;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ----- Remove pack -----
|
||||
document.querySelectorAll('.js-wpcode-remove-pack').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () {
|
||||
const detail = btn.closest('.wpcode-pack-detail');
|
||||
if (!detail) {
|
||||
return;
|
||||
}
|
||||
const packName = document.querySelector('.wpcode-detail-head h2').textContent;
|
||||
const snippetCount = detail.querySelectorAll('.wpcode-snippet-row').length;
|
||||
const msg = i18n.confirm_remove
|
||||
.replace('%1$s', packName)
|
||||
.replace('%2$d', snippetCount);
|
||||
|
||||
function doRemove() {
|
||||
const fd = new FormData();
|
||||
fd.append('action', 'wpcode_remove_pack');
|
||||
fd.append('nonce', detail.dataset.nonce);
|
||||
fd.append('slug', detail.dataset.packSlug);
|
||||
|
||||
fetch(ajaxurl, { method: 'POST', body: fd })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (resp) {
|
||||
if (resp.success) {
|
||||
window.location.href = resp.data.redirect_url;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Use WPCode's jquery-confirm dialog to match the rest of the admin,
|
||||
// falling back to the native confirm if it isn't available.
|
||||
const jq = window.jQuery;
|
||||
if (jq && typeof jq.confirm === 'function') {
|
||||
jq.confirm({
|
||||
title: i18n.remove_title,
|
||||
content: msg,
|
||||
type: 'blue',
|
||||
backgroundDismiss: true,
|
||||
buttons: {
|
||||
confirm: {
|
||||
text: i18n.remove_confirm_button,
|
||||
btnClass: 'wpcode-btn-confirm',
|
||||
action: doRemove
|
||||
},
|
||||
cancel: {
|
||||
text: i18n.cancel
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if (window.confirm(msg)) {
|
||||
doRemove();
|
||||
}
|
||||
});
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user