• 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    await(navigator as any).clipboard.writeText(text);
33  } catch (err) {
34    console.error(`Failed to copy "${text}" to clipboard: ${err}`);
35  }
36}
37
38export async function queryResponseToClipboard(resp: QueryResponse):
39    Promise<void> {
40  const lines: string[][] = [];
41  lines.push(resp.columns);
42  for (const row of resp.rows) {
43    const line = [];
44    for (const col of resp.columns) {
45      const value = row[col];
46      line.push(value === null ? 'NULL' : value.toString());
47    }
48    lines.push(line);
49  }
50  copyToClipboard(lines.map((line) => line.join('\t')).join('\n'));
51}
52
53export function download(file: File, name?: string): void {
54  const url = URL.createObjectURL(file);
55  const a = document.createElement('a');
56  a.href = url;
57  a.download = name === undefined ? file.name : name;
58  document.body.appendChild(a);
59  a.click();
60  document.body.removeChild(a);
61  URL.revokeObjectURL(url);
62}
63
64
65