|
|
@@ -1,6 +1,8 @@
|
|
|
(function(){
|
|
|
var OFFICE_EXT = ["docx","pptx","ppt","xlsx","xls"];
|
|
|
var PREVIEW_BASE = "http://" + location.hostname + ":8080";
|
|
|
+ var LAST_OPEN = { path: "", ts: 0 };
|
|
|
+ var OPEN_GUARD_MS = 1200;
|
|
|
|
|
|
function startsWithFiles(p){ return (p || "").toLowerCase().indexOf("/files/") === 0; }
|
|
|
|
|
|
@@ -42,15 +44,63 @@
|
|
|
var ext = (rel.split(".").pop() || "").toLowerCase();
|
|
|
var rawUrl = PREVIEW_BASE + buildRawPath(rel);
|
|
|
var nonce = Date.now().toString() + "-" + Math.random().toString(16).slice(2, 8);
|
|
|
- return PREVIEW_BASE + "/oo-preview.html?url=" + encodeURIComponent(rawUrl) + "&type=" + encodeURIComponent(ext) + "&title=" + encodeURIComponent(rel) + "&_fbv=" + encodeURIComponent(nonce);
|
|
|
+ var mode = (ext === "xlsx" || ext === "xls") ? "edit" : "view";
|
|
|
+ return PREVIEW_BASE + "/oo-preview.html?url=" + encodeURIComponent(rawUrl) + "&type=" + encodeURIComponent(ext) + "&mode=" + encodeURIComponent(mode) + "&title=" + encodeURIComponent(rel) + "&_fbv=" + encodeURIComponent(nonce);
|
|
|
+ }
|
|
|
+
|
|
|
+ function parentDirPath(pathname){
|
|
|
+ var p = pathname || "";
|
|
|
+ if (!startsWithFiles(p)) return "/files/";
|
|
|
+ var idx = p.lastIndexOf("/");
|
|
|
+ if (idx <= 6) return "/files/";
|
|
|
+ return p.slice(0, idx + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ function openPreviewInNewTab(pathname){
|
|
|
+ var p = pathname || "";
|
|
|
+ var u = buildPreviewUrlFromPath(p);
|
|
|
+ if (!u) return false;
|
|
|
+ var now = Date.now();
|
|
|
+ if (LAST_OPEN.path === p && (now - LAST_OPEN.ts) < OPEN_GUARD_MS) return true;
|
|
|
+ LAST_OPEN.path = p;
|
|
|
+ LAST_OPEN.ts = now;
|
|
|
+
|
|
|
+ var w = window.open(u, "_blank", "noopener");
|
|
|
+ if (!w) {
|
|
|
+ // Popup blocked fallback
|
|
|
+ location.assign(u);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ function interceptFileClick(){
|
|
|
+ document.addEventListener("click", function(e){
|
|
|
+ var a = e.target && e.target.closest && e.target.closest("a[href]");
|
|
|
+ if (!a) return;
|
|
|
+ var href = a.getAttribute("href") || "";
|
|
|
+ if (!href) return;
|
|
|
+ var pathname = "";
|
|
|
+ try {
|
|
|
+ pathname = new URL(href, location.origin).pathname || "";
|
|
|
+ } catch (err) {
|
|
|
+ pathname = href;
|
|
|
+ }
|
|
|
+ if (!startsWithFiles(pathname)) return;
|
|
|
+ if (!buildPreviewUrlFromPath(pathname)) return;
|
|
|
+ e.preventDefault();
|
|
|
+ e.stopPropagation();
|
|
|
+ if (e.stopImmediatePropagation) e.stopImmediatePropagation();
|
|
|
+ openPreviewInNewTab(pathname);
|
|
|
+ }, true);
|
|
|
}
|
|
|
|
|
|
function maybeJump(){
|
|
|
var p = location.pathname || "";
|
|
|
- var u = buildPreviewUrlFromPath(p);
|
|
|
- if (!u) return;
|
|
|
+ if (!buildPreviewUrlFromPath(p)) return;
|
|
|
if (location.pathname === "/oo-preview.html") return;
|
|
|
- location.replace(u);
|
|
|
+ if (openPreviewInNewTab(p)) {
|
|
|
+ location.replace(parentDirPath(p));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
var pushed = history.pushState;
|
|
|
@@ -68,5 +118,6 @@
|
|
|
window.addEventListener("popstate", function(){ setTimeout(maybeJump, 0); });
|
|
|
window.addEventListener("hashchange", function(){ setTimeout(maybeJump, 0); });
|
|
|
|
|
|
+ interceptFileClick();
|
|
|
setTimeout(maybeJump, 0);
|
|
|
})();
|