openprototype 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +58 -0
- package/CONVENTIONS.md +54 -0
- package/LICENSE +21 -0
- package/README.md +234 -3
- package/bin/cli.js +297 -0
- package/lib/config.js +162 -0
- package/package.json +32 -10
- package/product/demo/pc/customer/customer_form.html +137 -0
- package/product/demo/pc/customer/customer_form.md +33 -0
- package/product/demo/pc/customer/customer_list.html +132 -0
- package/product/demo/pc/customer/customer_list.md +35 -0
- package/product/demo/pc/demo-data.js +23 -0
- package/product/demo/pc/index.html +1082 -0
- package/product/demo/pc/nav-tree.json +15 -0
- package/proto-kit.config.json +13 -0
- package/runtime/server.js +664 -0
- package/runtime/shared/ai-polish.js +159 -0
- package/runtime/shared/base-manager.js +168 -0
- package/runtime/shared/common/component-renderer.js +917 -0
- package/runtime/shared/common/copy-manager.js +134 -0
- package/runtime/shared/common/data-bridge.js +45 -0
- package/runtime/shared/common/form-renderer.js +211 -0
- package/runtime/shared/common/form.js +357 -0
- package/runtime/shared/common/formatter.js +78 -0
- package/runtime/shared/common/index.js +37 -0
- package/runtime/shared/common/line-chart.js +357 -0
- package/runtime/shared/common/modal-manager.js +82 -0
- package/runtime/shared/common/prd-panel.css +395 -0
- package/runtime/shared/common/prd-panel.js +1280 -0
- package/runtime/shared/common/prd-popup.js +168 -0
- package/runtime/shared/common/search-renderer.js +95 -0
- package/runtime/shared/common/table-renderer.js +177 -0
- package/runtime/shared/common/table.js +160 -0
- package/runtime/shared/common/toast.js +99 -0
- package/runtime/shared/common/ui-preference-manager.js +74 -0
- package/runtime/shared/common/view-mode.js +30 -0
- package/runtime/shared/common.js +43 -0
- package/runtime/shared/constants/icons.js +28 -0
- package/runtime/shared/constants/status.js +67 -0
- package/runtime/shared/libs/marked.min.js +69 -0
- package/runtime/shared/prd-content.js +9 -0
- package/runtime/shared/styles.css +7673 -0
- package/runtime/shared/utils.js +110 -0
- package/runtime/shared/view-controls.js +148 -0
- package/runtime/shell/agent-panel.css +429 -0
- package/runtime/shell/agent-panel.js +629 -0
- package/scripts/check/run-checks.js +58 -0
- package/scripts/check/smoke-test.js +244 -0
- package/scripts/check/static-check.js +248 -0
- package/scripts/check/sync-nav-tree.js +139 -0
- package/templates/AGENTS.md +58 -0
- package/templates/CONVENTIONS.md +54 -0
- package/templates/gitignore +12 -0
- package/templates/product-shell/index.html +1082 -0
- package/templates/skills/auto-test/SKILL.md +62 -0
- package/templates/skills/xiaojia/SKILL.md +299 -0
- package/cli.js +0 -4
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* prd-popup.js — 独立弹窗页面的 PRD 浮动按钮
|
|
3
|
+
*
|
|
4
|
+
* 用法:在弹窗 HTML 中添加以下两行即可:
|
|
5
|
+
* <script src="../../../shared/libs/marked.min.js" defer></script>
|
|
6
|
+
* <script src="../../../shared/common/prd-popup.js"></script>
|
|
7
|
+
*
|
|
8
|
+
* 自动根据 HTML 文件名(如 新增_商务餐.html)查找同目录下的 .md 文件(如 新增_商务餐.md)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
(function() {
|
|
12
|
+
// 防止在已有 PRD 面板的页面上重复创建
|
|
13
|
+
if (document.getElementById('prdFloatBtn')) return;
|
|
14
|
+
|
|
15
|
+
// 优先复用 common/prd-panel.js 的完整面板,避免弹窗页缺少复制/编辑/更新按钮
|
|
16
|
+
if (typeof initPrdPanel === 'function') {
|
|
17
|
+
initPrdPanel();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 通知 prd-panel.js 跳过初始化(弹窗页面的 PRD 由本脚本管理)
|
|
22
|
+
window.__prdPopupReady = true;
|
|
23
|
+
|
|
24
|
+
var htmlName = window.location.pathname.split('/').pop();
|
|
25
|
+
var mdName = htmlName.replace(/\.html$/i, '.md');
|
|
26
|
+
var panelWidth = null;
|
|
27
|
+
var defaultPanelWidth = 680;
|
|
28
|
+
var minPanelWidth = 360;
|
|
29
|
+
var maxPanelRatio = 0.86;
|
|
30
|
+
|
|
31
|
+
// PRD 浮动按钮
|
|
32
|
+
var btn = document.createElement('div');
|
|
33
|
+
btn.id = 'prdFloatBtn';
|
|
34
|
+
btn.className = 'prd-float-btn';
|
|
35
|
+
btn.textContent = 'PRD';
|
|
36
|
+
document.body.appendChild(btn);
|
|
37
|
+
|
|
38
|
+
// PRD 侧滑面板
|
|
39
|
+
var panel = document.createElement('div');
|
|
40
|
+
panel.id = 'prdPanel';
|
|
41
|
+
panel.className = 'prd-slide-panel';
|
|
42
|
+
panel.innerHTML =
|
|
43
|
+
'<div class="prd-panel-resizer"></div>' +
|
|
44
|
+
'<div class="prd-panel-header">' +
|
|
45
|
+
' <div class="prd-panel-header-left">' +
|
|
46
|
+
' <h3>PRD文档</h3>' +
|
|
47
|
+
' </div>' +
|
|
48
|
+
' <button class="prd-panel-close" id="prdPanelCloseBtn">×</button>' +
|
|
49
|
+
'</div>' +
|
|
50
|
+
'<div class="prd-panel-content" id="prdPanelContent"></div>';
|
|
51
|
+
document.body.appendChild(panel);
|
|
52
|
+
|
|
53
|
+
var contentEl = document.getElementById('prdPanelContent');
|
|
54
|
+
|
|
55
|
+
// 切换面板
|
|
56
|
+
btn.addEventListener('click', function() {
|
|
57
|
+
if (panel.classList.contains('open')) {
|
|
58
|
+
panel.classList.remove('open');
|
|
59
|
+
} else {
|
|
60
|
+
applyPanelWidth(panelWidth || getSavedPanelWidth() || defaultPanelWidth);
|
|
61
|
+
panel.classList.add('open');
|
|
62
|
+
loadPrd();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// 关闭按钮
|
|
67
|
+
document.getElementById('prdPanelCloseBtn').addEventListener('click', function() {
|
|
68
|
+
panel.classList.remove('open');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 点击面板外部关闭
|
|
72
|
+
document.addEventListener('click', function(e) {
|
|
73
|
+
if (!panel.classList.contains('open')) return;
|
|
74
|
+
if (!panel.contains(e.target) && e.target !== btn) {
|
|
75
|
+
panel.classList.remove('open');
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
var resizeHandle = panel.querySelector('.prd-panel-resizer');
|
|
80
|
+
if (resizeHandle) {
|
|
81
|
+
var isResizing = false;
|
|
82
|
+
resizeHandle.addEventListener('mousedown', function(e) {
|
|
83
|
+
isResizing = true;
|
|
84
|
+
resizeHandle.classList.add('resizing');
|
|
85
|
+
document.body.style.cursor = 'ew-resize';
|
|
86
|
+
document.body.style.userSelect = 'none';
|
|
87
|
+
e.preventDefault();
|
|
88
|
+
});
|
|
89
|
+
document.addEventListener('mousemove', function(e) {
|
|
90
|
+
if (!isResizing) return;
|
|
91
|
+
applyPanelWidth(window.innerWidth - e.clientX);
|
|
92
|
+
});
|
|
93
|
+
document.addEventListener('mouseup', function() {
|
|
94
|
+
if (!isResizing) return;
|
|
95
|
+
isResizing = false;
|
|
96
|
+
resizeHandle.classList.remove('resizing');
|
|
97
|
+
document.body.style.cursor = '';
|
|
98
|
+
document.body.style.userSelect = '';
|
|
99
|
+
if (panelWidth) {
|
|
100
|
+
localStorage.setItem('prdPanelWidth', panelWidth);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getMaxPanelWidth() {
|
|
106
|
+
return Math.floor(window.innerWidth * maxPanelRatio);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function clampPanelWidth(width) {
|
|
110
|
+
width = parseInt(width, 10);
|
|
111
|
+
if (!isFinite(width)) {
|
|
112
|
+
width = defaultPanelWidth;
|
|
113
|
+
}
|
|
114
|
+
return Math.max(minPanelWidth, Math.min(getMaxPanelWidth(), width));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function applyPanelWidth(width) {
|
|
118
|
+
panelWidth = clampPanelWidth(width);
|
|
119
|
+
panel.style.width = panelWidth + 'px';
|
|
120
|
+
panel.style.minWidth = panelWidth + 'px';
|
|
121
|
+
panel.style.maxWidth = 'none';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function getSavedPanelWidth() {
|
|
125
|
+
var savedWidth = localStorage.getItem('prdPanelWidth');
|
|
126
|
+
return savedWidth ? parseInt(savedWidth, 10) : null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function loadPrd() {
|
|
130
|
+
if (!contentEl) return;
|
|
131
|
+
contentEl.innerHTML = '<div style="padding:20px;text-align:center;color:var(--rx-text-3);">加载中...</div>';
|
|
132
|
+
|
|
133
|
+
var xhr = new XMLHttpRequest();
|
|
134
|
+
xhr.open('GET', './' + mdName, true);
|
|
135
|
+
xhr.onload = function() {
|
|
136
|
+
if (xhr.status === 200 || xhr.status === 0) {
|
|
137
|
+
if (typeof marked !== 'undefined') {
|
|
138
|
+
contentEl.innerHTML = marked.parse(xhr.responseText);
|
|
139
|
+
// 给表格包 wrapper
|
|
140
|
+
var tables = contentEl.querySelectorAll('table');
|
|
141
|
+
tables.forEach(function(t) {
|
|
142
|
+
if (!t.parentElement.classList.contains('table-wrapper')) {
|
|
143
|
+
var w = document.createElement('div');
|
|
144
|
+
w.className = 'table-wrapper';
|
|
145
|
+
t.parentNode.insertBefore(w, t);
|
|
146
|
+
w.appendChild(t);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
contentEl.innerHTML = '<pre style="padding:12px;white-space:pre-wrap;">' + escapeHtml(xhr.responseText) + '</pre>';
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
contentEl.innerHTML = '<div style="padding:20px;color:var(--rx-danger);">PRD文档加载失败</div>';
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
xhr.onerror = function() {
|
|
157
|
+
contentEl.innerHTML = '<div style="padding:20px;color:var(--rx-danger);">PRD文档加载失败(请通过 file:// 协议打开页面)</div>';
|
|
158
|
+
};
|
|
159
|
+
xhr.send();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function escapeHtml(str) {
|
|
163
|
+
return String(str || '').replace(/[&<>"']/g, function(ch) {
|
|
164
|
+
var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
165
|
+
return map[ch] || ch;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SearchRenderer 模块
|
|
3
|
+
* 统一渲染 PC 列表页搜索条件和搜索区操作按钮
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
var SearchRenderer = {
|
|
7
|
+
_escape: function(value) {
|
|
8
|
+
if (value === undefined || value === null) return '';
|
|
9
|
+
return String(value)
|
|
10
|
+
.replace(/&/g, '&')
|
|
11
|
+
.replace(/"/g, '"')
|
|
12
|
+
.replace(/</g, '<')
|
|
13
|
+
.replace(/>/g, '>');
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
render: function(containerId, config) {
|
|
17
|
+
var container = document.getElementById(containerId);
|
|
18
|
+
if (!container) return;
|
|
19
|
+
|
|
20
|
+
var html = '';
|
|
21
|
+
var fields = config.fields || [];
|
|
22
|
+
var actions = config.actions || [];
|
|
23
|
+
|
|
24
|
+
fields.forEach(function(field) {
|
|
25
|
+
html += SearchRenderer.renderField(field);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (actions.length) {
|
|
29
|
+
html += SearchRenderer.renderActions(actions);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
container.innerHTML = html;
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
renderField: function(field) {
|
|
36
|
+
if (field.type === 'custom') {
|
|
37
|
+
return field.html || '';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
var labelHtml = field.label ? '<label class="form-label">' + this._escape(field.label) + '</label>' : '';
|
|
41
|
+
var controlHtml = '';
|
|
42
|
+
|
|
43
|
+
if (field.type === 'select') {
|
|
44
|
+
controlHtml = this.renderSelect(field);
|
|
45
|
+
} else if (field.type === 'checkbox') {
|
|
46
|
+
controlHtml = this.renderCheckbox(field);
|
|
47
|
+
} else {
|
|
48
|
+
controlHtml = this.renderInput(field);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return '<div class="form-item">' + labelHtml + controlHtml + '</div>';
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
renderInput: function(field) {
|
|
55
|
+
var type = field.inputType || 'text';
|
|
56
|
+
var placeholder = field.placeholder ? ' placeholder="' + this._escape(field.placeholder) + '"' : '';
|
|
57
|
+
var value = field.value ? ' value="' + this._escape(field.value) + '"' : '';
|
|
58
|
+
var maxlength = field.maxlength ? ' maxlength="' + this._escape(field.maxlength) + '"' : '';
|
|
59
|
+
var style = field.style ? ' style="' + this._escape(field.style) + '"' : '';
|
|
60
|
+
return '<input type="' + this._escape(type) + '" class="form-input" id="' + this._escape(field.id) + '"' + placeholder + value + maxlength + style + '>';
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
renderSelect: function(field) {
|
|
64
|
+
var optionsHtml = '';
|
|
65
|
+
(field.options || []).forEach(function(option) {
|
|
66
|
+
var selected = field.value === option.value ? ' selected' : '';
|
|
67
|
+
optionsHtml += '<option value="' + SearchRenderer._escape(option.value) + '"' + selected + '>' + SearchRenderer._escape(option.label) + '</option>';
|
|
68
|
+
});
|
|
69
|
+
return '<select class="form-input" id="' + this._escape(field.id) + '">' + optionsHtml + '</select>';
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
renderCheckbox: function(field) {
|
|
73
|
+
var checked = field.checked ? ' checked' : '';
|
|
74
|
+
return '<label class="search-checkbox"><input type="checkbox" id="' + this._escape(field.id) + '"' + checked + '> ' + this._escape(field.text || field.label || '') + '</label>';
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
renderActions: function(actions) {
|
|
78
|
+
var html = '<div class="search-actions">';
|
|
79
|
+
actions.forEach(function(action) {
|
|
80
|
+
if (action.type === 'divider') {
|
|
81
|
+
html += '<span class="search-actions-divider"></span>';
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (action.html) {
|
|
85
|
+
html += action.html;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
var cls = action.className || 'btn';
|
|
89
|
+
var iconHtml = action.icon ? icon(action.icon) + ' ' : '';
|
|
90
|
+
html += '<button type="button" class="' + SearchRenderer._escape(cls) + '" id="' + SearchRenderer._escape(action.id) + '">' + iconHtml + SearchRenderer._escape(action.label) + '</button>';
|
|
91
|
+
});
|
|
92
|
+
html += '</div>';
|
|
93
|
+
return html;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TableRenderer 模块
|
|
3
|
+
* 表格渲染器,统一处理表格渲染和分页逻辑
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
var TableRenderer = {
|
|
7
|
+
config: null,
|
|
8
|
+
currentPage: 1,
|
|
9
|
+
pageSize: 10,
|
|
10
|
+
filteredData: [],
|
|
11
|
+
|
|
12
|
+
init: function(config) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.currentPage = 1;
|
|
15
|
+
this.pageSize = config.pageSize || 10;
|
|
16
|
+
this.filteredData = [];
|
|
17
|
+
|
|
18
|
+
this._bindPaginationEvents();
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
render: function(data) {
|
|
22
|
+
this.filteredData = data || [];
|
|
23
|
+
this._renderPage();
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
getCurrentPage: function() {
|
|
27
|
+
return this.currentPage;
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
getPageSize: function() {
|
|
31
|
+
return this.pageSize;
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
setPage: function(page) {
|
|
35
|
+
var totalPages = Math.ceil(this.filteredData.length / this.pageSize);
|
|
36
|
+
if (page < 1 || page > totalPages) return;
|
|
37
|
+
this.currentPage = page;
|
|
38
|
+
this._renderPage();
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
setPageSize: function(size) {
|
|
42
|
+
this.pageSize = parseInt(size);
|
|
43
|
+
this.currentPage = 1;
|
|
44
|
+
this._renderPage();
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
_bindPaginationEvents: function() {
|
|
48
|
+
var self = this;
|
|
49
|
+
|
|
50
|
+
if (this._paginationEventsBound) return;
|
|
51
|
+
this._paginationEventsBound = true;
|
|
52
|
+
|
|
53
|
+
document.addEventListener('click', function(event) {
|
|
54
|
+
var btn = event.target.closest('[data-action="table-renderer-page"]');
|
|
55
|
+
if (!btn) return;
|
|
56
|
+
self.setPage(parseInt(btn.getAttribute('data-page'), 10));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
document.addEventListener('change', function(event) {
|
|
60
|
+
var select = event.target.closest('[data-action="table-renderer-page-size"]');
|
|
61
|
+
if (!select) return;
|
|
62
|
+
self.setPageSize(select.value);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
document.addEventListener('keydown', function(event) {
|
|
66
|
+
var input = event.target.closest('[data-action="table-renderer-jump"]');
|
|
67
|
+
if (!input || event.key !== 'Enter') return;
|
|
68
|
+
self.setPage(parseInt(input.value, 10));
|
|
69
|
+
input.value = '';
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
_renderPage: function() {
|
|
74
|
+
var tableBody = document.getElementById(this.config.tableBodyId);
|
|
75
|
+
var paginationEl = document.getElementById(this.config.paginationId);
|
|
76
|
+
|
|
77
|
+
if (!tableBody) return;
|
|
78
|
+
|
|
79
|
+
var startIndex = (this.currentPage - 1) * this.pageSize;
|
|
80
|
+
var endIndex = startIndex + this.pageSize;
|
|
81
|
+
var pageData = this.filteredData.slice(startIndex, endIndex);
|
|
82
|
+
|
|
83
|
+
tableBody.innerHTML = '';
|
|
84
|
+
|
|
85
|
+
var colCount = this.config.columns ? this.config.columns.length : 1;
|
|
86
|
+
|
|
87
|
+
if (pageData.length === 0) {
|
|
88
|
+
tableBody.innerHTML = '<tr><td colspan="' + colCount + '" style="text-align: center; padding: 40px; color: var(--text-disabled);">暂无数据</td></tr>';
|
|
89
|
+
if (paginationEl) paginationEl.innerHTML = '<span class="pagination-info">共 0 条</span>';
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
var self = this;
|
|
94
|
+
pageData.forEach(function(item) {
|
|
95
|
+
if (self.config.onRenderRows) {
|
|
96
|
+
self.config.onRenderRows(item).forEach(function(rowHtml) {
|
|
97
|
+
var tr = document.createElement('tr');
|
|
98
|
+
tr.innerHTML = rowHtml;
|
|
99
|
+
tableBody.appendChild(tr);
|
|
100
|
+
});
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
var row = document.createElement('tr');
|
|
105
|
+
|
|
106
|
+
if (self.config.onRenderRow) {
|
|
107
|
+
row.innerHTML = self.config.onRenderRow(item);
|
|
108
|
+
} else if (self.config.columns) {
|
|
109
|
+
row.innerHTML = self._renderRowByColumns(item);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
tableBody.appendChild(row);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
var totalPages = Math.ceil(this.filteredData.length / this.pageSize);
|
|
116
|
+
if (paginationEl) {
|
|
117
|
+
paginationEl.innerHTML = this._renderPagination(this.filteredData.length, totalPages);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
_renderRowByColumns: function(item) {
|
|
122
|
+
var html = '';
|
|
123
|
+
var self = this;
|
|
124
|
+
|
|
125
|
+
this.config.columns.forEach(function(column) {
|
|
126
|
+
var value = item[column.field];
|
|
127
|
+
|
|
128
|
+
if (column.render) {
|
|
129
|
+
html += '<td>' + column.render(value, item) + '</td>';
|
|
130
|
+
} else {
|
|
131
|
+
html += '<td>' + (value !== undefined && value !== null && value !== '' ? value : '-') + '</td>';
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return html;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
_renderPagination: function(totalCount, totalPages) {
|
|
139
|
+
var html = '<span class="pagination-info">共 ' + totalCount + ' 条</span>';
|
|
140
|
+
html += '<select class="pagination-select" data-action="table-renderer-page-size">' +
|
|
141
|
+
'<option value="10"' + (this.pageSize === 10 ? ' selected' : '') + '>10条/页</option>' +
|
|
142
|
+
'<option value="20"' + (this.pageSize === 20 ? ' selected' : '') + '>20条/页</option>' +
|
|
143
|
+
'<option value="50"' + (this.pageSize === 50 ? ' selected' : '') + '>50条/页</option></select>';
|
|
144
|
+
html += '<div class="pagination-btns">';
|
|
145
|
+
html += '<button class="pagination-btn" data-action="table-renderer-page" data-page="' + (this.currentPage - 1) + '"' + (this.currentPage === 1 ? ' disabled' : '') + '><</button>';
|
|
146
|
+
|
|
147
|
+
if (totalPages > 0) {
|
|
148
|
+
if (totalPages <= 7) {
|
|
149
|
+
for (var i = 1; i <= totalPages; i++) {
|
|
150
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="table-renderer-page" data-page="' + i + '">' + i + '</button>';
|
|
151
|
+
}
|
|
152
|
+
} else if (this.currentPage <= 4) {
|
|
153
|
+
for (var i = 1; i <= 5; i++) {
|
|
154
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="table-renderer-page" data-page="' + i + '">' + i + '</button>';
|
|
155
|
+
}
|
|
156
|
+
html += '<span class="pagination-ellipsis">...</span>';
|
|
157
|
+
html += '<button class="pagination-btn" data-action="table-renderer-page" data-page="' + totalPages + '">' + totalPages + '</button>';
|
|
158
|
+
} else if (this.currentPage >= totalPages - 3) {
|
|
159
|
+
html += '<button class="pagination-btn" data-action="table-renderer-page" data-page="1">1</button><span class="pagination-ellipsis">...</span>';
|
|
160
|
+
for (var i = totalPages - 4; i <= totalPages; i++) {
|
|
161
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="table-renderer-page" data-page="' + i + '">' + i + '</button>';
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
html += '<button class="pagination-btn" data-action="table-renderer-page" data-page="1">1</button><span class="pagination-ellipsis">...</span>';
|
|
165
|
+
for (var i = this.currentPage - 1; i <= this.currentPage + 1; i++) {
|
|
166
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="table-renderer-page" data-page="' + i + '">' + i + '</button>';
|
|
167
|
+
}
|
|
168
|
+
html += '<span class="pagination-ellipsis">...</span><button class="pagination-btn" data-action="table-renderer-page" data-page="' + totalPages + '">' + totalPages + '</button>';
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
html += '<button class="pagination-btn" data-action="table-renderer-page" data-page="' + (this.currentPage + 1) + '"' + (this.currentPage === totalPages || totalPages === 0 ? ' disabled' : '') + '>></button></div>';
|
|
173
|
+
html += '<div class="pagination-jump"><span>跳至</span><input type="number" class="form-input" min="1" data-action="table-renderer-jump"><span>页</span></div>';
|
|
174
|
+
|
|
175
|
+
return html;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommonTable 模块
|
|
3
|
+
* 通用表格管理器,处理表格渲染和分页
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
var CommonTable = {
|
|
7
|
+
config: null,
|
|
8
|
+
currentPage: 1,
|
|
9
|
+
pageSize: 10,
|
|
10
|
+
filteredData: [],
|
|
11
|
+
|
|
12
|
+
init: function(config) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.currentPage = 1;
|
|
15
|
+
this.pageSize = config.pageSize || 10;
|
|
16
|
+
this.filteredData = [];
|
|
17
|
+
this._bindPaginationEvents();
|
|
18
|
+
this.renderList();
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
renderList: function() {
|
|
22
|
+
var data = this.config.getData ? this.config.getData() : [];
|
|
23
|
+
this.filteredData = data;
|
|
24
|
+
this.currentPage = 1;
|
|
25
|
+
this._renderPage();
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
_renderPage: function() {
|
|
29
|
+
var tableBody = document.getElementById(this.config.tbodyId || 'tableBody');
|
|
30
|
+
var paginationEl = document.getElementById(this.config.paginationId || 'pagination');
|
|
31
|
+
|
|
32
|
+
if (!tableBody) return;
|
|
33
|
+
|
|
34
|
+
var startIndex = (this.currentPage - 1) * this.pageSize;
|
|
35
|
+
var endIndex = startIndex + this.pageSize;
|
|
36
|
+
var pageData = this.filteredData.slice(startIndex, endIndex);
|
|
37
|
+
|
|
38
|
+
tableBody.innerHTML = '';
|
|
39
|
+
|
|
40
|
+
var colCount = this.config.columns ? this.config.columns.length : 1;
|
|
41
|
+
|
|
42
|
+
if (pageData.length === 0) {
|
|
43
|
+
tableBody.innerHTML = '<tr><td colspan="' + colCount + '" style="text-align: center; padding: 40px; color: var(--text-disabled);">暂无数据</td></tr>';
|
|
44
|
+
if (paginationEl) paginationEl.innerHTML = '<span class="pagination-info">共 0 条</span>';
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
var self = this;
|
|
49
|
+
pageData.forEach(function(item) {
|
|
50
|
+
var row = document.createElement('tr');
|
|
51
|
+
row.innerHTML = self._renderRowByColumns(item);
|
|
52
|
+
tableBody.appendChild(row);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
var totalPages = Math.ceil(this.filteredData.length / this.pageSize);
|
|
56
|
+
if (paginationEl) {
|
|
57
|
+
paginationEl.innerHTML = this._renderPagination(this.filteredData.length, totalPages);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
_renderRowByColumns: function(item) {
|
|
62
|
+
var html = '';
|
|
63
|
+
var self = this;
|
|
64
|
+
|
|
65
|
+
if (this.config.columns) {
|
|
66
|
+
this.config.columns.forEach(function(column) {
|
|
67
|
+
var value = item[column.field];
|
|
68
|
+
|
|
69
|
+
if (column.render) {
|
|
70
|
+
html += '<td>' + column.render(value, item) + '</td>';
|
|
71
|
+
} else {
|
|
72
|
+
html += '<td>' + (value !== undefined && value !== null && value !== '' ? value : '-') + '</td>';
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return html;
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
_renderPagination: function(totalCount, totalPages) {
|
|
81
|
+
var html = '<span class="pagination-info">共 ' + totalCount + ' 条</span>';
|
|
82
|
+
html += '<select class="pagination-select" data-action="common-table-page-size">' +
|
|
83
|
+
'<option value="10"' + (this.pageSize === 10 ? ' selected' : '') + '>10条/页</option>' +
|
|
84
|
+
'<option value="20"' + (this.pageSize === 20 ? ' selected' : '') + '>20条/页</option>' +
|
|
85
|
+
'<option value="50"' + (this.pageSize === 50 ? ' selected' : '') + '>50条/页</option></select>';
|
|
86
|
+
html += '<div class="pagination-btns">';
|
|
87
|
+
html += '<button class="pagination-btn" data-action="common-table-page" data-page="' + (this.currentPage - 1) + '"' + (this.currentPage === 1 ? ' disabled' : '') + '><</button>';
|
|
88
|
+
|
|
89
|
+
if (totalPages > 0) {
|
|
90
|
+
if (totalPages <= 7) {
|
|
91
|
+
for (var i = 1; i <= totalPages; i++) {
|
|
92
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="common-table-page" data-page="' + i + '">' + i + '</button>';
|
|
93
|
+
}
|
|
94
|
+
} else if (this.currentPage <= 4) {
|
|
95
|
+
for (var i = 1; i <= 5; i++) {
|
|
96
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="common-table-page" data-page="' + i + '">' + i + '</button>';
|
|
97
|
+
}
|
|
98
|
+
html += '<span class="pagination-ellipsis">...</span>';
|
|
99
|
+
html += '<button class="pagination-btn" data-action="common-table-page" data-page="' + totalPages + '">' + totalPages + '</button>';
|
|
100
|
+
} else if (this.currentPage >= totalPages - 3) {
|
|
101
|
+
html += '<button class="pagination-btn" data-action="common-table-page" data-page="1">1</button><span class="pagination-ellipsis">...</span>';
|
|
102
|
+
for (var i = totalPages - 4; i <= totalPages; i++) {
|
|
103
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="common-table-page" data-page="' + i + '">' + i + '</button>';
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
html += '<button class="pagination-btn" data-action="common-table-page" data-page="1">1</button><span class="pagination-ellipsis">...</span>';
|
|
107
|
+
for (var i = this.currentPage - 1; i <= this.currentPage + 1; i++) {
|
|
108
|
+
html += '<button class="pagination-btn' + (i === this.currentPage ? ' active' : '') + '" data-action="common-table-page" data-page="' + i + '">' + i + '</button>';
|
|
109
|
+
}
|
|
110
|
+
html += '<span class="pagination-ellipsis">...</span><button class="pagination-btn" data-action="common-table-page" data-page="' + totalPages + '">' + totalPages + '</button>';
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
html += '<button class="pagination-btn" data-action="common-table-page" data-page="' + (this.currentPage + 1) + '"' + (this.currentPage === totalPages || totalPages === 0 ? ' disabled' : '') + '>></button></div>';
|
|
115
|
+
html += '<div class="pagination-jump"><span>跳至</span><input type="number" class="form-input" min="1" data-action="common-table-jump"><span>页</span></div>';
|
|
116
|
+
|
|
117
|
+
return html;
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
setPage: function(page) {
|
|
121
|
+
var totalPages = Math.ceil(this.filteredData.length / this.pageSize);
|
|
122
|
+
if (page < 1 || page > totalPages) return;
|
|
123
|
+
this.currentPage = page;
|
|
124
|
+
this._renderPage();
|
|
125
|
+
},
|
|
126
|
+
|
|
127
|
+
setPageSize: function(size) {
|
|
128
|
+
this.pageSize = parseInt(size);
|
|
129
|
+
this.currentPage = 1;
|
|
130
|
+
this._renderPage();
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
renderTable: function() {
|
|
134
|
+
this._renderPage();
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
_bindPaginationEvents: function() {
|
|
138
|
+
if (this._paginationEventsBound) return;
|
|
139
|
+
this._paginationEventsBound = true;
|
|
140
|
+
|
|
141
|
+
document.addEventListener('click', function(event) {
|
|
142
|
+
var btn = event.target.closest('[data-action="common-table-page"]');
|
|
143
|
+
if (!btn) return;
|
|
144
|
+
CommonTable.setPage(parseInt(btn.getAttribute('data-page'), 10));
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
document.addEventListener('change', function(event) {
|
|
148
|
+
var select = event.target.closest('[data-action="common-table-page-size"]');
|
|
149
|
+
if (!select) return;
|
|
150
|
+
CommonTable.setPageSize(select.value);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
document.addEventListener('keydown', function(event) {
|
|
154
|
+
var input = event.target.closest('[data-action="common-table-jump"]');
|
|
155
|
+
if (!input || event.key !== 'Enter') return;
|
|
156
|
+
CommonTable.setPage(parseInt(input.value, 10));
|
|
157
|
+
input.value = '';
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
};
|