Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements #8263, Adds a re-usable Copy to Clipboard service. #13976

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/ui/public/doc_table/components/table_row.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const MIN_LINE_LENGTH = 20;
* <tr ng-repeat="row in rows" kbn-table-row="row"></tr>
* ```
*/
module.directive('kbnTableRow', function ($compile, $httpParamSerializer, kbnUrl) {
module.directive('kbnTableRow', function ($compile, $httpParamSerializer, kbnUrl, copyToClipboardService) {
const cellTemplate = _.template(noWhiteSpace(require('ui/doc_table/components/table_row/cell.html')));
const truncateByHeightTemplate = _.template(noWhiteSpace(require('ui/partials/truncate_by_height.html')));

Expand All @@ -51,6 +51,13 @@ module.directive('kbnTableRow', function ($compile, $httpParamSerializer, kbnUrl
// when we compile the toggle button in the summary, we use this $scope
let $toggleScope;

$scope.copyToClipboard = (uriescape) => {
let text = window.location.origin + window.location.pathname;
text += '#/doc/' + $scope.indexPattern.id + '/' + $scope.row._index + '/' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use template strings instead of concatenation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

$scope.row._type + '/?id=' + ($scope.row._id || uriescape);
copyToClipboardService.copyText(text);
};

// toggle display of the rows details, a full list of the fields from each row
$scope.toggleRow = function () {
const $detailsTr = $el.next();
Expand Down
7 changes: 7 additions & 0 deletions src/ui/public/doc_table/components/table_row/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
>
View single document
</a>
<a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link isn't keyboard accessible, since it doesn't have an href element. Since this is an action rather than a navigation to a different page, it should be a button (and just styled whatever way you wish): https:/elastic/kibana/blob/master/style_guides/accessibility_guide.md#use-button-and-a-href

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

class="kuiButton kuiButton--small kuiButton--secondary"
data-test-subj="docTableRowAction"
ng-click="copyToClipboard(uriescape)"
>
Copy Link
</a>
</div>
<doc-viewer
columns="columns"
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/share/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './directives/share';
import './services/copyToClipboard';
34 changes: 34 additions & 0 deletions src/ui/public/share/services/copyToClipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Notifier } from 'ui/notify/notifier';
import { uiModules } from 'ui/modules';

const app = uiModules.get('kibana');

app.factory('copyToClipboardService', () => {
const notify = new Notifier({
location: `Copy To Clipboard`,
});
return {
copyText: text => {
const temp = document.createElement('textarea');
temp.value = text;
temp.cols = 1;
temp.rows = 1;
temp.style.color = 'transparent';
temp.style.border = 'none';
document.body.appendChild(temp);
temp.select();
let isCopied = false;
try {
isCopied = document.execCommand('copy');
if (isCopied) {
notify.info('URL copied to clipboard.');
} else {
notify.error('Failed to Copy to Clipboard.');
}
} catch (err) {
notify.error('Failed to Copy to Clipboard.');
}
document.body.removeChild(temp);
}
};
});