/**
* Implementation of local command <code>openDocument</code>.
*/
export class OpenDocumentCmd extends Command {
constructor() {
super();
}
execute(mode, docView, params, event=null) {
let readOnly = false;
let remoteURI = null;
let localURI = null;
if (params !== null) {
params = params.trim();
if (params.startsWith("[readWrite]")) {
readOnly = false;
params = params.substring(11);
} else if (params.startsWith("[readOnly]")) {
readOnly = true;
params = params.substring(10);
}
let docURIs = splitTrimString(params, /\s+/);
if (docURIs.length > 0) {
remoteURI = docURIs[0];
}
if (remoteURI !== null) {
if (remoteURI.startsWith("csri:")) {
localURI = URIUtil.csriURLToURI(remoteURI);
remoteURI = null;
}
}
}
if (mode === EXECUTE_TEST) {
return Promise.resolve(remoteURI !== null);
}
// ---
Command.consumeEvent(event);
if (remoteURI === null) {
if (localURI !== null) {
XUI.Alert.showError(`Don't know how open local files such as
"${localURI}"`,
/*reference*/ docView);
}
return Promise.resolve(CommandResult.FAILED);
}
let readOnlyMode = readOnly? " read-only" : "";
docView.showStatus(
`Opening remote file "${remoteURI}"${readOnlyMode}...`,
/*autoErase*/ true);
return this.openRemoteFile(docView, remoteURI, readOnly);
}
/**
* Open specified remote document in a new <em>browser</em> window/tab.
* <p>Redefine this method if for example, the document is to be opened
* in a new <em>application</em> tab.
*
* @param {DocumentView} docView - the <code>DocumentView</code>
* from which this command in invoked.
* @param {string} remoteURI - the URI of the document to be opened.
* @param {boolean} readOnly - specifies whether it must be opened
* in read-only mode.
* @returns {Promise} A Promise containing a {@link CommandResult}.
*/
openRemoteFile(docView, remoteURI, readOnly) {
const newWin = window.open(window.location.href, "_blank");
if (newWin === null) {
XUI.Alert.showError(`Blocked by the browser pop-up blocker?
If this is the case, please allow pop-ups for ${window.location.host}`,
/*reference*/ docView);
return Promise.resolve(CommandResult.FAILED);
}
// Hack but in this very specific case simpler than
// newWin.postMessage().
newWin.name = JSON.stringify([ "openRemoteFile", remoteURI, readOnly ]);
return Promise.resolve(CommandResult.DONE);
}
}
ALL_LOCAL_COMMANDS.openDocument = new OpenDocumentCmd();