51 lines
2.6 KiB
JavaScript
51 lines
2.6 KiB
JavaScript
/* ui-dock-sanitize.js —— 停靠栏状态的纯函数整理(v3.26.21)
|
|
*
|
|
* 从 ui-dock.js 抽出来,便于单测(jsdom 里跑 React + 拖拽太重)。规则:
|
|
* 1. 只保留当前存在(plugins 有注册)的面板;
|
|
* 2. 同侧去重(历史重复注册残留);
|
|
* 3. 默认面板回填:removed 里的不补、另一侧已有的不补;
|
|
* 4. 同一面板同时出现在左右两侧 → 只保留左侧。
|
|
*/
|
|
;(function (global) {
|
|
'use strict'
|
|
function sanitize (dock, panels, defaults) {
|
|
if (!dock || !panels) return dock
|
|
var changed = false
|
|
var next = { left: Object.assign({}, dock.left), right: Object.assign({}, dock.right) }
|
|
;['left', 'right'].forEach(function (side) {
|
|
var s0 = dock[side] || {}
|
|
var removed = Array.isArray(s0.removed) ? s0.removed : []
|
|
var defList = (defaults && defaults[side] && defaults[side].list) || []
|
|
// v3.26.22:不再丢弃“当前没注册”的面板 id(启动瞬间插件未加载完,清掉会覆盖用户摆放)
|
|
var list = (s0.list || []).slice()
|
|
list = list.filter(function (id, i) { return list.indexOf(id) === i })
|
|
defList.forEach(function (id) {
|
|
if (!panels[id]) return
|
|
if (removed.indexOf(id) >= 0) return
|
|
var other = side === 'left' ? 'right' : 'left'
|
|
var otherList = (dock[other] && dock[other].list) || []
|
|
if (otherList.indexOf(id) >= 0) return
|
|
if (list.indexOf(id) >= 0) return
|
|
list.push(id)
|
|
})
|
|
var usable = list.filter(function (id) { return !!panels[id] })
|
|
// v3.26.23:保存的 active 只要还在 list 里就保持 —— 插件还没加载完时不要切走(否则重启后当前面板会变)
|
|
var keepActive = !!s0.active && list.indexOf(s0.active) >= 0
|
|
var active = keepActive ? s0.active : (usable[0] || list[0] || '')
|
|
// 用内容比较而不是长度:去重(减 1) + 默认回填(加 1) 时长度可能不变,但内容已不同
|
|
if (list.join('|') !== (s0.list || []).join('|') || active !== s0.active) {
|
|
next[side] = Object.assign({}, s0, { list: list, active: active })
|
|
changed = true
|
|
}
|
|
})
|
|
var dup = (next.left.list || []).filter(function (id) { return (next.right.list || []).indexOf(id) >= 0 })
|
|
if (dup.length) {
|
|
next.right = Object.assign({}, next.right, { list: next.right.list.filter(function (id) { return dup.indexOf(id) < 0 }) })
|
|
if (dup.indexOf(next.right.active) >= 0) next.right.active = next.right.list[0] || ''
|
|
changed = true
|
|
}
|
|
return changed ? next : dock
|
|
}
|
|
global.IWBUiDockSanitize = { sanitize: sanitize }
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|