• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {Actions} from '../common/actions';
16import {QueryResponse} from '../common/queries';
17
18import {globals} from './globals';
19
20export function onClickCopy(url: string) {
21  return (e: Event) => {
22    e.preventDefault();
23    copyToClipboard(url);
24    globals.dispatch(Actions.updateStatus(
25        {msg: 'Link copied into the clipboard', timestamp: Date.now() / 1000}));
26  };
27}
28
29export async function copyToClipboard(text: string): Promise<void> {
30  try {
31    // TODO(hjd): Fix typescript type for navigator.
32    // tslint:disable-next-line no-any
33    await(navigator as any).clipboard.writeText(text);
34  } catch (err) {
35    console.error(`Failed to copy "${text}" to clipboard: ${err}`);
36  }
37}
38
39export async function queryResponseToClipboard(resp: QueryResponse):
40    Promise<void> {
41  const lines: string[][] = [];
42  lines.push(resp.columns);
43  for (const row of resp.rows) {
44    const line = [];
45    for (const col of resp.columns) {
46      const value = row[col];
47      line.push(value === null ? 'NULL' : value.toString());
48    }
49    lines.push(line);
50  }
51  copyToClipboard(lines.map(line => line.join('\t')).join('\n'));
52}
53
54export function download(file: File, name?: string): void {
55  const url = URL.createObjectURL(file);
56  const a = document.createElement('a');
57  a.href = url;
58  a.download = name === undefined ? file.name : name;
59  document.body.appendChild(a);
60  a.click();
61  document.body.removeChild(a);
62  URL.revokeObjectURL(url);
63}
64
65
66