Merge pull request #27102 from nextcloud/feature/auto_trigger_click_on_load

Trigger the default action when openfile URL param is set
This commit is contained in:
Julius Härtl 2021-06-10 09:07:21 +02:00 committed by GitHub
commit eb92c57db2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 116 additions and 34 deletions

View file

@ -91,6 +91,7 @@
fileActions: fileActions,
allowLegacyActions: true,
scrollTo: urlParams.scrollto,
openFile: urlParams.openfile,
filesClient: OC.Files.getClient(),
multiSelectMenu: [
{

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -679,12 +679,19 @@
permissions: OC.PERMISSION_READ,
icon: '',
actionHandler: function (filename, context) {
var dir = context.$file.attr('data-path') || context.fileList.getCurrentDirectory();
let dir, id
if (context.$file) {
dir = context.$file.attr('data-path')
id = context.$file.attr('data-id')
} else {
dir = context.fileList.getCurrentDirectory()
id = context.fileId
}
if (OCA.Files.App && OCA.Files.App.getActiveView() !== 'files') {
OCA.Files.App.setActiveView('files', {silent: true});
OCA.Files.App.fileList.changeDirectory(OC.joinPaths(dir, filename), true, true);
} else {
context.fileList.changeDirectory(OC.joinPaths(dir, filename), true, false, parseInt(context.$file.attr('data-id'), 10));
context.fileList.changeDirectory(OC.joinPaths(dir, filename), true, false, parseInt(id, 10));
}
},
displayName: t('files', 'Open')

View file

@ -414,6 +414,31 @@
});
}
if(options.openFile) {
// Wait for some initialisation process to be over before triggering the default action.
_.defer(() => {
try {
var fileInfo = JSON.parse(atob($('#initial-state-files-openFileInfo').val()))
var spec = this.fileActions.getDefaultFileAction(fileInfo.mime, fileInfo.type, fileInfo.permissions)
if (spec && spec.action) {
spec.action(fileInfo.name, {
fileId: fileInfo.id,
fileList: this,
fileActions: this.fileActions,
dir: fileInfo.directory
});
} else {
var url = this.getDownloadUrl(fileInfo.name, fileInfo.dir, true);
OCA.Files.Files.handleDownload(url);
}
OCA.Files.Sidebar.open(fileInfo.path);
} catch (error) {
console.error(`Failed to trigger default action on the file for URL: ${location.href}`, error)
}
})
}
this._operationProgressBar = new OCA.Files.OperationProgressBar();
this._operationProgressBar.render();
this.$el.find('#uploadprogresswrapper').replaceWith(this._operationProgressBar.$el);
@ -1320,31 +1345,6 @@
}, 0);
}
if(!this.triedActionOnce) {
var id = OC.Util.History.parseUrlQuery().openfile;
if (id) {
var $tr = this.$fileList.children().filterAttr('data-id', '' + id);
var filename = $tr.attr('data-file');
this.fileActions.currentFile = $tr.find('td');
var dir = $tr.attr('data-path') || this.getCurrentDirectory();
var spec = this.fileActions.getCurrentDefaultFileAction();
if (spec && spec.action) {
spec.action(filename, {
$file: $tr,
fileList: this,
fileActions: this.fileActions,
dir: dir
});
}
else {
var url = this.getDownloadUrl(filename, dir, true);
OCA.Files.Files.handleDownload(url);
}
}
this.triedActionOnce = true;
}
return newTrs;
},

View file

@ -179,10 +179,11 @@ class ViewController extends Controller {
* @param string $view
* @param string $fileid
* @param bool $fileNotFound
* @param string $openfile
* @return TemplateResponse|RedirectResponse
* @throws NotFoundException
*/
public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) {
public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) {
if ($fileid !== null) {
try {
return $this->redirectToFile($fileid);
@ -331,9 +332,61 @@ class ViewController extends Controller {
$policy->addAllowedFrameDomain('\'self\'');
$response->setContentSecurityPolicy($policy);
$this->provideInitialState($dir, $openfile);
return $response;
}
/**
* Add openFileInfo in initialState if $openfile is set.
* @param string $dir - the ?dir= URL param
* @param string $openfile - the ?openfile= URL param
* @return void
*/
private function provideInitialState(string $dir, ?string $openfile): void {
if ($openfile === null) {
return;
}
$user = $this->userSession->getUser();
if ($user === null) {
return;
}
$uid = $user->getUID();
$userFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $userFolder->getById((int) $openfile);
$node = array_shift($nodes);
if ($node === null) {
return;
}
// properly format full path and make sure
// we're relative to the user home folder
$isRoot = $node === $userFolder;
$path = $userFolder->getRelativePath($node->getPath());
$directory = $userFolder->getRelativePath($node->getParent()->getPath());
// Prevent opening a file from another folder.
if ($dir !== $directory) {
return;
}
$this->initialState->provideInitialState(
'openFileInfo', [
'id' => $node->getId(),
'name' => $isRoot ? '' : $node->getName(),
'path' => $path,
'directory' => $directory,
'mime' => $node->getMimetype(),
'type' => $node->getType(),
'permissions' => $node->getPermissions(),
]
);
}
/**
* Redirects to the file list and highlight the given file id
*

View file

@ -123,6 +123,7 @@ class FilesSearchProvider implements IProvider {
[
'dir' => dirname($path),
'scrollto' => $result->getName(),
'openfile' => $result->getId(),
]
);

View file

@ -58,4 +58,5 @@ window.addEventListener('DOMContentLoaded', function() {
AppSidebar.$mount('#app-sidebar')
window.OCA.Files.Sidebar.open = AppSidebar.open
window.OCA.Files.Sidebar.close = AppSidebar.close
window.OCA.Files.Sidebar.setFullScreenMode = AppSidebar.setFullScreenMode
})

View file

@ -110,6 +110,7 @@ export default {
loading: true,
fileInfo: null,
starLoading: false,
isFullScreen: false,
}
},
@ -202,7 +203,10 @@ export default {
'star-loading': this.starLoading,
active: this.activeTab,
background: this.background,
class: { 'has-preview': this.fileInfo.hasPreview },
class: {
'app-sidebar--has-preview': this.fileInfo.hasPreview,
'app-sidebar--full': this.isFullScreen,
},
compact: !this.fileInfo.hasPreview,
loading: this.loading,
starred: this.fileInfo.isFavourited,
@ -428,6 +432,14 @@ export default {
this.resetData()
},
/**
* Allow to set the Sidebar as fullscreen from OCA.Files.Sidebar
* @param {boolean} isFullScreen - Wether or not to render the Sidebar in fullscreen.
*/
setFullScreenMode(isFullScreen) {
this.isFullScreen = isFullScreen
},
/**
* Emit SideBar events.
*/
@ -448,7 +460,7 @@ export default {
</script>
<style lang="scss" scoped>
.app-sidebar {
&.has-preview::v-deep {
&--has-preview::v-deep {
.app-sidebar-header__figure {
background-size: cover;
}
@ -460,5 +472,12 @@ export default {
}
}
}
&--full {
position: fixed !important;
z-index: 2025 !important;
top: 0 !important;
height: 100% !important;
}
}
</style>