- assets/vendor 全量迁移: fontawesome5→assets/fontawesome5, css→assets/css, js→assets/js/runtime(避开 14 个同名自有文件), img 合并去重→assets/img, fonts→assets/fonts; 删除 assets/vendor - libs 五个与 widgets/third-party 字节相同的库不重复入库, loader paths 改指向 - 运行时切换: _win.uri=assets 根, loader baseUrl=/js/runtime, enlighter 注入 路径同步; PHP 7 个引用文件切换并改名 $zibll_*→$runtime_* - 去品牌: Qinver 头注释/品牌 console.log/演示链接清除, 默认文案中性化 - 功能标识符改名: zib__→aksrt__, zib_ajax→aksrt_ajax, window.zib→ window.aksrtLegacy, zibllblock/*→aksrtblock/*, .zib-widget→.aksrt-panel, .zib-slider→.aksrt-slider, zib_menu_options meta 键等 - 自有 CSS 改名: legacy-utilities/compat-vars/footer shell - admin SPA 重建; 契约测试同步(路径/断言/文件更名) - 例外保留: tests/visual 对照环境与 fixtures 前缀(外部 Zibll 产品引用) 验证: php -l 133 个改动文件通过; runtime 86 个 JS node --check 通过; 契约测试 68/70(2 个失败经 stash 对照确认为存量问题)
838 lines
34 KiB
JavaScript
838 lines
34 KiB
JavaScript
console.log('legacy Widget');
|
||
|
||
(function ($, document) {
|
||
function aksrt__(key) {
|
||
var map = window.aksrt_widget_set_var && window.aksrt_widget_set_var.i18n;
|
||
if (map && Object.prototype.hasOwnProperty.call(map, key)) {
|
||
return map[key];
|
||
}
|
||
return key;
|
||
}
|
||
|
||
/** 带图片上传的小工具 repeater 行(幻灯片 / 友情链接等) */
|
||
function buildMediaRepeaterRow(labelPrefix, index, nameAttr, labels) {
|
||
var i1 = index + 1;
|
||
var titleLabel = labelPrefix + i1 + (labels.title || '-标题:');
|
||
var decLabel = labelPrefix + i1 + (labels.dec || '-简介');
|
||
var hrefLabel = labelPrefix + i1 + (labels.href || '-链接');
|
||
var linkLabel = labelPrefix + i1 + (labels.link || '-图片');
|
||
return (
|
||
'<div class="widget_ui_slider_g">' +
|
||
'<div class="panel"><h4 class="panel-title">' +
|
||
labelPrefix +
|
||
i1 +
|
||
'</h4><div class="panel-conter">' +
|
||
'<label>' +
|
||
titleLabel +
|
||
'<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][title]" value=""></label>' +
|
||
'<label>' +
|
||
decLabel +
|
||
'<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][dec]" value=""></label>' +
|
||
'<label>' +
|
||
hrefLabel +
|
||
'<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][href]" value=""></label>' +
|
||
'<div class=""><label>' +
|
||
linkLabel +
|
||
'<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][link]" value=""></label>' +
|
||
'<button type="button" class="button ashu_upload_button">' +
|
||
aksrt__('admin_select_image') +
|
||
'</button>' +
|
||
'<button type="button" class="button delimg_upload_button">' +
|
||
aksrt__('admin_remove_image') +
|
||
'</button>' +
|
||
'<div class="widget_ui_slider_box"></div></div></div></div></div>'
|
||
);
|
||
}
|
||
|
||
/** 消息 repeater 行 */
|
||
function buildNoticeRepeaterRow(index, nameAttr) {
|
||
var i1 = index + 1;
|
||
return (
|
||
'<div class="widget_ui_slider_g">' +
|
||
'<div class="panel"><h4 class="panel-title">消息' +
|
||
i1 +
|
||
'</h4><div class="panel-conter">' +
|
||
'<label>消息' +
|
||
i1 +
|
||
'-内容(必填):<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][title]" value=""></label>' +
|
||
'<label>消息' +
|
||
i1 +
|
||
'-图标<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][icon]" value=""></label>' +
|
||
'<label>消息' +
|
||
i1 +
|
||
'-链接<input style="width:100%;" type="text" name="' +
|
||
nameAttr +
|
||
'[' +
|
||
index +
|
||
'][href]" value=""></label>' +
|
||
'</div></div></div>'
|
||
);
|
||
}
|
||
|
||
/** WIE:导入模式单选 HTML */
|
||
function getImportModeHtml(radioName, replaceSpanClass, appendSpanClass) {
|
||
return (
|
||
'<div class="aksrt-wie-modal__import-mode">' +
|
||
'<div class="aksrt-wie-modal__import-mode-title description"></div>' +
|
||
'<div class="flex ac hh">' +
|
||
'<label class="aksrt-wie-modal__import-mode-row"><input type="radio" name="' +
|
||
radioName +
|
||
'" value="replace" /> <span class="' +
|
||
replaceSpanClass +
|
||
'"></span></label>' +
|
||
'<label class="aksrt-wie-modal__import-mode-row"><input type="radio" name="' +
|
||
radioName +
|
||
'" value="append" checked="checked" /> <span class="' +
|
||
appendSpanClass +
|
||
'"></span></label>' +
|
||
'</div></div>'
|
||
);
|
||
}
|
||
|
||
/** WIE:填充导入模式文案 */
|
||
function bindImportModeI18n($root, replaceSelector, appendSelector) {
|
||
$root.find('.aksrt-wie-modal__import-mode-title').text(aksrt__('importModeLabel'));
|
||
$root.find(replaceSelector).text(aksrt__('importModeReplace'));
|
||
$root.find(appendSelector).text(aksrt__('importModeAppend'));
|
||
}
|
||
|
||
/** WIE:admin-ajax POST */
|
||
function wiePost(zibConfig, action, data) {
|
||
return $.ajax({
|
||
url: zibConfig.ajaxUrl,
|
||
type: 'POST',
|
||
dataType: 'json',
|
||
data: $.extend(
|
||
{
|
||
action: action,
|
||
nonce: zibConfig.nonce,
|
||
},
|
||
data || {}
|
||
),
|
||
});
|
||
}
|
||
|
||
/** WIE:解析服务端 message */
|
||
function getWieMessage(res, fallbackKey) {
|
||
return (res.data && res.data.message) || aksrt__(fallbackKey);
|
||
}
|
||
|
||
/** WIE:清空结果区样式 */
|
||
function clearWieResult($result) {
|
||
return $result.removeClass('is-ok is-warn is-err').text('');
|
||
}
|
||
|
||
/** WIE:显示错误 */
|
||
function showWieError($result, message) {
|
||
$result.removeClass('is-ok is-warn').addClass('is-err').text(message);
|
||
}
|
||
|
||
/** WIE:网络错误 */
|
||
function showNetworkError($result) {
|
||
showWieError($result, aksrt__('admin_network_error'));
|
||
}
|
||
|
||
/** WIE:导入成功后的刷新按钮 */
|
||
function getReloadBtnHtml() {
|
||
return (
|
||
'<p class="aksrt-wie-modal__reload-wrap"><a href="' +
|
||
window.location.href +
|
||
'" class="button button-primary aksrt-wie-modal__reload">' +
|
||
(aksrt__('reloadPage') || '刷新页面') +
|
||
'</a></p>'
|
||
);
|
||
}
|
||
|
||
/**
|
||
* WIE:渲染导入成功/失败结果
|
||
* @param {jQuery} $result
|
||
* @param {object} res - WP ajax 响应
|
||
* @param {object} [opts]
|
||
* @param {string} [opts.failKey]
|
||
* @param {boolean} [opts.autoReload]
|
||
* @param {Function} [opts.onSuccess]
|
||
*/
|
||
function renderImportResult($result, res, opts) {
|
||
opts = opts || {};
|
||
var failKey = opts.failKey || 'admin_import_failed';
|
||
clearWieResult($result);
|
||
if (res.success && res.data) {
|
||
var reloadBtn = getReloadBtnHtml();
|
||
var lines = [res.data.message || aksrt__('admin_import_success')];
|
||
if (res.data.warnings && res.data.warnings.length) {
|
||
lines = lines.concat(res.data.warnings);
|
||
$result.addClass('is-warn').html(lines.join('<br>') + reloadBtn);
|
||
} else {
|
||
$result.addClass('is-ok').html(lines[0] + reloadBtn);
|
||
}
|
||
if (opts.onSuccess) {
|
||
opts.onSuccess(res);
|
||
}
|
||
if (opts.autoReload) {
|
||
forceRefreshPage();
|
||
}
|
||
return true;
|
||
}
|
||
showWieError($result, getWieMessage(res, failKey));
|
||
return false;
|
||
}
|
||
|
||
/** WIE:提交按钮 loading */
|
||
function withButtonLoading($btn, loadingText, idleText, requestFn) {
|
||
$btn.prop('disabled', true).text(loadingText);
|
||
var jq = requestFn();
|
||
if (!jq || !jq.always) {
|
||
$btn.prop('disabled', false).text(idleText);
|
||
return jq;
|
||
}
|
||
return jq.always(function () {
|
||
$btn.prop('disabled', false).text(idleText);
|
||
});
|
||
}
|
||
|
||
/** WIE:强制刷新页面 */
|
||
function forceRefreshPage() {
|
||
setTimeout(function () {
|
||
window.location.reload();
|
||
}, 200);
|
||
}
|
||
|
||
/**
|
||
* WIE:弹层工厂(导出/导入、模板选择共用外壳)
|
||
* @param {object} opts
|
||
* @returns {{ ensure: Function, close: Function, open: Function, $result: Function, $el: Function, setImportModeDefault: Function }}
|
||
*/
|
||
function createWieModal(opts) {
|
||
var $modal = null;
|
||
|
||
function ensure() {
|
||
if ($modal && $modal.length) {
|
||
return $modal;
|
||
}
|
||
|
||
var shell =
|
||
'<div id="' +
|
||
opts.id +
|
||
'" class="aksrt-wie-modal' +
|
||
(opts.extraClass ? ' ' + opts.extraClass : '') +
|
||
'" aria-hidden="true">' +
|
||
'<div class="aksrt-wie-modal__backdrop" ' +
|
||
opts.closeAttr +
|
||
'="1"></div>' +
|
||
'<div class="aksrt-wie-modal__dialog" role="dialog" aria-modal="true">' +
|
||
'<button type="button" class="aksrt-wie-modal__close' +
|
||
(opts.closeBtnClass ? ' ' + opts.closeBtnClass : '') +
|
||
'" aria-label="关闭">×</button>' +
|
||
(opts.bodyHtml || '') +
|
||
(opts.actionsHtml || '') +
|
||
'<p class="aksrt-wie-modal__result' +
|
||
(opts.resultExtraClass ? ' ' + opts.resultExtraClass : '') +
|
||
'"></p>' +
|
||
'</div></div>';
|
||
|
||
$modal = $(shell).appendTo('body');
|
||
|
||
if (opts.importMode) {
|
||
bindImportModeI18n($modal, opts.importMode.replaceSelector, opts.importMode.appendSelector);
|
||
}
|
||
|
||
if (opts.onInit) {
|
||
opts.onInit($modal);
|
||
}
|
||
|
||
$modal.find('.aksrt-wie-modal__close, ' + (opts.cancelSelector || '')).on('click', close);
|
||
$modal.on('click', '[' + opts.closeAttr + ']', close);
|
||
$modal.find('.aksrt-wie-modal__dialog').on('click', function (e) {
|
||
e.stopPropagation();
|
||
});
|
||
|
||
return $modal;
|
||
}
|
||
|
||
function close() {
|
||
if ($modal && $modal.length) {
|
||
$modal.removeClass('is-open').attr('aria-hidden', 'true');
|
||
}
|
||
}
|
||
|
||
function open() {
|
||
ensure();
|
||
clearWieResult($result());
|
||
$modal.addClass('is-open').attr('aria-hidden', 'false');
|
||
}
|
||
|
||
function $result() {
|
||
return ensure().find(opts.resultSelector || '.aksrt-wie-modal__result');
|
||
}
|
||
|
||
function setImportModeDefault(radioName, value) {
|
||
ensure().find('input[name="' + radioName + '"][value="' + (value || 'append') + '"]').prop('checked', true);
|
||
}
|
||
|
||
return {
|
||
ensure: ensure,
|
||
close: close,
|
||
open: open,
|
||
$result: $result,
|
||
$el: function () {
|
||
return ensure();
|
||
},
|
||
setImportModeDefault: setImportModeDefault,
|
||
};
|
||
}
|
||
|
||
/** WIE:渲染模板列表项 */
|
||
function appendTemplateListItem($list, template) {
|
||
var id = template.id || '';
|
||
var $item = $('<div class="aksrt-wie-template-item"/>');
|
||
var $row = $('<div class="aksrt-wie-template-item-row"/>');
|
||
var $label = $('<label class="aksrt-wie-template-item-label"/>');
|
||
$label.append($('<input type="radio" name="aksrt-wie-template-id" />').val(id));
|
||
var $meta = $('<span class="aksrt-wie-template-item-meta"/>');
|
||
$meta.append($('<span class="aksrt-wie-template-item-title"/>').text(template.title || id));
|
||
if (template.desc) {
|
||
$meta.append($('<span class="aksrt-wie-template-item-desc"/>').html(template.desc));
|
||
}
|
||
if (template.remind) {
|
||
$meta.append($('<span class="aksrt-wie-template-item-remind"/>').html('<i class="fa fas fa-fw fa-info-circle"></i>' + template.remind));
|
||
}
|
||
$label.append($meta);
|
||
$row.append($label);
|
||
$item.append($row);
|
||
$list.append($item);
|
||
}
|
||
|
||
$(document).ready(function () {
|
||
var _body = $('body');
|
||
|
||
var img_ss = '';
|
||
|
||
$('body').on('click', '.add_lists_button', function () {
|
||
var _s_i = parseInt($(this).attr('data-count'));
|
||
var _s_m = $(this).attr('data-name');
|
||
|
||
var _s_html =
|
||
'<div class="widget_ui_slider_g">\
|
||
<div class="panel"><h4 class="panel-title">栏目' +
|
||
(_s_i + 1) +
|
||
'</h4><div class="panel-conter">\
|
||
<label>栏目' +
|
||
(_s_i + 1) +
|
||
'-标题(必填):\
|
||
<input style="width:100%;" type="text" name="' +
|
||
_s_m +
|
||
'[' +
|
||
_s_i +
|
||
'][title]" value="">\
|
||
</label>\
|
||
<label>栏目' +
|
||
(_s_i + 1) +
|
||
'-分类限制:\
|
||
<input style="width:100%;" type="text" name="' +
|
||
_s_m +
|
||
'[' +
|
||
_s_i +
|
||
'][cat]" value="">\
|
||
</label>\
|
||
<label>栏目' +
|
||
(_s_i + 1) +
|
||
'-专题限制:\
|
||
<input style="width:100%;" type="text" name="' +
|
||
_s_m +
|
||
'[' +
|
||
_s_i +
|
||
'][topics]" value="">\
|
||
</label>\
|
||
<label>栏目' +
|
||
(_s_i + 1) +
|
||
'-排序方式:\
|
||
<select style="width:100%;" name="' +
|
||
_s_m +
|
||
'[' +
|
||
_s_i +
|
||
'][orderby]">\
|
||
<option value="comment_count">评论数</option>\
|
||
<option value="views" selected="selected">浏览量</option>\
|
||
<option value="like">点赞数</option>\
|
||
<option value="favorite">收藏数</option>\
|
||
<option value="date">发布时间</option>\
|
||
<option value="modified">更新时间</option>\
|
||
<option value="rand">随机排序</option>\
|
||
</select>\
|
||
</label></div></div></div>';
|
||
$(this)
|
||
.attr('data-count', _s_i + 1)
|
||
.before(_s_html);
|
||
});
|
||
$('body').on('click', '.rem_lists_button', function () {
|
||
$(this).prev().prev('.widget_ui_slider_g').remove();
|
||
var add_b = $(this).siblings('.add_button');
|
||
var _s_i = parseInt(add_b.attr('data-count'));
|
||
add_b.attr('data-count', _s_i - 1);
|
||
});
|
||
|
||
var ashu_upload_frame;
|
||
img_ss = '';
|
||
$('body').on('click', '.delimg_upload_button', function () {
|
||
$(this).siblings('div').html('');
|
||
$(this).siblings('label').find('input').val('');
|
||
});
|
||
$('body').on('click', '.add_slider_button', function () {
|
||
var _s_i = parseInt($(this).attr('data-count'));
|
||
var _s_i1 = _s_i + 1;
|
||
var _s_m = $(this).attr('data-name');
|
||
var _s_html = buildMediaRepeaterRow('幻灯片', _s_i, _s_m, {
|
||
title: '-标题:',
|
||
dec: '-简介',
|
||
href: '-链接',
|
||
link: '-图片',
|
||
});
|
||
$(this).attr('data-count', _s_i1).before(_s_html);
|
||
});
|
||
$('body').on('click', '.add_links_button', function () {
|
||
var _s_i = parseInt($(this).attr('data-count'));
|
||
var _s_i1 = _s_i + 1;
|
||
var _s_m = $(this).attr('data-name');
|
||
var _s_html = buildMediaRepeaterRow('链接', _s_i, _s_m, {
|
||
title: '-名称(必填):',
|
||
dec: '-简介',
|
||
href: '-链接(必填)',
|
||
link: '-图片',
|
||
});
|
||
$(this).attr('data-count', _s_i1).before(_s_html);
|
||
});
|
||
$('body').on('click', '.add_notice_button', function () {
|
||
var _s_i = parseInt($(this).attr('data-count'));
|
||
var _s_i1 = _s_i + 1;
|
||
var _s_m = $(this).attr('data-name');
|
||
$(this).attr('data-count', _s_i1).before(buildNoticeRepeaterRow(_s_i, _s_m));
|
||
});
|
||
$('body').on('click', '.ashu_upload_button', function (event) {
|
||
var _his = $(this);
|
||
event.preventDefault();
|
||
if (ashu_upload_frame) {
|
||
ashu_upload_frame.open();
|
||
return;
|
||
}
|
||
ashu_upload_frame = wp.media({
|
||
title: aksrt__('admin_select_image_slide'),
|
||
button: {
|
||
text: aksrt__('confirm'),
|
||
},
|
||
multiple: true,
|
||
});
|
||
ashu_upload_frame.on('select', function () {
|
||
var attachment = ashu_upload_frame.state().get('selection').first().toJSON();
|
||
|
||
img_ss = '<img src="' + attachment.url + '">';
|
||
_his.siblings('div').html(img_ss);
|
||
_his.siblings('label').find('input').val(attachment.url);
|
||
});
|
||
ashu_upload_frame.open();
|
||
});
|
||
$('body').on('click', '.cat-help-button', function () {
|
||
$(this).siblings('.cat-help-con').slideToggle();
|
||
});
|
||
$('body').on('click', '.panel-title', function () {
|
||
$(this).siblings('.panel-conter').slideToggle();
|
||
});
|
||
|
||
/**
|
||
* 侧栏小工具:在侧栏描述下注入导出/导入,弹层内 JSON 文本域
|
||
*/
|
||
(function () {
|
||
var _zib = window.aksrt_widget_set_var;
|
||
if (!_zib || !_zib.ajaxUrl) {
|
||
return;
|
||
}
|
||
|
||
var state = { mode: 'export', sidebarId: '' };
|
||
var tplState = { sidebarId: '' };
|
||
|
||
var ioModal = createWieModal({
|
||
id: 'aksrt-wie-modal',
|
||
closeAttr: 'data-aksrt-wie-close',
|
||
cancelSelector: '.aksrt-wie-modal__cancel',
|
||
importMode: {
|
||
replaceSelector: '.aksrt-wie-import-txt-replace',
|
||
appendSelector: '.aksrt-wie-import-txt-append',
|
||
},
|
||
bodyHtml:
|
||
'<h3 class="aksrt-wie-modal__title"></h3>' +
|
||
'<p class="aksrt-wie-modal__hint description"></p>' +
|
||
'<textarea class="aksrt-wie-modal__textarea" spellcheck="false"></textarea>' +
|
||
getImportModeHtml('aksrt-wie-import-mode', 'aksrt-wie-import-txt-replace', 'aksrt-wie-import-txt-append'),
|
||
actionsHtml:
|
||
'<p class="aksrt-wie-modal__actions">' +
|
||
'<button type="button" class="button aksrt-wie-modal__copy"></button>' +
|
||
'<button type="button" class="button button-primary aksrt-wie-modal__submit"></button>' +
|
||
'<button type="button" class="button aksrt-wie-modal__cancel"></button>' +
|
||
'</p>',
|
||
onInit: function ($modal) {
|
||
$modal.find('.aksrt-wie-modal__copy').on('click', function () {
|
||
var ta = $modal.find('.aksrt-wie-modal__textarea')[0];
|
||
if (!ta) {
|
||
return;
|
||
}
|
||
ta.select();
|
||
ta.setSelectionRange(0, ta.value.length);
|
||
try {
|
||
document.execCommand('copy');
|
||
$(this).text(aksrt__('copyDone'));
|
||
} catch (err) {
|
||
alert(aksrt__('copyFail'));
|
||
}
|
||
});
|
||
$modal.find('.aksrt-wie-modal__submit').on('click', doImportSubmit);
|
||
},
|
||
});
|
||
|
||
var tplModal = createWieModal({
|
||
id: 'aksrt-wie-template-modal',
|
||
extraClass: 'aksrt-wie-template-layer',
|
||
closeAttr: 'data-aksrt-wie-tpl-close',
|
||
closeBtnClass: 'aksrt-wie-tpl-close',
|
||
cancelSelector: '.aksrt-wie-template-cancel',
|
||
resultExtraClass: 'aksrt-wie-template-result',
|
||
importMode: {
|
||
replaceSelector: '.aksrt-wie-tpl-txt-replace',
|
||
appendSelector: '.aksrt-wie-tpl-txt-append',
|
||
},
|
||
bodyHtml:
|
||
'<h3 class="aksrt-wie-modal__title aksrt-wie-template-head-title"></h3>' +
|
||
'<p class="aksrt-wie-modal__hint description aksrt-wie-template-head-hint"></p>' +
|
||
'<div class="aksrt-wie-template-list-wrap"><p class="aksrt-wie-template-list-status description"></p><div class="aksrt-wie-template-list"></div></div>' +
|
||
getImportModeHtml('aksrt-wie-template-import-mode', 'aksrt-wie-tpl-txt-replace', 'aksrt-wie-tpl-txt-append').replace(
|
||
'aksrt-wie-modal__import-mode',
|
||
'aksrt-wie-modal__import-mode aksrt-wie-template-import-mode'
|
||
),
|
||
actionsHtml:
|
||
'<p class="aksrt-wie-modal__actions">' +
|
||
'<button type="button" class="button button-primary aksrt-wie-template-submit"></button>' +
|
||
'<button type="button" class="button aksrt-wie-template-cancel"></button>' +
|
||
'</p>',
|
||
onInit: function ($modal) {
|
||
$modal.find('.aksrt-wie-template-head-title').text(aksrt__('templateModalTitle'));
|
||
$modal.find('.aksrt-wie-template-head-hint').html(aksrt__('templateHint'));
|
||
$modal.find('.aksrt-wie-template-submit').text(aksrt__('templateSubmit'));
|
||
$modal.find('.aksrt-wie-template-cancel').text(aksrt__('close'));
|
||
var $tplDialog = $modal.find('.aksrt-wie-modal__dialog');
|
||
$modal.find('.aksrt-wie-template-submit').on('click', doTemplateImportSubmit);
|
||
$tplDialog.on('click', '.aksrt-wie-template-item-expand', function (e) {
|
||
e.preventDefault();
|
||
e.stopPropagation();
|
||
var $btn = $(this);
|
||
var expanded = $btn.hasClass('is-expanded');
|
||
var $item = $btn.closest('.aksrt-wie-template-item');
|
||
var $mods = $item.find('.aksrt-wie-template-item-modules');
|
||
if (expanded) {
|
||
$btn.removeClass('is-expanded').attr('aria-expanded', 'false');
|
||
$btn.attr('aria-label', aksrt__('templateExpandModules'));
|
||
$mods.hide();
|
||
} else {
|
||
$btn.addClass('is-expanded').attr('aria-expanded', 'true');
|
||
$btn.attr('aria-label', aksrt__('templateCollapseModules'));
|
||
$mods.show();
|
||
}
|
||
});
|
||
},
|
||
});
|
||
|
||
function setModeUi(mode) {
|
||
var $modal = ioModal.$el();
|
||
var $copy = $modal.find('.aksrt-wie-modal__copy');
|
||
var $sub = $modal.find('.aksrt-wie-modal__submit');
|
||
if (mode === 'export') {
|
||
$copy.show().text(aksrt__('copy'));
|
||
$sub.hide();
|
||
$modal.find('.aksrt-wie-modal__import-mode').hide();
|
||
} else {
|
||
$copy.hide();
|
||
$sub.show().text(aksrt__('submitImport')).prop('disabled', false);
|
||
$modal.find('.aksrt-wie-modal__import-mode').show();
|
||
}
|
||
$modal.find('.aksrt-wie-modal__cancel').text(aksrt__('close'));
|
||
}
|
||
|
||
function openModal(mode, sidebarId) {
|
||
state.mode = mode;
|
||
state.sidebarId = sidebarId;
|
||
var $modal = ioModal.ensure();
|
||
clearWieResult(ioModal.$result());
|
||
$modal
|
||
.find('.aksrt-wie-modal__textarea')
|
||
.prop('readonly', mode === 'export')
|
||
.val('');
|
||
if (mode === 'export') {
|
||
$modal.find('.aksrt-wie-modal__title').text(aksrt__('exportTitle'));
|
||
$modal.find('.aksrt-wie-modal__hint').text(aksrt__('exportHint'));
|
||
setModeUi('export');
|
||
fetchExport();
|
||
} else {
|
||
$modal.find('.aksrt-wie-modal__title').text(aksrt__('importTitle'));
|
||
$modal.find('.aksrt-wie-modal__hint').text(aksrt__('importHint'));
|
||
ioModal.setImportModeDefault('aksrt-wie-import-mode', 'append');
|
||
setModeUi('import');
|
||
}
|
||
ioModal.open();
|
||
}
|
||
|
||
function fetchExport() {
|
||
var $modal = ioModal.$el();
|
||
var $ta = $modal.find('.aksrt-wie-modal__textarea');
|
||
$ta.val(aksrt__('loadingExport'));
|
||
wiePost(_zib, 'aksrt_wie_export_sidebar', {
|
||
sidebar_id: state.sidebarId,
|
||
})
|
||
.done(function (res) {
|
||
if (res.success && res.data && res.data.json_text) {
|
||
$ta.val(res.data.json_text);
|
||
} else {
|
||
$ta.val('');
|
||
showWieError(ioModal.$result(), getWieMessage(res, 'admin_export_failed'));
|
||
}
|
||
})
|
||
.fail(function () {
|
||
$ta.val('');
|
||
showNetworkError(ioModal.$result());
|
||
});
|
||
}
|
||
|
||
function doImportSubmit() {
|
||
var $modal = ioModal.$el();
|
||
var importMode = $modal.find('input[name="aksrt-wie-import-mode"]:checked').val() || 'replace';
|
||
var confirmMsg =
|
||
importMode === 'append'
|
||
? aksrt__('confirmImportAppend') || aksrt__('confirmImport')
|
||
: aksrt__('confirmImportReplace') || aksrt__('confirmImport');
|
||
if (!window.confirm(confirmMsg)) {
|
||
return;
|
||
}
|
||
var raw = $.trim($modal.find('.aksrt-wie-modal__textarea').val());
|
||
if (!raw) {
|
||
showWieError(ioModal.$result(), aksrt__('admin_paste_json_first'));
|
||
return;
|
||
}
|
||
var $sub = $modal.find('.aksrt-wie-modal__submit');
|
||
withButtonLoading($sub, aksrt__('loadingImport'), aksrt__('submitImport'), function () {
|
||
return wiePost(_zib, 'aksrt_wie_import_sidebar', {
|
||
sidebar_id: state.sidebarId,
|
||
json: raw,
|
||
import_mode: importMode,
|
||
})
|
||
.done(function (res) {
|
||
if (res.success && res.data) {
|
||
$modal.find('.aksrt-wie-modal__textarea').val('');
|
||
}
|
||
renderImportResult(ioModal.$result(), res);
|
||
})
|
||
.fail(function () {
|
||
showNetworkError(ioModal.$result());
|
||
});
|
||
});
|
||
}
|
||
|
||
function openTemplatePicker(sidebarId) {
|
||
tplState.sidebarId = sidebarId;
|
||
var $modal = tplModal.ensure();
|
||
tplModal.setImportModeDefault('aksrt-wie-template-import-mode', 'append');
|
||
var $list = $modal.find('.aksrt-wie-template-list').empty();
|
||
var $st = $modal.find('.aksrt-wie-template-list-status').text(aksrt__('templateLoadingList'));
|
||
tplModal.open();
|
||
wiePost(_zib, 'aksrt_wie_list_templates', {})
|
||
.done(function (res) {
|
||
$st.text('');
|
||
if (!res.success || !res.data || !res.data.templates || !res.data.templates.length) {
|
||
$list.empty();
|
||
$st.text(aksrt__('templateEmpty'));
|
||
return;
|
||
}
|
||
res.data.templates.forEach(function (t) {
|
||
appendTemplateListItem($list, t);
|
||
});
|
||
})
|
||
.fail(function () {
|
||
$st.text(aksrt__('admin_network_error'));
|
||
});
|
||
}
|
||
|
||
function doTemplateImportSubmit() {
|
||
var $modal = tplModal.$el();
|
||
var tid = $modal.find('input[name="aksrt-wie-template-id"]:checked').val();
|
||
if (!tid) {
|
||
alert(aksrt__('templatePick'));
|
||
return;
|
||
}
|
||
var importMode = $modal.find('input[name="aksrt-wie-template-import-mode"]:checked').val() || 'replace';
|
||
var titleText =
|
||
$.trim(
|
||
$modal
|
||
.find('input[name="aksrt-wie-template-id"]:checked')
|
||
.closest('.aksrt-wie-template-item')
|
||
.find('.aksrt-wie-template-item-title')
|
||
.first()
|
||
.text()
|
||
) || tid;
|
||
var modeLabel = importMode === 'append' ? aksrt__('importModeAppend') || '追加' : aksrt__('importModeReplace') || '覆盖';
|
||
var cmsg = (aksrt__('confirmTemplate') || '确定导入?').replace('{title}', titleText).replace('{mode}', modeLabel);
|
||
if (!window.confirm(cmsg)) {
|
||
return;
|
||
}
|
||
var $sub = $modal.find('.aksrt-wie-template-submit');
|
||
withButtonLoading($sub, aksrt__('loadingImport'), aksrt__('templateSubmit'), function () {
|
||
return wiePost(_zib, 'aksrt_wie_import_template', {
|
||
sidebar_id: tplState.sidebarId,
|
||
template_id: tid,
|
||
import_mode: importMode,
|
||
})
|
||
.done(function (res) {
|
||
renderImportResult(tplModal.$result(), res, { autoReload: true });
|
||
})
|
||
.fail(function () {
|
||
showNetworkError(tplModal.$result());
|
||
});
|
||
});
|
||
}
|
||
|
||
function injectToolbarAfter($anchor, sidebarId) {
|
||
if (!$anchor || !$anchor.length || !sidebarId) {
|
||
return;
|
||
}
|
||
if ($anchor.next('.aksrt-wie-toolbar').length) {
|
||
return;
|
||
}
|
||
var $tb = $(
|
||
'<div class="aksrt-wie-toolbar"><button type="button" class="button button-small aksrt-wie-btn-export">' +
|
||
aksrt__('admin_export_modules') +
|
||
'</button>' +
|
||
'<button type="button" class="button button-small aksrt-wie-btn-import">' +
|
||
aksrt__('admin_import_modules') +
|
||
'</button>' +
|
||
'<button type="button" class="button button-small aksrt-wie-btn-template">' +
|
||
aksrt__('admin_import_template') +
|
||
'</button></div>'
|
||
);
|
||
$tb.find('.aksrt-wie-btn-export').data('zibWieSid', sidebarId);
|
||
$tb.find('.aksrt-wie-btn-import').data('zibWieSid', sidebarId);
|
||
$tb.find('.aksrt-wie-btn-template').data('zibWieSid', sidebarId);
|
||
$anchor.after($tb);
|
||
}
|
||
|
||
function scanWidgetsScreen() {
|
||
$('.widgets-holder-wrap:not(#available-widgets):not(.inactive-sidebar):not(.is-wid-load)').each(function () {
|
||
var $wrap = $(this);
|
||
var $sort = $wrap.find('.widgets-sortables').first();
|
||
var sid = $sort.attr('id');
|
||
if (!sid) {
|
||
return;
|
||
}
|
||
var $desc = $sort.find('.sidebar-description .description').first();
|
||
var $anchor = $desc.length ? $desc : $sort.find('.description').first();
|
||
$wrap.addClass('is-wid-load');
|
||
injectToolbarAfter($anchor, sid);
|
||
});
|
||
}
|
||
|
||
function scanCustomize() {
|
||
$('.control-section-sidebar:not(.aksrt-is-load)').each(function () {
|
||
var $sec = $(this);
|
||
var accId = $sec.attr('id') || '';
|
||
var prefix = 'sub-accordion-section-sidebar-widgets-';
|
||
if (accId.indexOf(prefix) !== 0) {
|
||
return;
|
||
}
|
||
var sid = accId.slice(prefix.length);
|
||
if (!sid) {
|
||
return;
|
||
}
|
||
$sec.addClass('aksrt-is-load');
|
||
var $desc = $sec.find('.section-meta .description').first();
|
||
var $anchor = $desc.length ? $desc : $sec.find('.accordion-section-title, .customize-section-title').first();
|
||
injectToolbarAfter($anchor, sid);
|
||
});
|
||
|
||
$('.customize-control-widget_form:not(.aksrt-is-load)').each(function () {
|
||
var $form = $(this);
|
||
var $nav = $form.find('.widget-reorder-nav');
|
||
if ($nav.length) {
|
||
var $delete = $('<span class="aksrt-wie-btn_delete dashicons-trash" tabindex="0">' + aksrt__('admin_remove_module') + '</span>');
|
||
$form.find('.widget-reorder-nav').append($delete);
|
||
$form.addClass('aksrt-is-load');
|
||
}
|
||
});
|
||
}
|
||
|
||
_body.on('click', '.aksrt-wie-btn_delete', function () {
|
||
var $this = $(this);
|
||
var $form = $this.closest('.customize-control-widget_form');
|
||
$form.find('.widget-control-remove').click();
|
||
});
|
||
|
||
_body.on('click', '.aksrt-wie-modal__reload', function () {
|
||
window.location.reload();
|
||
});
|
||
|
||
_body.on('click', '.aksrt-wie-btn-export', function () {
|
||
var sid = $(this).data('zibWieSid');
|
||
if (sid) {
|
||
openModal('export', sid);
|
||
}
|
||
});
|
||
|
||
_body.on('click', '.aksrt-wie-btn-import', function () {
|
||
var sid = $(this).data('zibWieSid');
|
||
if (sid) {
|
||
openModal('import', sid);
|
||
}
|
||
});
|
||
|
||
_body.on('click', '.aksrt-wie-btn-template', function () {
|
||
var sid = $(this).data('zibWieSid');
|
||
if (sid) {
|
||
openTemplatePicker(sid);
|
||
}
|
||
});
|
||
|
||
if (_body.hasClass('widgets-php')) {
|
||
scanWidgetsScreen();
|
||
}
|
||
|
||
if (_body.hasClass('wp-customizer')) {
|
||
if (typeof wp !== 'undefined' && wp.customize) {
|
||
wp.customize.bind('ready', function () {
|
||
setTimeout(scanCustomize, 300);
|
||
});
|
||
}
|
||
|
||
_body.on('click', '.control-panel-widgets .control-section-sidebar', function () {
|
||
setTimeout(scanCustomize, 100);
|
||
});
|
||
}
|
||
})();
|
||
});
|
||
})(jQuery, document);
|