198 lines
8.0 KiB
JavaScript
198 lines
8.0 KiB
JavaScript
'use strict';
|
|
|
|
jQuery(function ($) {
|
|
$('[data-layout-components]').on('click', '[data-component-move]', function () {
|
|
const $item = $(this).closest('li');
|
|
if (this.dataset.componentMove === '-1') $item.prev().before($item);
|
|
else $item.next().after($item);
|
|
$(this).trigger('focus');
|
|
});
|
|
$('[data-provider-settings]').each(function () {
|
|
const $settings = $(this);
|
|
const $preset = $settings.closest('form').find('[data-layout-preset]');
|
|
const $mode = $settings.find('[name=provider_mode]');
|
|
const sync = () => {
|
|
$settings.prop('hidden', $preset.val() !== 'member');
|
|
$settings.find('[data-provider-mode]').each(function () {
|
|
$(this).prop('hidden', this.dataset.providerMode !== $mode.val());
|
|
});
|
|
};
|
|
$preset.add($mode).on('change', sync);
|
|
sync();
|
|
});
|
|
|
|
$('[data-page-list]').each(function () {
|
|
const $panel = $(this);
|
|
const $search = $panel.find('[data-page-search]');
|
|
const $status = $panel.find('[data-page-status]');
|
|
const $pages = $panel.find('[data-page-item]');
|
|
$panel.find('[data-page-filters]').prop('hidden', false);
|
|
$search.add($status).on('input change', function () {
|
|
const query = $search.val().trim().toLowerCase();
|
|
const status = $status.val();
|
|
let count = 0;
|
|
$pages.each(function () {
|
|
const visible = (!status || this.dataset.pageItem === status) && this.dataset.pageLabel.toLowerCase().includes(query);
|
|
$(this).prop('hidden', !visible);
|
|
if (visible) count++;
|
|
});
|
|
$panel.find('[data-page-empty]').prop('hidden', count !== 0);
|
|
});
|
|
});
|
|
|
|
const formats = {
|
|
markdown: {
|
|
heading: ['## ', '', '小标题'],
|
|
bold: ['**', '**', '粗体文字'],
|
|
list: ['- ', '', '列表项'],
|
|
link: ['[', '](https://)', '链接文字']
|
|
},
|
|
html: {
|
|
heading: ['<h3>', '</h3>', '小标题'],
|
|
bold: ['<strong>', '</strong>', '粗体文字'],
|
|
list: ['<ul>\n <li>', '</li>\n</ul>', '列表项'],
|
|
link: ['<a href="https://">', '</a>', '链接文字']
|
|
}
|
|
};
|
|
|
|
function clearSavedStatus() {
|
|
$('[data-save-confirmation]').prop('hidden', true);
|
|
}
|
|
|
|
if (document.querySelector('[data-save-confirmation]')) {
|
|
const url = new URL(window.location.href);
|
|
if (url.searchParams.get('saved') === '1') {
|
|
url.searchParams.delete('saved');
|
|
window.history.replaceState(window.history.state, '', url.href);
|
|
}
|
|
}
|
|
|
|
function refreshToken(response) {
|
|
if (response?.csrf) {
|
|
$('input[name="csrf"]').val(response.csrf);
|
|
}
|
|
}
|
|
|
|
function formError($form, message, status) {
|
|
let $feedback = $form.children('.ajax-feedback');
|
|
if (!$feedback.length) {
|
|
$feedback = $('<p>', {class: 'ajax-feedback feedback error', role: 'alert', tabindex: -1})
|
|
.prependTo($form);
|
|
}
|
|
$feedback.text(message);
|
|
if (status === 401) {
|
|
$feedback.append(' ', $('<a>', {
|
|
href: '/admin/login', target: '_blank', rel: 'noopener', text: '打开登录页'
|
|
}));
|
|
}
|
|
$feedback.prop('hidden', false).trigger('focus');
|
|
}
|
|
|
|
document.addEventListener('invalid', (event) => {
|
|
if (event.target.form?.method.toLowerCase() === 'post') clearSavedStatus();
|
|
}, true);
|
|
|
|
$('form[method="post"]').on('submit', function (event) {
|
|
event.preventDefault();
|
|
const $form = $(this);
|
|
if ($form.attr('aria-busy') === 'true') return;
|
|
const submitter = event.originalEvent?.submitter;
|
|
if (submitter?.dataset.confirm && !window.confirm(submitter.dataset.confirm)) return;
|
|
clearSavedStatus();
|
|
const action = $form.attr('action');
|
|
const isLogin = action === '/admin/login';
|
|
const $submit = $form.find(':submit:enabled');
|
|
const multipart = $form.attr('enctype') === 'multipart/form-data';
|
|
$form.attr('aria-busy', 'true').children('.ajax-feedback').prop('hidden', true);
|
|
$submit.prop('disabled', true);
|
|
$form.triggerHandler('studio:busy', [true]);
|
|
|
|
function finish() {
|
|
$form.removeAttr('aria-busy');
|
|
$submit.prop('disabled', false);
|
|
$form.triggerHandler('studio:busy', [false]);
|
|
}
|
|
|
|
function submit(canRetryLogin) {
|
|
$form.triggerHandler('studio:sync');
|
|
const fields = multipart ? new FormData($form.get(0)) : $form.serializeArray();
|
|
if (submitter?.name) {
|
|
if (multipart) fields.append(submitter.name, submitter.value);
|
|
else fields.push({name: submitter.name, value: submitter.value});
|
|
}
|
|
$.ajax({
|
|
url: action,
|
|
method: 'POST',
|
|
data: fields,
|
|
processData: !multipart,
|
|
contentType: multipart ? false : 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
dataType: 'json'
|
|
}).done(function (response) {
|
|
if (response.redirect) {
|
|
$form.triggerHandler('studio:saved');
|
|
window.location.assign(response.redirect);
|
|
return;
|
|
}
|
|
finish();
|
|
formError($form, '未收到保存结果,请刷新页面确认。');
|
|
}).fail(function (request) {
|
|
const response = request.responseJSON;
|
|
refreshToken(response);
|
|
if (canRetryLogin && request.status === 403 && response?.csrf) {
|
|
submit(false);
|
|
return;
|
|
}
|
|
finish();
|
|
formError($form, response?.message || '请求未完成,请稍后重试。', request.status);
|
|
});
|
|
}
|
|
|
|
submit(isLogin);
|
|
});
|
|
|
|
$('[data-editor]').each(function () {
|
|
const $editor = $(this);
|
|
if ($editor.find('[data-rich-canvas]').length) return;
|
|
const area = $editor.find('textarea').get(0);
|
|
const format = $editor.data('format');
|
|
const $preview = $editor.find('.editor-preview');
|
|
const $form = $editor.closest('form');
|
|
let pendingPreview;
|
|
|
|
$editor.on('click', '[data-insert]', function () {
|
|
const [before, after, example] = formats[format][$(this).data('insert')];
|
|
const start = area.selectionStart;
|
|
const end = area.selectionEnd;
|
|
const selection = area.value.slice(start, end) || example;
|
|
area.setRangeText(before + selection + after, start, end, 'end');
|
|
area.focus();
|
|
area.setSelectionRange(start + before.length, start + before.length + selection.length);
|
|
});
|
|
|
|
$editor.on('click', '[data-preview-button]', function () {
|
|
const $button = $(this);
|
|
const show = $preview.prop('hidden');
|
|
$preview.prop('hidden', !show);
|
|
$button.text(show ? '收起预览' : '预览正文').attr('aria-expanded', String(show));
|
|
pendingPreview?.abort();
|
|
if (!show) return;
|
|
|
|
$preview.text('正在加载预览…');
|
|
pendingPreview = $.ajax({
|
|
url: '/admin/preview',
|
|
method: 'POST',
|
|
data: {body: area.value, format, csrf: $form.find('input[name="csrf"]').val()},
|
|
dataType: 'text'
|
|
}).done((markup) => $preview.html(markup)).fail((request, status) => {
|
|
if (status === 'abort') return;
|
|
let feedback = request.responseJSON;
|
|
if (!feedback && request.getResponseHeader('Content-Type')?.startsWith('application/json')) {
|
|
try { feedback = JSON.parse(request.responseText); } catch { /* Use the fallback feedback. */ }
|
|
}
|
|
refreshToken(feedback);
|
|
$preview.text(feedback?.message || '预览无法加载,请重试。');
|
|
});
|
|
});
|
|
});
|
|
});
|