From 9710ef4f9335a8c42409f5ec7feb35c664fd7438 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Tue, 25 Oct 2016 17:39:20 +0200 Subject: [PATCH] * Filemanager: Implement copy_link action for filemanager contextmenu in order to be able to copy webdav url of a file/folder into clipboard --- filemanager/inc/class.filemanager_ui.inc.php | 8 +++ filemanager/js/app.js | 63 +++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/filemanager/inc/class.filemanager_ui.inc.php b/filemanager/inc/class.filemanager_ui.inc.php index 968cc2aaba..3bbed76f70 100644 --- a/filemanager/inc/class.filemanager_ui.inc.php +++ b/filemanager/inc/class.filemanager_ui.inc.php @@ -179,6 +179,14 @@ class filemanager_ui 'enabled' => 'javaScript:app.filemanager.paste_enabled', 'children' => array() ), + 'copylink' => array( + 'caption' => lang('Copy link address'), + 'group' => $group + 0.5, + 'icon' => 'copy', + 'allowOnMultiple' => false, + 'order' => 10, + 'onExecute' => 'javaScript:app.filemanager.copy_link' + ), 'documents' => filemanager_merge::document_action( $GLOBALS['egw_info']['user']['preferences']['filemanager']['document_dir'], ++$group, 'Insert in document', 'document_', diff --git a/filemanager/js/app.js b/filemanager/js/app.js index 4bb4bf7283..600cf10c15 100644 --- a/filemanager/js/app.js +++ b/filemanager/js/app.js @@ -794,7 +794,7 @@ app.classes.filemanager = AppJS.extend( * @param {egwAction} _action drop action we're checking * @param {egwActionObject[]} _senders selected files * @param {egwActionObject} _target Drop or context menu activated on this one - * + * * @returns boolean true if enabled, false otherwise */ paste_enabled: function paste_enabled(_action, _senders, _target) @@ -827,7 +827,7 @@ app.classes.filemanager = AppJS.extend( enabled: dir && dir.data && dir.data.class && dir.data.class.indexOf('noEdit') === -1 || !dir && path_widget && !path_widget.options.readonly }); - + // Target, if directory target_dir = this.id2path(_target.id); dir = egw.dataGetUIDdata(_target.id); @@ -1124,5 +1124,64 @@ app.classes.filemanager = AppJS.extend( this.egw.lang('Do you want more information about EPL subscription?'), this.egw.lang('File a file'), undefined, et2_dialog.BUTTONS_YES_NO, et2_dialog.QUESTION_MESSAGE); } + }, + + /** + * This function copies the selected file/folder entry as webdav link into clipboard + * + * @param {object} _action egw actions + * @param {object} _senders selected nm row + * @returns {Boolean} returns false if not successful + */ + copy_link: function (_action, _senders) + { + var data = egw.dataGetUIDdata(_senders[0].id); + var url = data ? data.data.download_url : '/webdav.php'+this.id2path(_senders[0].id); + if (url[0] == '/') url = egw.link(url); + if (url.substr(0,4) == 'http' && url.indexOf('://') <= 5) { + // it's already a full url + } + else + { + var hostUrl = new URL(window.location.href); + url = hostUrl.origin + url; + } + + if (url) + { + var elem = jQuery(document.createElement('div')); + var range = []; + elem.text(url); + elem.appendTo('body'); + if (document.selection) + { + range = document.body.createTextRange(); + range.moveToElementText(elem); + range.select(); + } + else if (window.getSelection) + { + var range = document.createRange(); + range.selectNode(elem[0]); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } + + var successful = false; + try { + successful = document.execCommand('copy'); + if (successful) + { + egw.message('WebDav link copied into clipboard'); + window.getSelection().removeAllRanges(); + + return true; + } + } + catch (e) {} + egw.message('Failed to copy the link!'); + elem.remove(); + return false; + } } });