PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / js / toolsMigratePlugin.js

toolsMigratePlugin.js in 404 Solution 4.3.0, at includes/js/toolsMigratePlugin.js

99 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Tools page: "Migrate from Another Plugin" two-step preview/confirm flow.
3 *
4 * Reads ajaxUrl, nonce, and translatable messages from a JSON blob on the
5 * configuration carrier element's `data-abj404-migrate` attribute
6 * (#abj404-migrate-config). Click "Preview Import" -> AJAX preview ->
7 * show count + Confirm Import (or "Nothing to import" + Back).
8 */
9 (function () {
10 'use strict';
11
12 function readConfig() {
13 var el = document.getElementById('abj404-migrate-config');
14 if (!el) { return null; }
15 var raw = el.getAttribute('data-abj404-migrate');
16 if (!raw) { return null; }
17 try {
18 return JSON.parse(raw);
19 } catch (e) {
20 return null;
21 }
22 }
23
24 var cfg = null;
25
26 function showStep1() {
27 document.getElementById('abj404-migrate-step1').style.display = '';
28 document.getElementById('abj404-migrate-step2').style.display = 'none';
29 }
30
31 function showStep2(count, source, label) {
32 document.getElementById('abj404-migrate-step1').style.display = 'none';
33 document.getElementById('abj404-migrate-step2').style.display = '';
34 var msgEl = document.getElementById('abj404-migrate-preview-msg');
35 var form = document.getElementById('abj404-migrate-confirm-form');
36 var noForm = document.getElementById('abj404-migrate-back-noform');
37 if (count > 0) {
38 msgEl.textContent = cfg.msgFound.replace('%d', count).replace('%s', label);
39 document.getElementById('abj404-migrate-confirm-source').value = source;
40 form.style.display = '';
41 noForm.style.display = 'none';
42 } else {
43 msgEl.textContent = cfg.msgNone.replace('%s', label);
44 form.style.display = 'none';
45 noForm.style.display = '';
46 }
47 }
48
49 function showError() {
50 document.getElementById('abj404-migrate-step1').style.display = 'none';
51 document.getElementById('abj404-migrate-step2').style.display = '';
52 document.getElementById('abj404-migrate-preview-msg').textContent = cfg.msgError;
53 document.getElementById('abj404-migrate-confirm-form').style.display = 'none';
54 document.getElementById('abj404-migrate-back-noform').style.display = '';
55 }
56
57 document.addEventListener('DOMContentLoaded', function () {
58 cfg = readConfig();
59 if (!cfg) { return; }
60
61 var previewBtn = document.getElementById('abj404-migrate-preview-btn');
62 var backBtn = document.getElementById('abj404-migrate-back-btn');
63 var backBtn2 = document.getElementById('abj404-migrate-back-btn2');
64 if (previewBtn) {
65 previewBtn.addEventListener('click', function () {
66 var select = document.getElementById('abj404-import-source');
67 var source = select ? select.value : '';
68 if (!source) { return; }
69 var spinner = document.getElementById('abj404-migrate-preview-spinner');
70 if (spinner) { spinner.style.display = ''; }
71 previewBtn.disabled = true;
72 var fd = new FormData();
73 fd.append('action', 'abj404_crossPluginPreview');
74 fd.append('nonce', cfg.nonce);
75 fd.append('import_source', source);
76 // ajax-direct-approved: cross-plugin migration preview posts FormData and manages the two-step UI state locally.
77 fetch(cfg.ajaxUrl, { method: 'POST', body: fd, credentials: 'same-origin' })
78 .then(function (r) { return r.json(); })
79 .then(function (resp) {
80 if (spinner) { spinner.style.display = 'none'; }
81 previewBtn.disabled = false;
82 if (resp && resp.success && resp.data) {
83 showStep2(parseInt(resp.data.count, 10) || 0, resp.data.source, resp.data.label);
84 } else {
85 showError();
86 }
87 })
88 .catch(function () {
89 if (spinner) { spinner.style.display = 'none'; }
90 previewBtn.disabled = false;
91 showError();
92 });
93 });
94 }
95 if (backBtn) { backBtn.addEventListener('click', function () { showStep1(); }); }
96 if (backBtn2) { backBtn2.addEventListener('click', function () { showStep1(); }); }
97 });
98 })();
99