152 lines
5.3 KiB
JavaScript
152 lines
5.3 KiB
JavaScript
function encodePickedPath(p) {
|
|
if (p === '/' || p === '') return '/';
|
|
return '/' + p.replace(/^\/+/, '').split('/').map(encodeURIComponent).join('/');
|
|
}
|
|
|
|
function promptPageName(title, initial, confirmLabel, onName) {
|
|
var input = document.createElement('input');
|
|
input.type = 'text';
|
|
input.className = 'modal-input';
|
|
input.placeholder = 'Page name';
|
|
if (initial) input.value = initial;
|
|
openModal({
|
|
title: title,
|
|
body: input,
|
|
confirm: {
|
|
label: confirmLabel,
|
|
onConfirm: function () {
|
|
var name = input.value.trim();
|
|
if (!name) return;
|
|
onName(name);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function newPage() {
|
|
var current = decodeURIComponent(window.location.pathname).replace(/\/+$/, '') || '/';
|
|
openTreePicker({
|
|
title: 'New page — where?',
|
|
mode: 'folder',
|
|
initialPath: current,
|
|
preselect: current,
|
|
hideFiles: true,
|
|
confirmLabel: 'NEXT',
|
|
onSelect: function (parentPath) {
|
|
promptPageName('New page — name?', '', 'CREATE', function (name) {
|
|
var base = parentPath === '/' ? '/' : encodePickedPath(parentPath) + '/';
|
|
window.location.href = base + encodeURIComponent(name) + '/?edit';
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
function movePage() {
|
|
var current = decodeURIComponent(window.location.pathname).replace(/\/+$/, '');
|
|
if (!current) return;
|
|
var segs = current.split('/').filter(Boolean);
|
|
var currentName = segs[segs.length - 1] || '';
|
|
var parent = '/' + segs.slice(0, -1).join('/');
|
|
if (parent === '/') parent = '/';
|
|
|
|
openTreePicker({
|
|
title: 'Move — new parent?',
|
|
mode: 'folder',
|
|
initialPath: parent,
|
|
preselect: parent,
|
|
hideFiles: true,
|
|
confirmLabel: 'NEXT',
|
|
onSelect: function (newParent) {
|
|
var input = document.createElement('input');
|
|
input.type = 'text';
|
|
input.className = 'modal-input';
|
|
input.placeholder = 'Page name';
|
|
input.value = currentName;
|
|
|
|
var linksCheckbox = document.createElement('input');
|
|
linksCheckbox.type = 'checkbox';
|
|
linksCheckbox.id = 'move-update-links';
|
|
|
|
var linksLabel = document.createElement('label');
|
|
linksLabel.htmlFor = linksCheckbox.id;
|
|
linksLabel.className = 'modal-checkbox';
|
|
linksLabel.appendChild(linksCheckbox);
|
|
linksLabel.appendChild(document.createTextNode('Update links'));
|
|
|
|
var body = document.createDocumentFragment();
|
|
body.appendChild(input);
|
|
body.appendChild(linksLabel);
|
|
|
|
openModal({
|
|
title: 'Move — new name?',
|
|
body: body,
|
|
confirm: {
|
|
label: 'MOVE',
|
|
onConfirm: function () {
|
|
var name = input.value.trim();
|
|
if (!name) return;
|
|
var dest = (newParent === '/' ? '' : newParent) + '/' + name;
|
|
var action = window.location.pathname + '?move=' +
|
|
encodeURIComponent(dest);
|
|
if (linksCheckbox.checked) action += '&links=1';
|
|
var form = document.createElement('form');
|
|
form.method = 'POST';
|
|
form.action = action;
|
|
document.body.appendChild(form);
|
|
form.submit();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
function deletePage() {
|
|
var decodedPath = decodeURIComponent(window.location.pathname);
|
|
|
|
openModal({
|
|
title: 'Delete page',
|
|
body: 'Delete ' + decodedPath + ' and everything inside it?',
|
|
confirm: {
|
|
label: 'DELETE',
|
|
danger: true,
|
|
enterConfirms: false,
|
|
onConfirm: function () {
|
|
var form = document.createElement('form');
|
|
form.method = 'POST';
|
|
form.action = window.location.pathname + '?delete=1';
|
|
document.body.appendChild(form);
|
|
form.submit();
|
|
}
|
|
},
|
|
cancel: { autofocus: true },
|
|
swapButtons: true
|
|
});
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var panel = document.querySelector('.actions');
|
|
if (!panel) return;
|
|
|
|
var toggle = document.createElement('button');
|
|
toggle.type = 'button';
|
|
toggle.className = 'panel-toggle';
|
|
toggle.textContent = 'Actions';
|
|
toggle.setAttribute('aria-expanded', 'false');
|
|
toggle.addEventListener('click', function () {
|
|
var open = panel.classList.toggle('is-open');
|
|
toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
|
|
});
|
|
|
|
var main = document.querySelector('main');
|
|
if (!main) return;
|
|
// Insert at the top of main's parent (right after the header) so the
|
|
// ACTIONS toggle sits above any toggles other panels may have added.
|
|
var header = document.querySelector('header');
|
|
var anchor = header ? header.nextSibling : main.parentNode.firstChild;
|
|
main.parentNode.insertBefore(toggle, anchor);
|
|
if (window.matchMedia('(max-width: 1100px)').matches) {
|
|
main.parentNode.insertBefore(panel, toggle.nextSibling);
|
|
}
|
|
});
|