/** * 目录操作树形列表多选:Ctrl/Cmd 切换、Shift 范围选、普通单击单选。 * 各页在 li 的 click 中调用 IARDirTreeMultiSelect.handleClick,用 getSelected* 取选中项。 */ (function () { if (typeof window === 'undefined') return; var _anchors = new WeakMap(); function isPathLi(li) { return li && li.tagName === 'LI' && li.hasAttribute('data-path'); } function flatPathLis(rootUl) { if (!rootUl) return []; return Array.prototype.filter.call( rootUl.querySelectorAll('li[data-path]'), function (li) { return isPathLi(li); } ); } function clearSelectedInRoot(rootUl) { if (!rootUl) return; rootUl.querySelectorAll('li.selected').forEach(function (x) { x.classList.remove('selected'); }); } function getSelectedInRoot(rootUl) { if (!rootUl) return []; return Array.prototype.filter.call( rootUl.querySelectorAll('li.selected'), function (li) { return isPathLi(li); } ); } function itemFromLi(li) { if (!li) return null; return { li: li, path: (li.getAttribute('data-path') || '').trim(), type: (li.getAttribute('data-type') || '').trim(), name: (li.getAttribute('data-name') || '').trim(), fullPath: (li.getAttribute('data-full-path') || '').trim(), subdir: (li.getAttribute('data-subdir') || '').trim() }; } function parentPath(relPath) { var p = String(relPath || '').replace(/\\/g, '/'); var idx = p.lastIndexOf('/'); return idx >= 0 ? p.substring(0, idx) : ''; } function isDirectoryType(type) { var t = String(type || '').trim().toLowerCase(); return t === 'directory' || t === 'dir'; } function isDirectoryItem(it) { if (!it) return false; if (isDirectoryType(it.type)) return true; if (isDirectoryType(it.itemType)) return true; var li = it.li; if (li && li.nodeType === 1) { if (isDirectoryType(li.getAttribute('data-type'))) return true; if (li.querySelector('.tree-folder')) return true; if (li.classList.contains('thesis-dir-item')) return true; } return false; } function pasteTargetParentFromItems(items) { if (!items || !items.length) return ''; var first = items[0]; if (!first || !first.path) return ''; if (items.length === 1) { if (isDirectoryItem(first)) return first.path; return parentPath(first.path); } var parents = items.map(function (it) { return isDirectoryItem(it) ? it.path : parentPath(it.path); }); var same = parents.every(function (p) { return p === parents[0]; }); if (same) return parents[0]; return parents[0]; } window.IARDirTreeMultiSelect = { flatPathLis: flatPathLis, clearSelectedInRoot: clearSelectedInRoot, getSelectedInRoot: getSelectedInRoot, itemFromLi: itemFromLi, parentPath: parentPath, isDirectoryType: isDirectoryType, isDirectoryItem: isDirectoryItem, pasteTargetParentFromItems: pasteTargetParentFromItems, getSelectedItems: function (rootUl) { return getSelectedInRoot(rootUl).map(itemFromLi).filter(Boolean); }, getPrimaryLi: function (rootUl) { var sel = getSelectedInRoot(rootUl); return sel.length ? sel[sel.length - 1] : null; }, /** * @param {MouseEvent} e * @param {HTMLElement} li * @param {HTMLElement} rootUl 列根 ul.file-tree * @param {{ onChange?: function(selectedLi[], primaryLi, rootUl), clearOtherRoots?: HTMLElement[] }} opts * @returns {{ selected: HTMLElement[], primary: HTMLElement|null }} */ handleClick: function (e, li, rootUl, opts) { opts = opts || {}; if (!isPathLi(li) || !rootUl) { return { selected: [], primary: null }; } if (!e.shiftKey && !e.ctrlKey && !e.metaKey && opts.clearOtherRoots) { opts.clearOtherRoots.forEach(clearSelectedInRoot); } var items = flatPathLis(rootUl); var idx = items.indexOf(li); if (idx < 0) { return { selected: getSelectedInRoot(rootUl), primary: null }; } var multi = e.ctrlKey || e.metaKey; var range = e.shiftKey; if (range) { var anchor = _anchors.get(rootUl); if (!anchor || items.indexOf(anchor) < 0) anchor = items[0]; var a = items.indexOf(anchor); var b = idx; var lo = Math.min(a, b); var hi = Math.max(a, b); clearSelectedInRoot(rootUl); for (var i = lo; i <= hi; i++) items[i].classList.add('selected'); } else if (multi) { if (li.classList.contains('selected')) li.classList.remove('selected'); else li.classList.add('selected'); _anchors.set(rootUl, li); } else { clearSelectedInRoot(rootUl); li.classList.add('selected'); _anchors.set(rootUl, li); } if (!range && !multi) _anchors.set(rootUl, li); var selected = getSelectedInRoot(rootUl); var primary = selected.length ? selected[selected.length - 1] : null; if (typeof opts.onChange === 'function') opts.onChange(selected, primary, rootUl); return { selected: selected, primary: primary }; }, clearInRoots: function (rootUls) { (rootUls || []).forEach(clearSelectedInRoot); }, restoreSelection: function (rootUl, paths) { if (!rootUl || !paths || !paths.length) return null; clearSelectedInRoot(rootUl); var found = []; paths.forEach(function (p) { var li = rootUl.querySelector('li[data-path="' + CSS.escape(String(p)) + '"]'); if (li) { li.classList.add('selected'); found.push(li); } }); if (found.length) _anchors.set(rootUl, found[found.length - 1]); return found; }, requireOne: function (items, message) { if (!items || items.length !== 1) { alert(message || '请只选择一项'); return null; } return items[0]; }, requireAny: function (items, message) { if (!items || !items.length) { alert(message || '请先选择文件或目录'); return null; } return items; }, requireOneFile: function (items, message) { var one = this.requireOne(items, message || '请只选择一个文件'); if (!one) return null; if (one.type !== 'file') { alert('请选择文件'); return null; } return one; } }; })();